A computer system consists of hardware, system programs, and application programs figs 2

45 346 0
A computer system consists of hardware, system programs, and application programs figs 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

FILES 6.2 DIRECTORIES 6.3 FILE SYSTEM IMPLEMENTATION 6.4 EXAMPLE FILE SYSTEMS 6.5 RESEARCH ON FILE SYSTEMS 6.6 SUMMARY

2 PROCESSES AND THREADS 2.1 PROCESSES 2.2 THREADS 2.3 INTERPROCESS COMMUNICATION 2.4 CLASSICAL IPC PROBLEMS 2.5 SCHEDULING 2.6 RESEARCH ON PROCESSES AND THREADS 2.7 SUMMARY A B C D D C B A Process switch One program counter Four program counters Process Time BCDA (a) (b) (c) Fig. 2-1. (a) Multiprogramming of four programs. (b) Conceptual model of four independent, sequential processes. (c) Only one program is active at once. 123 4 Blocked Running Ready 1. Process blocks for input 2. Scheduler picks another process 3. Scheduler picks this process 4. Input becomes available Fig. 2-2. A process can be in running, blocked, or ready state. Transitions between these states are as shown. 0 1 n – 2 n – 1 Scheduler Processes Fig. 2-3. The lowest layer of a process-structured operating system handles interrupts and scheduling. Above that layer are sequential processes. Process management Memory management File management Registers Pointer to text segment Root directory Program counter Pointer to data segment Working directory Program status word Pointer to stack segment File descriptors Stack pointer User ID Process state Group ID Priority Scheduling parameters Process ID Parent process Process group Signals Time when process started CPU time used Children’s CPU time Time of next alarm Fig. 2-4. Some of the fields of a typical process table entry. 1. Hardware stacks program counter, etc. 2. Hardware loads new program counter from interrupt vector. 3. Assembly language procedure saves registers. 4. Assembly language procedure sets up new stack. 5. C interrupt service runs (typically reads and buffers input). 6. Scheduler decides which process is to run next. 7. C procedure returns to the assembly code. 8. Assembly language procedure starts up new current process. Fig. 2-5. Skeleton of what the lowest level of the operating system does when an interrupt occurs. Thread Thread Kernel Kernel Process 1 Process 1 Process 1 Process User space Kernel space (a) (b) Fig. 2-6. (a) Three processes each with one thread. (b) One process with three threads. Per process items Per thread items Address space Program counter Global variables Registers Open files Stack Child processes State Pending alarms Signals and signal handlers Accounting information Fig. 2-7. The first column lists some items shared by all threads in a process. The second one lists some items private to each thread. Kernel Thread 3's stack Process Thread 3 Thread 1 Thread 2 Thread 1's stack Fig. 2-8. Each thread has its own stack. Kernel Keyboard Disk Four score and seven years ago, our fathers brought forth upon this continent a new nation: conceived in liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting place for those who here gave their lives that this nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate, we cannot consecrate we cannot hallow this ground. The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us, that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion, that we here highly resolve that these dead shall not have died in vain that this nation, under God, shall have a new birth of freedom and that government of the people by the people, for the people Fig. 2-9. A word processor with three threads. [...]... thread when a message arrives (a) Before the message arrives (b) After the message arrives Time Thread 1 Thread 2 Access (errno set) Open (errno overwritten) Errno inspected Fig 2- 16 Conflicts between threads over the use of a global variable Thread 1's code Thread 2' s code Thread 1's stack Thread 2' s stack Thread 1's globals Thread 2' s globals Fig 2- 17 Threads can have private global variables Spooler... Process table Thread table Fig 2- 13 (a) A user-level threads package (b) A threads package managed by the kernel Multiple user threads on a kernel thread User space Kernel Kernel thread Fig 2- 14 Multiplexing user-level threads onto kernel-level threads Kernel space Process Existing thread Pop-up thread created to handle incoming message Incoming message Network (a) (b) Fig 2- 15 Creation of a new thread... Fig 2- 10 (a) Dispatcher thread (b) Worker thread Model Threads Single-threaded process Finite-state machine Characteristics Parallelism, blocking system calls No parallelism, blocking system calls Parallelism, nonblocking system calls, interrupts Fig 2- 12 Three ways to construct a server Thread Process Process Thread User space Kernel space Kernel Kernel Run-time system Thread table Process table... send back empty reply */ consume item(item); /* do something with the item */ } } Fig 2- 29 The producer-consumer problem with N messages Process Barrier B C A B B C Barrier A Barrier A D D Time (a) C D Time Time (b) (c) Fig 2- 30 Use of a barrier (a) Processes approaching a barrier (b) All processes but one blocked at the barrier (c) When the last process arrives at the barrier, all of them are let... process Dispatcher thread Worker thread User space Web page cache Kernel Network connection Fig 2- 10 A multithreaded Web server Kernel space while (TRUE) { get next request(&buf); handoff work(&buf); } (a) while (TRUE) { wait for work(&buf) look for page in cache(&buf, &page); if (page not in cache(&page)) read page from disk(&buf, &page); return page(&page); } (b) Fig 2- 11 A rough outline of the code... end end; Fig 2- 27 An outline of the producer-consumer problem with monitors Only one monitor procedure at a time is active The buffer has N slots public class ProducerConsumer { static final int N = 100; // constant giving the buffer size static producer p = new producer( ); // instantiate a new producer thread static consumer c = new consumer( ); // instantiate a new consumer thread static our monitor... Spooler directory 4 5 prog.c 6 Process A abc prog.n 7 out = 4 in = 7 Process B Fig 2- 18 Two processes want to access shared memory at the same time A enters critical region A leaves critical region Process A B leaves critical region B enters critical region B attempts to enter critical region Process B B blocked T1 T2 T3 Time Fig 2- 19 Mutual exclusion using critical regions T4 while (TRUE) { while (TRUE)... instantiate a new monitor public static void main(String args[ ]) { p.start( ); // start the producer thread c.start( ); // start the consumer thread } static class producer extends Thread { public void run( ) {// run method contains the thread code int item; while (true) { // producer loop item = produce item( ); mon.insert(item); } } private int produce item( ) { } // actually produce } static class... int semaphore; semaphore mutex = 1; semaphore db = 1; int rc = 0; /* use your imagination */ /* controls access to ’rc’ */ /* controls access to the database */ /* # of processes reading or wanting to */ void reader(void) { while (TRUE) { down(&mutex); rc = rc + 1; if (rc == 1) down(&db); up(&mutex); read data base( ); down(&mutex); rc = rc − 1; if (rc == 0) up(&db); up(&mutex); use data read( ); }... so return | mutex is busy; schedule another thread | try again later | return to caller; critical region entered mutex unlock: MOVE MUTEX,#0 RET | store a 0 in mutex | return to caller Fig 2- 25 Implementation of mutex lock and mutex unlock monitor example integer i; condition c; procedure producer( ); end; procedure consumer( ); end; end monitor; Fig 2- 26 A monitor monitor ProducerConsumer . threads over the use of a global vari- able. Thread 1's code Thread 2's code Thread 1's stack Thread 2's stack Thread 1's globals Thread 2's globals Fig. 2-17. Threads. request(&buf); wait for work(&buf) handoff work(&buf); look for page in cache(&buf, &page); } if (page not in cache(&page)) read page from disk(&buf, &page); return page(&page); } (a) . server. Process ProcessThread Thread Process table Process table Thread table Thread table Run-time system Kernel space User space Kernel Kernel Fig. 2-13. (a) A user-level threads package. (b) A threads package managed

Ngày đăng: 28/04/2014, 16:35

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan