You are on page 1of 9

SVKM’S NMIMS

MUKESH PATEL SCHOOL OF TECHNOLOGY MANAGEMENT&


ENGINEERING

(Campus Name)

Academic Year: 2020-2021

Practical 2

Dear all,

Kindly complete the following task with your name in output file.

1.Write a Program to display that fork() is working and returning two process id.

Code :

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void forkexample()

printf("Madhav Vakharia - I062\n");

if(fork()==0)

printf("Hello from Child! %d\n",getpid());

else

printf("Hello from Parent! %d\n",getppid());

}
int main()

forkexample();

return 0;

Output :
2. Write a program to display "Hello World" with fork() with process id.

Code :

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main(){

int p_id,p_pid;

fork();

p_id=getpid();

p_pid=getppid();

printf("hello world is created by %d\t\t %d\n",p_id,p_pid);

printf(“Madhav Vakharia -I062\n”);

return 0;

}
Output :
3. Write a Program to display 1 to 100 numbers. child process print 1-50 and Parent process
should print 51-100.

Code :

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void childProcess()

int i;

for (i = 1; i <= 50; i++)

printf("The line is from Child value = %d\n", i);

printf("**Child process is done**\n");

void parentProcess()

int i;

for (i = 51; i <= 100; i++)

printf("The line is from Parent value = %d\n", i);

printf("**Parent process is done**\n");

}
int main()

pid_t pid;

pid = fork();

if (pid == 0)

childProcess();

else

parentProcess();

Output :

You might also like