I have a program that should launch another process and work simultaneously with it. I am using fork()
and system()
to accomplish this. I have code verifying that that my system()
call returns, but every time I execute I have to manually end the execution by typing ctrl c
. I can tell that one of my processes terminates, but I am not quite sure which one. I believe it is the parent. Both should be caught by return statements though.
Here is the parent process code (forkWaitTest):
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
pid_t childPID;
int status;
childPID = fork();
if(childPID >=0)
{
if(childPID == 0)
{
cout <<"I'm the fork child";
cout.flush();
status=system("child");
cout << "status = " << status;
//exit(0);
//cout << "Am I getting here?";
}
else{
cout << "I am the parent and can keep doing things ";
int y=1;
int x=7;
cout<< y+x << " ";
}
}
return 0;
}
This is the child process that is called
#include <stdio.h>
int main(int argc, char *argv[] )
{
printf("I am the child\n");
return 0;
}
Here is my output:
-bash-4.2$ forkWaitTest
I am the parent and can keep doing things 8 I'm the fork child-bash-4.2$ I am the child
status = 0