#include<iostream>
#include <sys/wait.h>
#include <cstring>
#include <fstream>
#include <stdlib.h>
#include <unistd.h>
#include<fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sstream>
#include<signal.h>
using namespace std;
int main()
{
string postbox1[10];
string postbox2[10];
string postbox3[10];
string postbox4[10];
string postbox5[10];
string channel[20];
string line;
string convert;
ofstream p1_input("p1.in.txt");
ofstream p2_input("p2.in.txt");
ofstream p3_input("p3.in.txt");
ofstream p4_input("p4.in.txt");
ofstream p5_input("p5.in.txt");
if(p1_input && p2_input && p3_input && p4_input && p5_input)
{
cout<<"file is successfully created "<<endl;
for (int i=0;i<100;i++)
{
p1_input<<rand()%100<<endl;
p2_input<<rand()%100<<endl;
p3_input<<rand()%100<<endl;
p4_input<<rand()%100<<endl;
p5_input<<rand()%100<<endl;
}
}
cout<<"Parent PID "<<getpid()<<endl;
/*************************************************Producer processes tasks started**************************************************/
for(int i=1,j=0;i<=5;i++,j=j+3)
{
int status;
if(fork())
{
wait(&status);
}
else
{
cout<<"Child process"<<" PROD # "<<i<<" has pid of "<<getpid()<<" and parent process id is "<<getppid()<<endl;
cout<<endl;
if(i==1)
{
ifstream myfile ("p1.in.txt");
if (myfile.is_open())
{
getline (myfile,line);
ostringstream oss;
oss <<i<<line;
convert=oss.str();
for(int y=0;y<3;y++,j++)
{channel[j]=convert[y];}
cout<<endl<<"putting this into channel "<<endl;
getline (myfile,line);
myfile.close();
}
else cout << "Unable to open file";
}
if(i==2)
{
ifstream myfile ("p2.in.txt");
if (myfile.is_open())
{
getline (myfile,line);
ostringstream oss;
oss <<i<<line;
convert=oss.str();
for(int y=0;y<3;y++,j++)
{channel[j]=convert[y];}
cout<<endl<<"putting this into channel "<<endl;
getline (myfile,line);
myfile.close();
}
else cout << "Unable to open file";
}
if(i==3)
{
ifstream myfile ("p3.in.txt");
if (myfile.is_open())
{
getline (myfile,line);
ostringstream oss;
oss <<i<<line;
convert=oss.str();
for(int y=0;y<3;y++,j++)
{channel[j]=convert[y];}
cout<<endl<<"putting this into channel "<<endl;
getline (myfile,line);
myfile.close();
}
else cout << "Unable to open file";
}
if(i==4)
{
ifstream myfile ("p4.in.txt");
if (myfile.is_open())
{
getline (myfile,line);
ostringstream oss;
oss <<i<<line;
convert=oss.str();
for(int y=0;y<3;y++,j++)
{channel[j]=convert[y];}
cout<<endl<<"putting this into channel "<<endl;
getline (myfile,line);
myfile.close();
}
else cout << "Unable to open file";
}
if(i==5)
{
ifstream myfile ("p5.in.txt");
if (myfile.is_open())
{
getline (myfile,line);
ostringstream oss;
oss <<i<<line;
convert=oss.str();
for(int y=0;y<3;y++,j++)
{channel[j]=convert[y];}
cout<<endl<<"putting this into channel "<<endl;
getline (myfile,line);
myfile.close();
}
else cout << "Unable to open file";
}
exit(status);
}
}
/*************************************************Producer processes tasks completed**************************************************/
/*************ROUTER PROCESS**************now routing the items of each producer from channel to postbox*******************************/
int status,k=0;
if(fork())
{
wait(&status);
}
else
{
cout<<"The process Router now doing its job.Routing the items of each producer"<<endl<<"from channel to their respective postbox "<<endl;
cout<<endl;
string item;
}
return 0;
}
这是我的代码到目前为止。在这段代码中我在if(fork())的其他地方使用的通道数组,当我将数据(3位数字形成文件第1行)从转换字符串存储到通道数组中时。 。 它没有存储,但是当i == 2运行时,接下来存储了3个数字但是通道数组中的0 1 2变为空,等等当i == 3时0 1 2 3 4 5变为空,7 8 9包含最近存储的数据?为什么会这样?