Presentation is loading. Please wait.

Presentation is loading. Please wait.

Process Synchronization: Semaphores & Mutex

Similar presentations


Presentation on theme: "Process Synchronization: Semaphores & Mutex"— Presentation transcript:

1 Process Synchronization: Semaphores & Mutex
1 1

2 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

3 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

4 Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta
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

5 Traffic intersections in rail cross
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta 5

6 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. 6

7 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; }

8 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() 8

9 Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta
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

10 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

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

12 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>; }

13 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

14 Semaphore based solutions to benchmark synchronization problems
Cigarette smokers problem Dining philosophers problem Readers-writers problem Sleeping barber problem Producers-consumers problem

15 Dining Philosophers: an intellectual game
1 N philosophers and N forks Philosophers eat/think 4 1 2 4 3 2 3 N=5

16 Dining Philosophers while(TRUE) { think(); eat(); }
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(); }

17 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);

18 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.

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

20 Bounded Buffer Problem
Empty Pool Producer Consumer Full Pool

21 Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta
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

22 Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta
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

23 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

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

25 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

26 Readers-Writers Problem

27 Readers-Writers Problem (2)
Shared Resource

28 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

29 First Solution First reader competes with writers
while(TRUE) { <other computing>; P(mutex); readCount++; if(readCount == 1) P(writeBlock); V(mutex); /* Critical section */ access(resource); readCount--; if(readCount == 0) V(writeBlock); } 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

30 First Solution (2) First reader competes with writers
while(TRUE) { <other computing>; P(mutex); readCount++; if(readCount == 1) P(writeBlock); V(mutex); /* Critical section */ access(resource); readCount--; if(readCount == 0) V(writeBlock); } 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

31 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); readCount--; if(readCount == 0) V(writeBlock); } 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); }

32 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.

33 The Sleepy Barber Barber can cut one person’s hair at a time
Other customers wait in a waiting room Entrance to Waiting Room (sliding door) Shop Exit Entrance to Barber’s Room (sliding door) Waiting Room

34 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

35 Sleepy Barber (aka Bounded Buffer)
while(TRUE) { P(waitingCustomer); P(mutex); emptyChairs++; takeCustomer(); V(mutex); V(chair); } 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);

36 Cigarette smoker’s problem
Assume a cigarette  requires three ingredients to smoke: Tobacco , Smoking Paper, A Match Assume 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.

37 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

38 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

