ifndef 发表于 2013-05-07 23:01

普通i/o中 异步io信号SIGIO是怎么产生的啊

我在linux下想测试下 异步 I/OSIGIO这个信号
我对这个信号的理解是 进程可以一直去做别的事。有输入时
会产生这个信号。不知道对不对。

然后我写了一个 读程序他创建一个FIFO。然后以非阻塞读打开
然后就做别的事去了(我用了一个循环输出模拟 做别的事)。
等到有数据 然后产生信号 才转到信号处理程序中去读数据

另一个写进程。只是打开fifo 然后写入一些数据

纠结好久了··书上没有对这个信号详细介绍。网上都是关于套接字上的SIGIO.对我这个测试也基本没帮助,

read.c


1 #include<signal.h>
2 #include<fcntl.h>
3 #include<stdio.h>
4 #include<stdlib.h>
5 #include<unistd.h>
6 #include<sys/stat.h>
7 char buf;
8 int nreads;
9
10 int fifo_fd;
11
12 void sig_io(int signo){
13         nreads=read(fifo_fd,buf,sizeof(buf)-1);
14         buf='\0';
15         printf("read:%s\n",buf);
16 }
17
18 int main(void){
19
20         if(mkfifo("fifo",0666)==-1){
21               printf("mkfifo error\n");
22               exit(1);
23         }
24
25
26         fifo_fd=open("fifo",O_NONBLOCK | O_RDONLY);
27         if(-1==fifo_fd){
28               perror("open error");
29               exit(1);
30         }
31         if(fcntl(fifo_fd,F_SETOWN,getpid())==-1){
32               printf("fcntl error\n");
33               exit(1);
34         }
if(signal(SIGIO,sig_io)==SIG_ERR){
printf("signal error\n");
exit(1);
}
            int val;
40         val=fcntl(0,F_GETFL);
41         val |= O_ASYNC ;
42         if(fcntl(0,F_SETFL,val)==-1){
43               printf("fcnrl error\n");
44               exit(1);
45         }
46         while(1){
47               printf("dealing with something else\n");
48               sleep(5);
49
50         }
51         exit(0);
52 }

write.c

1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<fcntl.h>
4 #include<sys/stat.h>
5 #include<unistd.h>
6 int main(void){
7         int fifo_fd;
8         fifo_fd=open("fifo",O_WRONLY);
9         if(-1==fifo_fd){
10               perror("open error\n");
11               exit(1);
12         }
13         char *buf={"0","1","2","3","4","5","6","7","8"};
14         int count=0;
15         while(count<9){
16               printf("write %s\n",buf);
17               write(fifo_fd,buf,1);
18               sleep(1);
19               count++;
20         }
21
22         exit(0);
23 }


运行后不能得到想要的结果,希望谁能给我说下···

能具体解释下 在普通i/o方面SIGIO到底是怎么产生的

ifndef 发表于 2013-05-08 11:30

求解释啊··:'(

linux_c_py_php 发表于 2013-05-08 17:32

看APUE, UNP, 看manpage, 没难度的东西, 内核给你发个信号你就处理就是了.

ifndef 发表于 2013-05-08 18:13

回复 3# linux_c_py_php


    那高手能解释下吗·?就是书上遇到的问题。你知道SIGIO信号的怎么产生的吗?
页: [1]
查看完整版本: 普通i/o中 异步io信号SIGIO是怎么产生的啊