Operating System n n A program that controls

  • Slides: 25
Download presentation
Operating System n n A *program* that controls the execution of application programs Serves

Operating System n n A *program* that controls the execution of application programs Serves as interface between applications and hardware Can evolve as hardware evolves And, of course, makes it convenient to use the hardware 1

Services provided by OS n n n Program dev. (editors, debuggers) Access to I/O

Services provided by OS n n n Program dev. (editors, debuggers) Access to I/O devices Error detection (and response) Accounting (usage stats, monitoring performance, …) Controlled access to files More generally: Responsible for managing resources 2

Operating System n n ***OS is a program that is executed*** The CPU executes

Operating System n n ***OS is a program that is executed*** The CPU executes the OS program for a while, then a user program, back to the OS … Kernel: Portion of OS that remains in main memory Kernel contains most frequently used functions 3

Key idea: Kernel mode vs. user mode n n How do we make sure

Key idea: Kernel mode vs. user mode n n How do we make sure that the *user* prog. doesn't do "bad" things? Most CPUs have (at least) two modes of operation: kernel/supervisor vs. user In kernel mode, the CPU will execute all instructions In user mode, an attempt to execute "dangerous" instructions will lead to an interrupt 4

Kernel mode vs. user mode (contd) n n How does the CPU know whether

Kernel mode vs. user mode (contd) n n How does the CPU know whether it is in user mode or kernel mode? Via a bit in the PSW (sometimes more than one bit if there are multiple levels of "privilege"); often called "Supervisor mode" bit Puzzle: how is this bit set/cleared? If there is an instruction to set it, *that* has to be usable when S is 0; … but then the *user* can set it! 5

XOPs/SYSCALL n n n When CPU is powered up, the S bit is set

XOPs/SYSCALL n n n When CPU is powered up, the S bit is set to 1 and the boot routine (part of OS) executes When a user program begins, the S bit is set to 1 When an OS routine is called -- sys call or XOP, the S bit is set to 1 XOP is like JSR but assigns a new value to both PC and PSW When it sys call finishes, it returns via RTS which restores PC and PSW 6

XOPs/SYSCALL/TRAP (contd) n n Somewhat similar to interrupts; also called "software interrupts" or traps

XOPs/SYSCALL/TRAP (contd) n n Somewhat similar to interrupts; also called "software interrupts" or traps There is an "XOP vector" with addresses and PSW's of XOP routines When user makes a sys-call (i. e. , CPU executes an "XOP k" instruction), the PC and PSW are saved on stack; the kth address/PSW from the XOP vector loaded into PC and PSW When it finishes, restore PC, PSW 7

XOPs/SYSCALL/TRAP (contd) n n But is all this really *necessary*? What is a sys

XOPs/SYSCALL/TRAP (contd) n n But is all this really *necessary*? What is a sys call, anyway? Example: malloc() If we are not careful, it can mess up the *other users* of the system and the system itself! 8

XOPs/SYSCALL/TRAP (contd) n n n Interrupts are handled in the same way … but

XOPs/SYSCALL/TRAP (contd) n n n Interrupts are handled in the same way … but there is a crucial difference … The XOP is an actual instruction in the code; the interrupt … "happens"; i. e. , is triggered by an external event Hence, XOPs are "software interrupts" 9

Key task of OS: managing the processes n n Interleave execution of multiple processes

Key task of OS: managing the processes n n Interleave execution of multiple processes Allocate resources to processes, and protect the resources of each process from other processes, *But* enable processes to share and exchange information, Enable synchronization among processes. 10

Process (various definitions) n n A program in execution An instance of a program

Process (various definitions) n n A program in execution An instance of a program running on a computer The entity that can be assigned to and executed on a processor For us: A unit of activity with a: n n n A single sequential thread of execution A current state An associated set of system resources 11

Typical process implementation 12

Typical process implementation 12

Process Elements n While the process is running it has a number of elements

Process Elements n While the process is running it has a number of elements including: n n n n Identifier Priority Program counter Memory pointers Context data I/O status information Accounting information Process Control Block (PCB) Key: PCB contains information needed to interrupt a process and resume it later with (almost) no impact on it 13

Trace of execution (as seen by CPU) 14

Trace of execution (as seen by CPU) 14

Five-State Process Model 15

Five-State Process Model 15

Using Two Queues There a number of issues related to how processes are created

