Monday, October 3, 2016

Linux 进程状态浅析

众所周知,现在的分时操作系统能够在一个CPU上运行多个程序,让这些程序表面上看起来是在同时运行的。linux就是这样的一个操作系统。

在linux系统中,每个被运行的程序实例对应一个或多个进程。linux内核需要对这些进程进行管理,以使它们在系统中“同时”运行。linux内核对进程的这种管理分两个方面:进程状态管理,和进程调度。本文主要介绍进程状态管理,进程调度见《linux进程调度浅析》。

进程状态

在linux下,通过ps命令我们能够查看到系统中存在的进程,以及它们的状态:
R(TASK_RUNNING),可执行状态。

只有在该状态的进程才可能在CPU上运行。而同一时刻可能有多个进程处于可执行状态,这些进程的task_struct结构(进程控制块)被放入对应CPU的可执行队列中(一个进程最多只能出现在一个CPU的可执行队列中)。进程调度器的任务就是从各个CPU的可执行队列中分别选择一个进程在该CPU上运行。

只要可执行队列不为空,其对应的CPU就不能偷懒,就要执行其中某个进程。一般称此时的CPU“忙碌”。对应的,CPU“空闲”就是指其对应的可执行队列为空,以致于CPU无事可做。

有人问,为什么死循环程序会导致CPU占用高呢?因为死循环程序基本上总是处于TASK_RUNNING状态(进程处于可执行队列中)。除非一些非常极端情况(比如系统内存严重紧缺,导致进程的某些需要使用的页面被换出,并且在页面需要换入时又无法分配到内存……),否则这个进程不会睡眠。所以CPU的可执行队列总是不为空(至少有这么个进程存在),CPU也就不会“空闲”。
很多操作系统教科书将正在CPU上执行的进程定义为RUNNING状态、而将可执行但是尚未被调度执行的进程定义为READY状态,这两种状态在linux下统一为 TASK_RUNNING状态。

S(TASK_INTERRUPTIBLE),可中断的睡眠状态。

处于这个状态的进程因为等待某某事件的发生(比如等待socket连接、等待信号量),而被挂起。这些进程的task_struct结构被放入对应事件的等待队列中。当这些事件发生时(由外部中断触发、或由其他进程触发),对应的等待队列中的一个或多个进程将被唤醒。

通过ps命令我们会看到,一般情况下,进程列表中的绝大多数进程都处于TASK_INTERRUPTIBLE状态(除非机器的负载很高)。毕竟CPU就这么一两个,进程动辄几十上百个,如果不是绝大多数进程都在睡眠,CPU又怎么响应得过来。

D(TASK_UNINTERRUPTIBLE),不可中断的睡眠状态。

与TASK_INTERRUPTIBLE状态类似,进程处于睡眠状态,但是此刻进程是不可中断的。不可中断,指的并不是CPU不响应外部硬件的中断,而是指进程不响应异步信号。

绝大多数情况下,进程处在睡眠状态时,总是应该能够响应异步信号的。否则你将惊奇的发现,kill -9竟然杀不死一个正在睡眠的进程了(TASK_INTERRUPTIBLE状态)!于是我们也很好理解,为什么ps命令看到的进程几乎不会出现TASK_UNINTERRUPTIBLE状态,而总是TASK_INTERRUPTIBLE状态。

而TASK_UNINTERRUPTIBLE状态存在的意义就在于,内核的某些处理流程是不能被打断的。如果响应异步信号,程序的执行流程中就会被插入一段用于处理异步信号的流程(这个插入的流程可能只存在于内核态,也可能延伸到用户态),于是原有的流程就被中断了(参见《linux异步信号handle浅析》)。

