process synchronization: semaphores & mutex 1. process synchronization in the software context,...

86
Process Synchronization: Semaphores & Mutex 1

Upload: myrtle-kennedy

Post on 19-Jan-2016

258 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Process Synchronization: Semaphores & Mutex

1

Page 2: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Process synchronization

• In the software context, synchronization refers to the act of ensuring that independent processes/threads begin to execute a designated block of code at the same logical time

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

2

Page 3: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Race Condition• A race condition or race hazard is a flaw in an

electronic system or process whereby the output and/or result of the process is unexpectedly and critically dependent on the sequence or timing of other events.

• The condition where data consistency is lost because of lack of synchronization of the component task of a system is called a race condition

• To avoid this problem, shared global variables must be used only with synchronization

Page 4: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Critical section code

Process { while (true) {

ENTER CRITICAL SECTION Access shared variables; LEAVE CRITICAL SECTION Do other work

} }

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

4

Page 5: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Traffic intersections in rail cross

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

5

Page 6: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphores

• Semaphore is a type of generalized lock– Defined by Dijkstra in the last 60s– Main synchronization primitives used in UNIX– Consist of a positive integer value– Two operations

➢P(): an atomic operation that waits for semaphore to become positive, then decrement it by 1

➢V(): an atomic operation that increments semaphore by 1 and wakes up a waiting thread at P(), if any.

Page 7: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Operations

• P(), V() . P and V stand for Dutch words "Proberen", to test, and "Verhogen", to increment.

• function V(semaphore S) { S ← S +1 }

• function P(semaphore S){ While (S ==0 ){

wait };

S=S-1; }

Page 8: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphores vs. Integers

• No negative values• Only operations are P() and V()

– Cannot read or write semaphore values– Except at the initialization times

• Operations are atomic– Two P() calls cannot decrement the value below

zero– A sleeping thread at P() cannot miss a wakeup

from V()

Page 9: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Types of semaphore

• Type of semaphores;• binary is a semaphore with an integer value of 0 and 1.• counting is a semaphore with an integer value ranging

between 0 and an arbitrarily large number. Its initial value might represent the number of units of the critical resources that are available. This form is also known as a general semaphore.

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

9

Page 10: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Review: Semaphores

• Before entering critical section – semWait(s)/ P / down

• receive signal via semaphore s• “down” on the semaphore

• After finishing critical section– semSignal(s)/ V / up

• transmit signal via semaphore s• “up” on the semaphore

Page 11: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

11

Page 12: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Sharing Two Variables (signaling)

proc_A() { while(TRUE) { <compute section A1>; update(x); /* Signal proc_B */ V(s1); <compute section A2>; /* Wait for proc_B */ P(s2); retrieve(y); }}

semaphore s1 = 0;semaphore s2 = 0;

fork(proc_A, 0);fork(proc_B, 0);

proc_B() { while(TRUE) { /* Wait for proc_A */ P(s1); retrieve(x); <compute section B1>; update(y); /* Signal proc_A */ V(s2); <compute section B2>; }}

Page 13: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Mutex

• Simplest and most efficient thread synchronization mechanism• Like a semaphore with maximal value 1• A special variable that can be either in

– locked state: a distinguished thread that holds or owns the mutex; or

– unlocked state: no thread holds the mutex• When several threads compete for a mutex, one wins. The rest

block at that call – The mutex also has a queue of threads that are waiting to hold

the mutex. • POSIX does not require that this queue be accessed FIFO

Page 14: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore based solutions to benchmark synchronization problems

• Cigarette smokers problem• Dining philosophers problem• Readers-writers problem• Sleeping barber problem• Producers-consumers problem

Page 15: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Dining Philosophers: an intellectual game

• N philosophers and N forks• Philosophers eat/think

1

2

0

3

4

N=5

1

0

4

32

Page 16: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Dining Philosophers• Philosophers Think and

Ignore Food• When They Want to Eat,

Need Two Forks• Pick Up One and Then

Another• How Do We Prevent Two

Philosophers Holding Same Fork at Same Time?

• What is a Synchronization Scheme for the Problem?while(TRUE) {

think(); eat();}

Page 17: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Dining Philosophers Is Semaphore Solution Correct?