Using Two Queues There a number of issues related to how processes are created (and then admitted to the "Ready Queue") or destroyed ("released") which are fairly straightforward; we will skip them. Another idea is the notion of "threads" which are simpler versions of processes; we won't talk about them either. One other item we will skip: should the OS be considered a process or should it have "special status"? Both approaches have been used … 16

Key OS problems/concepts relate to … n Managing multiple processes n n Multiprogramming Multiprocessing

Key OS problems/concepts relate to … n Managing multiple processes n n Multiprogramming Multiprocessing Distributed Processing The problems concurrency introduces: n n Mutual exclusion & synchronization Deadlock, starvation Scheduling processes (and i/o …) Memory management 17

A simple example … void echo() { chin = getchar(); chout = chin; putchar(chout);

A simple example … void echo() { chin = getchar(); chout = chin; putchar(chout); } Possible solution: only one process may enter echo() at a time and it must run to completion Same problem with multiprocessors as well; and same solution. Suppose there are two processes P 1, P 2 and they use a common copy of echo(): • P 1 starts, reads a character into chin; is then suspended • P 2 starts, reads a character into chin, outputs it, and finishes • P 1 resumes and finishes its work. Result: first character missing, two copies of second char! 18

A more complex example … P 1: a = a + 1; b =

A more complex example … P 1: a = a + 1; b = b + 1; P 2: b =2 * b; a = 2 * a; // intent: a and b are and will remain equal to each other Key terms/ideas: n Mutual exclusion: some "critical resources" must be accessed by only one process at a time n Critical section: region of code in a process accessing a (set of) critical resources n Deadlock: P 1 is assigned R 1 but also needs R 2 to finish; P 2 is assigned R 2 but also needs R 1 to finish … n Starvation: P 1, P 2, P 3 all need to use R 1 (repeatedly; maybe R 1 is a printer); P 1 and P 2 might get it repeatedly and P 3 gets "starved" OS has to take care of all these problems 19

Hardware support for mut. excl. n n n In uniprocessor only one process can

Hardware support for mut. excl. n n n In uniprocessor only one process can execute at a time It will continue until it is interrupted (or it invokes an OS service) So to get mutual exclusion: Disable interrupts while the process is in its critical section while (true) { /* disable interrupts */ /* critical section */ /* disable interrupts */ /* remainder */ } Disadvantages: n Poor efficiency, starvation, etc. n Approach doesn't work for multiprocessor

Compare & Swap Instruction int compare_and_swap (int *word, int testval, int newval){ int oldval;

Compare & Swap Instruction int compare_and_swap (int *word, int testval, int newval){ int oldval; oldval = *word; if (oldval == testval) *word = newval; return oldval; } n n Executed atomically If memory location’s current value is testval, it is replaced with newval; otherwise unchanged. n Old value always returned

Compare & Swap Instruction int compare_and_swap (int *word, int testval, int newval){ int oldval;

Compare & Swap Instruction int compare_and_swap (int *word, int testval, int newval){ int oldval; oldval = *word; if (oldval == testval) *word = newval; return oldval; } n n Executed atomically If memory location’s current value is testval, it is replaced with newval; otherwise unchanged. n Old value always returned

Mut. excl. using Comp. N Swap const int n = … //no. of processes

Mut. excl. using Comp. N Swap const int n = … //no. of processes int bolt; void P(int i) { // process I while (true) { while (cns(bolt, 0, 1) == 1); //busy wait! /* critical section */ bolt = 0; /* remainder */ } void main() { bolt = 0; parbegin (P(1), P(2), …, P(n)); //parallel processes }

Alternative: Exchange instruction void exchange (int *register, int *memory) { int temp; temp =

Alternative: Exchange instruction void exchange (int *register, int *memory) { int temp; temp = *memory; *memory = *register; *register = temp; } void P(int i) { int keyi = 1; while (true) { do exchange(&keyi, &bolt) while (keyi != 0); //busy wait /* critical section */ bolt = 0; /* remainder */ } Advantages: n Any number of processes and processors n Can support multiple critical sections (each with its own bolt) … But: n Busy waiting n Starvation possible n Deadlock possible

(Binary) Semaphore A special kind of variable with just the following operations: n Initialized

(Binary) Semaphore A special kind of variable with just the following operations: n Initialized to 0 or 1 n sem. Wait. B (also called "P") checks the sem. value; if it is 0, the process if blocked; if it is 1, the value is reduced to 0, and the process continues n sem. Signal. B (called "V") checks if any process is blocked (on this semaphore; if so, then a blocked process is unblocked; if no process is blocked, the semaphore is set to 1.