Operating System Concepts and Techniques Lecture 13 Interprocess

  • Slides: 17
Download presentation
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh,

Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed. , i. Universe Inc. , 2011. To order: www. iuniverse. com, www. amazon. com, or www. amazon. com

Dekker’s solution Dekker has a similar solution to Peterson’s, turn and desire are initialized

Dekker’s solution Dekker has a similar solution to Peterson’s, turn and desire are initialized first int desire[2]= {0, 0}; int turn =0; void Get. Permission( int process) { desire[process] = true; while ( desire[1 -process] == true) { if ( turn != process) desire[process] = false; while (turn != process); desire [process] =true; } } void Free. Permission(int process) { turn =1 -process; desire [process] = false; } 2

Test and set lock (tsl) Method In this method we use some help from

Test and set lock (tsl) Method In this method we use some help from hardware n A machine instruction which does two actions in one instruction, test the content of a memory location and set it equal to one Its scientific name is test and set lock tsl register, x load register, x store #1, x // Store integer 1 into x Using this instruction get permission and free resource inline codes are: Get. Permission: tsl register, x cmp register, #0 jnz Get. Permission Free. Permission: store #0, x 3

Test and set lock (tsl) Method… The procedure-based implementation of these two operations will

Test and set lock (tsl) Method… The procedure-based implementation of these two operations will look like: void Get. Permission (void) { ASM; top: tsl register, x cmp register, #0 jnz top ENDASM; } void Free. Permission (void) { x = 0; } 4

An actual tsl-like implementation Some IBM Machines use TS Get. Permission TS x BNZ

An actual tsl-like implementation Some IBM Machines use TS Get. Permission TS x BNZ *-4 //Go back 4 bytes, if not zero Free. Permission MVI x, ’ 00’ Pentium processors use BTS (bit test and set) Motorola processors have TAS (test and set operand) Test and set lock method is w not restricted to two processes w not restricted to single processor w but it also suffers from busy-wait 5

Swap (or exchange) method A shared variabel, say S, shows whether the resource is

Swap (or exchange) method A shared variabel, say S, shows whether the resource is available, i. e. , S=0, or taken, i. e. , S=1 An atomic operation called exchange will exchange the content of two memory locations uninterruptably Each process will use the following procedures, correctly and in proper places to use the resource void Get. Permission (void) { int p = 1; while (p != false) exchange (p, S); } void Free. Permission(void) { S = 0; } • The exchange method is applicable in the single-processor or multiprocessor environment • It is applicable for two or more processes. 6

Busy-wait sideeffect Busy-wait-based methods may sometimes cause priority inversion n n When there is

Busy-wait sideeffect Busy-wait-based methods may sometimes cause priority inversion n n When there is no high priority process a low priority starts Enters a critical region to use a resource A higher priority process arrives and the system preempts the low priority and starts the high priority This process requests the same resource Cannot get it because lower priority is not finished with it, therefore no progress on either processes 7

Semaphore-Based IPC method Think of a semaphore as an abstract data type n n

Semaphore-Based IPC method Think of a semaphore as an abstract data type n n It includes data objects and operations (methods) on these data objects The whole entity is encapsulated to prevent manipulation of the data objects by any operation except the methods It has one protected integer data and two methods n n Down (or wait): checks the content of the semaphore variable; if it is grater than zero decrements it by one and continues Otherwise, puts the process in the waiting queue Up (or signal): increments the content of semaphore variable by one; if there is any process waiting for this semaphore wakes one up to continue its execution; the process that has just wakened up must do its down operation again 8

Semaphore types Binary: takes any of the values zero and one; it is usually

Semaphore types Binary: takes any of the values zero and one; it is usually initialized to one n n It is used for the cases where only one process can use the resource at any given time For example, there can be one professor teaching in the class Integer: takes any non-negative integer to a maximum; it is usually initialized tot to its maximum n n It is used for the cases where one or more processes can simultaneously use the resource For example, there can be many students simultaneously participating in a university classroom 9

Producer-consumer problem Let’s see semaphore in action in a race-free solution to the producer-consumer

Producer-consumer problem Let’s see semaphore in action in a race-free solution to the producer-consumer problem Producer process(es) produce entities and put them in the shared storage Consumer(s) take entities from the shared storage and use them n n n For example, all simultaneous processes in a system could be producers of output files to be printed Al printers could be consumers The queue of file names to be printed is the common storage The size of buffer queue is limited to say BC slots 10

Producer-consumer problem. . We start the solution by listing all constraints and assigning one

Producer-consumer problem. . We start the solution by listing all constraints and assigning one semaphore to each constraint n n n Enforce mutual exclusion in using the entity-buffer Make sure not to put new entities in a full buffer Make sure not to remove articles from an empty buffer We now assign one semaphore to each constraint n n n mutex , for mutual exclusion enforcement occupied, to show many slots of the buffer are occupied available, to show many slots are available Recall that the operation available=BC-accupied is not allowed as we are only allowed to use down and up operations on semaphores 11

Producer-consumer problem. . Semaphores have to be initialized n #define true 1 #define BC

Producer-consumer problem. . Semaphores have to be initialized n #define true 1 #define BC 1000 typedef int semaphore; semaphore available = BC; semaphore occupied = 0; n semaphore mutex =1; n n // Buffer capacity //Defines the keyword semaphore //All buffer slots are available //Buffer is empty //Shared Resource is 12

Producer procedure void producer (void) { struct buffer_slot entity; while (true) { produce_an_entity(&entity); wait

Producer procedure void producer (void) { struct buffer_slot entity; while (true) { produce_an_entity(&entity); wait (&available); //Wait for a free slot wait (&mutex); //Get permission insert_the_entity(&entity); //Put entity in queue signal (&mutex); //Free resource signal (&occupied); //One slot is filled } } 13

Consumer procedure void consumer (void) { struct buf_slot entity; while (true) { wait (&occupied);

Consumer procedure void consumer (void) { struct buf_slot entity; while (true) { wait (&occupied); wait (&mutex); remove_the_entity(&entity); signal (&mutex); signal (&available); consume_the_entity(&entity); } } //Wait until there is an entity 14

Summary The test-and-set-lock method solves the mutual exclusion problem with the help of a

Summary The test-and-set-lock method solves the mutual exclusion problem with the help of a machine instruction provided only for this purpose Another similar method is the swap method. This tries to obtain the permission to use a shared resource by swapping the value of a local variable with the value of a global variable Some processors have a machine instruction that can swap the contents of two memory locations in one machine instruction, otherwise we have to implement it by software These methods (except the disable interrupt) all suffer from busy -wait consumption of CPU time, however the waste may not be high and OS implementers may decide to use them A semaphore-based method is a busy-wait free form of guaranteeing mutual exclusion It is general and efficient 15

Find out A machine instruction in your computer’s processor which works like tsl A

Find out A machine instruction in your computer’s processor which works like tsl A machine instruction in your computer’s processor which works like swap The percent of CPU time which is wasted due to busywait in a system running three concurrent processes sharing one critical resource An operating system problem scenario which resembles producer-consumer problem Why busy-wait causes priority inversion Which of the ME problem solutions you like the best and why 16

Any questions? 17

Any questions? 17