C Program to show an example of Process Management
#include<unistd.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
pid_t childpid;
int retval;
int status;
childpid=fork();
if(childpid>=0)
{
if(childpid==0)
{
printf("CHILD : I am the child process\n");
printf("CHILD : Here's my PID : %d\n", getpid());
printf("CHILD : My parent's PID is : %d\n",getpid());
printf("CHILD : The value of my copy of childpid is :%d\n",childpid);
printf("CHILD : Sleeping for 4 seconds...\n");
sleep(4);
printf("CHILD : Enter an exit value (0 to 255): ");
scanf("%d",&retval);
printf("CHILD : Good Bye\n");
exit(retval);
}
else
{
printf("PARENT : I am the parent process\n");
printf("PARENT : Here's my PID : %d\n", getpid());
printf("PARENT : The value of my copy of childpid is :%d\n",childpid);
printf("PARENT : I will now wait for my child to exit.\n");
wait(&status);
printf("PARENT : CHILD's exit code is : %d\n", WEXITSTATUS(status));
printf("PARENT : Good Bye\n");
exit(0);
}
}
else
{
perror("fork");
exit(0);
}
}
Comments
Post a Comment