320 likes | 500 Views
CS423UG Operating Systems. Processes. Indranil Gupta Lecture 3 Aug 29, 2005. Overview. “Process”? Hey, what’s that? What’s in a process?. Processes : pronounced as “process-ees”, rhymes with “armies”; to avoid confusion with “processors”. OS: A Reminder. OS Manages resources
E N D
CS423UG Operating Systems Processes Indranil Gupta Lecture 3 Aug 29, 2005
Overview • “Process”? Hey, what’s that? • What’s in a process? Processes: pronounced as “process-ees”, rhymes with “armies”; to avoid confusion with “processors”. CS 423UG - Operating Systems, Indranil Gupta
OS: A Reminder • OS • Manages resources • Involves asynchronous and sometimes parallel activities • But how does it do this? • Through several abstractions • Let’s look at one such abstraction: the Process CS 423UG - Operating Systems, Indranil Gupta
Users, Programs, Processes • Users have accounts on the system • Users write programs, then launch programs • Different users may launch same program • One user may launch many instances of the same program • Process: = an executing program = a program in action = a running program CS 423UG - Operating Systems, Indranil Gupta
Analogy • Program: steps for attending the lecture • walk to Siebel Center Building • enter 1404 Lecture Room • find a seat • listen and take notes • Process: instance of above program in action = attending the lecture • Action • Right now, you are in the middle of this process! • You have a “condition” that changes every second! • Different processes from the same program may behave differently CS 423UG - Operating Systems, Indranil Gupta
An OS runs many processes simultaneously • Listing all running processes • Windows: Task Manager • Unix/Linux> “ps –all” PID TTY TIME CMD 1625 pts/6 0:02 tcsh 17246 pts/1 0:35 pine 1272 pts/7 0:00 ps 8865 pts/3 0:01 emacs • Examples of running processes: • Command Shell (e.g., tcsh) • Editors: pine (also emacs, vim, etc.) • Others: Firefox windows (each firefox window is a separate process) • And “ps” itself appears as a process! CS 423UG - Operating Systems, Indranil Gupta
Concretely! Can we define the term “process”? • OK, a process consists of: • The program code (does not change, ideally) • The Data • The Program Counter • The Stack • The Register values And anything else that defines the process’ current condition CS 423UG - Operating Systems, Indranil Gupta
R1 R2 R3 R4 PC (hex): 00d0 A Process, Pictorially A process in a machine with 4 registers CS 423UG - Operating Systems, Indranil Gupta
Hmm… how does one “play” around with a process? • A process is an abstraction for sequence of operations that implement a computation. • There are 2 types of processes: • OS processes executing system code • User processes executing user code • A user process may be given kernel privilege sometimes (e.g., once it executes a TRAP) • Regardless of type, a process may be manipulated, suspended, scheduled and terminated • By whom? By the OS, as well as by other processes! CS 423UG - Operating Systems, Indranil Gupta
“New Rules” for Process Management (with apologies to Bill Maher): • One CPU can execute only one process at a time. • If there are 4 CPU’s in a machine, how many processes could be executed simultaneously by the machine? • The OS manages the CPU’s selection of which process is the lucky one in (1). The OS may change its mind as time goes along! • That’s called scheduling – we’ll study it later! • The OS may be told by other processes (through “subtle” system calls) how to make the decision in (2). • Is it a good idea to allow a system call that explicitly selects which process CPU will run next? Consider two “colluding” processes that can hog the CPU. CS 423UG - Operating Systems, Indranil Gupta
Process Creation Processes are created and deleted dynamically (by the OS or by other processes) • System initialization, e.g., boot or reboot • User request to create a new process • By command line process (type in or double-click) • Initiation of a batch job, e.g., through cron • Execution of a process creation system call, e.g., fork() system call in Unix, CreateProcess() in Windows • OS assigns each process a unique id called pid (process id). No two processes on same system can have identical pid’s. CS 423UG - Operating Systems, Indranil Gupta
int parentpid; int childpid; if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes */ printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /*parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); } Forks PID: 837 PC Suppose the OS is running this, and it executes the fork() instruction CS 423UG - Operating Systems, Indranil Gupta
int parentpid; int childpid; if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes */ printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /*parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); } int parentpid; int childpid; if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes */ printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /*parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); } Forks: a bite can create a child! PID: 839 : New Process! PID: 837 PC PC CS 423UG - Operating Systems, Indranil Gupta
int parentpid; int childpid; if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes */ printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /*parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); } int parentpid; int childpid; if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes */ printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /*parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); } Forks: a bite can create a child! PID: 839 : New Process! PID: 837 PC Will be executed PC Will be executed • Processes 837 (parent) and 839 (child) are clones! Except… • They each have their own and separate code, pc, registers, stack • They have the same data except that • childpid==839 in parent (837). So parent knows child’s pid. • childpid==0 in child (839) • They are free to operate on their own from now onwards CS 423UG - Operating Systems, Indranil Gupta
Process Hierarchies • Created via process creation operations (incl. fork()) • Tree is called “Process Group” in Unix PID 837 PID 839 CS 423UG - Operating Systems, Indranil Gupta
Process Termination • Normal exit (voluntary): end of main() • Error exit (voluntary): exit(2) • Fatal error (involuntary): divide by 0, core dump • Killed by another process (involuntary): kill procID, end task • 1-2 above achieved through system call exit() • 3 above achieved process itself executing a TRAP • 4 achieved through“signal” received by some other process • Signal=software interrupt • Kill uses signal type SIGKILL CS 423UG - Operating Systems, Indranil Gupta
Process Creationism • When creating a process, it needs resources such as CPU, memory files, I/O devices • Process can get resources from the OS or from the parent process (e.g., CPU, disk) • When getting resources from a parent, the child process is restricted to a subset of parent resources • Prevents many processes from overloading system • When creating a new process, execution possibilities are • Parent waits until child has terminated. $emacs • Parent continues concurrently with child. $ emacs& • When creating a new process, address space possibilities: • Child process is duplicate of parent process • Child process has a new, different program loaded into it, e.g., execve() system call CS 423UG - Operating Systems, Indranil Gupta
Vs. The Terminator • When a process finishes last statement, it asks OS to delete it • Child process may return output to parent process, and then all child’s resources are de-allocated. • Other termination possibilities: Abort invoked by parent process • Child has exceeded its usage of some resources • Task assigned to child is no longer required • Parent is exiting, and OS does not allow child to continue without parent • Windows also kills parent when child is killed (not in Unix/Linux) CS 423UG - Operating Systems, Indranil Gupta
Process: A Manipulatable Object • OS manipulates a process as if it is an object with a condition • The operations that OS performs on the process change the condition of the process. • An important component of a process’ condition: its state CS 423UG - Operating Systems, Indranil Gupta
Process State “state” as in “state of the union”, not as in “state of Idaho” • Possible process states • Running (occupy CPU) • Blocked • Ready (does not occupy CPU) • Other states: suspended, terminated • Notice the transitions between states • Question: in a single processor machine, how many processes can be in running state? CS 423UG - Operating Systems, Indranil Gupta
The Process Model (as seen in memory) (as seen by CPU) • 4 independent, sequential processes in a 1 CPU machine • Only one program active at any instant • Real life analogy? • You doing homeworks for 4 different courses (as seen by OS) CS 423UG - Operating Systems, Indranil Gupta
Well, so I lied. A process’ condition also consists of ... • Process State • new, ready, running, waiting, halted; • Program Counter • the address of the next instruction to be executed for this process; • CPU Registers • index registers, stack pointers, general purpose registers; • CPU Scheduling Information • process priority and pointer; • Memory Management Information • base/limit information; • Accounting Information • time limits, process number; owner • I/O Status Information • list of I/O devices allocated to the process; All present in OS-maintained data structure called PCB=process control block, for each process that has not been terminated CS 423UG - Operating Systems, Indranil Gupta
Process Control Block (PCB) Fields of a process table entry e.g.: Linux: struct task_struct CS 423UG - Operating Systems, Indranil Gupta
Remember This? Application Libraries User space/level Kernel space/level Portable OS Layer Machine-dependent layer CS 423UG - Operating Systems, Indranil Gupta
Main Memory/ Main Memory 1GB Set aside for OS/kernel use only • One (common) approach • Kernel is high memory • User is low memory • What restrictions apply? • read(f, buf, nbytes) PID 901 (code,data, stack) PID 902 (code,data, stack) 0 CS 423UG - Operating Systems, Indranil Gupta
Process Address Space • Program segments • Text=static • Data=static data • Heap=dynamic data • Stack=dynamic • Lots of flexibility • Allows stack growth • Allows heap growth • No predetermined division PID 901 (code,data, stack) 0xffff…. Kernel space Stack Heap Code & Data 0x0000… Why is this needed? Remember system call libraries? CS 423UG - Operating Systems, Indranil Gupta
Process Scheduling • Objective of multiprogramming – maximal CPU utilization, i.e., have always a process running • Objective of time-sharing – switch CPU among processes frequently enough so that different users can use the single CPU as if they had their own • Need Context Switching CS 423UG - Operating Systems, Indranil Gupta
Context Switch • Switch CPU from one process to another • Performed by scheduler (later this week) • It does the following: • saves PCB of the old process; • loads PCB of the new process; • flushes memory cache; • change memory mapping (TLB) – more later on this • Context switch is expensive(1-1000 microseconds) • No useful work is done (pure overhead) • Can become a bottleneck • Real life analogy? • Your switching back and forth betw. HW’s for different courses • Needs hardware support CS 423UG - Operating Systems, Indranil Gupta
Process Context Switch OS jobs PCB-1 PCB-0 Interrupt/system call exec Save PCB-0 idle Reload from PCB-1 Interrupt/ System call idle exec Save into PCB-1 idle Reload from PCB-0 exec CS 423UG - Operating Systems, Indranil Gupta
“Process”? Hey, Ah That’s it! • It’s one executing instance of a “program” • It’s separate from other instances • It can started/manipulated/killed by other processes • It can be started/manipulated/killed by by them • Also covered • Process model, fork, PCB, context switch • This lecture read: 2.1 • Next lecture: 2.2 CS 423UG - Operating Systems, Indranil Gupta
Administrative • C++ Tutorial - Tuesday (8/30) 8 pm – 9 pm in 1404 SC. Repeated same time on Wednesday (8/31). • Nachos tutorial - Tuesday (8/30) 8 pm – 9 pm in 1404 SC. Repeated same time on Wednesday (8/31). • HW1 is out. Due Friday Sep 9. • MP1 out tomorrow. Due Monday Sep 19, 2005. CS 423UG - Operating Systems, Indranil Gupta