- 论坛徽章:
- 0
|
不好意思,虽然不是初学者。但水平一直很菜。
在看fifo函数的时候,编写了个小程序,但总达不到我想要的结果。又找不到问题所在。
只有在此求教各位,望不吝赐教。非常感谢。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <wait.h>
#define ERR_EXIT(m)\
do{\
perror(m);\
exit(-1);\
}while(0)\
int main(int argc, char* argv[])
{
int fd,status;
pid_t pid;
char buf[10];
unlink("/tmp/fifo");
mkfifo("/tmp/fifo",0666);
pid=fork();
if(pid<0)
ERR_EXIT("fork error");
if(pid>0)
{
wait(&status);
sleep(5);
fd=open("/tmp/fifo",O_RDWR);
read(fd,buf,10);
close(fd);
printf("the buf is :%s\n",buf);
}
else
{
fd=open("/tmp/fifo",O_RDWR);
write(fd,"ABCDEFG",7);
close(fd);
exit(0);
}
return 0;
}
|
|