principle of operating system _fork

17
Forks Prof. Mrs. Trupti S. Indi Walchand Institute of Technology Department of Information Technology Solapur University

Upload: satyamevjayte-haxor

Post on 16-Apr-2017

690 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Principle Of Operating System _Fork

Forks

Prof. Mrs. Trupti S. IndiWalchand Institute of TechnologyDepartment of Information TechnologySolapur University

Page 2: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

2

System Calls

System Calls:The system call provides an interface to the operating system services.

Page 3: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

3

Three general methods exist for passing parameters to the OS:

1. Parameters can be passed in registers.2. Parameters can be stored in a block and the

block address can be passed as a parameter to a register.

3. Parameters can also be pushed on or popped off the stack by the operating system.

System Calls Parameters

Page 4: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

4

There are 5 different categories of system calls:- process control- file manipulation- device manipulation- information maintenance and - communication

Types of System Calls

Page 5: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

5

Forks

fork: It is a system call that creates a new process under the UNIX operating system.

Page 6: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

6

For example,

If a program contains a call to fork( ), the execution of the program results in

the execution of two processes.

One process is created to start executing the program. When the fork( )

system call is executed, another process is created. The original process is

called the parent process and the second process is called the child process.

The child process is an almost exact copy of the parent process.

Both processes continue executing from the point where the fork( ) calls

returns execution to the main program.

Since UNIX is a time-shared operating system, the two processes can

execute concurrently.

Page 7: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

7

#include <iostream>using namespace std;#include <sys/types.h> #include <unistd.h>

/* getpid() is a system call declared in unistd.h. It returns a value of type pid_t. This pid_t is a special type for process ids. */

/* It's equivalent to int. */

int main(void) {

for (int i = 0; i < 5; i++) {cout << "This is process " << getpid() << endl;/* sleep is a system call or library function that suspends this process for the indicated number of seconds */sleep(1);}return 0;

}

Page 8: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

8

Output

This is process 2281This is process 2281This is process 2281This is process 2281This is process 2281

Page 9: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

9

#include <iostream>using namespace std;#include <sys/types.h> #include <unistd.h> /* getpid() and fork() are

system calls declared in unistd.h. They return values of type pid_t. This pid_t is a special type for process ids.

It's equivalent to int. */

int main(void) { pid_t childpid;

int x = 5;childpid = fork();

while (1) {

cout << "This is process " << getpid() << endl;cout << "The parent of this process has id “ << getppid() << endl;cout << "childpid = " << childpid << endl;cout << "x = " << x << endl;sleep(1);x++;

}return 0;

}

Page 10: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

10

Output

?

Page 11: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

11

#include <iostream>using namespace std;#include <sys/types.h> #include <unistd.h> /* getpid() and fork() are

system calls declared in unistd.h. They return values of type pid_t. This pid_t is a special type for process ids.

It's equivalent to int. */

int main(void) {

pid_t childpid;childpid = fork();

for (int i = 0; i < 5; i++) {cout << "This is process " << getpid() << endl;/* sleep is a system call or library function that suspends this process for the indicated number of seconds */

sleep(2);}

return 0;}

Page 12: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

12

Output

This is process 2462This is process 2463This is process 2462This is process 2463This is process 2462This is process 2463This is process 2462This is process 2463This is process 2462This is process 2463

Page 13: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

13

Fedora Environment

Compile program# g++ F1.cpp -o f1Out

Execute or run program#./f1Out

Page 14: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

14

Some differences between the child and parent process are:

different pids

in the parent, fork( ) returns the pid of the child process if a child

process is created

in the child, fork( ) always returns 0

separate copies of all data, including variables with their current

values and the stack

separate program counter (PC) indicating where to execute next;

originally both have the same value but they are thereafter separate

after fork, the two processes do not share variables

Page 15: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

15

fork returns: • the pid of the new child process: to the parent

process; this is equivalent to telling the parent the name of its child.

• 0: to the child process • -1: 1 if there is an error; i.e., fork( ) failed

because a new process could not be created

Page 16: Principle Of Operating System _Fork

16

Check following for documentation

• man 2 getpid• man 2 fork• man 2 wait

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

Page 17: Principle Of Operating System _Fork

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

17

• http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/fork/fork.html

• http://www.sal.ksu.edu/faculty/tim/ossg/Introduction/sys_calls.html