Skip to content

Instantly share code, notes, and snippets.

@albertzak
Created June 2, 2016 19:43
Show Gist options
  • Save albertzak/f6825627f08318ce01ccc59f7bcdb738 to your computer and use it in GitHub Desktop.
Save albertzak/f6825627f08318ce01ccc59f7bcdb738 to your computer and use it in GitHub Desktop.
semaphore
/*****************************
* Fernlehre Semaphore Example
* File: main.c
* Author: Granzer Wolfgang
* Version: 1.0
* Date: 30.4.2012
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
/*
* TODO: Add code here for semaphore data structure
*
* Available Semaphore operations are:
* int sem_init (sem_t *sem, int pshared, unsigned int value);
* int sem_wait (sem_t *sem);
* int sem_post (sem_t *sem);
*/
// forward declaration
void* threadIWorker(void* param);
void* threadJWorker(void* param);
int loopFlag = 1;
sem_t s1;
sem_t s2;
/**
* Main Program.
*/
int main(int argc, const char* argv[])
{
pthread_t threadI, threadJ;
/*
* TODO: Add code here to initialize the semaphore.
*/
sem_init(&s1, 0, 1);
sem_init(&s2, 0, 0);
printf("Example Semaphore\n");
printf("----------------------------\n");
printf("Starting Thread I ...\n");
printf("Starting Thread J ...\n");
printf("Press q <enter> to exit\n");
pthread_create(&threadI, NULL, threadIWorker, NULL);
pthread_create(&threadJ, NULL, threadJWorker, NULL);
while (loopFlag)
{
char c = getchar();
if (c == 'q')
{
printf("Bye ...\n");
loopFlag = 0;
}
}
pthread_join(threadI, NULL);
pthread_join(threadJ, NULL);
return 0;
}
/**
* Thread I
*/
void* threadIWorker(void* param)
{
while (loopFlag)
{
/*
* TODO: Enter critical section.
*/
sem_wait(&s1);
printf("I: =========\n");
usleep(100000*(rand()%10));
printf("I: Do\n");
usleep(100000*(rand()%10));
printf("I: some\n");
usleep(100000*(rand()%10));
printf("I: usefull\n");
usleep(100000*(rand()%10));
printf("I: stuff\n");
usleep(100000*(rand()%10));
printf("I: within\n");
usleep(100000*(rand()%10));
printf("I: a\n");
usleep(100000*(rand()%10));
printf("I: critical\n");
usleep(100000*(rand()%10));
printf("I: section\n");
usleep(100000*(rand()%10));
printf("I: =========\n");
/*
* TODO: Leave critical section.
*/
sem_post(&s2);
usleep(100000*(rand()%10));
}
return 0;
}
/**
* Thread J
*/
void* threadJWorker(void* param)
{
while(loopFlag)
{
/*
* TODO: Enter critical section.
*/
sem_wait(&s2);
printf("J: =========\n");
usleep(100000*(rand()%10));
printf("J: Do\n");
usleep(100000*(rand()%10));
printf("J: some\n");
usleep(100000*(rand()%10));
printf("J: usefull\n");
usleep(100000*(rand()%10));
printf("J: stuff\n");
usleep(100000*(rand()%10));
printf("J: within\n");
usleep(100000*(rand()%10));
printf("J: a\n");
usleep(100000*(rand()%10));
printf("J: critical\n");
usleep(100000*(rand()%10));
printf("J: section\n");
usleep(100000*(rand()%10));
printf("J: =========\n");
/*
* TODO: Leave critical section.
*/
sem_post(&s1);
usleep(100000*(rand()%10));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment