/*WRITE A PROGRAM (using fork() and/or exec() commands) where parent and child execute:*/
a) same program, same code.
//ducskecode.blogspot.com
a) same program, same code.
//ducskecode.blogspot.com
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main ()
{
int pid_t,pid, p;
p=fork();.
pid=getpid() ;
if(p<0)
{
printf(" Fork Failed ") ;
return 1;
}
printf("output of fork id %d \n",P);
printf("process id is : %d \n",pid);
return 0;
}
b) same program, different code
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int p_id;
pid fork();
if(p_id<0)
{
Print("\n Error "):
exit(1);
}
else if(p_id==0)
{
printf("\n Hello I an child process ");
printf("\n my pid is %d ",getpid ());
exit(o);
}
else
{
printf("\n Hello i am parent process ");
printf("\n My actual pid is %d \n",getpid ());
exit(1);
}
}
C) parents waits for child to terminate first then parent process terminates after child.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int pid;
pid=fork()
if(pid<0)
{
printf("\n Error ")
exit(1)
}
else if(pid==0)
{
printf("\n Hello I arn child process ");
printf("\n my pid is xd ",getpid ( ));
exit(0);
}
else if(pid>0)
{
Printf("\n Hello 1 an parent Process ").
printf("\n My actual pid is %d \n",getpid ());
wait(null);
exit(1);
}
}
Output:
No comments:
Post a Comment