|
exec2.C
/* Copyright (c) 2002 Natkeeran Ledchumikanthan (nledchum@ee.ryerson.ca) */
/* Aim: Learn that Execl Function executes as the child process */
#include
#include
#include
#include
#include
/**
* Function main creates a child process and executes a file as that process?
*/
int main()
{ int fork_return;
int count = 0;
printf("===> (PARENT) hi, I am the parent process\n");
printf("===> (PARENT) My Pid is: %d \n", getpid());
printf("===> (PARENT) I will create a child.\n ");
fork_return = fork(); /* This forks a process */
if ( fork_return < 0)
{ printf(" Sorry \n");
exit(-1);
}
if (fork_return > 0)
{ printf("===> (PARENT) I will sleep for 10 seconds\n");
usleep(10000000);
printf("===> (PARENT) Killing my child \n");
printf("===> (PARENT): I am quiting too. \n ");
kill(getppid(), SIGQUIT);
kill(getpid(), SIGQUIT);
}
else
{
/* Lets fork it */
printf("===> (CHILD) hi, I am the child process %d \n ", getpid());
printf("===> (CHILD): calling hello process\n");
execl("hello","hello",NULL); /* The child executing the program hello*/
while (count <= 25)
{
printf("Child Timeline: %d second mark. \n", count);
usleep(2000000);
count = count + 5;
}
}
return(0);
}
|