360 likes | 499 Views
Chapter 3 Process Scheduling. Bernard Chen Spring 2007. Outline. Process Concept Process Scheduling Process Operation. Process Concept.
E N D
Chapter 3 Process Scheduling Bernard Chen Spring 2007
Outline • Process Concept • Process Scheduling • Process Operation
Process Concept • Early computer systems allow only one program running at a time. In contrast, current day computer systems allow multiple programs to be loaded into memory and executed concurrently. • Process concept makes it happen
The Process Process: a program in execution • Text section: program code • program counter (PC) • Stack: to save temporary data • Data section: store global variables • Heap: for memory management
Stack and Queue • Stack: First in, last out • Queue: First in, first out • Do: push(8) push(17) push(41) pop() push(23) push(66) pop() pop() pop()
Heap (Max Heap) Provide O(logN) to find the max 97 53 59 26 41 58 31 16 21 36
The Process • Program itself is not a process, it’s a passive entity • A program becomes a process when an executable (.exe) file is loaded into memory. • Process: a program in execution
The Process • Although two processes may be the same program, they are considered two separate execution sequences. • They may share the same text section, but data, heap, stack section vary.
Process State • new: The process is being created • running: Instructions are being executed • waiting: The process is waiting for some event to occur • ready: The process is waiting to be assigned to a process • terminated: The process has finished execution
Process Control Block (PCB) • Each process is represented in a operating system by a Process Control Block (PCB) • Process state • Program counter (PC) • CPU scheduling information (Ch5) • Memory-management information (Ch8) • Accounting information • I/O status information
Process Scheduling • As processes entered the system, they are put into job queue • The processes that are stay in main memory and are ready and waiting to execute are kept on a list called ready queue • A ready queue contains pointers to the first and final PCBs in the list • The list of processes waiting for a particular I/O is called a device queue
Queueing Diagram • A common representation for discussion for of process scheduling is a queueing diagram
Queueing Diagram A new process is initially put in the ready queue, once the process is located in CPU, one of several events may occur: • The process issue a I/O request, and then be placed in I/O queue • The process create a new subprogram and wait for the subprocess’ termination • The process could be remove from CPU and be placed back to ready queue
Schedulers • Long-term scheduler (or job scheduler) –selects which processes should be brought into the ready queue • Short-term scheduler (or CPU scheduler) –selects which process should be executed next and allocates CPU
Schedulers • Short-term scheduler is invoked very frequently (milliseconds) ⇒(must be fast) • Long-term scheduler is invoked very infrequently (seconds, minutes) ⇒(may be slow) • The long-term scheduler controls the degree of multiprogramming
Schedulers Processes can be described as either: • I/O-bound process–spends more time doing I/O than computations, many short CPU bursts • CPU-bound process–spends more time doing computations; few very long CPU bursts
Schedulers • On some systems, the long-term scheduler maybe absent or minimal • Just simply put every new process in memory for short-term scheduler • The stability depends on physical limitation or self-adjustment nature of human users
Schedulers • Sometimes it can be advantage to remove process from memory and thus decrease the degree of multiprogrammimg • This scheme is called swapping
Process Creation • A process may create several new processes. The creating process is called a parent process, and new processes are called children process • Each of these processes may create other processes, forming a tree processes • Most OS identify processes according to a unique process identifier (pid) which is typically an integer
Process Creation Resource sharing • Parent and children share all resources • Children share subset of parent’s resources (such as memory of files) • Parent and child share no resources (MPI programming)
Process Creation Execution • Parent and children execute concurrently • Parent waits until children terminate
Process Creation • UNIX examples • Fork system call creates new process • Exec system call used after a fork to replace the process’ memory space with a new program • Both processes (parent and child) continue execution after fork(), with one difference: the return code for the fork() is zero for child processes.
Share Memory Parallelization System Example m_set_procs(number): prepare number of child for execution m_fork(function): childes execute “function” m_kill_procs(); terminate childs
Real Example main(argc , argv) { int nprocs=9; m_set_procs(nprocs); /* prepare to launch this many processes */ m_fork(slaveproc); /* fork out processes */ m_kill_procs(); /* kill activated processes */ } void slaveproc() { int id; id = m_get_myid(); m_lock(); printf(" Hello world from process %d\n",id); printf(" 2nd line: Hello world from process %d\n",id); m_unlock(); }
Real Example int array_size=1000 int global_array[array_size] main(argc , argv) { int nprocs=4; m_set_procs(nprocs); /* prepare to launch this many processes */ m_fork(sum); /* fork out processes */ m_kill_procs(); /* kill activated processes */ } void sum() { int id; id = m_get_myid(); for (i=id*(array_size/nprocs); i<(id+1)*(array_size/nprocs); i++) global_array[id*array_size/nprocs]+=global_array[i]; }
Process Termination • Process executes last statement and asks the operating system todelete it (exit) • Output data from child to parent (via wait) • Process’resources are deallocated by operating system
Process Termination • Parent may terminate execution of children processes (abort) • Child has exceeded allocated resources • Task assigned to child is no longer required • If parent is exiting, some operating system do not allow child to continue if its parent terminates