在进程对某些硬件进行操作时(比如进程调用read系统调用对某个设备文件进行读操作,而read系统调用最终执行到对应设备驱动的代码,并与对应的物理设备进行交互),可能需要使用TASK_UNINTERRUPTIBLE状态对进程进行保护,以避免进程与设备交互的过程被打断,造成设备陷入不可控的状态。(比如read系统调用触发了一次磁盘到用户空间的内存的DMA,如果DMA进行过程中,进程由于响应信号而退出了,那么DMA正在访问的内存可能就要被释放了。)这种情况下的TASK_UNINTERRUPTIBLE状态总是非常短暂的,通过ps命令基本上不可能捕捉到。

linux系统中也存在容易捕捉的TASK_UNINTERRUPTIBLE状态。执行vfork系统调用后,父进程将进入TASK_UNINTERRUPTIBLE状态,直到子进程调用exit或exec(参见《神奇的vfork》)。

通过下面的代码就能得到处于TASK_UNINTERRUPTIBLE状态的进程:

#include
void main() {
if (!vfork()) sleep(100);
}

编译运行,然后ps一下:

kouu@kouu-one:~/test$ ps -ax | grep a.out
4371 pts/0      D+       0:00 ./a.out
4372 pts/0      S+       0:00 ./a.out
4374 pts/1      S+       0:00 grep a.out

然后我们可以试验一下TASK_UNINTERRUPTIBLE状态的威力。不管kill还是kill -9,这个TASK_UNINTERRUPTIBLE状态的父进程依然屹立不倒。

T(TASK_STOPPED or TASK_TRACED),暂停状态或跟踪状态:

向进程发送一个SIGSTOP信号,它就会因响应该信号而进入TASK_STOPPED状态(除非该进程本身处于TASK_UNINTERRUPTIBLE状态而不响应信号)。(SIGSTOP与SIGKILL信号一样,是非常强制的。不允许用户进程通过signal系列的系统调用重新设置对应的信号处理函数。)

向进程发送一个SIGCONT信号,可以让其从TASK_STOPPED状态恢复到TASK_RUNNING状态。

当进程正在被跟踪时,它处于TASK_TRACED这个特殊的状态。“正在被跟踪”指的是进程暂停下来,等待跟踪它的进程对它进行操作。比如在gdb中对被跟踪的进程下一个断点,进程在断点处停下来的时候就处于TASK_TRACED状态。而在其他时候,被跟踪的进程还是处于前面提到的那些状态。

对于进程本身来说,TASK_STOPPED和TASK_TRACED状态很类似,都是表示进程暂停下来。

而TASK_TRACED状态相当于在TASK_STOPPED之上多了一层保护,处于TASK_TRACED状态的进程不能响应SIGCONT信号而被唤醒。只能等到调试进程通过ptrace系统调用执行PTRACE_CONT、PTRACE_DETACH等操作(通过ptrace系统调用的参数指定操作),或调试进程退出,被调试的进程才能恢复TASK_RUNNING状态。

Z(TASK_DEAD – EXIT_ZOMBIE),退出状态,进程成为僵尸进程。

进程在退出的过程中,处于TASK_DEAD状态。

在这个退出过程中,进程占有的所有资源将被回收,除了task_struct结构(以及少数资源)以外。于是进程就只剩下task_struct这么个空壳,故称为僵尸。
之所以保留task_struct,是因为task_struct里面保存了进程的退出码、以及一些统计信息。而其父进程很可能会关心这些信息。比如在shell中,$?变量就保存了最后一个退出的前台进程的退出码,而这个退出码往往被作为if语句的判断条件。

当然,内核也可以将这些信息保存在别的地方,而将task_struct结构释放掉,以节省一些空间。但是使用task_struct结构更为方便,因为在内核中已经建立了从pid到task_struct查找关系,还有进程间的父子关系。释放掉task_struct,则需要建立一些新的数据结构,以便让父进程找到它的子进程的退出信息。

父进程可以通过wait系列的系统调用(如wait4、waitid)来等待某个或某些子进程的退出,并获取它的退出信息。然后wait系列的系统调用会顺便将子进程的尸体(task_struct)也释放掉。

