exec functions under unix
Code:
#include <iostream>
#include <cstring>
#include <ctime>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <poll.h>
#include "string2int.h"
using namespace std;
void parent(int m);
int main()
{
parent(100);
return 0;
}
void parent(int m)
{
int pipe1[2];
int pipe2[2];
pipe(pipe1);
pipe(pipe2);
char buf[80];
const int BSIZE = 100;
int pid = fork();
if(pid < 0)
cout<<"ERROR"<<endl;
else if(pid == 0)
{
int b;
pollfd my_fds; int poll_ret;
my_fds.fd = pipe1[0];
my_fds.events = POLLRDNORM;
my_fds.revents = POLLRDNORM;
buf[0]='a';
do
{
poll_ret=poll(&my_fds, (nfds_t)1, 10);
if(poll_ret>0)
{
read(pipe1[0],buf,BSIZE);
cout<<"parent: ";
for(b=0; buf[b]!='#';b++)
{
if(buf[b] == '*')
{
cout<<endl;
cout<<"parent: ";
}
else
cout<<buf[b];
}
cout<<endl;
write(pipe2[1],"RESPONSE#",9);
}
}while(buf[0] != 'b');
}
else
{
int numMsgs=m; const int BSIZE2 =100;
int msgCount=1; int n;
string pLinStr;
string pMsgStr;
int pRanLines;
int lineTot=0;
char response[80];
char pMsgLine[80];
pollfd my_fds2; int poll_ret2;
my_fds2.fd = pipe2[0];
my_fds2.events = POLLRDNORM;
my_fds2.revents = POLLRDNORM;
int x;
int y;
int z;
int a;
srand(time(0));
while(msgCount < 101)
{
pRanLines = (1+ rand() %5);
if(msgCount == 100)
write(pipe1[1],"bye#",4);
else
{
y=0;
for(x=0; x<pRanLines; x++)
{
pMsgLine[y] = 'm';
y++;
pMsgStr = BasicTypeToStr(msgCount);
for(z=0; z<pMsgStr.size(); z++)
{
pMsgLine[y]=pMsgStr[z];
y++;
}
pMsgLine[y]=' ';
y++;
pMsgLine[y]='l';
y++;
pMsgLine[y]='i';
y++;
pMsgLine[y]='n';
y++;
pMsgLine[y]='e';
y++;
pMsgLine[y]=' ';
y++;
lineTot++;
pLinStr=BasicTypeToStr(lineTot);
for(z=0; z<pLinStr.size(); z++)
{
pMsgLine[y]=pLinStr[z];
y++;
}
pMsgLine[y]='*';
if(x == pRanLines-1)
pMsgLine[y]='#';
y++;
}
write(pipe1[1], pMsgLine, y);
do
{
poll_ret2=poll(&my_fds2, (nfds_t)1, 10);
if(poll_ret2>0)
{
read(pipe2[0],response,BSIZE2);
for(a=0;response[a]!='#';a++)
cout<<response[a];
cout<<endl;
}
}while(poll_ret2 <= 0);
}
msgCount++;
}
}
}
the code within else if(pid == 0) statement needs to be executed in that is another image, and I need to use one of the exec() fucntions, how could i make it so that the code executes in a different file and pass the pipes to the code so that messages can be transfered?