Docsity

System Programming: Lecture 12 - Synchronization Primitives: Semaphore and Mutex, Study notes of Computer Science

Partial preview of the text

Download System Programming: Lecture 12 - Synchronization Primitives: Semaphore and Mutex and more Study notes Computer Science in PDF only on Docsity!

CS 241 Spring 2007System Programming

CS 241 Spring 2007System Programming

Synchronization Primitives – Semaphoreand Mutex

Lecture 12 Klara Nahrstedt

Outline †^

Using Semaphores – Examples

†^

Thread Synchronization^ „^

Mutex Synchronization Primitive „^

Operations on Mutex „^

Examples

Review: Implementation of Semaphoresin POSIX^ POSIX:SEM semaphore is

variable of type sem_t Atomic Operations:

int sem_init(sem_t *sem, int pshared, unsigned value);int sem_destroy(sem_t *sem);int sem_post(sem_t *sem);Int sem_trywait(sem_t *sem);Int sem_wait(sem_t *sem); Use <semaphore.h>

Example, creating shared variable

#include <errno.h> #include <semaphore.h> static int shared = 0; static sem_t sharedsem; int initshared(int val)

if (

sem_init(&sharedsem, 0, 1)

return -1; shared = val; return 0; }

Example – shared variable

*int getshared(int sval)

while (

sem_wait(&sharedsem)

if (errno != EINTR)

return -1; *sval = shared; return

sem_post(&sharedsem);

} int incshared()

while (

sem_wait(&sharedsem)

if (errno != EINTR)

return -1; shared++; return

sem_post(&sharedsem);

Thread with Critical Section

(Example

RR:500) -^ #include <errno.h>^ #include <pthread.h>^ #include <semaphore.h>^ #include <stdio.h>^ #include <unistd.h>^ #define TEN_MILLION 10000000L^ #define BUFSIZE 1024^ **void threadout(void args)

char buffer[BUFSIZE]; char *c;^ *sem_t semlockp; struct timespec sleeptime;

Thread with Critical Section /******************

entry section

*************************/

while (

sem_wait(semlockp)

== -1)

/* Entry section */

if(errno != EINTR) {

fprintf(stderr, "Thread failed to lock semaphore\n"); return NULL; } /******************

start of critical section

***********/

while (c != '\0') {*

fputc(c, stderr); c++; nanosleep(&sleeptime, NULL); }*^ /******************

exit section

******************/

if (

sem_post(semlockp)

== -1)

/* Exit section */

fprintf(stderr, "Thread failed to unlock semaphore\n"); /******************

remainder section

****************/

return NULL; }

Main program (Example RR:501)^ if (argc != 2){/* check for valid number of command-linearguments */

fprintf (stderr, "Usage: %s numthreads\n", argv[0]); return 1; } n = atoi(argv[1]); *tids = (pthread_t )calloc(n, sizeof(pthread_t)); if (tids == NULL) {

perror("Failed to allocate memory for thread IDs"); return 1; } if (

sem_init(&semlock, 0, 1)

== -1) {

perror("Failed to initialize semaphore"); return 1; }^

What happens if we replace sem_init with sem_init(&semlock,0,0)?

Main program

for (i = 0; i < n; i++)

if (error =

pthread_create(tids + i, NULL,

threadout,^ &semlock))

fprintf(stderr, "Failed to create thread:%s\n",

strerror(error));

return 1; } for (i = 0; i < n; i++)

if (error =

pthread_join(tids[i], NULL))

fprintf(stderr, "Failed to join thread:%s\n",

strerror(error));

return 1; } return 0; }

POSIX Mutex-related Functions int

pthread_mutex_init

(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);

int

pthread_mutex_destroy

(pthread_mutex_t *mutex);

int

pthread_mutex_lock

(pthread_mutex_t *mutex);

int

pthread_mutex_trylock

(pthread_mutex_t *mutex);

int

pthread_mutex_unlock

(pthread_mutex_t *mutex);

Mutex and Shared Variables Mutex locks are usually used to protect access to a shared

variable.

The idea:

lock the mutex

critical section

unlock the mutex

Unlike a semaphore, a mutex does not have a value, it has

states

(locked and unlocked).

Only the owner of the mutex should unlock the mutex. Do not lock a mutex that is already locked. Do not unlock a mutex that is not already locked.

Use A^

mutex

has type

pthread_mutex_t

Since a

mutex

is meant to be used by multiple threads, it is

usually declared to have

static storage class

It can be defined inside a function using the

static

qualifier

if it will only be used by that function or it can

be defined at the top level. A^

mutex

must be initialized before it is used.

This can be done when the

mutex

is defined, as in:

pthread_mutex_t mymutex =

PTHREAD_MUTEX_INITIALIZER;

Mutex vs. Semaphore Differences/similarity between the two? Mutex is also called binary semaphore in contrast

to general counting semaphore^ Counter is initialized to ???^ pthread_mutex_lock = ???^ pthread_mutex_unlock = ???