免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1994 | 回复: 3
打印 上一主题 下一主题

[Linux] 普通i/o中 异步io信号SIGIO是怎么产生的啊 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-07 23:01 |只看该作者 |倒序浏览
我在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到底是怎么产生的

论坛徽章:
0
2 [报告]
发表于 2013-05-08 11:30 |只看该作者
求解释啊··

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
3 [报告]
发表于 2013-05-08 17:32 |只看该作者
看APUE, UNP, 看manpage, 没难度的东西, 内核给你发个信号你就处理就是了.

论坛徽章:
0
4 [报告]
发表于 2013-05-08 18:13 |只看该作者
回复 3# linux_c_py_php


    那高手能解释下吗·?就是书上遇到的问题。  你知道SIGIO信号的怎么产生的吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP