无法理解:
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);
假设有两个进程在等待时调用了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:尚待我去理解的地方!有时间在去慢慢理解吧!
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:尚待我去理解的地方!有时间在去慢慢理解吧!