Professional Documents
Culture Documents
Processes
Processes
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
int x = 4;
if (fork() == 0)
printf("Child has x = %d\n", ++x);
else
printf("Parent has x = %d\n", --x);
}
int main()
{
forkexample();
return 0;
}
Output:
Parent has x = 3
Child has x = 5
Different-works.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int x=8;
int main()
{
int y=10,pid;
printf("I am parent process before child and my pid=%d\n",getpid());
pid=fork();
if (pid == 0)
{ int z=100;
sleep(10);
printf("child z= %d\n", z) ;
printf("y= %d Child has x = %d\n", y,++x);
printf("I am child process my pid=%d, my parent id
=%d\n",getpid(),getppid());
}
else
{ int z=200;
sleep(20) ;
printf("parent z is =%d\n", z) ;
printf("Parent has x = %d y=%d \n", --x , y);
printf("I am parent process after child and my pid=%d, and my child
pid is= %d\n",getpid(),pid);
_exit(1);
}
return 0;
}
Output 1:
child z= 100
y= 10 Child has x = 9
parent z is =200
I am parent process after child and my pid=7786, and my child pid is= 7787
Output 2:
child z= 100
y= 10 Child has x = 9
parent z is =200
I am parent process after child and my pid=7889, and my child pid is= 7890
Global.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int x=8 ;
int main()
{
int y=10;
if (fork() == 0)
printf("y= %d Child has x = %d\n", y,++x);
else
printf("Parent has x = %d y=%d \n", --x , y);
return 0;
}
Output:
y= 10 Child has x = 9
Local.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int x=8 ;
int main()
{
int y=10;
if (fork() == 0)
{ int z=100;
printf("child z= %d", z) ;
printf("y= %d Child has x = %d\n", y,++x);
}
else
{ int z=200;
printf("parent z is =%d", z) ;
printf("Parent has x = %d y=%d \n", --x , y);
}
return 0;
}
Output:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int x=8 ;
int main()
{
int y=10,pid;
printf("I am parent process before child and my pid=%d\n",getpid());
pid=fork();
if (pid == 0)
{ int z=100;
sleep(10);
printf("child z= %d\n", z) ;
printf("y= %d Child has x = %d\n", y,++x);
printf("I am child process my pid=%d, my parent id
=%d\n",getpid(),getppid());
}
else
{ int z=200;
sleep(20) ;
printf("parent z is =%d\n", z) ;
printf("Parent has x = %d y=%d \n", --x , y);
printf("I am parent process after child and my pid=%d, and my child
pid is= %d\n",getpid(),pid);
_exit(1);
}
return 0;
}
Output 1:
child z= 100
y= 10 Child has x = 9
parent z is =200
I am parent process after child and my pid=8622, and my child pid is= 8623
Output 2:
child z= 100
y= 10 Child has x = 9
parent z is =200
I am parent process after child and my pid=8713, and my child pid is= 8714
Orphan.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int x=8 ;
int main()
{
int y=10,pid;
printf("I am parent process before child and my pid=%d\n",getpid());
pid=fork();
if (pid == 0)
{ int z=100;
sleep(10);
printf("child z= %d\n", z) ;
printf("y= %d Child has x = %d\n", y,++x);
printf("I am child process my pid=%d, my parent id
=%d\n",getpid(),getppid());
}
else
{ int z=200;
//sleep(20) ;
printf("parent z is =%d\n", z) ;
printf("Parent has x = %d y=%d \n", --x , y);
printf("I am parent process after child and my pid=%d, and my child
pid is= %d\n",getpid(),pid);
_exit(1);
}
return 0;
}
Output:
abhishek@ZephyrusG14:~/SSY-C-PGM$ ./a.out
parent z is =200
I am parent process after child and my pid=9250, and my child pid is= 9251
y= 10 Child has x = 9
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int x=8 ;
int main()
{
int y=10,pid;
printf("I am parent process before child and my pid=%d\n",getpid());
pid=fork();
if (pid == 0)
{ int z=100;
printf("child z= %d\n", z) ;
printf("y= %d Child has x = %d\n", y,++x);
printf("I am child process my pid=%d, my parent id
=%d\n",getpid(),getppid());
}
else
{ int z=200;
printf("parent z is =%d\n", z) ;
printf("Parent has x = %d y=%d \n", --x , y);
printf("I am parent process after child and my pid=%d, and my child
pid is= %d\n",getpid(),pid);
}
return 0;
}
Output 1:
parent z is =200
I am parent process after child and my pid=8812, and my child pid is= 8813
child z= 100
y= 10 Child has x = 9
Output 2:
parent z is =200
I am parent process after child and my pid=8837, and my child pid is= 8838
child z= 100
y= 10 Child has x = 9
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf("Hello world!\n");
return 0;
}
Output:
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!