System calls for Process management Process creation, termination, waiting.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Zombie and orphan processes. Zombie process (from wikipedia) When a process ends, all of the memory and resources associated with it are deallocated.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Threads, Thread management & Resource Management.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions.
Creating and Executing Processes
CE Operating Systems Lecture 10 Processes and process management in Linux.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
System calls for Process management
Operating Systems Processes 1.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Threads, Thread management & Resource Management.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Section 8: Processes What is a process Creating processes Fork-Exec
Using Processes.
Unix Process Management
Processes in Unix, Linux, and Windows
UNIX PROCESSES.
System Structure and Process Model
System Structure and Process Model
Processes in Unix, Linux, and Windows
Fork and Exec Unix Model
System Structure B. Ramamurthy.
Processes in Unix, Linux, and Windows
System Structure and Process Model
Tutorial 3 Tutorial 3.
Operation System Program 1
Processes Prof. Ikjun Yeom TA – Mugyo
Tutorial: The Programming Interface
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix, Linux, and Windows
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
System Programming: Process Management
Presentation transcript:

System calls for Process management Process creation, termination, waiting

System calls Fork() Exec() Wait() Exit()

Operating Systems: A Modern Perspective, Chapter 6 Creating a Process in UNIX pid = fork(); UNIX kernel … Process Table Process Descriptor

Fork() Will spawn a new child process which is an identical process to the parent except that has a new system pid The process is copied in memory from the parent and a new process structure is assigned by the kernel

Fork() It takes no arguments and returns a process ID. Returns a –ve value if the creation is unsuccessful After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of fork(): – The return value to the parent process will be the Process ID (PID) of the child, whilst the child will get a return value of 0.

example If the call to fork() is executed successfully, Unix will make two identical copies of address spaces, one for the parent and the other for the child. Both processes will start their execution at the next statement following the fork() call. In this case, both processes will start their execution at the assignment statement as shown below

Both processes start their execution right after the system call fork(). Since both processes have identical but separate address spaces, those variables initialized before the fork() call have the same values in both address spaces. Since every process has its own address space, any modifications will be independent of the others. In other words, if the parent changes the value of its variable, the modification will only affect the variable in the parent process's address space.

Sample program

The kernel does the following sequence of operations for fork system call 1. It allocates a slot in the process table for the new process. 2. It assigns a unique ID number to the child process. 3. It makes a logical copy o f the context o f the parent process. 4. It increments counters for files associated with the process. 5. It returns the ID number o f the child to the parent process, and a 0 value to the child process.

Logical view of parent and child processes and their relationships to other kernel data structures immediately after completion of the fork system call

Forking provides a way for an existing process to start a new one, but what about the case where the new process is not part of the same program as parent process? This is where the exec system call comes into play. exec will replace the contents of the currently running process with the information from a program binary

Exec "The exec family of functions replaces the current process image with a new process image.“ Commonly a process generates a child process because it would like to transform the child process by changing the program code the child process is executing. The text, data and stack segment of the process are replaced and only the u (user) area of the process remains the same.

Exec versions The versions of exec are: execl execv execle execve execlp execvp The naming convention: exec* 'l' indicates a list arrangement (a series of null terminated arguments) 'v' indicate the array or vector arrangement (like the argv structure). 'e' indicates the programmer will construct (in the array/vector format) and pass their own environment variable list 'p' indicates the current PATH string should be used when the system searches for executable files.

A few additional notes about fork(): an orphan is a child process that continues to execute after its parent has finished execution (or died) to avoid this problem, the parent should execute a wait system call

Wait system call A parent process usually needs to synchronize its actions by waiting until the child process has either stopped or terminated its actions. The wait() system call allows the parent process to suspend its activities until one of these actions has occurred. If any child processes are still active, the calling process will suspend its activity until a child process terminates.

Sample program for wait() int status; pid_t fork_return; fork_return = fork(); if (fork_return == 0) /* child process */ { printf("\n I'm the child!"); exit(0); } else /* parent process */ { wait(&status); printf("\n I'm the parent!"); if (WIFEXITED(status)) printf("\n Child returned: %d\n", WEXITSTATUS(status)); }

Wait summary A few notes on this program: wait(&status) causes the parent to sleep until the child process is finished execution details of how the child stopped are returned via the status variable to the parent.

How Linux actually handles fork and exec Clone () system call In the kernel, fork is actually implemented by a clone system call. This clone interfaces effectively provides a level of abstraction in how the Linux kernel can create processes. clone allows you to explicitly specify which parts of the process are copied into the new process, and which parts are shared between the two processes. This may seem a bit strange at first, but allows us to easily implement threads with one very simple interface.

Clone ( ) System Call Thread clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0); Process with fork( ) clone(SIGCHLD, 0); Process with vfork( ) clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0);

Exit() The exit operation typically performs clean-up operations within the process space before returning control back to the operating system. When the child process terminates ("dies"), either normally by calling exit, or abnormally due to a fatal error or signal (e.g., SIGTERM, SIGINT, SIGKILL), an exit status is returned to the operating system and a SIGCHLD signal is sent to the parent process. The exit status can then be retrieved by the parent process via the wait system call.

Orphans and zombies : operating systems handle a child process whose parent process has terminated in a special manner. Such an orphan process becomes a child of a special process, A similar strategy is used to deal with a zombie process, which is a child process that has terminated but whose exit status is ignored by its parent process. Such a process becomes the child of a special process, which retrieves the child's exit status and allows the operating system to complete the termination of the dead process.

#include 5 int main(void) { pid_t pid; 10 printf("parent : %d\n", getpid()); pid = fork(); if (pid == 0) { 15 printf("child : %d\n", getpid()); sleep(2); printf("child exit\n"); exit(1); } 20 else{ /* in parent */ while (1) { sleep(1); 25 }} } ps ax | grep [z]ombie pts/9 S 0:00./zombie pts/9 Z 0:00 [zombie]

System calls their corresponding low level algorithms