1 OS Structure, Processes & Process Management. 2 What is a Process? A process is a program during execution.  Program = static file (image)  Process.

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

CSE 451: Operating Systems Winter 2007 Module 4 Processes Ed Lazowska Allen Center 570.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
CSE 451: Operating Systems Winter 2006 Module 4 Processes Ed Lazowska Allen Center 570.
1 Last Time: OS & Computer Architecture Modern OS Functionality (brief review) Architecture Basics Hardware Support for OS Features.
Processes CSCI 444/544 Operating Systems Fall 2008.
Operating Systems Processes (Ch 4.1). Processes “A program in execution” Modern computers allow several at once –“pseudoparallelism” A B C Program Counter.
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.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Processes April 5, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Processes CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Processes CS Intoduction to Operating Systems.
Implementing Processes and Process Management Brian Bershad.
Chapter 41 Processes Chapter 4. 2 Processes  Multiprogramming operating systems are built around the concept of process (also called task).  A process.
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
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
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
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.
System calls for Process management
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
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 CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems Process Creation
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.
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.
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.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
Lecture Topics: 11/1 Processes Process Management
Process Management Presented By Aditya Gupta Assistant Professor
Operating Systems: A Modern Perspective, Chapter 6
Chapter 3: Processes.
Review An OS in action Processes and Programs
Processes in Unix, Linux, and Windows
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Cs561 Presenter: QIAOQIAO CHEN Spring 2012
Processes in Unix, Linux, and Windows
System Structure and Process Model
Lecture Topics: 11/1 General Operating System Concepts Processes
Processes Hank Levy 1.
Processes and Process Management
Chapter 3: Processes.
October 7, 2002 Gary Kimura Lecture #4 October 7, 2002
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Operating Systems: A Modern Perspective, Chapter 6
Processes in Unix, Linux, and Windows
CSE 451: Operating Systems Autumn 2009 Module 4 Processes
IT 344: Operating Systems Winter 2008 Module 4 Processes
Processes in Unix and Windows
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Processes Hank Levy 1.
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

1 OS Structure, Processes & Process Management

2 What is a Process? A process is a program during execution.  Program = static file (image)  Process = executing program = program + execution state. A process is the basic unit of execution in an operating system  Each process has a number, its process identifier (pid). Different processes may run different instances of the same program  E.g., my javac and your javac process both run the Java compiler At a minimum, process execution requires following resources:  Memory to contain the program code and data  A set of CPU registers to support execution

