University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.

Slides:



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

1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
The Process Model.
Thursday, June 08, 2006 The number of UNIX installations has grown to 10, with more expected. The UNIX Programmer's Manual, 2nd Edition, June, 1972.
Process Description and Control Module 1.0. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
Processes CSCI 444/544 Operating Systems Fall 2008.
Page 1 Processes and Threads Chapter 2. Page 2 Processes The Process Model Multiprogramming of four programs Conceptual model of 4 independent, sequential.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
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.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Operating Systems (CSCI2413) Lecture 3 Processes phones off (please)
Process Description and Control A process is sometimes called a task, it is a program in execution.
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 Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
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.
Introduction to Processes CS Intoduction to Operating Systems.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Implementing Processes and Process Management Brian Bershad.
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
Chapter 3 Process Description and Control
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
The Structure of Processes (Chap 6 in the book “The Design of the UNIX Operating System”)
Operating Systems 1 K. Salah Module 2.0: Processes Process Concept Trace of Processes Process Context Context Switching Threads –ULT –KLT.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
OS, , Part II Processes Department of Computer Engineering, PSUWannarat Suntiamorntut.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
8-Sep Operating Systems Yasir Kiani. 8-Sep Agenda for Today Review of previous lecture Process scheduling concepts Process creation and termination.
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
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
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.
Processes Dr. Yingwu Zhu. Process Concept Process – a program in execution – What is not a process? -- program on a disk - a process is an active object,
Operating Systems Process Creation
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
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.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Introduction to Kernel
Process Management Process Concept Why only the global variables?
Chapter 3: Processes.
Structure of Processes
Operating Systems Lecture 6.
Process & its States Lecture 5.
Processes Hank Levy 1.
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Lecture 6: Multiprogramming and Context Switching
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix and Windows
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
Process Description and Control in Unix
Processes Hank Levy 1.
Process Description and Control in Unix
Presentation transcript:

University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3

University of Pennsylvania 9/12/00CSE 3802 Concurrent Processes: fork(), wait() To see how processes can be used in application and how they are implemented, we study at how processes are created and manipulated in UNIX. Important source of info on UNIX is “man.” UNIX supports multiprogramming, so there will be many processes in existence at any given time. –Processes are created in UNIX with the fork() system call. –When a process P creates a process Q, Q is called the child of P and P is called the parent of Q. So, processes are organized as a "family tree."

University of Pennsylvania 9/12/00CSE 3803 UNIX Process Control At the root of the family tree of a UNIX system is the special process init: –created as part of the bootstrapping procedure –process-id = 1 –among other things, init spawns a child to listen to each terminal, so that a user may log on. –do "man init” learn more about it UNIX provides a number of system calls for process control including: –fork - used to create a new process –exec - to change the program a process is executing –exit - used by a process to terminate itself normally –abort - used by a process to terminate itself abnormally –kill - used by one process to kill or signal another –wait - to wait for termination of a child process –sleep - suspend execution for a specified time interval –getpid - get process id –getppid - get parent process id

University of Pennsylvania 9/12/00CSE 3804 The Fork System Call The fork() system call creates a "clone" of the calling process. Identical in every respect except (1) the parent process is returned a non-zero value (namely, the process id of the child) and (2) the child process is returned zero. The process id returned to the parent can be used by parent in a wait or kill system call. Typical usage of fork(): pid_t pid; pid = fork(); if (pid == 0) { /* code to be executed by child process */ } else { /* code executed by parent process */ } fork() > >

University of Pennsylvania 9/12/00CSE 3805 More on fork()  child process is an exact copy of parent except for value returned by the fork() call  other items copied not depicted (e.g. contents of CPU registers)  in practice, text segment is shared  child process begins its execution by returning from the fork() call! ParentChild text stack data text stack data Open file table etc.

University of Pennsylvania 9/12/00CSE 3806 Example of spawn using fork 1. #include 2. Main(){ 3. pid_t pid; 4. printf(“Just one process so far\n”); 5. pid = fork(); 6. if (pid = 0) 7. printf(“I’m the child\n”); 8. else if (pid > 0) 9. printf(“The parent, ch pid =%d\n”, 10. pid); 11. else 12. printf(“Fork returned error code\n”); 13. }

University of Pennsylvania 9/12/00CSE 3807 Why fork()? Often used in conjunction with exec or execve (the real system call) #include pid_t pid; if ( ( pid = fork() ) == 0 ) { execl( "/usr/games/tetris","tetris", "-easy",0 ) } else { wait( &status ) }

University of Pennsylvania 9/12/00CSE 3808 exec System Call A family of routines, execl, execv,..., all eventually make a call to execve. execlp( program_name, arg1, arg2,..., 0 )  text and data segments of current process replaced with those of program_name  stack reinitialized with parameters  open file table of current process remains intact  as in example, program_name is actually path name of executable file containing program Note: unlike subroutine call, there is no return after this call. That is, the process calling exec is gone forever!

University of Pennsylvania 9/12/00CSE 3809 Parent-Child Synchronization exit( status ) - executed by a child process when it wants to terminate. Makes status (an integer) available to parent. wait( &status ): return pid_t - suspends execution of process until *some* child process terminates –status indicates reason for termination –return value is process-id of terminated child

