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

Lab03 Processes

This document discusses processes and the fork() function in C++. It contains the following key points: 1. The fork() function is used to create child processes from a parent process. It returns twice - the parent process returns the PID of the child, while the child returns 0. 2. A program example is provided where the main process forks two child processes. Each child adds 1 to a variable 10 times. The parent then waits for the children to terminate. 3. Important notes about wait(), declaring main() as int, and clearing the terminal using clear are also included. Process termination, the ps command, and killing processes are briefly discussed.

Uploaded by

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

Lab03 Processes

This document discusses processes and the fork() function in C++. It contains the following key points: 1. The fork() function is used to create child processes from a parent process. It returns twice - the parent process returns the PID of the child, while the child returns 0. 2. A program example is provided where the main process forks two child processes. Each child adds 1 to a variable 10 times. The parent then waits for the children to terminate. 3. Important notes about wait(), declaring main() as int, and clearing the terminal using clear are also included. Process termination, the ps command, and killing processes are briefly discussed.

Uploaded by

Ruba Bdrani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Processes

Lab 03

1
Lab Objective
•  To practice creating child process using
fork().

2
The fork Function
•  In computing, when a process forks, it
creates a copy of itself, which is called a
"child process." The original process is
then called the "parent process .
•  The fork() function is used from a
parent process to create a duplicate
process, the child .
•  The parent and the child processes can tell
each other apart by examining the return
value of the fork() system call
3
The fork Function
pid_t fork(void);

•  If successful, the fork function returns


twice:
Parent Child
returns PID of the newly- returns 0
created child process

•  On failure, the fork function returns once:


Parent
returns -1 4
Parent and Child
•  A child inherits its parent's permissions,
working-directory, root-directory, open
files, etc.
•  All descriptors that were open in the
parent before the call to fork are shared
with the child after the fork returns.

5
Practice
Ex1:
•  In the following C++ program, the main
process forks two children.
•  Every child repeats adding the value 1 to
the variable “a” ten times.
•  Write, compile and run the program in
Linux.

8
1st fork()
>0 0

Parent Child 1
Add 1 to a
ten times,
2nd fork() one each
second
0

Parent Child 2
Wait for child Add 1 to a
termination ten times,
one each
second
9
#include <iostream>
#include <stdlib.h> /* exit() */
#include <unistd.h> /* fork() */
#include <sys/types.h> /* pid_t */
#include <sys/wait.h> /* wait() */
using std::cout; // it is a predefined variable which
allow to send data to the console to be printed as
The main process
text. It stands for “character output” forks child 1

int main() Error


{ When fork()
pid_t pid1, pid2, cpid;
int i, j, a, status;
returns a negative
a = 0; number, an error
happened
pid1 = fork(); //fork child 1 process
if (pid1 < 0) //error occurred Child 1
{
When fork()
cout<< "First Fork Failed\n";
exit(-1); returns 0, we are
}/end if in the child 1
else if (pid1 == 0) //child 1 process process
{ for (i=0; i<10; i++)
{ a++;
cout<< "Child1: a = "<<a<<"\n"; Here child 1 Add 1
sleep(1); to ‘a’ ten times
}//end for
}//end else if a= 10
0 11
else //parent process Parent
{ The parent forks
pid2 = fork(); //fork child 2 process another child
if (pid2 < 0) //error occurred
{
Error
cout<< “Second Fork Failed\n"; When fork()
exit(-1); returns a negative
}//end if number, an error
else if (pid2 == 0) //child 2 process
happened
{
for (j=0; j<10; j++) Child 2
{
When fork()
a++;
cout<< "Child2: a = " returns 0, we are
<<a<<"\n"; in the child 2
sleep(1); process
}//end for
}//end else if Here child 2 Add 1
else //parent process to ‘a’ ten times
{
cpid = wait(&status);
cout<< "\n*****Parent is a= 10
0
Closing*****\n";
exit(0); Parent
} When fork()
}//end else The parent waits returns a positive
}//end main
for children number, we are in
12
termination the parent process
Output

17
Output

18
Notes
•  wait() System Call

This function blocks the calling process until one of its child processes exits or
a signal is received. wait() takes the address of an integer variable and returns
the process ID of the completed process.

•  The main() should be declared as int , because when you declare it as void,
it causes an error.

•  clear command uses to Clear Linux Terminal.

10
Output

16
fork() in C

Fork system call is used for creating a new process,


which is called child process, which runs concurrently
with the process that makes the fork() call (parent
process). After a new child process is created, both
processes will execute the next instruction following the
fork() system call. A child process uses the same
pc(program counter), same CPU registers, same
open files which use in the parent process.
It takes no parameters and returns an integer value. Below are
different values returned by fork().

Negative Value: creation of a child process was unsuccessful.


Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains
process ID of newly created child process.

Samar Alsaleh 19
OS - CS242 - Spring 2009
Process Termination
•  Process executes last statement and asks
the operating system to delete it (exit)
•  Parent may terminate execution of
children processes (abort)
–  Child has exceeded allocated resources
–  Task assigned to child is no longer required
–  If parent is exiting
•  Some operating systems do not allow child to
continue if its parent terminates.

27
Check Off on Ex1

1) Why the final value of a is 10 and not 20?


2) Use the command ps –all in a separate
window while the above program is running.
Write down the PID of the processes related
to the program.
3) Kill child 1 and then child 2 while the
program is running. Briefly explain what will
happen.
4) Kill the main process while the program is
running. Briefly explain what will happen.

28
Important
•  The ps Command

Source:
https://docs.oracle.com/cd/E19455-01/805-7229/6j6q8svgp/
index.html 32
Important
•  The ps Command

Source:
https://docs.oracle.com/cd/E19455-01/805-7229/6j6q8svgp/
index.html 33
??? ANY QUESTIONS ???
J

34

You might also like