39 Agent process 1 do forever { 2 P( lock );
randNum = rand( 1, 3 ); // Pick a random number from 1-3 if ( randNum == 1 ) { // Put tobacco on table // Put paper on table V( smoker_match ); // Wake up smoker with match } else if ( randNum == 2 ) { // Put tobacco on table // Put match on table V( smoker_paper ); // Wake up smoker with paper } else { // Put match on table // Put paper on table V( smoker_tobacco ); // Wake up smoker with tobacco 16 } V( lock ); P( agent ); // Agent sleeps } // end forever loop

40 Smoker process 1 do forever {
P( smoker_tobacco ); // Sleep right away P( lock ); // Pick up match // Pick up paper V( agent ); V( lock ); // Smoke (but don't inhale). 9 }

41 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;

42 Implementing Semaphores: Test and Set Instruction
“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;] Data Register CC Register TRUE m Primary Memory FALSE R3 =0 Data Register CC (b) After Executing TS R3 m FALSE Primary Memory Before Executing TS

43 Using the TS Instruction
boolean s = FALSE; . . . while(TS(s)) ; <critical section> s = FALSE; semaphore s = 1; . . . P(s) ; <critical section> V(s);

44 Implementing the General Semaphore
struct 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 V(struct semaphore s) { while(TS(s.mutex)) ; s.value++; if(s.value <= 0) { while(!s.hold) ; s.hold = FALSE; } s.mutex = FALSE;

45 Operating Systems: A Modern Perspective, Chapter 9
Inter process Communication Mechanisms Operating Systems: A Modern Perspective, Chapter 9

46 Operating Systems: A Modern Perspective, Chapter 9
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 46

47 IPC Mechanisms Sharing of Information can be in User space
Message OS IPC Mechanism Info copy Info to be shared Address Space for p0 Address Space for p1 Sharing of Information can be in User space Kernel space

48 Operating Systems: A Modern Perspective, Chapter 9
Main IPC methods Operating Systems: A Modern Perspective, Chapter 9

49 Operating Systems: A Modern Perspective, Chapter 9
IPC mechanisms System V IPC Shared memory Semaphore Message queue Signals Pipes (named pipe also) Sockets Operating Systems: A Modern Perspective, Chapter 9

50 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.

51 Operating Systems: A Modern Perspective, Chapter 9
SYS V IPC Each mechanism contains Table Key Get Operating Systems: A Modern Perspective, Chapter 9

52 COP5570 – Advanced Unix Programming
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

53 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) Shared memory is one among the three SYS V IPC mechanism. Any number of unrelated processes can communicate using this concept. The shared memory shared by all the unrelated processes lies in the user space ( as a segment in the address space of one of the processes). As many processes try to share the same piece of memory, this shared piece of memory becomes the critical section. So, we require an external synchronization mechanism to manage the access to the shared resource (shared memory). Shared memory is specially useful, when processes want to share large data i.e., this provides an efficient access to large areas of memory.

54 Shared memory is a special range of address that is there in one processes address space and all the processes that are interested in making the IPC, attaches themselves to this memory segment. By this, this shared segment of memory is there in all the processes addresses space. Out of all the processes, one process gets this special ranged address space, and all other processes including the first, will attach this memory segment in their address space.

55 shmget shmat shmdt shmctl Create / retrieve
Attach to the virtual address space shmdt Detach form virtual address space shmctl Manipulation of various parameters Now let us look at using the shared memory concept. The API’s associated with the shared memory are shmget, shmat, shmdt, shmctl Shmget is the API used to create a new shared memory region or it is used to retrieve the existing shared memory region with the requirements. The existing shared memory can be retrieved by the key associated with it. shmat API logically attaches the shared memory to its virtual memory address space. After attaching the region to its process address space, we can share the data by writing and reading to and from the shared memory. Shmdt is the API used to detach the shared address space from its process address space. Shmctl is the API to control the shared memory. It is used to get the properties of the shared memory segment. A special command IPC_RMID passed to shmctl, that particular shared memory segment with the key is destroyed.

56 Syntax Example shmid = shmget((key_t)1234,4,IPC_CREAT | IPC_EXCL) ;
#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) ; } First of the API’s shmget: Let us look at the syntax of the shmget Arguments are: key: this is the user identifier to the shared memory segment. Size: size of the shared memory segment that is to be shared. By default, a page size of shared memory is allocated. Flags: IPC_CREAT: to create , IPC_EXCL: exclusively create a shared segment, IPC_PRIVATE: only for that particular process Let us look at the example: The arguments that we used are key:1234, size:4 bytes, Flags: IPC_CREAT | IPC_EXCL. This combination of flags helps to find whether any shared segment is available with the key 1234 or not. If it is already existing, the function fails. Then, we try to take the permissions to get an access to the shared segment On success of this function, shared memory identifier is return in shmid. From this point, when ever we want to access the shared memory, we access it using the shmid

57 void *shmat(int shm_id, const void *shm_addr, int shmflg);
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) ; } Next API used is to attach shared memory to the process address space – shmat() This has 3 arguments: 1. Shmid – associated shared memory ID, 2. Shm_addr – the virutal address of the shared memory. If we don’t know the shared memory address, we pass NULL. Internally that would search for the address through the id. 3. Shmflg – specifies whether the region is read-only and whether kernel should round-off the user specified address. Return value is the virtual memory address where the kernel attached the shared memory.

58 int shmdt(const void*memory) ;
Syntax: Example int shmdt(const void*memory) ; retval = shmdt(memory) ; if(retval < 0) { printf("process 1 could not detach the memory\n") ; exit(0) ; } Shmdt is the 3rd API in the list. After processing the shared memory and the data, we need to detach the shared memory from the process address space. This API shmdt would help in detaching. This call takes one argument Memory – address of the virtual memory region from shmat call. Returns whether it is successfully detached or an error number.

59 retval = shmctl(shmid,IPC_RMID,NULL) ; if(retval < 0) {
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. retval = shmctl(shmid,IPC_RMID,NULL) ; if(retval < 0) { printf("removing the shared memory had failed\n") ; } Now let us look at the 2nd argument in the shmctl command. This argument can take 3 values, IPC_STAT, IPC_SET are to be used to query status or set the permissions with the 3rd argument with buf. The command IPC_RMID is used to destroy the shared memory segment. Here with this command, 3rd argument would be NULL. This is shown in the example.

60 Operating Systems: A Modern Perspective, Chapter 9
debugging ipcs –shm Shared Memory Segments key shmid owner perms bytes nattch status 0x user If this memory segment was erroneously left behind by a program, you can use the ipcrm command to remove it. ipcrm shm Operating Systems: A Modern Perspective, Chapter 9

61 Pros Cons Easy to access as it is in user space Fastest IPC mechanism
Synchronization mechanism is not there. Coming to advantages and disadvantages of shared memory – Lets discuss about the advantages first. As it is in the user space, it is very easy and fast to access it. Also, the data in the shared memory is persistent. But coming to the disadvantages, mainly there is no synchronization between the processes. Think of a scenario, process-1 is supposed to collect a message after process-2 posts it. If process-2 comes first and posts it, process-1 collects it easily. But if process -1 comes first, it cannot find the data so it comes out.

62 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

63 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

64 Creating and Accessing Semaphore Sets
Figure 7.1. Data structures for a set of three semaphores. 64

65 semget semop semctl Create / retrieve
Wait and signal operations use the same call with different arguments semctl Manipulation of various parameters, controlling the semaphore Semaphores uses 3 API’s Semget, semop and semctl. We will see more about these functions in the next slides.

66 Creating and Accessing Semaphore Sets
2 Manual Section <sys/types.h> <sys/ipc.h> <sys/sem.h> Include File(s) int semget (key_t key,int nsems,int semflg); Summary Return Sets errno Failure Success Yes -1 The semaphore identifier 66

67 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

68 SEMAPHORES 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

69 Semaphore Control 2 Manual Section <sys/types.h>
SEMAPHORES Semaphore Control 2 Manual Section <sys/types.h> <sys/ipc.h> <sys/sem.h> Include File(s) int semctl(int semid, int semnum, int cmd, union semun arg); Summary Return Sets errno Failure Success Yes -1 0 or the value requested 69

70 Semaphore Control The semctl system call takes four arguments:
SEMAPHORES 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

71 SEMAPHORES 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

72 SEMAPHORES Semaphore Operations Additional operations on individual semaphores are accomplished by using the semop system call. 72

73 Semaphore Operations 2 Manual Section <sys/types.h>
SEMAPHORES Semaphore Operations 2 Manual Section <sys/types.h> <sys/ipc.h> <sys/sem.h> Include File(s) int semop(int semid, struct sembuf *sops, unsigned nsops); Summary Return Sets errno Failure Success Yes -1 Table 7.5. Summary of the semop System Call 73

74 Semaphore Operations The semop system call takes three arguments:
SEMAPHORES 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

75 Semaphore Operation Details
SEMAPHORES 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

76 Semaphore Operation Details
SEMAPHORES 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

77 SEMAPHORES 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

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

79 Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta
P() operation Copyright ©: Nahrstedt, Angrave, Abdelzaher, Kravets, Gupta 79

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

81 Semaphore Control Details
SEMAPHORES 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. 81

82 Semaphore Control Details
SEMAPHORES 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. 82

83 Semaphore Control Details
SEMAPHORES 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. 83

84 Semaphore Control Details
SEMAPHORES 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. 84

85 Semaphore Control Details
SEMAPHORES 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. 85

86 Semaphore Control Details
SEMAPHORES 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. 86


Download ppt "Process Synchronization: Semaphores & Mutex"

Similar presentations


Ads by Google