#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#define PATH "/tmp/Jan.c"
#define BUFF_SIZE 60
int main(int argc, char **argv)
{
int fd;
int ret;
int pid;
char wbuff[BUFF_SIZE] = "hello, Jan!";
char rbuff[BUFF_SIZE];
if ((ret = mkfifo(PATH, 0644)) < 0) { //Create fifo
perror("mkfifo");
return -1;
}
if ((pid = fork()) < 0) { // Create process
perror("pid");
return -1;
}
if (pid == 0) { // Child process
while (1) {
fd = open(PATH, O_WRONLY);
write(fd, wbuff, sizeof(wbuff));
sleep(1);
}
} else { // Father process
while (1) {
fd = open(PATH, O_RDONLY);
read(fd, rbuff, sizeof(rbuff));
printf("%s\n", rbuff);
sleep(1);
}
}
return 0;
}
注:本代码中存在一个bug,还未处理,当第一次创建成功之后,后每一次创建时,均需修改PATH
read ________________________ write
father process <------------- |___________Fifo__________| <------------------child process
^ ^ ^
| | |
| It's a empty file |
| |
| |
| |
| |
|
Create process(father and child)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。