当前位置: 技术问答>linux和unix
Linux FIFO代码问题
来源: 互联网 发布时间:2016-08-28
本文导语: /*speak.c*/ #include #include #include #include #include #include #include #include #define FIFO_NAME "american_maid" int main(void) { char s[300]; int num, fd; mknod(FIFO_NAME, S_IFIFO | 0666, 0); printf("waiting for readers...n"); fd = open(FIFO_NAME, ...
/*speak.c*/
#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for readers...n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuffn");
while (gets(s), !feof(stdin)) {
if ((num = write(fd, s, strlen(s))) == -1)
perror("write");
else
printf("speak: wrote %d bytesn", num);
}
return 0;
}
/*tick.c*/
#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for writers...n");
fd = open(FIFO_NAME, O_RDONLY);
printf("got a writern");
do {
if ((num = read(fd, s, 300)) == -1)
perror("read");
else {
s[num] = '';
printf("tick: read %d bytes: "%s"n", num, s);
}
} while (num > 0);
return 0;
}
利用两个shell分别运行speak(speak.c) 和tick(tick.c)
先终止tick.
用#gdb speak运行,输入字符,回车。会出现 write:Broken Pipe 错误。即speak收到了一个SIGPIPE的信号。
但是#./speak运行的时候,不会出现write:Broken Pipe 错误。
请高手指点,为什么 #./speak 运行的时候,不会出现write:Broken Pipe 错误。
#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for readers...n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuffn");
while (gets(s), !feof(stdin)) {
if ((num = write(fd, s, strlen(s))) == -1)
perror("write");
else
printf("speak: wrote %d bytesn", num);
}
return 0;
}
/*tick.c*/
#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for writers...n");
fd = open(FIFO_NAME, O_RDONLY);
printf("got a writern");
do {
if ((num = read(fd, s, 300)) == -1)
perror("read");
else {
s[num] = '';
printf("tick: read %d bytes: "%s"n", num, s);
}
} while (num > 0);
return 0;
}
利用两个shell分别运行speak(speak.c) 和tick(tick.c)
先终止tick.
用#gdb speak运行,输入字符,回车。会出现 write:Broken Pipe 错误。即speak收到了一个SIGPIPE的信号。
但是#./speak运行的时候,不会出现write:Broken Pipe 错误。
请高手指点,为什么 #./speak 运行的时候,不会出现write:Broken Pipe 错误。
|
没有调试器的情况下,speek在调用write时收到信号直接就终止了。perror都没有执行,
|
用gdb调试时,信号会发到调试器。