3 Program to Process We write a program in e.g., Java. A compiler turns that program into an instruction list. The CPU interprets the instruction list (which is more a graph of basic blocks). void X (int b) { if(b == 1) { … int main() { int a = 2; X(a); }

4 Process in Memory Program to process. void X (int b) { if(b == 1) { … int main() { int a = 2; X(a); } What you wrote What is in memory. void X (int b) { if(b == 1) { … int main() { int a = 2; X(a); } Code main; a = 2 X; b = 2 Heap Stack What must the OS track for a process?

5 Processes and Process Management Details for running a program A program consists of code and data On running a program, the loader:  reads and interprets the executable file  sets up the process’s memory to contain the code & data from executable  pushes “argc”, “argv” on the stack  sets the CPU registers properly & calls “_start()” Program starts running at _start() _start(args) { initialize_java(); ret = main(args); exit(ret) } we say “process” is now running, and no longer think of “program” When main() returns, OS calls “exit()” which destroys the process and returns all resources

6 Keeping track of a process A process has code.  OS must track program counter (code location). A process has a stack.  OS must track stack pointer. OS stores state of processes’ computation in a process control block (PCB).  E.g., each process has an identifier (process identifier, or PID) Data (program instructions, stack & heap) resides in memory, metadata is in PCB (which is a kernel data structure in memory)

7 Process Life Cycle Processes are always either executing, waiting to execute or blocked waiting for an event to occur Running Ready Blocked Start Done A preemptive scheduler will force a transition from running to ready. A non-preemptive scheduler waits.

8 Process Contexts Example: Multiprogramming Operating System “System Software” User Program 1 User Program 2 User Program n... Program 1Program 2OS I/O Device k: read() k+1: startIO() endio{ interrupt main{ } read{ } } schedule() Memory save state schedule() restore state save state save state

9 When a process is waiting for I/O what is its scheduling state? 1. Ready 2. Running 3. Blocked 4. Zombie 5. Exited

10 Scheduling Processes OS has PCBs for active processes. OS puts PCB on an appropriate queue.  Ready to run queue.  Blocked for IO queue (Queue per device).  Zombie queue. Stopping a process and starting another is called a context switch.  ,000 per second, so must be fast.

11 Why Use Processes? Consider a Web server get network message (URL) from client fetch URL data from disk compose response send response How well does this web server perform? With many incoming requests? That access data all over the disk?

12 Why Use Processes? Consider a Web server get network message (URL) from client create child process, send it URL Child fetch URL data from disk compose response send response If server has configuration file open for writing  Prevent child from overwriting configuration How does server know child serviced request?  Need return code from child process

13 The Genius of Separating Fork/Exec Life with CreateProcess(filename);  But I want to close a file in the child. CreateProcess(filename, list of files);  And I want to change the child’s environment. CreateProcess(filename, CLOSE_FD, new_envp);  Etc. (and a very ugly etc.) fork() = split this process into 2 (new PID)  Returns 0 in child  Returns pid of child in parent exec() = overlay this process with new program (PID does not change)

14 The Genius of Separating Fork/Exec Decoupling fork and exec lets you do anything to the child’s process environment without adding it to the CreateProcess API. int ppid = getpid(); // Remember parent’s pid fork();// create a child if(getpid() != ppid) {// child continues here // Do anything (unmap memory, close net connections…) exec(“program”, argc, argv0, argv1, …); } fork() creates a child process that inherits:  identical copy of all parent’s variables & memory  identical copy of all parent’s CPU registers (except one) Parent and child execute at the same point after fork() returns:  by convention, for the child, fork() returns 0  by convention, for the parent, fork() returns the process identifier of the child  fork() return code a convenience, could always use getpid()

15 Program Loading: exec() The exec() call allows a process to “load” a different program and start execution at main (actually _start). It allows a process to specify the number of arguments (argc) and the string argument array (argv). If the call is successful  it is the same process …  but it runs a different program !! Code, stack & heap is overwritten  Sometimes memory mapped files are preserved.

16 What creates a process? 1. Fork 2. Exec 3. Both

17 General Purpose Process Creation In the parent process: main() … int ppid = getpid(); // Remember parent’s pid fork(); // create a child if(getpid() != ppid) {// child continues here exec_status = exec(“calc”, argc, argv0, argv1, …); printf(“Why would I execute?”); } else {// parent continues here printf(“Who’s your daddy?”); … child_status = wait(pid); }

18 pid = 127 open files = “.history” last_cpu = 0 pid = 128 open files = “.history” last_cpu = 0 A shell forks and then execs a calculator int pid = fork(); if(pid == 0) { close(“.history”); exec(“/bin/calc”); } else { wait(pid); int pid = fork(); if(pid == 0) { close(“.history”); exec(“/bin/calc”); } else { wait(pid); Process Control Blocks (PCBs) OS USER int pid = fork(); if(pid == 0) { close(“.history”); exec(“/bin/calc”); } else { wait(pid); int calc_main(){ int q = 7; do_init(); ln = get_input(); exec_in(ln); pid = 128 open files = last_cpu = 0 int pid = fork(); if(pid == 0) { close(“.history”); exec(“/bin/calc”); } else { wait(pid);

19 pid = 127 open files = “.history” last_cpu = 0 pid = 128 open files = “.history” last_cpu = 0 A shell forks and then execs a calculator int shell_main() { int a = 2; … Code main; a = 2 Heap Stack 0xFC0933CA int shell_main() { int a = 2; … Code main; a = 2 Heap Stack 0xFC0933CA int calc_main() { int q = 7; … Code Heap Stack 0x pid = 128 open files = last_cpu = 0 Process Control Blocks (PCBs) OS USER

20 At what cost, fork()? Simple implementation of fork():  allocate memory for the child process  copy parent’s memory and CPU registers to child’s  Expensive !! In 99% of the time, we call exec() after calling fork()  the memory copying during fork() operation is useless  the child process will likely close the open files & connections  overhead is therefore high vfork()  a system call that creates a process “without” creating an identical memory image  child process should call exec() almost immediately  Unfortunate example of implementation influence on interface  Current Linux & BSD 4.4 have it for backwards compatibility  Copy-on-write to implement fork avoids need for vfork

21 Orderly Termination: exit() After the program finishes execution, it calls exit() This system call:  takes the “result” of the program as an argument  closes all open files, connections, etc.  deallocates memory  deallocates most of the OS structures supporting the process  checks if parent is alive:  If so, it holds the result value until parent requests it; in this case, process does not really die, but it enters the zombie/defunct state  If not, it deallocates all data structures, the process is dead  cleans up all waiting zombies Process termination is the ultimate garbage collection (resource reclamation).

22 The wait() System Call A child program returns a value to the parent, so the parent must arrange to receive that value The wait() system call serves this purpose  it puts the parent to sleep waiting for a child’s result  when a child calls exit(), the OS unblocks the parent and returns the value passed by exit() as a result of the wait call (along with the pid of the child)  if there are no children alive, wait() returns immediately  also, if there are zombies waiting for their parents, wait() returns one of the values immediately (and deallocates the zombie)

23 Process Control OS must include calls to enable special control of a process: Priority manipulation:  nice(), which specifies base process priority (initial priority)  In UNIX, process priority decays as the process consumes CPU Debugging support:  ptrace(), allows a process to be put under control of another process  The other process can set breakpoints, examine registers, etc. Alarms and time:  Sleep puts a process on a timer queue waiting for some number of seconds, supporting an alarm functionality

24 Tying it All Together: The Unix Shell while(! EOF) { read input handle regular expressions int pid = fork();// create a child if(pid == 0) {// child continues here exec(“program”, argc, argv0, argv1, …); } else {// parent continues here … } Translates to the kill() system call with SIGKILL Translates to the kill() system call with SIGSTOP Allows input-output redirections, pipes, and a lot of other stuff that we will see later

25 A Dose of Reality: Scheduling in Solaris Close to our scheduling diagram, but more complicated

26 Anatomy of a Process Code Header Initialized data Executable File Code Initialized data Heap Stack DLL’s mapped segments Process’s address space PC Stack Pointer Registers PID UID Scheduling Priority List of open files … PC Stack Pointer Registers PID UID Scheduling Priority List of open files … Process Control Block

27 main { int childPID; S 1 ; childPID = fork(); if(childPID == 0) else { wait(); } S 2 ; } Unix fork() example The execution context for the child process is a copy of the parent’s context at the time of the call  fork() returns child PID in parent, and 0 in child Code Data Stack Code Data Stack Parent Child fork() childPID = 0 childPID = xxx