子进程在退出的过程中,内核会给其父进程发送一个信号,通知父进程来“收尸”。这个信号默认是SIGCHLD,但是在通过clone系统调用创建子进程时,可以设置这个信号。

通过下面的代码能够制造一个EXIT_ZOMBIE状态的进程:

#include
void main() {
if (fork())
while(1) sleep(100);
}

编译运行,然后ps一下:

kouu@kouu-one:~/test$ ps -ax | grep a.out
10410 pts/0      S+       0:00 ./a.out
10411 pts/0      Z+       0:00 [a.out]
10413 pts/1      S+       0:00 grep a.out

只要父进程不退出,这个僵尸状态的子进程就一直存在。那么如果父进程退出了呢,谁又来给子进程“收尸”?

当进程退出的时候,会将它的所有子进程都托管给别的进程(使之成为别的进程的子进程)。托管给谁呢?可能是退出进程所在进程组的下一个进程(如果存在的话),或者是1号进程。所以每个进程、每时每刻都有父进程存在。除非它是1号进程。

1号进程,pid为1的进程,又称init进程。

linux系统启动后,第一个被创建的用户态进程就是init进程。它有两项使命:

1、执行系统初始化脚本,创建一系列的进程(它们都是init进程的子孙);
2、在一个死循环中等待其子进程的退出事件,并调用waitid系统调用来完成“收尸”工作;

init进程不会被暂停、也不会被杀死(这是由内核来保证的)。它在等待子进程退出的过程中处于TASK_INTERRUPTIBLE状态,“收尸”过程中则处于TASK_RUNNING状态。

X(TASK_DEAD – EXIT_DEAD),退出状态,进程即将被销毁。

而进程在退出过程中也可能不会保留它的task_struct。比如这个进程是多线程程序中被detach过的进程(进程?线程?参见《linux线程浅析》)。或者父进程通过设置SIGCHLD信号的handler为SIG_IGN,显式的忽略了SIGCHLD信号。(这是posix的规定,尽管子进程的退出信号可以被设置为SIGCHLD以外的其他信号。)

此时,进程将被置于EXIT_DEAD退出状态,这意味着接下来的代码立即就会将该进程彻底释放。所以EXIT_DEAD状态是非常短暂的,几乎不可能通过ps命令捕捉到。

进程的初始状态

进程是通过fork系列的系统调用(fork、clone、vfork)来创建的,内核(或内核模块)也可以通过kernel_thread函数创建内核进程。这些创建子进程的函数本质上都完成了相同的功能——将调用进程复制一份,得到子进程。(可以通过选项参数来决定各种资源是共享、还是私有。)

那么既然调用进程处于TASK_RUNNING状态(否则,它若不是正在运行,又怎么进行调用?),则子进程默认也处于TASK_RUNNING状态。

另外,在系统调用clone和内核函数kernel_thread也接受CLONE_STOPPED选项,从而将子进程的初始状态置为 TASK_STOPPED。

进程状态变迁

进程自创建以后,状态可能发生一系列的变化,直到进程退出。而尽管进程状态有好几种,但是进程状态的变迁却只有两个方向——从TASK_RUNNING状态变为非TASK_RUNNING状态、或者从非TASK_RUNNING状态变为TASK_RUNNING状态。也就是说,如果给一个TASK_INTERRUPTIBLE状态的进程发送SIGKILL信号,这个进程将先被唤醒(进入TASK_RUNNING状态),然后再响应SIGKILL信号而退出(变为TASK_DEAD状态)。并不会从TASK_INTERRUPTIBLE状态直接退出。

进程从非TASK_RUNNING状态变为TASK_RUNNING状态,是由别的进程(也可能是中断处理程序)执行唤醒操作来实现的。执行唤醒的进程设置被唤醒进程的状态为TASK_RUNNING,然后将其task_struct结构加入到某个CPU的可执行队列中。于是被唤醒的进程将有机会被调度执行。

而进程从TASK_RUNNING状态变为非TASK_RUNNING状态,则有两种途径:

