You are on page 1of 16

Address-space.

#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:

I am parent process before child and my pid=7786

child z= 100

y= 10 Child has x = 9

I am child process my pid=7787, my parent id =7786

parent z is =200

Parent has x = 7 y=10

I am parent process after child and my pid=7786, and my child pid is= 7787

Output 2:

I am parent process before child and my pid=7889

child z= 100

y= 10 Child has x = 9

I am child process my pid=7890, my parent id =7889

parent z is =200

Parent has x = 7 y=10

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:

Parent has x = 7 y=10

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:

parent z is =200Parent has x = 7 y=10

child z= 100y= 10 Child has x = 9


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 1:

I am parent process before child and my pid=8622

child z= 100

y= 10 Child has x = 9

I am child process my pid=8623, my parent id =8622

parent z is =200

Parent has x = 7 y=10

I am parent process after child and my pid=8622, and my child pid is= 8623

Output 2:

I am parent process before child and my pid=8713

child z= 100

y= 10 Child has x = 9

I am child process my pid=8714, my parent id =8713

parent z is =200

Parent has x = 7 y=10

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

I am parent process before child and my pid=9250

parent z is =200

Parent has x = 7 y=10

I am parent process after child and my pid=9250, and my child pid is= 9251

abhishek@ZephyrusG14:~/SSY-C-PGM$ child z= 100

y= 10 Child has x = 9

I am child process my pid=9251, my parent id =957


Pids.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;
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:

I am parent process before child and my pid=8812

parent z is =200

Parent has x = 7 y=10

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

I am child process my pid=8813, my parent id =957

Output 2:

I am parent process before child and my pid=8837

parent z is =200

Parent has x = 7 y=10

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

I am child process my pid=8838, my parent id =957


Process-fork.c

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{

// make two process which run same


// program after this instruction
fork();
fork();
fork();

printf("Hello world!\n");
return 0;
}
Output:

Hello world!

Hello world!

Hello world!

Hello world!

Hello world!

Hello world!

Hello world!

Hello world!

You might also like