1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Operating system principles - Chapter 5: Concurrency: Mutual exclusion and synchronization

42 68 1

Đ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

Thông tin cơ bản

Định dạng
Số trang 42
Dung lượng 495,32 KB

Nội dung

After studying this chapter, you should be able to: Discuss basic concepts related to concurrency, such as race conditions, OS concerns, and mutual exclusion requirements; understand hardware approaches to supporting mutual exclusion; define and explain semaphores; define and explain monitors.

Chapter Concurrency: Mutual Exclusion and Synchronization • • • • Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem Multiple Processes • Central to the design of modern Operating Systems is managing multiple processes – Multiprogramming – Multiprocessing – Distributed Processing • Big Issue is Concurrency – Managing the interaction of all of these processes Interleaving and Overlapping Processes • Earlier (Ch2) we saw that processes may be interleaved on uniprocessors Interleaving and Overlapping Processes • And not only interleaved but overlapped on multi-processors • Both interleaving and overlapping present the same problems in concurrent processing One Difficulty of Concurrency • Sharing of global resources can be problematic A Simple Example Suppose echo is a shared procedure and P1 echoes ‘x’ and P2 echoes ‘y’ void echo() { // send a keyboard-input character to display chin = getchar(); What would happen if P1 is interrupted here by P2? chout = chin; putchar(chout); } What would happen if only one process is permitted at a time to be in the procedure? A Simple Example: On a Multiprocessor Process P1 chin = getchar(); chout = chin; putchar(chout); Process P2 chin = getchar(); chout = chin; putchar(chout); Enforce Single Access • If we enforce a rule that only one process may enter the function at a time then: – P1 & P2 run on separate processors – P1 enters echo first, • P2 tries to enter but is blocked – P1 completes execution • P2 resumes and executes echo Solution: Control access to shared resource Competition among Processes for Resources Three main control problems: • Need for Mutual Exclusion – Only one program at a time be allowed in its critical section – Critical resource: nonsharable resource, e.g., printer – Critical section: portion of the program that uses a critical resource • Deadlock • Starvation Requirements for Mutual Exclusion • Only one process at a time is allowed in the critical section for a resource • A process that halts in its noncritical section must so without interfering with other processes • No deadlock or starvation 10 Producer/Consumer Problem • General Situation: – One or more producers are generating data and placing these in a buffer – A single consumer is taking items out of the buffer one at time – Only one producer or consumer may access the buffer at any one time • The Problem: – Ensure that the Producer can’t add data into full buffer and consumer can’t remove data from empty buffer 28 Functions • Assume an infinite buffer b with a linear array of elements Producer while (true) { /* produce item v */ b[in] = v; in++; } Consumer while (true) { while (in out) before proceeding The producer can generate items and store them in the buffer at its own pace Each time, in is incremented 30 Semaphores n is equal to the number of items in the buffer Prevent the consumer and any other producer from accessing the buffer during the append operation The consumer must wait on both semaphores before proceeding 31 Bounded Buffer The buffer is treated as a circular storage; pointer values are expressed modulo the size of the buffer 32 Functions in a Bounded Buffer in and out are initialized to and n is the size of the buffer Producer Consumer while (true) { while (true) { /* produce item v */ while (in == out) while ((in + 1) % n == /* nothing */; out) w = b[out]; /* nothing */; out = (out + 1) % b[in] = v; n; in = (in + 1) % n /* consume item w } */ } 33 Semaphores e keeps track of the number of empty spaces 34 Roadmap • • • • Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem 35 Readers/Writers Problem • A data area (e.g., a file) is shared among many processes – Some processes (readers) only read the data area, some (writers) only write to the area • Conditions to satisfy: 1.Multiple readers may simultaneously read the file 2.Only one writer at a time may write 3.If a writer is writing to the file, no reader may read it 36 Readers have Priority readcount keeps track of the number of readers x is used to assure that readcount is updated properly the first reader that attempts to read should wait on wsem wsem is used to enforce mutual exclusion: as long as one writer is accessing the shared data area, no other writers and no readers may access it 37 Readers/Writers Problem • Once a single reader has begun to access the data area, it is possible for readers to retain control of the data area as long as there is at least one reader reading – Therefore, writers are subject to starvation • An alternative solution: no new readers are allowed access to the data area once at least one writer wants to write 38 Writers have Priority y controls the updating of writecount writecount controls the setting of rsem rsem inhibits all readers while there is at least one writer desiring access to the data area 39 Writers have Priority only one reader is allowed to queue on rsem, with any additional readers queuing on z 40 Writers have Priority 41 Key Terms 42 ... it invokes an operating system service or until it is interrupted – Disabling interrupts guarantees mutual exclusion because the critical section cannot be interrupted 13 Pseudo-Code while (true)... Interleaving and Overlapping Processes • Earlier (Ch2) we saw that processes may be interleaved on uniprocessors Interleaving and Overlapping Processes • And not only interleaved but overlapped on multi-processors... } 15 Mutual Exclusion (fig 5.2) • The only process that may enter its critical section is one that finds bolt equal to • All other processes go into a busy waiting mode 16 Hardware Mutual Exclusion:

Ngày đăng: 30/01/2020, 05:11

TỪ KHÓA LIÊN QUAN