【发布时间】:2015-05-19 01:21:19
【问题描述】:
我正在开发一个在三个进程之间发送消息的项目:一个父母和两个孩子。第一个进程将接收消息,然后将消息发送到第二个进程。第二个进程将读入一条消息,然后在将其发送到第三个进程之前对其进行转换。第三个过程将读入一条消息并进一步转换该消息。最后,消息将被发送回第一个进程,打印它。
这将在 UNIX 中使用管道和系统调用来实现。我对这个过程不太熟悉,所以我很感激你能提供的任何提示/建议。下面是我的代码。谢谢!
*#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define ERR (-1) /* indicates an error condition */
#define READ 0 /* read end of a pipe */
#define WRITE 1 /* write end of a pipe */
#define STDIN 0 /* file descriptor of standard in */
#define STDOUT 1 /* file descriptor of standard out */
int main()
{
int pid_1, /* will be process id of first child, which inverts the string */
pid_2; /* will be process id of second child, which converts the string to uppercase */
int fd[2]; //descriptor array for parent process
int fd2[2]; //descriptor array for first child process
int fd3[2]; //descriptor array for second child process
char ch [100]; //original character array
char ch2 [100]; //character array after reversal
int index = 0; //size
char character;
while((character = getchar()) != '\n') //get input and put it into array
{
ch[index] = character;
index++;
}
if(pipe (fd) == ERR)
{
perror("Parent pipe cannot be created\n");
exit (ERR);
}
if (pipe (fd2) == ERR) /* create a pipe */
{ /* must do before a fork */
perror ("Pipe cannot be created.\n");
exit (ERR);
}
if (pipe (fd3) == ERR) /* create a pipe */
{ /* must do before a fork */
perror ("Second pipe cannot be created.\n");
exit (ERR);
}
if ((pid_1 = fork()) == ERR) /* create 1st child */
{
perror ("Second process cannot be created.\n");
exit (ERR);
}
if (pid_1 != 0) /* in parent */
{
close(fd2[0]); //close read end of first child
close(fd[1]); //close write end of parent
printf("Parent process sends message %s\n", ch);
write(fd2[1], ch, sizeof(ch)); //write to write end of first child
close(fd2[1]); //close write end of first child
close(fd[0]);
if ((pid_2 = fork ()) == ERR) /* create 2nd child */
{
perror ("Third process cannot be created.\n");
exit (ERR);
}
if (pid_2 != 0) /* still in parent */
{
wait ((int *) 0); /* wait for children to die */
wait ((int *) 0);
read(fd[0], ch2, sizeof(ch2)); //read read end of parent process
printf("Parent process receives message %s\n", ch2);
int i = 0;
while (i < index)
{
printf("%c", ch2[i]); //print message
i++;
}
printf("\n");
close(fd3[1]); //close write end of second child
close(fd[0]); //close read end of parent process
}
else /* in 2nd child */
{
close(fd3[1]); //close write end of second child
close(fd2[0]); //close read end of first child
read(fd3[0], ch2, sizeof(ch2)); //read read end of second child
printf("Second child receives %s\n", ch2);
int i = 0;
while (i < index)
{
ch2[i] = toupper(ch2[i]); //convert to uppercase
i ++;
}
printf("Second child sends message %s\n", ch2);
write(fd[1],ch2, sizeof(ch2)); //write to write end of parent process
close(fd3[0]); //close read end of second child
close(fd[1]); //close read end of parent process
}
}
else /* in 1st child */
{
close(fd2[1]); //close write end of first child
close(fd[0]); //close read end of parent process
read(fd2[0], ch, sizeof(ch)); //read read end of first child
printf("First child receives message %s\n", ch);
int i = 0;
while (i < index)
{
ch2[i] = ch[index - 1 - i]; //reverse
i++;
}
printf("First child sends message %s\n", ch2);
write(fd3[1], ch2, sizeof(ch2)); //write to write end of second child
close(fd3[1]); //close write end of second child
close(fd2[0]); //close read end of first child
}
exit(0);
}
【问题讨论】:
-
先试试更简单的。
-
不确定您的意思,以及如何解决。我尝试遵循一个只有一个孩子和一个父母的例子,并且可以正常工作,所以我只是添加了另一个孩子。这可能就是我出错的地方。
-
好吧,你同时尝试了几件事;哪一个失败了?有错误信息吗?父进程是否收到来自用户的字符串?第一个孩子是通过fd2接收的吗?它是否正确反转字符串?第二个孩子是否通过fd3接收到反转的字符串?它是否正确更改大小写?父级是否通过 fd 接收大写字符串?你能隔离出不工作的东西吗?
-
你忘记
pipe(fd3)了吗? -
除非这是家庭作业,否则最好使用 shell 进行管道处理,并将 C 程序编写为简单的 stdin-to-stdout 处理器。您可以使用 mkfifo 来帮助创建流程“循环”。
标签: c unix operating-system pipe