University of Pennsylvania 9/12/00CSE Process Termination Besides being able to terminate itself with exit, a process can be killed by another process using kill: –kill( pid, sig ) - sends signal sig to process with process- id pid. One signal is SIGKILL which means terminate the target process immediately. When a process terminates, all the resources it owns are reclaimed by the system: –“process control block” reclaimed –its memory is deallocated –all open files closed and Open File Table reclaimed. Note: a process can kill another process only if: –it belongs to the same user –super user

University of Pennsylvania 9/12/00CSE How shell executes a command This is how the command interpreter (the “shell”) executes your commands. when you type a command, the shell forks a clone of itself the child process makes an exec call, which causes it to stop executing the shell and start executing your command the parent process, still running the shell, waits for the child to terminate fork wait exec exit parent child shell command ….

University of Pennsylvania 9/12/00CSE Background Processing You may be aware that shell has capability of managing several concurrent processes. Example: % longjob & % jobs [1] running longjob % executes longjob in background mode. Notice the shell returns right away without waiting for longjob to finish. There are also fg, bf, and CTL-Z. How does shell do this? while (1) { /* main command loop */ /* print prompt */ /* read command */ if ( ( pid = fork() ) == 0 ) { execl( cmd, … ); } else { /* store pid in a table */ }

University of Pennsylvania 9/12/00CSE Multiprogramming (multiple processes) For each process, the O.S. maintains a data structure, called the process control block (PCB). The PCB provides a way of accessing all information relevant to a process: –This data is either contained directly in the PCB, or else the PCB contains pointers to other system tables. Processes (PCBs) are manipulated by two main components of the process subsystem in order to achieve the effects of multiprogramming: –Scheduler: determines the order by which processes will gain access to the CPU. Efficiency and fair-play are issues here. –Dispatcher: actually allocates CPU to process next in line as determined by the scheduler.

University of Pennsylvania 9/12/00CSE The Process Control Block (PCB) The PCB typically contains the following types of information: Process status (or state): new, ready to run, user running, kernel running, waiting, halted –Program counter: where in program the process is executing –CPU registers: contents of general-purpose register stack pointer, PSW, index registers –Memory Management info: segment base and limit registers, page table, location of pages on disk, process size –User ID, Group ID, Process ID, Parent PID,... –Event Descriptor: when process is in the “sleep” or waiting state –Scheduling info: process priority, size of CPU quantum, length of current CPU burst

University of Pennsylvania 9/12/00CSE PCB (cont.) –List of pending signals –Accounting info: process execution time, resource utilization –Real and Effective User IDs: determine various privileges allowed the process such as file access rights –More timers: record time process has spent executing in user and Kernel mode –Array indicating how process wishes to react to signals –System call info: arguments, return value, error field for current system call –Pending I/O operation info: amount of data to transfer, addr in user memory, file offset,... –Current directory and root: file system environment of process –Open file table: records files process has open

University of Pennsylvania 9/12/00CSE PCB in Unix In UNIX, the notion of a PCB is split in two: –Process Table entry The process table contains an entry for every process in system. Table is of fixed size (tunable) –User Area Extension of a process table entry. Created along with the creation of an actual process. These two structures point to each other!

University of Pennsylvania 9/12/00CSE Process States & Transitions user running kernel running ready to run asleep sys call or interrupt return interrupt interrupt return block/sleep wakeup context switch permissible schedule process

University of Pennsylvania 9/12/00CSE Context Switching Let us first review the user/system mode distinction. When system starts (during system bootstrapping or boot) it is in system mode. This is process 0 in V.2, which creates process 1 (init). This process execs /etc/init and is then executing in user mode. Process 1, like any user process, continues executing in user mode until one of the following: –interrupt by an asynchronous device like timer, disk, or terminal –the process makes a system call by executing an instruction to cause a software interrupt Occurrence of such an event causes the CPU to switch to system mode and begin execution appropriate interrupt handler.

University of Pennsylvania 9/12/00CSE When to context switch Typically, hardware automatically saves the user PC and PSW when interrupt occurs, and takes new PC and PSW from interrupt vector. Interrupt handler may simply perform its function and then return to the same process that was interrupted (restoring the PC and PSW from the stack). Alternatively, may no longer be appropriate to resume execution of process that was running because:  process has used up its current CPU quantum  process has requested I/O and must wait for results  process has asked to be suspended (sleep) for some amount of time  a signal or error requires process be destroyed (killed)  a “higher priority” process should be given the CPU In such a situation, a context switch is performed to install appropriate info for running a new process.

University of Pennsylvania 9/12/00CSE Mechanics of a Context Switch 1copy contents of CPU registers (general-purpose, SP, PC, PSW, etc.) into a save area in the PCB of running process 2change status of running process from “running” to “waiting” (or “ready”) 3change a system variable running-process to point to the PCB of new process to run 4copy info from register save area in PCB of new process into CPU registers Note:  context switching is pure overhead and should be done as fast as possible  often hardware-assisted - special instructions for steps 1 and 4  determining new process to run accomplished by consulting scheduler queues  step 4 will start execution of new process - known as dispatching.

University of Pennsylvania 9/12/00CSE MULTIPROGRAMMING Through CONTEXT SWITCHING executing process Aprocess B i/o request, timeout,... waiting save A’s state restore B’s state dispatch save B’s state restore A’s state