|
|
发表于 2006-3-30 11:51:27
|
显示全部楼层
你的fd没有赋值。改过的程序:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <limits.h>
- void quit(char *buf)
- {
- perror(buf);
- exit(EXIT_FAILURE);
- }
- int main()
- {
- int fd;
- time_t tp;
- fd=mkfifo("fi",0644);
- if(fd<0)
- quit("mkfifo");
- int pid=fork();
- if(pid<0)
- quit("fork");
- else if(pid>0)
- {
- if((fd = open("fi",O_RDONLY,0644))<0)
- quit("parent open");
- char buf[PIPE_BUF-1];
- int i=read(fd,buf,PIPE_BUF-1);
- if(i>0)
- printf("parent read : %s",buf);
- }
- else
- {
- if((fd = open("fi",O_WRONLY,0644))<0)
- quit("child open");
- char buf1[PIPE_BUF-1];
- int j=sprintf(buf1,"%d send %s",getpid(),ctime(&tp));
- write(fd,buf1,j+1);
- }
- close(fd);
- exit(EXIT_SUCCESS);
- }
复制代码 |
|