这是我的程序的一部分,其中父进程创建一个管道和分支,并使用管道将父代数字传递给子代。
除了从整数char转换之外,管道和分叉在main中不起作用的问题!
#include <iostream>
#include<sys/types.h>
#include<cstdlib>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sstream>
#include <unistd.h>
#include <cstdlib>
#include <sys/wait.h>
#include <assert.h>
#include <sys/types.h>
using namespace std;
int main()
{
int arr[20];
char arrCH[20];
//print process id
cout << "The current process id is: " << getpid() << endl;
//random value
int N = rand()% 10;
cout << "The genrated random value is: " << N << endl;
for (int i=1;i<N;i++)
{
arr[i] = rand()% 10;
arrCH[i] = (char)arr[i]; //somthing wrong
}
//create a pipe
int p1[2];
pipe(p1);
close(p1[0]);
write(p1[1],arrCH,strlen(arrCH)+1);
int r = fork(); //creat a child
if(r==0)
{
cout << "**The fist child**" <<endl;
close(p1[1]);
read(p1[0],arrCH,strlen(arrCH));
//sort the numbers
//creat another pipe
//send the sorted numbers to the second child using another pipe
int r2 = fork();
if(r2 == 0)
{
cout << "**The second child**" <<endl;
}
else
{
wait(NULL);
}
exit(0);
}
else
{
wait(NULL);
}
return 0;
}