- 论坛徽章:
- 0
|
我在linux下想测试下 异步 I/O SIGIO这个信号
我对这个信号的理解是 进程可以一直去做别的事。有输入时
会产生这个信号。不知道对不对。
然后我写了一个 读程序 他创建一个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[20];
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[nreads]='\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[10]={"0","1","2","3","4","5","6","7","8"};
14 int count=0;
15 while(count<9){
16 printf("write %s\n",buf[count]);
17 write(fifo_fd,buf[count],1);
18 sleep(1);
19 count++;
20 }
21
22 exit(0);
23 }
运行后不能得到想要的结果,希望谁能给我说下···
能具体解释下 在普通i/o 方面 SIGIO到底是怎么产生的 |
|