1、响应信号而进入TASK_STOPED状态、或TASK_DEAD状态;
2、执行系统调用主动进入TASK_INTERRUPTIBLE状态(如nanosleep系统调用)、或TASK_DEAD状态(如exit系统调用);或由于执行系统调用需要的资源得不到满足,而进入TASK_INTERRUPTIBLE状态或TASK_UNINTERRUPTIBLE状态(如select系统调用)。

显然,这两种情况都只能发生在进程正在CPU上执行的情况下。

Thursday, September 29, 2016

WebSockets

WebSockets

A WebSocket is a standard protocol for two-way data transfer between a client and server. The WebSockets protocol does not run over HTTP, instead it is a separate implementation on top of TCP.

Why use WebSockets?

A WebSocket connection allows full-duplex communication between a client and server so that either side can push data to the other through an established connection. The reason why WebSockets, along with the related technologies of Server-sent Events (SSE) and WebRTC data channels, are important is that HTTP is not meant for keeping open a connection for the server to frequently push data to a web browser. Previously, most web applications would implement long polling via frequent Asynchronous JavaScript and XML (AJAX) requests as shown in the below diagram.
Long polling via AJAX is incredibly inefficient for some applications.
Server push is more efficient and scalable than long polling because the web browser does not have to constantly ask for updates through a stream of AJAX requests.
WebSockets are more efficient than long polling for server sent updates.
While the above diagram shows a server pushing data to the client, WebSockets is a full-duplex connection so the client can also push data to the server as shown in the diagram below.
WebSockets also allow client push in addition to server pushed updates.
The WebSockets approach for server- and client-pushed updates works well for certain categories of web applications such as chat room, which is why that's often an example application for a WebSocket library.

Implementing WebSockets

Both the web browser and the server must implement the WebSockets protocol to establish and maintain the connection. There are important implications for servers since WebSockets connections are long lived, unlike typical HTTP connections.
A multi-threaded or multi-process based server cannot scale appropriately for WebSockets because it is designed to open a connection, handle a request as quickly as possible and then close the connection. An asynchronous server such as Tornado or Green Unicorn monkey patched with gevent is necessary for any practical WebSockets server-side implementation.
On the client side, it is not necessary to use a JavaScript library for WebSockets. Web browsers that implement WebSockets will expose all necessary client-side functionality through the WebSockets object.
However, a JavaScript wrapper library can make a developer's life easier by implementing graceful degradation (often falling back to long-polling when WebSockets are not supported) and by providing a wrapper around browser-specific WebSocket quirks. Examples of JavaScript client libraries and Python implementations are found below.

JavaScript client libraries

  • Socket.io's client side JavaScript library can be used to connect to a server side WebSockets implementation.
  • web-socket-js is a Flash-based client-side WebSockets implementation.

Python implementations

  • Autobahn uses Twisted or asyncio to implement the WebSockets protocol.
  • Crossbar.io builds upon Autobahn and includes a separate server for handling the WebSockets connections if desired by the web app developer.

Nginx WebSocket proxying

Nginx officially supports WebSocket proxying as of version 1.3. However, you have to configure the Upgrade and Connection headers to ensure requests are passed through Nginx to your WSGI server. It can be tricky to set this up the first time.
Here are the configuration settings I use in my Nginx file as part of my WebSockets proxy.
# this is where my WSGI server sits answering only on localhost
# usually this is Gunicorn monkey patched with gevent
upstream app_server_wsgiapp {
  server localhost:5000 fail_timeout=0;
}

server {

  # typical web server configuration goes here

  # this section is specific to the WebSockets proxying
  location /socket.io {
    proxy_pass http://app_server_wsgiapp/socket.io;
    proxy_redirect off;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 600;
  }
}
Note if you run into any issues with the above example configuration you'll want to scope out the official HTTP proxy module documentation.
The following resources are also helpful for setting up the configuration properly.

