我想制作一个简单的程序,即fork和子进程写入命名管道,父进程从命名管道读取并显示。 问题是,它进入父级,第一个printf,然后它变得奇怪,它不做任何其他事情,没有得到第二个printf,它只是在控制台输入的方式。C命名管道(fifo)。父进程卡住
所有的#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void main()
{
char t[100];
mkfifo("myfifo",777);
pid_t pid;
pid = fork();
if (pid==0)
{
//execl("fifo2","fifo2",(char*)0);
char r[100];
printf("scrie2->");
scanf("%s",r);
int fp;
fp = open("myfifo",O_WRONLY);
write(fp,r,99);
close(fp);
printf("exit kid \n");
exit(0);
} else
{
wait(0);
printf("entered parent \n"); // <- this it prints
// whats below this line apparently its not being executed
int fz; printf("1");
fz = open("myfifo",O_RDONLY); printf("2");
printf("fd: %d",fz);
char p[100];
int size;
printf("------");
//struct stat *info;
//stat("myfifo",info); printf("%d",(*info).st_size);
read(fz,p,99);
close(fz);
printf("%s",p);
printf("exit"); exit(0);
}
}
我打开FIFO,但我在同一个过程中关闭它。所以它不应该阻止。我需要我的父进程在孩子之后等待,因为孩子得到了输入,并且我不能等待父进程的第一行,因为那意味着开始从fifo读取,当我甚至没有完成时写在它上面 – Blitzkr1eg 2010-05-30 11:06:21
现在我有: char c [2]; c [1] = 0; C [0] = 0; c [0] = strlen(r); // c [0]可以说是4 write(fp,c,1); // FP是FIFO 然后在另一个过程: //开口管,我检查错误,但没有错误,所以它打开细 炭C [2]; C [1] = 0; C [0] = 0; read(fz,c,1); // fz是管道 //现在c [0]是0而不是它应该是的,4 – Blitzkr1eg 2010-05-30 15:05:22
在评论中描述的代码更改之后,可能很难。你最好编辑你的问题来展示你的尝试。我在我的回复中添加了一个例子,可以帮助你。 – jschmier 2010-05-30 17:23:14