• 阻塞原理 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].

  • 相关阅读:
    如何复制图文消息封面图片?正文没显示
    微信企业号已开通账号超过10万 日均消息量超过100万条
    微信服务号模板消息接口新增"设置行业"和"添加模板"及细节优化
    张小龙在微信公开课上的演讲
    做微信营销你知道男女用微信的习惯吗?
    微信时代,"邮"你选择 腾讯企业邮箱推新玩法
    亲身体验,不要再拖! 拖! 拖!
    The Promise of Deep Learning
    判断两个数组是否相等
    matlab学习笔记(一)单元数组
  • 原文地址:https://www.cnblogs.com/tangr206/p/3081696.html
Copyright © 2020-2023  润新知