进程休眠之sleepy不懂的地方

无法理解:

ssize_t sleepy_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{
    printk(KERN_DEBUG "process %i (%s) going to sleep\n",
    current->pid, current->comm);
    wait_event_interruptible(wq, flag != 0);
    flag = 0;
    printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
    return 0; /* EOF */
}

ssize_t sleepy_write (struct file *filp, const char __user *buf, size_t count,
loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
current->pid, current->comm);
  flag = 1;
    wake_up_interruptible(&wq);

    return count; /* succeed, to avoid retrial */

}


假设有两个进程在等待时调用了sleepy_write函数,具体的过程是怎么样的?
为什么说第二个被唤醒的进程马上又会进入休眠呢?

等待是什么意思啊?是休眠了,是不是调用read休眠了,如果是那为什么还可以调用sleepy_write。

写的测试程序:

//testsleepy.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int fd;
    char* filename;  
    char buf[1] ={0x54};
    if (argc != 3){
        return 0;
    }
 
    filename = argv[1];

    fd = open(filename, O_RDWR);
    
    if (fd < 0){
        printf("error, can't open %s\n", filename);
        return 0;
    }
    if (!strcmp("read", argv[2])){ 
read(fd, buf, 1);
    }
    
    else if(!strcmp("write", argv[2])){
write(fd, buf, 1);    
    }
    return 0;
}

terminal1:
$ ./testsleepy /dev/sleepy read  

terminal2:
$ ./testsleepy /dev/sleepy read 

ternimal1 && terminal2 running testsleepy is sleepy

How can the testsleepy to wake_up throught calling write function ?

When I open another terminal and run ./testsleepy /dev/sleepy write。


Please guess the result which the terminal will wake up。

The result is that the second terminal program will wake up.

when I run ./testsleepy /dev/sleepy write again. 
The first terminal will wake up


user four terminal to read 
cat /var/log/kern.log
Oct 16 23:52:15 ubuntu kernel: [19326.544325] process 7211 (testsleep) going to sleep //terminal1
Oct 16 23:52:18 ubuntu kernel: [19329.207916] process 7212 (testsleep) going to sleep //terminal2
Oct 16 23:52:19 ubuntu kernel: [19331.122049] process 7213 (testsleep) going to sleep //terminal3
Oct 16 23:52:23 ubuntu kernel: [19334.319102] process 7214 (testsleep) going to sleep //terminal4
Oct 16 23:52:26 ubuntu kernel: [19337.326668] process 7216 (testsleep) awakening the readers... //terminal write
Oct 16 23:52:26 ubuntu kernel: [19337.326801] awoken 7214 (testsleep)
Oct 16 23:52:28 ubuntu kernel: [19339.622754] process 7217 (testsleep) awakening the readers... //terminal write
Oct 16 23:52:28 ubuntu kernel: [19339.622837] awoken 7211 (testsleep)
Oct 16 23:52:34 ubuntu kernel: [19345.777920] process 7218 (testsleep) awakening the readers... //terminal write
Oct 16 23:52:34 ubuntu kernel: [19345.778015] awoken 7213 (testsleep)
Oct 16 23:52:36 ubuntu kernel: [19347.477079] process 7219 (testsleep) awakening the readers... //terminal write
Oct 16 23:52:36 ubuntu kernel: [19347.477159] awoken 7212 (testsleep)
 
I don't know why the results will look like this ! 
 
PS:尚待我去理解的地方!有时间在去慢慢理解吧!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

john_liqinghan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值