我正在尝试编写一个简单的程序,该程序将一个过程中的值传递给另一个过程,而又不使用命名管道具有父子属性。我编写的代码是:
#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
int main(int argc, char **argv)
{ int fd,i,j;
int * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
pid_t c=fork();
if(c==0){
i=10;
fd = open(myfifo, O_WRONLY);
write(fd, &i, sizeof(i));
printf("my pid is %d \n",getpid());
}
if(c>0){
pid_t c2=fork();
if(c2==0){
fd = open(myfifo, O_RDONLY);
read(fd, &j, sizeof(j));
printf("passes value is %d",j);
}
wait(0);
}
}
我希望第一个孩子向管道中写入10,第二个孩子从文件中读取并打印,以便我能知道它的工作。这段代码给了我很多错误,尽管我看不见。有什么想法吗?
我得到的错误:
In function ‘main’:|
warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]|
warning: implicit declaration of function ‘mkfifo’ [-Wimplicit-function-declaration]|
warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]|
]main.c|27|note: each undeclared identifier is reported only once for each function it appears in|
error:'O_WRONLY' undeclared(first use in this function)
error:'O_RDONLY' undeclared(first use in this function)