Open source Python examples with WebSockets

  • The python-websockets-example contains code to create a simple web application that provides WebSockets using Flask, Flask-SocketIO and gevent.
  • The Flask-SocketIO project has a chat web application that demos sending server generated events as well as input from users via a text box input on a form.

General WebSockets resources

  • The official W3C candidate draft for WebSockets API and the working draft for WebSockets are good reference material but can be tough for those new to the WebSockets concepts. I recommend reading the working draft after looking through some of the more beginner-friendly resources list below.
  • WebSockets 101 by Armin Ronacher provides a detailed assessment of the subpar state of HTTP proxying in regards to WebSockets. He also discusses the complexities of the WebSockets protocol including the packet implementation.
  • The "Can I Use?" website has a handy WebSockets reference chart for which web browsers and specific versions support WebSockets.
  • Mozilla's Developer Resources for WebSockets is a good place to find documentation and tools for developing with WebSockets.
  • WebSockets from Scratch gives a nice overview of the protocol then shows how the lower-level pieces work with WebSockets, which are often a black box to developers who only use libraries like Socket.IO.
  • websocketd is a WebSockets server aiming to be the "CGI of WebSockets". Worth a look.

A decentralized vehicle monitoring and communication pattern design

Recently, V2X or telematics becomes a very hot topic. It's also a very interesting point to dig into the topic, how to design a V2X communication pattern.

People say that, what is so called telematics is embedded your iPhone in your car. So the this inspires me, how the MNOs track the mobile phone's status? How MNOs detect if an iPhone is online or not?

So the first question come into my mind, how the IP addresses are allocated to the mobile devices?

Then I searched google for some resources I found out, for current dedicated GSN implementation for OEM, all the resources in fact are centrally managed, it's really hard to distinguish the geo differences.

Then it becomes very interesting that the vehicle communication design should follow cellular communications pattern, then something came to my mind recently here are some results.


Sunday, April 24, 2016

mesos installation hint on Ubuntu 14.04 LTS


Today I spent 4 hours to install mesos on my vmware machine. I figured out a small error in the get started guide. I hope this can help someone later on:-)

In the get started guide, it says that you should download and unzip by using following command

$ wget http://www.apache.org/dist/mesos/0.28.0/mesos-0.28.0.tar.gz
$ tar -zxf mesos-0.28.0.tar.gz

But later on, in the "Building Mesos" part, it says that you should build mesos by changing to following directory

# Change working directory.
$ cd mesos

# Bootstrap (Only required if building from git repository).
$ ./bootstrap

# Configure and build.
$ mkdir build
$ cd build
$ ../configure
$ make

The point is, if you also "git clone" in the same folder, then you will have both "mesos" and "mesos-0.28.0" under the same directory, it will confuse make which folder shall use, So if you want to clone the code, better to choose another directory than wget one:-)


Then you should just wait dozen of minutes, since mesos still needs to download and install a lot of software packages and extensions (zookeeper, etc.) so you need really some time to wait until it's done.

Monday, January 26, 2015

telematics experience in Euro

Something very interesting to drive a car in Euro highway (x, Euro; x' CN):
1. in Euro esp in Germany, some very restricted laws in place, so the driver are driving in a more standard way, and also the people who are crossing the road, they are strictly following the rules;
1'. Was very easy to acquire the driving license, very crowded road, the passenger on the road are not really followed the rules, improper driving behavior everywhere (fatigue driving, overload, unexpected lane cut in...)

2. They are a lot of outdated car driving on the road, and because of very restricted driving license examination, and good traffic condition, Europeans esp Germans,  they are used to manual transmission car than auto transmission car;
2'. Nearly no manual transmission car available on the road, and very bad traffic condition everywhere...

3. Current car related industry, biz mode and regulation are existing for very long time, people are already used to it. (e.g. ADAC is known for every driver for road side assistance);
3'. Car related industry, biz mode and regulation are not long existing as in Europe, car becomes a normal thing for a family only happens recently few years, a lot of process and support infra and related services should be improved or set up.

4. Europeans normally they do not have only one car, or they often share cars with each other and car rental services are very well accepted here, so new tech is not very well accepted here, because the driver needs to down grade compatible with older cars for a lot of use cases;
4'. Car is not shared with each other, very rare to see the outdated cars, new tech is very quickly adapted here;

5. Driving in Europe, still is a fun thing to do, it's a relax thing.
5'. In China, most of time it's boring, because of the air condition and homogenization development.

Which makes European hard to embrace the new telematics tech, because they already used to the current situation, in China, on contrary, they do not have historical problems and because of only this country was only caring abt GDP growth in past years, not everything was fully set up even not set up, but China just keep iterating again and again, so in the coming future, China might become a bigger telematics market then Europe at least, but regarding the north America, we still need to evaluate:)

