Wait() 导致程序在打开 FIFO 命名管道时暂停
Wait() causing program to pause when opening FIFO named pipe
每当我使用等待函数时,我的代码都会在 open() 上暂停,没有它程序将无法运行。没有错误代码,但我想知道哪里出了问题。
代码最终会是controller.c会打开c1,c2,c3。它将等待 c1 获取输入并将其放入管道,然后管道将转到 c2 打印它,然后 c3 以不同的格式打印它。
int main(int ac, char**av)
{
int fd;
int test;
char * myfifo = "/tmp/myfifo";
unlink(myfifo); //makes sure no fifo already exists
/* create the FIFO (named pipe) */
if((mkfifo(myfifo, 0666)) <0)
perror("MKFIFO: ");
int statval;
pid_t pid1 = fork(); //creates a process for c1 to run in
wait(&statval);
if ( pid1 == 0) //makes sure to only run the program in the child process
{
printf("Opening c1\n");
int x = execvp("./c1",av); //Runs the other file based on the array
if (x == -1)
exit (1);
perror("Error"); //used for error reporting
printf("Closing c1\n");
exit(0); //ends process when finished
}
if (WIFEXITED(statval)) //if there is an exit code
printf("\nChilds exit code %d\n", WEXITSTATUS(statval));//prints the exit status of the child process
pid_t pid2 = fork(); //creates a process for c2 to run in
wait(&statval);
if ( pid2 == 0) //makes sure to only run the program in the child process
{
printf("Opening c2\n");
int x = execvp("./c2",av); //Runs the other file based on the array
if (x == -1)
exit (1);
perror("Error"); //used for error reporting
exit(0); //ends process when finished
}
if (WIFEXITED(statval)) //if there is an exit code
printf("\nChilds exit code %d\n", WEXITSTATUS(statval));//prints the exit status of the child process
unlink(myfifo);
C1:
printf("TEST1\n");
char *line = NULL;
size_t len = 0;
ssize_t read;
int fd;
char * myfifo = "/tmp/myfifo";
char buf[BUFSIZE];
fd = open(myfifo, O_WRONLY);
if (fd < 0)
perror("OPEN: ");
//while (1) {
printf("Enter a line of text\n");
//scanf("%s", buf);
if ((strcmp(line,"-999"))==0){
printf("TEST");
//break;
}
//write(fd, buf, sizeof(buf));
write(fd, "hi", sizeof("hi"));
//}
close(fd);
unlink(myfifo);
return 0;
C2:
int fd;
char * myfifo = "/tmp/myfifo";
char buf[BUFSIZE];
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
read(fd, buf, BUFSIZE);
printf("Received: %s\n", buf);
close(fd);
return 0;
程序将打开 c1,打印 TEST 然后什么都不做,直到我手动退出。
在第一次分叉之后,parent 和 child wait()
立即出现。 child 的调用失败,因为它没有 un-waited-for children,但您忽略了结果。 child 然后执行程序 c1
,它(假设执行成功)依次打开 FIFO 进行写入。这个 阻塞 直到 FIFO 的另一端打开,但是另一端不会被你的程序打开,因为 parent 进程正在等待 child在继续之前退出。您造成了死锁。
在另一个(或其他程序)打开 FIFO 的另一端之前,child 都无法继续打开 FIFO,因此 parent 必须分叉两个 child ren 在等待任何一个之前。此外,children 到 wait()
毫无意义,尽管他们对该函数的调用应该很快失败。
每当我使用等待函数时,我的代码都会在 open() 上暂停,没有它程序将无法运行。没有错误代码,但我想知道哪里出了问题。
代码最终会是controller.c会打开c1,c2,c3。它将等待 c1 获取输入并将其放入管道,然后管道将转到 c2 打印它,然后 c3 以不同的格式打印它。
int main(int ac, char**av)
{
int fd;
int test;
char * myfifo = "/tmp/myfifo";
unlink(myfifo); //makes sure no fifo already exists
/* create the FIFO (named pipe) */
if((mkfifo(myfifo, 0666)) <0)
perror("MKFIFO: ");
int statval;
pid_t pid1 = fork(); //creates a process for c1 to run in
wait(&statval);
if ( pid1 == 0) //makes sure to only run the program in the child process
{
printf("Opening c1\n");
int x = execvp("./c1",av); //Runs the other file based on the array
if (x == -1)
exit (1);
perror("Error"); //used for error reporting
printf("Closing c1\n");
exit(0); //ends process when finished
}
if (WIFEXITED(statval)) //if there is an exit code
printf("\nChilds exit code %d\n", WEXITSTATUS(statval));//prints the exit status of the child process
pid_t pid2 = fork(); //creates a process for c2 to run in
wait(&statval);
if ( pid2 == 0) //makes sure to only run the program in the child process
{
printf("Opening c2\n");
int x = execvp("./c2",av); //Runs the other file based on the array
if (x == -1)
exit (1);
perror("Error"); //used for error reporting
exit(0); //ends process when finished
}
if (WIFEXITED(statval)) //if there is an exit code
printf("\nChilds exit code %d\n", WEXITSTATUS(statval));//prints the exit status of the child process
unlink(myfifo);
C1:
printf("TEST1\n");
char *line = NULL;
size_t len = 0;
ssize_t read;
int fd;
char * myfifo = "/tmp/myfifo";
char buf[BUFSIZE];
fd = open(myfifo, O_WRONLY);
if (fd < 0)
perror("OPEN: ");
//while (1) {
printf("Enter a line of text\n");
//scanf("%s", buf);
if ((strcmp(line,"-999"))==0){
printf("TEST");
//break;
}
//write(fd, buf, sizeof(buf));
write(fd, "hi", sizeof("hi"));
//}
close(fd);
unlink(myfifo);
return 0;
C2:
int fd;
char * myfifo = "/tmp/myfifo";
char buf[BUFSIZE];
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
read(fd, buf, BUFSIZE);
printf("Received: %s\n", buf);
close(fd);
return 0;
程序将打开 c1,打印 TEST 然后什么都不做,直到我手动退出。
在第一次分叉之后,parent 和 child wait()
立即出现。 child 的调用失败,因为它没有 un-waited-for children,但您忽略了结果。 child 然后执行程序 c1
,它(假设执行成功)依次打开 FIFO 进行写入。这个 阻塞 直到 FIFO 的另一端打开,但是另一端不会被你的程序打开,因为 parent 进程正在等待 child在继续之前退出。您造成了死锁。
在另一个(或其他程序)打开 FIFO 的另一端之前,child 都无法继续打开 FIFO,因此 parent 必须分叉两个 child ren 在等待任何一个之前。此外,children 到 wait()
毫无意义,尽管他们对该函数的调用应该很快失败。