280 likes | 284 Vues
ITEC 502 컴퓨터 시스템 및 실습. Chapter 2-1: Process Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH. Contents. Processes Process Concept The Process Model Process Creation Process Termination Process Hierarchies Process States Implementation of Processes. Process Concept.
E N D
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH
Contents • Processes • Process Concept • The Process Model • Process Creation • Process Termination • Process Hierarchies • Process States • Implementation of Processes
Process Concept • An operating system executes a variety of programs: • Batch system –jobs • Time-shared systems –user programs or tasks • Uses the terms job and process almost interchangeably • Process–a program in execution; process execution must progress in sequential fashion • A process includes: • program counter • stack • data section
Process in Memory • Current activity of a process is represented by • the value of program counter • The contents of the processor’s registers • Stack • contains temporary data • function parameters, return address, local variables • Data section • contains global variables • Heap • is a memory that is dynamically allocated during process run time • Program is a passive entity • Process is a active entity • Two processes may be associated with the same program
Processes: The Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant
Process Creation (1) Principal events that cause process creation • System initialization • Execution of a process creation system • User request to create a new process • Initiation of a batch job
Process Creation (2) • Parent process create children processes, which, in turn create other processes, forming a tree of processes • via create-process system call • Three possibilities in terms of resource sharing • Parent and children share all resources • Children share subset of parent’s resources • Parent and child share no resources, child receives resources from OS directly • Two possibilities in terms of execution • Parent and children execute concurrently • Parent waits until children terminate
Process Creation (3) • Two possibilities in terms of address space • Child is a duplicate of parent; • child has same program and data as the parent • Child has a program loaded into it • UNIX examples • forksystem call creates new process • execsystem call used after a fork to replace the process’ memory space with a new program
fork() & exec() • A new process is created by the fork() system call • The new process consists of a copy of the address space of the original process • Both processes (parent & child) continue execution at the instruction after the fork(). • The return code for the fork() is zero for the child process, whereas the (nonzero) pid of the child process is returned to the parent • The exec()loads a binary file into memory, destroys the memory image of the program, and starts its execution
C Program Forking – UNIX (1) int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execve("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } }
Process Creation – Win32 • Windows example • CreateProcess() function is used, which is similar to fork • creates a new child process • requires loading a specified program into the address space of the child process at process creation • expects no fewer than ten parameters
C Program Forking – Wind32 (1) void _tmain( int argc, TCHAR *argv[] ) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process if( !CreateProcess( NULL, // No module name (use command line) “c:\\Windows\\system32\mspaint.exe”, // program name NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ) // Pointer to PROCESS_INFORMATION structure )
C Program Forking – Win32 (2) { printf( "CreateProcess failed (%d)\n", GetLastError() ); return; } // Wait until child process exits WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); }
Process Termination (1) Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)
Process Termination (2) • Process executes last statement and asks the operating system to delete it (exit) • Status value returned from child to parent (via wait) • Process’ resources are deallocated by operating system • Memory, open files, I/O buffers • Parent may terminate execution of children processes (abort) with various reason • Child has exceeded allocated resources • Task assigned to child is no longer required • If parent is exiting • Some operating systems do not allow child to continue if its parent terminates • All children terminated - cascading termination
Process Hierarchies • Parent creates a child process, child processes can create its own process • Forms a hierarchy • UNIX calls this a "process group" • Windows has no concept of process hierarchy • all processes are created equal
Process States (1): 3 states • Possible process states • running • blocked • ready • Transitions between states shown
Process States (2) • Lowest layer of process-structured OS • handles interrupts, scheduling • Above that layer are sequential processes
Process States – 5 states • As a process executes, it changes 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 processor (CPU) • terminated: The process has finished execution • The state of a process is defined in part by the current activity of that process
Diagram of Process States • It is important to realize that only one process can be running on any processor at any instant • Many processes may be ready and waiting states
Process Control Block (PCB) (1) • Each process is represented in the operating system by a process control block (PCB) • Information associated with each process • Process state • Program counter • CPU registers • CPU scheduling information • Memory-management information • Accounting information • I/O status information
Process Control Block (PCB) (2) • Process state • New, ready, running, waiting, terminated • Program counter • The address of the next instruction to be executed for this process • CPU registers • Accumulators, index registers, stack pointers, and general-purpose registers, etc. • CPU scheduling information • Process priority, pointers to scheduling queue, etc. • Memory-management information • Value of base and limit registers, the page tables or segment tables, etc. • Accounting information • The amount of CPU and real time used, time limits, account number, job and process number • I/O status information • List of I/O devices allocated, open files, etc.
Implementation of Processes (1) Fields of a process table entry
Implementation of Processes (2) Skeleton of what lowest level of OS does when an interrupt occurs
Summary • A process is a program in execution • As a process executes, the process may be in one of the 3 states or 5 states: • running, blocked, ready • new, ready, running, waiting, terminated • Each process is represented in the OS by its PCB • OS provides a mechanism for parent process to create a child process
Review • Processes • Process Concept • The Process Model • Process Creation • Process Termination • Process Hierarchies • Process States • Implementation of Processes