Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Principles of Operating Systems Lecture 2 Abhishek Dubey Daniel Balasubramanian Slides Based On Power points and book material from William Stallings Spring 2014 – A computer platform consists of a collection of hardware resources – Computer applications are developed to perform some task – It is inefficient for applications to be written directly for a given hardware platform • The OS was developed to provide a convenient, feature-rich, secure, and consistent interface for applications to use • We can think of the OS as providing a uniform, abstract representation of resources that can be requested and accessed by applications • Fundamental to the structure of operating systems A process can be defined as: a program in execution an instance of a running program the entity that can be assigned to, and executed on, a processor a unit of activity characterized by a single sequential thread of execution, a current state, and an associated set of system resources Development of the Process Three major lines of computer system development created problems in timing and synchronization that contributed to the development: multiprogramming batch operation • processor is switched among the various programs residing in main memory time sharing • be responsive to the individual user but be able to support many users simultaneously real-time transaction systems • a number of users are entering queries or updates against a database Process analogy • Think of the process lifecycle like a game of racquetball • Player is the program: contains the logic to play – But cannot play until a court is available • Player tries to enter the rec center – Needs appropriate rights • Player gets ready before starting to play • The court needs to be available Process analogy (cont’d) • Our scenario comprises a group of players who want to play – We must take turns • Might give up the court for two reasons 1. Thirsty 2. Number of allowable attempts is met • When games are done, we wrap up and leave • A process contains three components: – an executable program – the associated data needed by the program (variables, work space, buffers, etc.) – the execution context (or “process state”) of the program • The execution context is the internal data by which the OS is able to supervise and control the process. • It includes the contents of the various process registers • Also includes information such as the priority of the process and whether the process is waiting for the completion of a particular I/O event identifier state memory pointers context data priority program counter I/O status accounting information information Contains the process elements It is possible to interrupt a running process and later resume execution as if the interruption had not occurred Created and managed by the operating system Key tool that allows support for multiple processes • • The most important data structure in an OS – contains all of the information about a process that is needed by the OS – blocks are read and/or modified by virtually every module in the OS – defines the state of the OS Difficulty is not access, but protection – a bug in a single routine could damage process control blocks, which could destroy the system’s ability to manage the affected processes – a design change in the structure or semantics of the process control block could affect a number of modules in the OS Processes and Resources OS Control Tables • Process creation is by means of the kernel system call, fork( ) • This causes the OS, in Kernel Mode, to: 1 2 • Allocate a slot in the process table for the new process • Assign a unique process ID to the child process 3 • Make a copy of the process image of the parent, with the exception of any shared memory 4 • Increments counters for any files owned by the parent, to reflect that an additional process now also owns those files 5 6 • Assigns the child process to the Ready to Run state • Returns the ID number of the child to the parent process, and a 0 value to the child process Table 3.1 Reasons for Process Creation • The fork () function in the C Library is used to create new processes on UNIX-like Oss • The new process that is created by fork is an exact copy of the parent process, except: – The child process has a unique identifier – The parent identifier in the child is set to the process that invoked fork – The child has a copy of all of the parent’s resource descriptors (e.g., if a file is open in the parent, it is also open in the child) – The child process resource utilization is set to zero • Execution in the child begins immediately after the return from fork (), but with a different return value. void spawn_process (void) { int pid = fork (); if (pid == 0) { // we are in the child process. //Do something useful } else if (pid > 0) { // we are in the parent process int status; int pid_2 = waitpid (pid, &status, 0); int retcode = WEXITSTATUS(status); } else { // an error occurred while forking, and no child was spawned. } } Process startup • A C program starts execution with its main function: – int main(int argc, char *argv[]) • When C program is executed by kernel, a special startup routine is called before main – This is setup by the linker • The startup routine takes values from the kernel and sets things up for main – Command line args and environment Process Termination 1/2 • There must be a means for a process to indicate its completion • A batch job should include a HALT instruction or an explicit OS service call for termination • For an interactive application, the action of the user will indicate when the process is completed (e.g. log off, quitting an application) Process termination 2/2 • Processes in Unix can terminate in various ways; the “normal” ways are: 1. 2. 3. 4. 5. Return from main Call exit (performs cleanup then returns to kernel) Call _exit or _Exit (return to kernel immediately) The last thread returns The last thread calls pthread_exit • The “abnormal” ways are: 1. 2. 3. Call abort Receipt of a signal The last thread responds to a cancellation request • The startup routine will call exit if the main function returns Table 3.2 Reasons for Process Termination Process Memory – Address space • The compiler generates addresses for a virtual address space – The machine’s memory management unit translates the virtual addresses into addresses locations in physical memory • Memory management subsystem: the subsystems of the kernel and hardware that cooperate to translate virtual to physical addresses – Study this in future lectures Memory layout of a program • Memory allocated to each process is composed of different parts called segments: – Text segment: machine language instructions of the program run by the process. This is read-only. – Initialized data segment: global and static variables that are explicitly initialized – Uninitialized data segment: global and static variables that are not explicitly initialized • Also called the “BSS”: block started by symbol – Stack: dynamically growing and shrinking segment containing stack frames, one for each currently called function – Heap: where dynamic memory allocation takes place • Usually between the BSS and the stack Memory layout of active processes Detailed View of the process memory layout in Linux From: http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/ Process States Trace Dispatcher the behavior of an individual process by listing the sequence of instructions that execute for that process the behavior of the processor can be characterized by showing how the traces of the various processes are interleaved small program that switches the processor from one process to another Single Process View - Two-State Process Model • A process may be in one of two states: – running – not-running Multi Process View Queuing Diagram Process Execution The entire state of the process at any instant is contained in its context Five-State Process Model A refined multi process view Multiple Blocked Queues – Swapping – involves moving part of all of a process from main memory to disk – when none of the processes in main memory is in the Ready state, the OS swaps one of the blocked processes out on to disk into a suspend queue • The process is not immediately available for execution • The process may or may not be waiting on an event • The process was placed in a suspended state by an agent: either itself, a parent process, or the OS, for the purpose of preventing its execution • The process may not be removed from this state until the agent explicitly orders the removal Reasons for Process Suspension Table 3.3 Reasons for Process Suspension One Suspend State Two Suspend States Appendix http://www.cs.columbia.edu/~junfeng/10sp-w4118/lectures/l07-proc-linux.pdf