0% found this document useful (0 votes)
256 views

Exp# 1a Fork System Call Aim: CS2257 Operating System Lab

This document describes an experiment to create a new child process using the fork system call in C. It provides the algorithm, program code, and output of the program. The algorithm declares a shared variable x, uses fork() to create the child process, and prints process IDs and the value of x from both the child and parent to show the child was successfully created as a copy of the parent process.

Uploaded by

Abuzar Sh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
256 views

Exp# 1a Fork System Call Aim: CS2257 Operating System Lab

This document describes an experiment to create a new child process using the fork system call in C. It provides the algorithm, program code, and output of the program. The algorithm declares a shared variable x, uses fork() to create the child process, and prints process IDs and the value of x from both the child and parent to show the child was successfully created as a copy of the parent process.

Uploaded by

Abuzar Sh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS2257 Operating System Lab

Exp# 1a fork system call

Aim
To create a new child process using fork system call.

Algorithm
1. Declare a variable x to be shared by both child and parent.
2. Create a child process using fork system call.
3. If return value is -1 then
a. Print "Process creation unsuccessfull"
b. Terminate using exit system call.
4. If return value is 0 then
a. Print "Child process"
b. Print process id of the child using getpid system call
c. Print value of x
d. Print process id of the parent using getppid system call
5. Otherwise
a. Print "Parent process"
b. Print process id of the parent using getpid system call
c. Print value of x
d. Print process id of the shell using getppid system call.
6. Stop

Result
Thus a child process is created with copy of its parent's address space.

http://cseannauniv.blogspot.com Vijai Anand


CS2257 Operating System Lab

Program

/* Process creation - fork.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

main()
{
pid_t pid;
int x = 5;
pid = fork();
x++;

if (pid < 0)
{
printf("Process creation error");
exit(-1);
}
else if (pid == 0)
{
printf("Child process:");
printf("\nProcess id is %d", getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of parent is %d\n", getppid());
}
else
{
printf("\nParent process:");
printf("\nProcess id is %d", getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of shell is %d\n", getppid());
}
}

http://cseannauniv.blogspot.com Vijai Anand


CS2257 Operating System Lab

Output

$ gcc fork.c

$ ./a.out
Child process:
Process id is 19499
Value of x is 6
Process id of parent is 19498

Parent process:
Process id is 19498
Value of x is 6
Process id of shell is 3266

http://cseannauniv.blogspot.com Vijai Anand

You might also like