阻塞原理 tsleep 和 wake 系统调用

摘选: TcpV2 15.10节

tsleep and wakeup Functions

阻塞:

  When a process executing within the kernel cannot proceed because a kernel
resource is unavailable, it waits for the resource by calling tsleep, which has
the following prototype:  

    int tsleep (caddr_t chan, int pri, char *mesg, int timeo);

  The first argument to tsleep, chan, is called the wait channel. It identifies the
particular resource or event such as an incoming network connection, for
which the process is waiting. Many processes can be sleeping on a single wait
channel.  pri:唤醒时被调度的优先级.

  联系:ps -l 显示的WCHAN列: 进程正在睡眠的内核函数名称;
该函数的名称是从/root/system.map文件中获得的。也就是mesg参数

mesg is a string identifying the call to tsleep and is included
in debugging messages and in ps output.

唤醒:

  When the resource becomes available or when the event occurs, the
kernel calls wakeup with the wait channel as the single argument. The
prototype for wakeup is:
    void wakeup (caddr_t chan);
  All processes waiting for the channel are awakened and set to the run state.
The kernel arranges for tsleep to return when each of the processes resumes
execution.

 

注意:

  当多个process wait在同一个channel上时,一但唤醒,这些process都被放入执行队列。
但是各个precess消耗到来的资源之前必须判断一下资源是否可用。因为该资源可能已经被同时唤醒
的另一process取到。If the resource is not available, the process calls tsleep once again.

举例:

  One use of multiple processes sleeping on the same wait channel is to have
multiple server processes reading from a UDP socket. Each server calls
recvfrom and, as long as no data is available, the calls block in tsleep. When
a datagram arrives on the socket, the socket layer calls wakeup and each
server is placed on the run queue. The first server to run receives the
datagram while the others call tsleep again. In this way, incoming datagrams
are distributed to multiple servers without the cost of starting a new process
for each datagram. This technique can also be used to process incoming
connection requests in TCP by having multiple processes call accept on the
same socket. This technique is described in [Comer and Stevens 1993].

 

 

posted @ 2013-05-16 13:50  tangr206  阅读(502)  评论(0编辑  收藏  举报