Principle Of Operating System _Fork

46 %
54 %
Information about Principle Of Operating System _Fork
Software

Published on October 2, 2014

Author: ShubhamShah001

Source: slideshare.net

Description

Principle Of Operating System _Fork,Fork Creation,Fork Concept,Fork Program

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

2. System Calls System Calls: The system call provides an interface to the operating system services. 2 Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

3. System Calls Parameters 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. 3 Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

4. Types of System Calls There are 5 different categories of system calls: - process control - file manipulation - device manipulation - information maintenance and - communication 4 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. 5 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. 6 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; } 7 Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

8. Output This is process 2281 This is process 2281 This is process 2281 This is process 2281 This is process 2281 8 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; } 9 Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

10. Output ? 10 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; } 11 Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

12. Output This is process 2462 This is process 2463 This is process 2462 This is process 2463 This is process 2462 This is process 2463 This is process 2462 This is process 2463 This is process 2462 This is process 2463 12 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 13 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 14 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 15 Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

16. Check following for documentation • man 2 getpid • man 2 fork • man 2 wait 16 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/Intr oduction/sys_calls.html 17 Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

Add a comment

Related presentations

Speaker: Matt Stine Developing for the Cloud Track Marc Andressen has famou...

This presentation explains how to develop a Web API in Java using (JAX-RS or Restl...

1 App,

1 App,

November 10, 2014

How to bring innovation to your organization by streamlining the deployment proces...

Cisco Call-control solutions can handle voice, video and data

Nathan Sharp of Siemens Energy recently spoke at the SAP Project Management in Atl...

Related pages

Principles of Operating Systems - Free

Process-thread relationships Stallings, W. (2004) Operating Systems: Internals and Design Principles ... Principles of Operating Systems - 2. Processes 72
Read more

Principles of Operating Systems: Design and Applications ...

Principles of Operating Systems: ... The main focus of this text is to foster an understanding of operating system fundamentals: ...
Read more

Principles of Operating Systems | Naresh Chauhan ...

Principles of Operating Systems. Naresh Chauhan. Divided into eight parts, ... He is also the author of Software Testing: Principles and Practices [OUP, 2010].
Read more

CS 325-Principles of Operating Systems

CS 325 -- Spring 2012 PRINCIPLES OF OPERATING SYSTEMS: ... Operating System Concepts, ... Operating Systems: Internals and Design Principles, ...
Read more

Principles of Operating Systems - Free

4/27/2006 CS 446/646 - Principles of Operating Systems - 6. File System 49 6.d File System Implementation File block allocation
Read more

Principles of Operating Systems - Oxford University Press

>Principles of Operating Systems > Principles of Operating Systems; Teaching Resources. Online Resource Student Resources. Online Resource ...
Read more

Principles of Operating Systems - Donald Bren School of ...

Principles of Operating Systems ... not the operating system. ... Only three system calls needed for message transfer
Read more

Principles of Operating Systems - Naresh Chauhan - Oxford ...

Principles of Operating Systems Naresh Chauhan. ... Contains a separate chapter on shell programming that will be helpful for the operating system laboratory.
Read more

Operating Systems Principles (CSE 30341)

Operating Systems Principles (CSE 30341) Spring 2016 ... Projects; Links; Course Overview An operating system is a layer of software that manages hardware ...
Read more