philosopher(int i) { while(TRUE) { // Think // Eat P(fork[i]); P(fork[(i+1) mod 5]); eat(); V(fork[(i+1) mod 5]); V(fork[i]); }}semaphore fork[5]=(1,1,1,1,1);fork(philosopher, 1, 0);fork(philosopher, 1, 1);fork(philosopher, 1, 2);fork(philosopher, 1, 3);fork(philosopher, 1, 4);

Page 18: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

The bounded buffer (Producer Consumer Problem)

• System incorporates two single threaded processes– One of which produces information(producer)– Another that uses information (consumer)

• Two processes communicate by having the producer obtain an empty buffer pool, fill it with information and place it in a pool of full buffers.

Page 19: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

19

Page 20: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Bounded Buffer Problem

Producer

Consumer

Empty Pool

Full Pool

Page 21: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

producer

while (true) { /* produce an item and put in nextProduced

while (count == BUFFER_SIZE); // do nothing

buffer [in] = nextProduced;in = (in + 1) % BUFFER_SIZE;count++;

}

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

21

Page 22: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

consumer

while (1) {

while (count == 0); // do nothing

nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;count--;/* consume the item in nextConsumed

}

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

22

Page 23: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Solving the producer-consumer problem using semaphores

• The solution uses three semaphores;• One called full for counting the number of slots that are

full • one called empty for counting the number of slots that

are empty• one called mutex to make sure the producer and the

consumer• do not access the buffer at the same time. mutex is

initially 1 (binary semaphore) if each process does a down just before entering its CR and an up just after leaving it, the mutual exclusion is guaranteed.

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

23

Page 24: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

24

Page 25: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Readers-writers problem

• Here a resource is to be shared among a community of processes of distinct types : Readers and Writers,– A reader process can share the resource with

any other reader process, but not with any writer process

– A writer process requires exclusive access to the resource whenever it acquires any access to the resource

– Eg: a file is to be shared among a set of processes

Page 26: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Readers-Writers Problem

Readers

Writers

Page 27: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Readers-Writers Problem (2)

Reader

Shared Resource

ReaderReader

ReaderReader

ReaderReader

Reader

WriterWriter

WriterWriter

WriterWriter

Writer

Page 28: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

First Solution• As long as the reader holds the resource and

there are new readers arriving, any writer must wait for the resource become available. – The first reader accessing the shared resource

must compete with any writers, but once a reader succeeds, other readers can pass directly into the critical section, provided that at least one reader is in the critical section

– Readers keep the count of the no. of readers in the critical section, with the readcount variable which is updated and tested inside its own critical section

Page 29: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

First Solutionreader() { while(TRUE) { <other computing>; P(mutex); readCount++; if(readCount == 1) P(writeBlock); V(mutex); /* Critical section */ access(resource); P(mutex); readCount--; if(readCount == 0) V(writeBlock); V(mutex); }}resourceType *resource;int readCount = 0;semaphore mutex = 1;semaphore writeBlock = 1;fork(reader, 0);fork(writer, 0);

writer() { while(TRUE) { <other computing>; P(writeBlock); /* Critical section */ access(resource); V(writeBlock); }}

•First reader competes with writers•Last reader signals writers

Page 30: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

First Solution (2)reader() { while(TRUE) { <other computing>; P(mutex); readCount++; if(readCount == 1) P(writeBlock); V(mutex); /* Critical section */ access(resource); P(mutex); readCount--; if(readCount == 0) V(writeBlock); V(mutex); }}resourceType *resource;int readCount = 0;semaphore mutex = 1;semaphore writeBlock = 1;fork(reader, 0);fork(writer, 0);

writer() { while(TRUE) { <other computing>; P(writeBlock); /* Critical section */ access(resource); V(writeBlock); }}

•First reader competes with writers•Last reader signals writers•Any writer must wait for all readers•Readers can starve writers•“Updates” can be delayed forever•May not be what we want

Page 31: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Writer Precedence reader() { while(TRUE) { <other computing>; P(writePending); P(readBlock); P(mutex1); readCount++; if(readCount == 1) P(writeBlock); V(mutex1); V(readBlock); V(writePending); access(resource); P(mutex1); readCount--; if(readCount == 0) V(writeBlock); V(mutex1); }}int readCount = 0, writeCount = 0;semaphore mutex = 1, mutex2 = 1;semaphore readBlock = 1, writeBlock = 1, writePending = 1;fork(reader, 0);fork(writer, 0);

writer() { while(TRUE) { <other computing>; P(mutex2); writeCount++; if(writeCount == 1) P(readBlock); V(mutex2); P(writeBlock); access(resource); V(writeBlock); P(mutex2) writeCount--; if(writeCount == 0) V(readBlock); V(mutex2); }}

Page 32: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

The Sleeping-Barber Problem.

A barbershop consists of awaiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.

Page 33: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

The Sleepy Barber

Waiting Room

Entrance to WaitingRoom (sliding door)

Entrance to Barber’sRoom (sliding door)

Shop Exit

• Barber can cut one person’s hair at a time

• Other customers wait in a waiting room

Page 34: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Sleepy barber – problems• The problems are all related to the fact that the actions by both

the barber and the customer (checking the waiting room, entering the shop, taking a waiting room chair, etc.) all take an unknown amount of time.

• In one example, A customer may arrive and observe that the barber is cutting hair, so he goes to the waiting room. While he is on his way, the barber finishes the haircut he is doing and goes to check the waiting room. Since there is no one there (the customer not having arrived yet), he goes back to his chair and sleeps. The barber is now waiting for a customer and the customer is waiting for the barber.

• In another example, two customers may arrive at the same time when there happens to be a single seat in the waiting room. They observe that the barber is cutting hair, go to the waiting room, and both attempt to occupy the single chair

Page 35: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Sleepy Barber (aka Bounded Buffer)

customer() { while(TRUE) { customer = nextCustomer(); if(emptyChairs == 0) continue; P(chair); P(mutex); emptyChairs--; takeChair(customer); V(mutex); V(waitingCustomer); }}

semaphore mutex = 1, chair = N, waitingCustomer = 0;int emptyChairs = N;fork(customer, 0);fork(barber, 0);

barber() { while(TRUE) { P(waitingCustomer); P(mutex); emptyChairs++; takeCustomer(); V(mutex); V(chair); }}

Page 36: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Cigarette smoker’s problem Assume a cigarette requires three ingredients to smoke: Tobacco ,

Smoking Paper, A MatchAssume there are also three chain smokers around a table, each of whom

has an infinite supply of one of the three ingredients — one smoker has an infinite supply of tobacco, another has an infinite supply of paper, and the third has an infinite supply of matches.

Assume there is also a non-smoking arbiter. The arbiter enables the smokers to make their cigarettes by arbitrarily (non deterministically) selecting two of the smokers, taking one item out of each of their supplies, and placing the items on the table. The arbiter then notifies the third smoker that they have done this. The third smoker removes the two items from the table and uses them (along with their own supply) to make a cigarette, which they smoke for a while. Meanwhile, the arbiter, seeing the table empty, again chooses two smokers at random and places their items on the table. This process continues forever.

Page 37: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

• The smokers do not hoard items from the table; a smoker only begins to roll a new cigarette once they have finished smoking the last one. For instance if the arbiter places tobacco and paper on the table while the match-supply smoker is smoking, the tobacco and paper will remain untouched on the table until the match-supply smoker is finished with their cigarette and then collects the items

Page 38: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Cigarette Smoker’s Problem• Three smokers (processes)• Each wish to use tobacco, papers, &

matches– Only need the three resources periodically– Must have all at once

• 3 processes sharing 3 resources– Solvable, but difficult

Page 39: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Agent process 1 do forever { 2 P( lock ); 3 randNum = rand( 1, 3 ); // Pick a random number from 1-3 4 if ( randNum == 1 ) { 5 // Put tobacco on table 6 // Put paper on table 7 V( smoker_match ); // Wake up smoker with match 8 } else if ( randNum == 2 ) { 9 // Put tobacco on table10 // Put match on table11 V( smoker_paper ); // Wake up smoker with paper12 } else {13 // Put match on table14 // Put paper on table15 V( smoker_tobacco ); // Wake up smoker with tobacco16 }17 V( lock );18 P( agent ); // Agent sleeps19 } // end forever loop

Page 40: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Smoker process1 do forever { 2 P( smoker_tobacco ); // Sleep right away 3 P( lock ); 4 // Pick up match 5 // Pick up paper 6 V( agent ); 7 V( lock ); 8 // Smoke (but don't inhale). 9 }

Page 41: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Operations

• P(), V() . P and V stand for Dutch words "Proberen", to test, and "Verhogen", to increment.

• function V(semaphore S) { S ← S +1 }

• function P(semaphore S) { While (S == 0){ wait

};S=S-1; }

Page 42: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Implementing Semaphores: Test and Set Instruction

FALSE

m

Primary

Memory

…R3 …

DataRegist

er

CCRegist

er

(a) Before Executing TS

TRUEm

Primary

Memory

FALSE

R3 =0

DataRegist

er

CCRegist

er

(b) After Executing TS

• “TS R3, m “ loads register R3 tests its value and writes a TRUE back to location m.

• OS function - TS(m): [Reg_i = memory[m]; memory[m] = TRUE;]

Page 43: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Using the TS Instruction

boolean s = FALSE; . . . while(TS(s)) ; <critical section> s = FALSE; . . .

semaphore s = 1; . . . P(s) ; <critical section> V(s); . . .

Page 44: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Implementing the General Semaphorestruct semaphore { int value = <initial value>; boolean mutex = FALSE; boolean hold = TRUE;};

shared struct semaphore s;

P(struct semaphore s) { while(TS(s.mutex)) ; s.value--; if(s.value < 0) { s.mutex = FALSE; while(TS(s.hold)) ; } else s.mutex = FALSE;}

V(struct semaphore s) { while(TS(s.mutex)) ; s.value++; if(s.value <= 0) { while(!s.hold) ; s.hold = FALSE; } s.mutex = FALSE;}

Page 45: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Inter process Communication Mechanisms

Operating Systems: A Modern Perspective, Chapter 9

Page 46: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

• Inter process communication (IPC) is the transfer of data among processes. Some of the examples are

• A Web browser may request a Web page from a Web server, which then sends HTML data.This transfer of data usually uses sockets in a telephone-like connection.

• one may want to print the filenames in a directory using a command such as ls | lpr. The shell creates an ls process and a separate lpr process, cothe two with a pipe, represented by the “|” symbol.A pipe permits one-way communication between two related processes.The ls process writes data into the pipe, and the lpr process reads data from the pipennecting

Operating Systems: A Modern Perspective, Chapter 9

Page 47: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

IPC Mechanisms

Info to beshared

Info copyMessage

OS IPCMechanismAddress Space for p0 Address Space for p1

◼Sharing of Information can be in ▪ User space ▪ Kernel space

Page 48: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Main IPC methods

Operating Systems: A Modern Perspective, Chapter 9

Page 49: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

IPC mechanisms• System V IPC

– Shared memory– Semaphore– Message queue

• Signals• Pipes (named pipe also)• Sockets

Operating Systems: A Modern Perspective, Chapter 9

Page 50: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

System V IPC• System V IPC was first introduced in SVR2, but is

available now in most versions of unix

– Message Queues represent linked lists of messages, which can be written to and read from

– Shared memory allows two or more processes to share a region of memory, so that they may each read from and write to that memory region

– Semaphores synchronize access to shared resources by providing synchronized access among multiple processes trying to access those critical resources.

Page 51: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

SYS V IPC

◼Each mechanism contains▪ Table▪ Key▪ Get

Operating Systems: A Modern Perspective, Chapter 9

Page 52: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

• Identifiers and Keys– Each IPC structure (message queue,

semaphore, or share memory segment) is identified by a nonnegative integer identifier.

• Similar to a file descriptor, but can be a large value.– A key must be specified when creating an IPC

structure. • Kernel convert the key to the identifier.• All process access to an IPC structure must have a

way to specify the same key?

COP5570 – Advanced Unix Programming

Page 53: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

◼Shares the memory in user space◼Shared memory is the special

address range in the address range of a process

◼Provides efficient access to large areas of memory .

◼Other process can attach this shared memory segment into their own address space (Virtual address space)

Page 54: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent
Page 55: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

◼shmget▪ Create / retrieve

◼shmat▪ Attach to the virtual address space

◼shmdt▪ Detach form virtual address space

◼shmctl▪ Manipulation of various parameters

Page 56: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

◼Syntax

◼Example

#include<sys/shm.h>int shmget(key_t key, size_t size, int shmflg);

shmid = shmget((key_t)1234,4,IPC_CREAT | IPC_EXCL) ;

if(shmid < 0){

printf("Shared memory already exists\n") ;

shmid = shmget((key_t)1234,4,0666) ;

}

Page 57: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

◼Syntax:

◼Example

void *shmat(int shm_id, const void *shm_addr, int shmflg);

memory = shmat(shmid,NULL,0) ;if(memory == NULL ){ printf("attaching to the shared

memory has failed\n") ; exit(0) ;}

Page 58: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

◼Syntax:

◼Exampleint shmdt(const void*memory) ;

retval = shmdt(memory) ;if(retval < 0){ printf("process 1 could not detach

the memory\n") ; exit(0) ;}

Page 59: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

retval = shmctl(shmid,IPC_RMID,NULL) ;if(retval < 0){ printf("removing the shared memory had failed\n") ;}

Command Description

IPC_STAT To retrieve the values associated with the shared memory.

IPC_SET Sets the values

IPC_RMID Deletes the shared memory segment.

Page 60: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

debugging• ipcs –shm

------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00000000 1627649 user 640 25600 0

• If this memory segment was erroneously left behind by a program, you can use the ipcrm command to remove it.

• ipcrm shm 1627649

Operating Systems: A Modern Perspective, Chapter 9

Page 61: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

◼Pros▪ Easy to access as it is in user space▪ Fastest IPC mechanism

◼Cons▪ Synchronization mechanism is not there.

Page 62: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Linux semphore IPC mechanism

• Semaphore is a nonnegative integer that is stored in the kernel.

• Access to the semaphore is provided by a series of semaphore system calls.

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

62

Page 63: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Creating and Accessing Semaphore Sets

▪ Before a semaphore set can be used, it must be created.

▪ The creation of the semaphore set generates a unique data structure that the system uses to identify and manipulate the semaphores.

▪ A conceptual arrangement of a system semaphore structure for a newly allocated set of three semaphores is shown in

63

SEMAPHORES

Page 64: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Creating and Accessing Semaphore Sets

64

SEMAPHORES

Figure 7.1. Data structures for a set of three semaphores.

Page 65: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

◼semget▪ Create / retrieve

◼semop▪ Wait and signal operations use the same

call with different arguments◼semctl

▪ Manipulation of various parameters, controlling the semaphore

Page 66: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Creating and Accessing Semaphore Sets

66

SEMAPHORES

Return

int semget (key_t key,int nsems,int semflg);Summary

2Manual Section>sys/types.h<

>sys/ipc.h<>sys/sem.h<

Include File(s)

Sets errnoFailureSuccess

Yes-1The semaphore identifier

Page 67: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Creating and Accessing Semaphore Sets

▪ The semget system call takes three arguments: ➢ The first argument, key, is used by the system to

generate a unique semaphore identifier. ➢ The second argument, nsems, is the number of

semaphores in the set.➢ The third argument, semflg, is used to specify

access permission and/or special creation conditions.

67

SEMAPHORES

Page 68: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Control▪ The semctl system call allows the user to

perform a variety of generalized control operations on the system semaphore structure, on the semaphores as a set, and on individual semaphores.

68

SEMAPHORES

Page 69: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Control

69

SEMAPHORES

Return

int semctl(int semid, int semnum, int cmd,

union semun arg);

Summary

2Manual Section>sys/types.h<

>sys/ipc.h<>sys/sem.h<

Include File(s)

Sets errnoFailureSuccess

Yes-10 or the value requested

Page 70: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Control▪ The semctl system call takes four arguments:

➢ The first argument, semid, is a valid semaphore

identifier that was returned by a previous semget system call.

➢ The second argument, semnum, is the number of semaphores in the semaphore set (array), 0 means this number (index) is not relevant.

➢ The third argument to semctl, cmd, is an integer command value. the cmd value directs semctl to take one of several control actions. Each action requires specific access permissions to the semaphore control structure.

70

SEMAPHORES

Page 71: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Control

➢ The fourth argument to semctl, arg, is a union of type semun. Given the action specified by the preceding cmd argument, the data in arg can be one of any of the following four values:

71

SEMAPHORES

Page 72: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Operations

▪ Additional operations on individual semaphores are accomplished by using the semop system call.

72

SEMAPHORES

Page 73: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Operations

73

SEMAPHORES

Return

int semop(int semid, struct sembuf *sops,

unsigned nsops);

Summary

2Manual Section>sys/types.h<

>sys/ipc.h<>sys/sem.h<

Include File(s)

Sets errnoFailureSuccess

Yes-10

Table 7.5. Summary of the semop System Call

Page 74: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Operations▪ The semop system call takes three arguments:

➢ The first argument, semid, is a valid semaphore

identifier that was returned by a previous successful semget system call.

➢ The second argument, sops, is a reference to the base address of an array of semaphore operations that will be performed on the semaphore set associated with by the semid value.

➢ The third argument, nsops, is the number of elements in the array of semaphore operations.

74

SEMAPHORES

Page 75: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Operation Details▪ When the sem_op value is negative, the process

specifying the operation is attempting to decrement the semaphore.

▪ The decrement of the semaphore is used to record the acquisition of the resource affiliated with the semaphore.

▪ When a semaphore value is to be modified, the accessing process must have alter permission for the semaphore set.

75

SEMAPHORES

Page 76: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Semaphore Operation Details

▪ When the sem_op value is positive, the process is adding to the semaphore value. The addition is used to record the return (release) of the resource affiliated with the semaphore.

▪ Again, when a semaphore value is to be modified, the accessing process must have alter permission for the semaphore set.

76

SEMAPHORES

Page 77: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Deleting Semaphores

▪ The semctl command with IPC_RMID removes the semaphore in the program

▪ The command "ipcs -s" will list all semaphores on a system.

▪ The command "ipcrm -s {semid}" will delete a semaphore on system promt.

▪ To delete all semaphores you have authority over, you can use this on system;

for semid in `ipcs -s | cut -d\ -f2`; do ipcrm -s $semid; done

77

SEMAPHORES

Page 78: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

78

Page 79: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

P() operation

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

79

Page 80: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

V()

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta

80

Page 81: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

81

Semaphore Control Details▪ The following cmd values cause semctl to act

upon the system semaphore structure➢ IPC_STAT

» Return the current values of the semid_ds structure for the indicated semaphore identifier. The returned information is stored in a user-generated structure

referenced by the fourth argument to semctl. To specify IPC_STAT, the process must have read permission for the semaphore set associated with the semaphore identifier.

SEMAPHORES

Page 82: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

82

Semaphore Control Details➢ IPC_SET

» Modify a restricted number of members in the semid_ds structure. The members sem_perm.uid, sem_perm.gid and sem_perm.mode can be changed if the effective ID of the accessing process is that of the superuser or is the same as the ID value stored in sem_perm.cuid or sem_perm.uid. To make these changes, a structure of the type semid_ds must be allocated. The appropriate members' values are then assigned, and a reference to the modified structure is passed as the fourth argument to the semctl system call.

➢ IPC_RMID» Remove the semaphore set associated with the

semaphore identifier.

SEMAPHORES

Page 83: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

83

Semaphore Control Details▪ The following cmd values cause semctl to act

upon the entire set of semaphores:➢ GETALL

» Return the current values of the semaphore set. The values are returned via the array reference passed as the fourth argument to semctl. The user is responsible for allocating the array of the proper size and type prior to passing its address to semctl. Read permission for the semaphore set is required to specify GETALL. When specifying GETALL, the argument semnum is ignored.

SEMAPHORES

Page 84: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

84

Semaphore Control Details➢ SETALL

» Initialize all semaphores in a set to the values stored in the array referenced by the fourth argument to semctl. Again, the user must allocate the initializing array and assign values prior to passing the address of the array to semctl. The process must have alter access for the semaphore set to use SETALL. When specifying SETALL, the sem_ctime member of the system semaphore data structure is updated.

SEMAPHORES

Page 85: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

85

Semaphore Control Details▪ The last set of semctl cmd values acts upon

individual semaphores or upon specific members in the semid_ds structure. All of these commands require read permission except for SETVAL, which requires alter permission:

➢ GETVAL» Return the current value of the individual semaphore

referenced by the value of the semnum argument.

➢ SETVAL» Set the value of the individual semaphore referenced

by the semnum argument to the value specified by the fourth argument to semctl.

SEMAPHORES

Page 86: Process Synchronization: Semaphores & Mutex 1. Process synchronization In the software context, synchronization refers to the act of ensuring that independent

86

Semaphore Control Details➢ GETPID

» Return the PID from the sem_perm structure within the semid_ds structure.

➢ GETNCNT» Return the number of processes waiting for the

semaphore referenced by the semnum argument to increase in value.

➢ GETZCNT » Return the number of processes waiting for the

semaphore referenced by the semnum argument to become 0.

SEMAPHORES