/* main program illustrating the UNIX fork() system call.
Compile using cc -o main main.c
*/
#include <stdio.h>
main() {
int fork(), childpid, value;
childpid = fork(); /* create a new process */
if(childpid == -1) {
perror("can’t fork. Help!!");
exit(-1);
}
else if(childpid == 0) { /* child process */
printf("child: my_process_id= %d,parent_process_id=%d\n", getpid(), getppid());
/* Transform the child process to another program called Hello */
/* Note the many variations of exec that exist! */
printf("Parent: About to run hello program \n");
if((value = execl("hello", "hello",0)) == -1) {
perror("couldn’t do exec!!");
exit(-1);
}
}
else { /* parent process */
printf("Parent: my_process_id=%d,my_child’s_process_id=%d",getpid(), childpid);
exit(0);
} /* end if */
}