Presentation is loading. Please wait.

Presentation is loading. Please wait.

Synchronization.

Similar presentations


Presentation on theme: "Synchronization."— Presentation transcript:

1 Synchronization

2 Implementation of Semaphores in 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>

3 Unnamed Semaphores Ch 14 pp 491-501
#include <semaphore.h> Sem_t sem; You cannot make a copy of a semaphore variable!!! #include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned value); pshared == 0: only threads of process creating semaphore can use semaphore.

4 Sharing Semaphores Sharing semaphores between threads within a process is easy, use pshared==0 Forking a process creates copies of any semaphore it has… this does not share the semaphore Making pshared non zero allows any process that can access the semaphore to use it. This places the semaphore in global environment.

5 sem_init can fail!!! In unsuccessful, sem_init returns -1 and sets errno. Example: sem_t semA; if (sem_init(&semA, 0, 1) == -1) perror(“Failed to initialize semaphore semA”); Value > sem_value_max Resources exhausted Insufficient privileges EINVAL ENOSPC EPERM cause errno

6 Semaphore Operations #include <semaphore.h>
int sem_destroy(sem_t *sem); Destroying a semaphore that’s been destroyed gives undefined result. Destroying a semaphore on which a thread is blocked gives undefined results.

7 Semaphore Operations #include <semaphore.h>
int sem_post(sem_t *sem); Unlocks the semaphore. If the semaphore value resulting from this operation is: positive, then no threads were blocked waiting for the semaphore. zero, then one of the threads blocked waiting for the semaphore will be allowed to return successfully from its call to sem_wait(). if unsuccessful, returns -1 and sets errno errno == EINVAL if semaphore doesnt exist

8 Semaphore Operations int sem_trywait(sem_t *sem);
locks the semaphore if it is currently not locked (>0). doesn’t block returns -1 and errno==EAGAIN if semaphore zero can be interrupted by signal – errno == EINTR int sem_wait(sem_t *sem); locks the semaphore. If the semaphore value is currently zero, then the calling thread will block. Can be interrupted by signal – errno == EINTR

9 Example 1 on Semaphore (RR: 497)
We want a shared variable ‘shared’ (critical section) to be protected by semaphore to allow for two functions getshared – is a function that returns the current value of the shared variable ‘shared’ incshared – is a function that that atomically increments the ‘shared’ variable.

10 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) == -1) return -1; shared = val; return 0; }

11 Example – shared variable
int getshared(int *sval) { while (sem_wait(&sharedsem) == -1) if (errno != EINTR) return -1; *sval = shared; return sem_post(&sharedsem); } int incshared() { shared++;

12 Example 2 on Semaphore (RR:500)
A program to generate a set of threads and each thread writes to standard error Standard error (stderr) is a shared resource, hence if a thread outputs an informative message to standard error one character at the time, it becomes a critical region and we must protect it.

13 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 L #define BUFSIZE 1024

14 Thread with Critical Section
void *threadout(void *args) { char buffer[BUFSIZE]; char *c; sem_t *semlockp; struct timespec sleeptime; semlockp = (sem_t *)args; sleeptime.tv_sec = 0; sleeptime.tv_nsec = TEN_MILLION; snprintf(buffer, BUFSIZE, "This is a thread from process %ld\n", (long) getpid()); c = buffer;

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

16 Main program (Example RR:501)
#include <pthread.h> #include <semaphore.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void *threadout(void *args); int main(int argc, char *argv[]) { int error; int i; int n; sem_t semlock; pthread_t *tids;

17 Main program (Example RR:501)
if (argc != 2){/* check for valid number of command-line arguments */ 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"); if (sem_init(&semlock, 0, 1) == -1) { perror("Failed to initialize semaphore");

18 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; } if (error = pthread_join(tids[i], NULL)) { fprintf(stderr, "Failed to join thread:%s\n", return 0; }


Download ppt "Synchronization."

Similar presentations


Ads by Google