我的 fork 输出不正确
I am getting incorrect output from my forks
我正在尝试创建一个程序来创建 2 个子进程和 4 个管道(我知道这并不理想,但此特定任务的规范要求它)。虽然它正确地对 5 个命令行参数整数中的两个进行了排序,但其余的只是吐出来,因为我认为是未初始化的整数,I.E. 7 打印为 33234951。
我对管道还很陌生,很难理解,所以我相信这个问题与此有关,而不是代码中的一些任意错误。
我仅使用 1 个父子就能够成功完成此操作,但是当我尝试实施多个时,事情就变得很冒险了。
我有很多未使用的包含,只是为了解决问题而乱搞东西。
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char **argv) {
printf("Starting\n");
pid_t pid;
pid_t pid2;
int mypipe0[2];
int mypipe1[2];
int mypipe2[2];
int mypipe3[2];
pipe(mypipe0);
pipe(mypipe1);
pipe(mypipe2);
pipe(mypipe3);
/* Create the child process. */
pid = fork();
std::cout << "Fork " << pid << std::endl;
// Child: Sorts Array
if (pid == 0) {
printf("pid == (pid_t) 0 p2\n");
/* This is the child process.
Close other end first. */
close(mypipe0[1]);
char valuesArray[5];
for (int a = 0; a < 5; a++)
read(mypipe0[0], &valuesArray[a], sizeof(char));
printf("finish reading mypipe0");
std::sort(valuesArray, valuesArray + 5);
printf("sorted");
close(mypipe1[0]);
close(mypipe2[0]);
for (int a = 0; a < 5; a++) {
write(mypipe1[1], &valuesArray[a], sizeof(char));
write(mypipe2[1], &valuesArray[a], sizeof(char));
}
close(mypipe1[1]);
close(mypipe2[1]);
exit(0);
}
else if (pid > 1) {
std::cout << "pid == (pid_t) 1" << std::endl;
/* This is the parent process.
Close other end first. */
close(mypipe0[0]); // Closes reading
int valuesArray[5];
valuesArray[0] = atoi(argv[1]);
valuesArray[1] = atoi(argv[2]);
valuesArray[2] = atoi(argv[3]);
valuesArray[3] = atoi(argv[4]);
valuesArray[4] = atoi(argv[5]);
printf("Argv init");
for (int a = 0; a < 5; ++a)
write(mypipe0[1], &valuesArray[a], sizeof(char));
printf("wrote to pipe 1");
close(mypipe0[1]);
wait(NULL);
close(mypipe1[1]); // Closes writing
// char outputArray[6];
int sortedArray[5];
for (int a = 0; a < 5; ++a)
read(mypipe1[0], &sortedArray[a], sizeof(char));
// Printing Array]
for (int a = 1; a < 5; ++a)
printf(", %d", sortedArray[a]);
printf("]");
// wait(NULL);
// close(mypipe2[1]); // Closes writing
// int median;
// read(mypipe1[0], median, sizeof(charian));
exit(0);
}
else {
pid2 = fork();
// Other child
if (pid2 == 0) {
printf("pid == (pid_t) 0\n");
/* This is the child process.
Close other end first. */
close(mypipe0[1]);
char valuesArray[5];
for (int a = 0; a < 5; a++)
read(mypipe0[0], &valuesArray[a], sizeof(char));
printf("finish reading mypipe0");
std::sort(valuesArray, valuesArray + 5);
printf("sorted");
close(mypipe1[0]);
close(mypipe2[0]);
for (int a = 0; a < 5; a++) {
write(mypipe1[1], &valuesArray[a], sizeof(char));
write(mypipe2[1], &valuesArray[a], sizeof(char));
}
close(mypipe1[1]);
close(mypipe2[1]);
exit(0);
}
}
}
我希望给定命令行参数 4 2 5 6 7 的输出为 2 4 5 6 7。相反,我得到 [1, 28932, 5, 6, -14276913]
第一个 fork
调用失败时到达 else
分支。
您当前的代码结构是,
pid = fork();
if (pid == 0)
{
runFirstChild();
}
else if (pid > 1)
{
runParent();
}
else // This branch is not taken unless there is a failure
{
pid2 = fork();
if (pid2 == 0)
{
runSecondChild();
}
}
你应该像这样构造它,
pid = fork();
if (pid == 0)
{
runFirstChild();
}
else if (pid > 1)
{
pid2 = fork(); // Fork the second child in the parent process
if (pid2 == 0)
{
runSecondChild();
}
else if (pid2 > 1)
{
runParent();
}
}
我正在尝试创建一个程序来创建 2 个子进程和 4 个管道(我知道这并不理想,但此特定任务的规范要求它)。虽然它正确地对 5 个命令行参数整数中的两个进行了排序,但其余的只是吐出来,因为我认为是未初始化的整数,I.E. 7 打印为 33234951。
我对管道还很陌生,很难理解,所以我相信这个问题与此有关,而不是代码中的一些任意错误。
我仅使用 1 个父子就能够成功完成此操作,但是当我尝试实施多个时,事情就变得很冒险了。
我有很多未使用的包含,只是为了解决问题而乱搞东西。
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char **argv) {
printf("Starting\n");
pid_t pid;
pid_t pid2;
int mypipe0[2];
int mypipe1[2];
int mypipe2[2];
int mypipe3[2];
pipe(mypipe0);
pipe(mypipe1);
pipe(mypipe2);
pipe(mypipe3);
/* Create the child process. */
pid = fork();
std::cout << "Fork " << pid << std::endl;
// Child: Sorts Array
if (pid == 0) {
printf("pid == (pid_t) 0 p2\n");
/* This is the child process.
Close other end first. */
close(mypipe0[1]);
char valuesArray[5];
for (int a = 0; a < 5; a++)
read(mypipe0[0], &valuesArray[a], sizeof(char));
printf("finish reading mypipe0");
std::sort(valuesArray, valuesArray + 5);
printf("sorted");
close(mypipe1[0]);
close(mypipe2[0]);
for (int a = 0; a < 5; a++) {
write(mypipe1[1], &valuesArray[a], sizeof(char));
write(mypipe2[1], &valuesArray[a], sizeof(char));
}
close(mypipe1[1]);
close(mypipe2[1]);
exit(0);
}
else if (pid > 1) {
std::cout << "pid == (pid_t) 1" << std::endl;
/* This is the parent process.
Close other end first. */
close(mypipe0[0]); // Closes reading
int valuesArray[5];
valuesArray[0] = atoi(argv[1]);
valuesArray[1] = atoi(argv[2]);
valuesArray[2] = atoi(argv[3]);
valuesArray[3] = atoi(argv[4]);
valuesArray[4] = atoi(argv[5]);
printf("Argv init");
for (int a = 0; a < 5; ++a)
write(mypipe0[1], &valuesArray[a], sizeof(char));
printf("wrote to pipe 1");
close(mypipe0[1]);
wait(NULL);
close(mypipe1[1]); // Closes writing
// char outputArray[6];
int sortedArray[5];
for (int a = 0; a < 5; ++a)
read(mypipe1[0], &sortedArray[a], sizeof(char));
// Printing Array]
for (int a = 1; a < 5; ++a)
printf(", %d", sortedArray[a]);
printf("]");
// wait(NULL);
// close(mypipe2[1]); // Closes writing
// int median;
// read(mypipe1[0], median, sizeof(charian));
exit(0);
}
else {
pid2 = fork();
// Other child
if (pid2 == 0) {
printf("pid == (pid_t) 0\n");
/* This is the child process.
Close other end first. */
close(mypipe0[1]);
char valuesArray[5];
for (int a = 0; a < 5; a++)
read(mypipe0[0], &valuesArray[a], sizeof(char));
printf("finish reading mypipe0");
std::sort(valuesArray, valuesArray + 5);
printf("sorted");
close(mypipe1[0]);
close(mypipe2[0]);
for (int a = 0; a < 5; a++) {
write(mypipe1[1], &valuesArray[a], sizeof(char));
write(mypipe2[1], &valuesArray[a], sizeof(char));
}
close(mypipe1[1]);
close(mypipe2[1]);
exit(0);
}
}
}
我希望给定命令行参数 4 2 5 6 7 的输出为 2 4 5 6 7。相反,我得到 [1, 28932, 5, 6, -14276913]
第一个 fork
调用失败时到达 else
分支。
您当前的代码结构是,
pid = fork();
if (pid == 0)
{
runFirstChild();
}
else if (pid > 1)
{
runParent();
}
else // This branch is not taken unless there is a failure
{
pid2 = fork();
if (pid2 == 0)
{
runSecondChild();
}
}
你应该像这样构造它,
pid = fork();
if (pid == 0)
{
runFirstChild();
}
else if (pid > 1)
{
pid2 = fork(); // Fork the second child in the parent process
if (pid2 == 0)
{
runSecondChild();
}
else if (pid2 > 1)
{
runParent();
}
}