进程如何使用 C 管道按顺序工作?
How processes work sequentially with C pipe?
我想做的是 2 个子进程将输入他们的名字并等待其他进程输入他的名字。例如,如果有第一个和第二个进程,第一个将她的名字放在屏幕上并等待其他人的名字。所以我想使用流程,我想看到它们按顺序工作。
输出:
first
second
first
second
first
second
我刚刚尝试了一些关于 C(linux) 的东西。
int main(void)
{
pid_t child_a, child_b;
int pipe1[2], pipe2[2];
char mesazhi1[] = "first";
char mesazhi2[] = "second";
char buf[1024];
int first_pipe = pipe(pipe1);
pipe(pipe2);
if(first_pipe == -1){
perror("pipe");
exit(1);
}
child_a = fork();
if (child_a == 0)
{
/* Child A code */
int i;
for (i = 0; i < 3; i++)
{
write(pipe1[1],mesazhi1, strlen(mesazhi1) + 1);
//printf("first\n");
int a = read(pipe2[0], buf, strlen(mesazhi2) + 1);
printf("%s - %d\n", buf, a);
}
}
else
{
child_b = fork();
if (child_b == 0)
{
int i;
for (i = 0; i < 3; i++)
{
write(pipe2[1],mesazhi2, strlen(mesazhi2) + 1);
//printf("second\n");
int a = read(pipe1[0], buf, strlen(mesazhi1) + 1);
printf("%s - %d\n", buf, a);
}
}
else
{
/* Parent Code */
int returnStatusA,returnStatusB;
waitpid(child_a, &returnStatusA, 0); // Parent process waits here for child to terminate.
waitpid(child_b, &returnStatusB, 0); // Parent process waits here for child to terminate.
if (returnStatusA == 0 && returnStatusB == 0) // Verify child process terminated without error.
{
printf("%s\n", "The child processes terminated normally.\n");
}
if (returnStatusA == 1 && returnStatusB == 1)
{
printf("%s\n", "The child processes terminated with an error!. \n" );
}
}
}
}
这是随机输入的名字。我的意思是我认为,有时第二个过程比第一个过程更快。这样的输出:
first
second
second
first
second
...
那么为什么第二个进程不等待第一个进程,因为我认为 read() 函数应该等到 pipe1 中有东西。
在发布的代码中,两个进程都写入各自的管道,然后读取。之后,就是一场比赛,看哪个进程先打印。
为了更可控的情况,让 child B 调用 read
和 printf
在 调用 write
之前。这样B在打印之前必须等待A,反之亦然。
if (child_b == 0)
{
int i;
for (i = 0; i < 3; i++)
{
int a = read(pipe1[0], buf, strlen(mesazhi1) + 1);
printf("%s - %d\n", buf, a);
write(pipe2[1],mesazhi2, strlen(mesazhi2) + 1);
}
}
我想做的是 2 个子进程将输入他们的名字并等待其他进程输入他的名字。例如,如果有第一个和第二个进程,第一个将她的名字放在屏幕上并等待其他人的名字。所以我想使用流程,我想看到它们按顺序工作。
输出:
first
second
first
second
first
second
我刚刚尝试了一些关于 C(linux) 的东西。
int main(void)
{
pid_t child_a, child_b;
int pipe1[2], pipe2[2];
char mesazhi1[] = "first";
char mesazhi2[] = "second";
char buf[1024];
int first_pipe = pipe(pipe1);
pipe(pipe2);
if(first_pipe == -1){
perror("pipe");
exit(1);
}
child_a = fork();
if (child_a == 0)
{
/* Child A code */
int i;
for (i = 0; i < 3; i++)
{
write(pipe1[1],mesazhi1, strlen(mesazhi1) + 1);
//printf("first\n");
int a = read(pipe2[0], buf, strlen(mesazhi2) + 1);
printf("%s - %d\n", buf, a);
}
}
else
{
child_b = fork();
if (child_b == 0)
{
int i;
for (i = 0; i < 3; i++)
{
write(pipe2[1],mesazhi2, strlen(mesazhi2) + 1);
//printf("second\n");
int a = read(pipe1[0], buf, strlen(mesazhi1) + 1);
printf("%s - %d\n", buf, a);
}
}
else
{
/* Parent Code */
int returnStatusA,returnStatusB;
waitpid(child_a, &returnStatusA, 0); // Parent process waits here for child to terminate.
waitpid(child_b, &returnStatusB, 0); // Parent process waits here for child to terminate.
if (returnStatusA == 0 && returnStatusB == 0) // Verify child process terminated without error.
{
printf("%s\n", "The child processes terminated normally.\n");
}
if (returnStatusA == 1 && returnStatusB == 1)
{
printf("%s\n", "The child processes terminated with an error!. \n" );
}
}
}
}
这是随机输入的名字。我的意思是我认为,有时第二个过程比第一个过程更快。这样的输出:
first
second
second
first
second
...
那么为什么第二个进程不等待第一个进程,因为我认为 read() 函数应该等到 pipe1 中有东西。
在发布的代码中,两个进程都写入各自的管道,然后读取。之后,就是一场比赛,看哪个进程先打印。
为了更可控的情况,让 child B 调用 read
和 printf
在 调用 write
之前。这样B在打印之前必须等待A,反之亦然。
if (child_b == 0)
{
int i;
for (i = 0; i < 3; i++)
{
int a = read(pipe1[0], buf, strlen(mesazhi1) + 1);
printf("%s - %d\n", buf, a);
write(pipe2[1],mesazhi2, strlen(mesazhi2) + 1);
}
}