CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

The Process Model.
1 Processes Professor Jennifer Rexford
Processes CSCI 444/544 Operating Systems Fall 2008.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
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
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Process Concept An operating system executes a variety of programs
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Phones OFF Please Processes Parminder Singh Kang Home:
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
CE Operating Systems Lecture 5 Processes. Overview of lecture In this lecture we will be looking at What is a process? Structure of a process Process.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Computer Studies (AL) Operating System Process Management - Process.
Processes – Part I Processes – Part I. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Review on OSs Upon brief introduction of OSs,
System calls for Process management
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
CS212: OPERATING SYSTEM Lecture 2: Process 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
Operating Systems Process Creation
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
Lecture 4: Processes & Threads. Lecture 4 / Page 2AE4B33OSS Silberschatz, Galvin and Gagne ©2005 Contents The concept of Process Process states and life-cycle.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
System calls for Process management Process creation, termination, waiting.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Lecture 3 Process.
Chapter 3: Processes.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
Topic 3 (Textbook - Chapter 3) Processes
Operating System Concepts
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 3: Process Concept
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
System Structure and Process Model
Chapter 3: Processes.
System Structure and Process Model
Processes in Unix, Linux, and Windows
CGS 3763 Operating Systems Concepts Spring 2013
Lecture 2: Processes Part 1
System Structure B. Ramamurthy.
Processes in Unix, Linux, and Windows
System Structure and Process Model
Process & its States Lecture 5.
Chapter 3: Processes.
Jonathan Walpole Computer Science Portland State University
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6

CS4315 A. Berrached:CMS:UHD2 Processes n Process Concept n Process Scheduling n Operating on Processes n Threads n Internet Links to Multi-threads: –IBMIBM –Tom Wagner SiteTom Wagner Site

CS4315 A. Berrached:CMS:UHD3 Process Concept n An operating system executes a variety of programs: –Batch systems -- Jobs –Time-Shared system -- user programs or tasks n A Process -- a program in execution n A Process -- a schedulable unit of computation n There may be several processes executing the same program at the same time. E.g. several users running vi at the same time: –Each instance of vi creates a separate process.

CS4315 A. Berrached:CMS:UHD4 Process Manager n In a multi-programmed OS, several processes can be “executed at the same time”. n The Process Manager is that part of the OS that is responsible for managing all the processes on the system. n When the computer is powered on, there is only one program in execution: the initial process. n The initial process creates the OS, which can then create other processes as needed. n A process can create another process with a system call (e.g. fork in UNIX). n The created process is called a child process

CS4315 A. Berrached:CMS:UHD5 Process Manager n When a process is created, it specifies to the Process Manager its resource needs (e.g. memory requirements, files etc.) n The Process Manager allocates the needed resources and causes the process to be executed. n The process manager is responsible –for monitoring the state of each process executing on the system –process scheduling on CPU –process synchronization and deadlock –protection & security

CS4315 A. Berrached:CMS:UHD6

CS4315 A. Berrached:CMS:UHD7 Process Model n A process is composed of the following elements: –a program (code) –The data operated on by the process –A set of resources to provide an environment for execution –A Process Descriptor: a record kept by the OS to keep track of the progress of each process.Process Descriptor

CS4315 A. Berrached:CMS:UHD8 Process Descriptor Contains information associated with a process: n PID n Process state n Owner n Parent process n List of child processes n list of allocated resources n list of open files n …. n Copy of CPU registers at the last time the process executed on the CPU Process Descriptor Table

CS4315 A. Berrached:CMS:UHD9 Process Model CONTD Process creation/initialization: –Process Descriptor is created and initialized –Resources needed by the process are allocated (e.g. files, memory to store code, data, and stack). –Process may inherent some resources from its parent (e.g. open files, etc.) –Process Descriptor must reflect all allocated resources –Process is loaded in memory, into its Address Space, ready to begin executionAddress Space –From then on, process competes for CPU and other resources with other processes.

CS4315 A. Berrached:CMS:UHD10 Process Address Space n A process address space includes: –program code –data section –stack section n Process Descriptor is kept in OS space. Program text Data Section Main Memory Stack Section

CS4315 A. Berrached:CMS:UHD11 Process Model CONTD n Each process uses resources as its executes; main memory, I/O devices, files, and the CPU n The CPU is also a hardware resource n During execution a process may request other resources (e.g. more memory) and may release some of its resources ==> dynamic allocation/de- allocation n When a process can NOT get its requested resources it gets blocked in a queue waiting for that resource. n Multiprogramming: While one process uses the CPU, the remaining are using I/O resources or waiting for a resource (I/O or CPU) to be available.

CS4315 A. Berrached:CMS:UHD12 Process Model CONTD

CS4315 A. Berrached:CMS:UHD13 Process Scheduling Queues

CS4315 A. Berrached:CMS:UHD14 Context Switch

CS4315 A. Berrached:CMS:UHD15 Process Creation

