Hi everybody!
I am trying to use fork() and wait to perform a simple task with c++ but I have noticed that the performance is quite different from what I expected.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int number = atoi(argv[1]);
int i;
int pid;
pid=fork();
if (pid!=0){
wait();
printf ("hi!");
}
else {
sleep (3);
int i;
printf("I am the child\n");
printf("my pid=%d\n", getpid());
printf("and my parent pid=%d\n", getppid());
}
}
This code should create a child process and the parent waits for the child to end to print "hi!".
If I compile this code with gcc "gcc -o fork2 fork2.c" the code works as expected, whereas if I use g++ "g++ -o fork2 fork2.c" the output is different and the parent doesn't wait the child to end to print his message!
I need this to work in a bigger c++ program.
I am using RHEL with 2.6.18-194 kernel
Any advice will be greatly apreciated!
Thanks in advance!