You are on page 1of 3

a.

Write c program using fork system call to create a process and study parent child mechanism

#include <stdio.h>

#include<unistd.h>

#include<sys/types.h>

void forkexample(void);

void forkexample()

int a = 10;

printf("a = %d\n", a);

if(fork()==0)

printf("Child has a = %d \n", ++a);

else

printf("Parent has a = %d \n", --a);

void main()

forkexample();

Output:

b. /* C Compiler and Editor */

#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

void main()
{

int count = 0, ret;

ret = fork();

if(ret == 0)

printf("count in child=%d\n", count);

else

count = 1;

OUTPUT:

(the child has its own copy of the variable)

The parent executes the statement ”count = 1” before the child executes for the first time. Now,

what is the value of count printed by the code above?

c. /* C Compiler and Editor */

#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

void main()

int ret = fork();

if(ret == 0) {

printf("Hello1\n");

//execl("/bin/ls", "ls", NULL);

printf("Hello2\n");

} else if(ret > 0) {


wait();

printf("Hello3\n");

} else {

printf("Hello4\n");

You might also like