CS4315 A. Berrached:CMS:UHD16 Process Creation

CS4315 A. Berrached:CMS:UHD17 Process Termination n Process executes last statement and asks the operating system to delete it –process resources are de-allocated by the operating system n A process may be terminated by another process –A parent terminates the execution of its children n When a process exits what happens to its children? –do not allow a child to exist if its parent has terminated ==> cascaded termination (VMS) –allow children to exist after parent ==> orphan processes (UNIX )

CS4315 A. Berrached:CMS:UHD18 UNIX Processes n Each process has its own address space –subdivided into code, data & stack –a.out file describes the address apace OS creates a Process Descriptor to manage each process. The collection of all Process Descriptors is referred to as the Process Descriptor Table

CS4315 A. Berrached:CMS:UHD19 UNIX Processes n Each process is assigned a unique process ID (PID) n The PID is essentially a pointer into the Process Table of the OS. A process can use the system call getpid() to obtain its own PID n Each process has one parent process (the process that created it), except for process 1 Process 1 ( the init process) is the ancestor of all other processes a process can use the system call getppid() to obtain the PID of it parent (i.e. PPID)

CS4315 A. Berrached:CMS:UHD20 UNIX Processes n When Unix is first started, it has only one process. The process is called "init", and its PID is 1. n The "init" process creates other operating system processes to do OS functions n For each port supporting user logins (e.g. a terminal), init creates a process running the getty program. n The getty process waits for a user to begin using the port. n When the port begins to be used, getty creates a new process to run the login program.

CS4315 A. Berrached:CMS:UHD21 UNIX Processes n The login process prompts the user for username and password, reads the username and password and verifies by looking up the /etc/passwd file. n If login successful, the login process changes directory to the user's directory and creates a new process running the shell program specified in the user's entry of the /etc/passwd file. n The shell process displays a "shell prompt" on the terminal and waits for the user to type a command.

CS4315 A. Berrached:CMS:UHD22 UNIX Processes n When the user types a command, the shell process, reads it, parses it, verifies it, and creates a new process running the program specified in the command. In the mean time, the shell process gets suspended until the command process finishes. n When the command process is done, the shell process is resumed again. n When the user logs out, the shell process is terminated and the login process is resumed, etc. etc. etc.

CS4315 A. Berrached:CMS:UHD23 UNIX Processes UNIX fork creates a process UNIX wait allows a process to wait for a child to terminate UNIX exec allows a child to run a new program

CS4315 A. Berrached:CMS:UHD24 Creating Processes UNIX fork() creates a process ==> fork( ) creates a child process that is identical to its parent, except that it has: –a different and unique PID –a different PPID n fork() –creates a new address space for the child process –copies code, data and stack into new address space –provides child with access to open files of parent.

CS4315 A. Berrached:CMS:UHD25 Process Creation: fork int pid; pid = fork(); the fork is called by the parent but returns in both the parent and the child –In the parent, it returns the PID of the child process –in the child it returns 0 n If fork() fails no child is created and -1 is returned to the parent. After the child is created, the parent and the child processes execute concurrently starting from the instruction following the fork.

CS4315 A. Berrached:CMS:UHD26 Process Creation: fork int pid; pid = fork( ) if ( pid == 0 ) { /* code for child here */ exit(0); } if (pid < 0) { /* fork failed... Put error handling code here */ } /*remaining code for parent goes here */

CS4315 A. Berrached:CMS:UHD27 #include int main( ) {int pid; if ( (pid = fork( ) ) == 0 ) {printf(“I am the child, my pid=%d and my parent pid=%d\n”, getpid( ), getppid( ) ); exit(0); } if (pid < 0) {fprintf(stderr, “fork failed\n”) exit(1); } printf(“I am the parent, my pid=%d\n”, getpid( ) ); }

CS4315 A. Berrached:CMS:UHD28 Process Creation: fork( ) n After the child is created, the parent and the child processes execute concurrently starting from the instruction following the fork n Since only one can be using CPU at a time, either may go first.

CS4315 A. Berrached:CMS:UHD29 Example: Chain of Processes #include int i, n, pid; for (i=1, i < n; ++i ) if ( ( pid=fork() ) != 0) break; fprintf(stdout,”This is process %d with parent %d\n”, getpid(),getppid() );

CS4315 A. Berrached:CMS:UHD30 Example: a fan of processes #include int i, n, pid; for (i=1, i<n; ++i) if ( (pid=fork()) == 0) break; fprintf(stdout,”This is process %d with parent %d\n”, getpid(),getppid() );

CS4315 A. Berrached:CMS:UHD31 Synchronization of Parent and Child n What happens to the parent after it creates a child? –They both execute concurrently If parent wants to wait until the child terminates before proceeding further it executes a wait() or waitpid() system call. n When a process is terminated (with exit( ) ), a signal is sent to its parent notifying it of the termination.

