Docsity

Semaphores - Operating Systems - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Semaphores - Operating Systems - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Operating Systems

Lecture 10:

Semaphores

Mutual Exclusion

Idea #3: Semaphores

There is no way to

inspect or manipulate

semaphores other than

these three operations

A variable that has an

integer value upon

which only three

operations are defined:

1) May be initialized to a nonnegative integer value

2) The semWait operation decrements the value

3) The semSignal operation increments the value

Binary Semaphore Primitives

Consequences

Three consequences of the semaphore definition:

  • There is no way to know before a process decrements a

semaphore whether it will block or not.

  • After a process increments a semaphore and another process

gets woken up, both processes continue running

concurrently. There is no way to know which process, if

either, will continue immediately on a uniprocessor system.

  • When you signal a semaphore, you don’t necessarily know

whether another process is waiting, so the number of

unblocked processes may be zero or one.

Example of Semaphore Mechanism

Mutual Exclusion

How would you fix the code? #include <pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ void *runner(void param); / the thread / main(int argc, char argv[]) { pthread_t tidl, tid; / the thread identifier / pthread_attr_t attr; / set of thread attributes / if (argc != 2) { fprintf(stderr, ’’usage: a.out \n’’); exit(); } if (atoi(argv[1] < 0) { fprintf(stderr, ’’%d must be >= 0\n’’, atoi(argv[1])); exit(); } pthread_attr_init(&attr); / create the threads/ pthread_create(&tid,&attr,runner,argv[1]); pthread_create(&tidl,&attr,runner,argv[1]); pthread_join(tid,NULL); pthread_join(tidl,NULL); printf(sum = %d\n,sum); }

/* The thread will begin control in this
function */
void *runner(void *param)
int upper = atoi(param);
int i;
sum = 0;
if (upper > 0) {
for (i = 1; i <= upper; i++)
sum += i;
pthread_exit(0);

How would you implement… 11

Rendez-vous Requirement: Thread A has to wait for Thread B and vice-versa: a1 happens before b2 and b1 happens before a2.

Thread A

statement a

statement a

Thread B

statement b

statement b

Mutual Exclusion Requirement: only one thread accesses the shared variable at a time. Question: what would be the problem if mutual exclusion not enforced in this case?

Thread A

count = count + 1;

Thread B

count = count + 1;

Implementation of Semaphores

  • Imperative that the semWait and semSignal operations be implemented as atomic primitives
  • Can be implemented in hardware or firmware
  • Software schemes such as Dekker’s or Peterson’s algorithms can be used
  • Use one of the hardware-supported schemes for mutual exclusion

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 an empty buffer

Is This Correct?