Next step for China, make car and transportation infra more connected, more marketing adv, make it as a standard service for a car both for regulation level and customer experience/expectation level!

Next step for Euro, in some insensitive region make more tests on ADAS, if before market telematics is not realistic now then thing hard about after market and transportation infra, if car itself can not be changed in recently at least GPS locator or some communicator with transportation objects must be setup and standardized A.S.A.P!

Sunday, November 9, 2014

chinese consumer telematics market

今天突然想起来点东西,最近在中国有很多车联网公司雨后春笋般的在中国成立,国家也把车联网当成了一个发展的一个战略方向放到了一个很高的高度,也加大了投资力度。但是今天突然想到了一个问题,车辆网,telematics如果基于现在的汽车的模型,那么汽车现在作为一个重要信息的载体,中国是否已经准备好了?

随着中国经济的飞速发张,中国最近出现了很多的很具规模的汽车制造公司(奇瑞,长城,BYD等等)但是要是谈论到车联网,根据现在一个比较流行的模式,对于终端的消费市场其实就是基于OBD的数据采集器,然后上传到后台的服务器存储分析,基本所有的厂商都是大同小异,没有质的区别。那么现在问题就来了,首先OBD最初是用来检测汽车尾气的一个标准,他是否是一个合适数据收集接口?(根据现在很多OEM的测试部门的经验,这个也许可以有);下一个问题,要想做车联网,就需要一个稳定的标准,现在的OBD标准基本都是欧美市场制定的,而且很多车上一些高端的硬件和sensor现在基本上还在欧美的垄断之下,在中国是否可以真的做出中国自己的车联网产品?基于现在的模型,telematics主要是就是3大块

1. 车上的数据;
2. 无线网络;
3. 后台的数据处理分析;

第三个问题,其实就是软件开发的问题,现在中国在这个方面做一个基于最多也就是百万级别的应用,这个问题应该问题不大,中国已经有阿里和百度,这个应该不是问题;
第二个问题,基本上也没有什么特别的变化,主要就是标准化的问题,这个其实汽车和普通的通讯终端没有什么区别,只不过是已经镶嵌到车里而已,这个也不是什么大的问题;
第一个问题,这个问题就来了,汽车作为一个有百年历史的产业,尤其工艺层面,这个不是说想软件开发一样直接可以copy过来就可以用,由于汽车还不同于现在普通的消费电子,汽车的寿命相对较长,而且他所工作的环境非常苛刻,需要考虑到告诉雨雪等等复杂的自然环境,不是可以一直保证稳定的机房的环境(恒温恒湿等)。这就对车载电子设备提出了较高的要求。所以在这方面,在不久的将来一旦欧美的汽车制造强国改变了现在的生产流程或者对标准做出了修改,将会对现有的产品产生巨大的影响,虽然可以采用apple store的模式,更新手机端的应用但是对硬件的更改可不是一件很容易的事情。这将是未来所有创业型企业将面对的一个很大的潜在的风险。

不过这个问题也并不是不可以解决,我们可以加快硬件研发的速度,或者需要中国的企业家们齐心协力,制定出符合中国市场的标准化体系。