CS4315 A. Berrached:CMS:UHD32 exit( ) n void exit(int status) n exit( ) closes all process' file descriptors, de-allocates code, data, and stack, and then terminates the process. n It sends a signal to the parent process telling of its termination status and waits until the parent accepts the signal. n A process that is waiting for its parent to accept its termination is called a "zombie" n A parent accepts a child's termination by executing wait( ).

CS4315 A. Berrached:CMS:UHD33 Synch. of Parent and Child int pid; pid = fork( ) if ( pid == 0 ) { /* child executes this part concurrently with parent */ exit(0); } /*parent works concurrently with child and independent of each other*/

CS4315 A. Berrached:CMS:UHD34 Synch. of Parent and Child int pid; pid = fork( ) if ( pid == 0 ) { /* child executes this part concurrently with parent */ exit(0); } wait(...); /* parent waits for child*/ /*parent proceeds*/

CS4315 A. Berrached:CMS:UHD35 Synch. of Parent and Child If a parent terminates without waiting for a child, child becomes an orphan and is adopted by the system init process by setting their PPID to 1. n init periodically executes wait( ) to remove zombies from the system.

CS4315 A. Berrached:CMS:UHD36 Wait() #include int wait(int *status); 1. if there are no child processes, wait returns with -1 (an error) 2. if one or more processes are already in the zombie state, wait selects an arbitrary one, stores its status in status, and returns its PID 3. otherwise, wait sleeps until one of the child processes terminates and then goes to step 2

CS4315 A. Berrached:CMS:UHD37 Child Executing a Different Program n Parent process calls fork to create a child process child process calls an exec system call. n The exec system call replaces the address space of the child with a new program n several exec calls: execv, execvp, etc. int execv(char *filename, char *argv[ ]); n filename is the name of an executable file n argv is the command-line arguments for the executable program.

CS4315 A. Berrached:CMS:UHD38 Example:Parent main( ) { Int pid; /*code to set up the argv array for the child here*/ pid = fork(); if (pid==0) { execv(child_prog, argv); /* execv does not return unless there is an error*/ fprintf(stderr,”error in the exec…terminating child..”); exit(1); } wait( );/*parent waits for child to terminate*/ …….}

CS4315 A. Berrached:CMS:UHD39 Example:Child File child_prog.c: main( ) { /* code to be executed by child process */ } n child_prog.c must be compiled into an executable file.

CS4315 A. Berrached:CMS:UHD40 Threads (read Chap2) n A thread (or lightweight process) is a basic unit of computation. It uses less state and less resources than heavyweight process n In a system that supports threads, each (heavyweight) process consist of one or more threads. n A traditional process is a process with one thread.

CS4315 A. Berrached:CMS:UHD41 Threads

CS4315 A. Berrached:CMS:UHD42 Threads n Each thread has its own –thread ID –program counter –stack space –possibly some of its own data n A thread shares with its sibling threads: –code section –data section –operating system resources (e.g. open files, CPU)

CS4315 A. Berrached:CMS:UHD43 POSIX Threads n DEC Unix conforms to the POSIX standard ==> # include n When a thread is created it is assigned a thread ID, a stack, a starting address for execution. n pthread_create(ChildID, Thread_attributes, function_name, arguments) –ChildID is the returned child ID –Thread_attributes: set to NULL for default attributes –function_name: function to be executed by the thread –arguments: a pointer to the argument to be passed to the function

CS4315 A. Berrached:CMS:UHD44 Why threads? n Reduced context switching overhead n An application that needs to block occasionally waiting for I/O (e.g. disk): While one thread waits, a second thread can run and do other computation==> better performance for the application. n Windowing systems: –heavyweight process: physical screen manager –a thread for each window: all threads share the same physical screen.

CS4315 A. Berrached:CMS:UHD45 Threads for a windows system

CS4315 A. Berrached:CMS:UHD46 Why threads? n Since sibling threads use same data segment, data sharing among cooperating threads is easily achieved + Applications that require sharing a common buffer (e.g. producer-consumer) can benefit from thread utilization. –no protection between threads: synchronization among threads when accessing shared data must be enforced by the programmer. n Threads can be used in multiprocessor systems (each thread runs on a separate processor, they all share same address space on a shared memory).

CS4315 A. Berrached:CMS:UHD47 User vs. kernel threads n A heavyweight process consists of one or more sibling threads, all competing for the processor n A thread scheduler (or thread manager) switches the processor among the threads. n In some systems, the thread manager is a user program, supported by a thread library (user- supported threads)-- older versions of UNIX n In other systems the thread manager is part of the OS (kernel-supported threads)-- OS/2, NT, Solaris

CS4315 A. Berrached:CMS:UHD48 User vs. kernel threads n User threads: –switching among threads is faster -- since they don’t involve calling the kernel –Any call to the OS can cause the whole heavywheight process to be context switched out of the CPU (since in this case OS supports only process scheduling). – Thread scheduling can be unfair, in favor of processes with large number of threads