Click to See Complete Forum and Search --> : Semaphore problem


mjones256
February 25th, 2009, 09:39 PM
So, I'm supposed to implement a semaphore program to synchronize the following process graph. Put printfs at the beginning and end of each thread and a sleep(1) as the body. The graph http://bcook.cs.georgiasouthern.edu/cs523/syncgraph.JPG

what i've got:

#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <semaphore.h>

sem_t *ab, *ac, *ad, *ce, *de, *bf, *ef;

void *a(void *arg){
printf("A");
sleep(1);
assert(sem_post(ab)==0);
assert(sem_post(ac)==0);
assert(sem_post(ad)==0);
printf("\n");
pthread_exit((void *)99);}
void *b (void *arg){
assert(sem_wait(ab)==0);
printf("B");
sleep(1);
assert(sem_post(bf)==0);
pthread_exit((void *)99);}
void *c(void *arg){
assert(sem_wait(ac)==0);
printf("C");
sleep(1);
assert(sem_post(ce)==0);
pthread_exit((void *)99);}
void *d(void *arg){
assert(sem_wait(ad)==0);
printf("D");
sleep(1);
assert(sem_post(de)==0);
pthread_exit((void *)99);}
void *e(void *arg){
assert(sem_wait(ce)==0);
assert(sem_wait(de)==0);
printf("E");
sleep(1);
assert(sem_post(ef)==0);
pthread_exit((void *)99);}
void *f(void *arg){
assert(sem_wait(bf)==0);
assert(sem_wait(ef)==0);
printf("F");
pthread_exit((void *)99);}

int main(){
pthread_t x;
pthread_t y;
pthread_t z;
pthread_t w;
pthread_t v;
pthread_t t;
void *r;

assert(pthread_create(&a, NULL, x, (void *)34) == 0);
assert(pthread_create(&b, NULL, y, (void *)34) == 0);
assert(pthread_create(&c, NULL, z, (void *)34) == 0);
assert(pthread_create(&d, NULL, w, (void *)34) == 0);
assert(pthread_create(&e, NULL, v, (void *)34) == 0);
assert(pthread_create(&f, NULL, t, (void *)34) == 0);

return 0;}

MrViggy
February 26th, 2009, 01:13 PM
Ok. What is your problem/question? Also, please use code tags (it makes reading code easier).

Viggy

zerver
March 2nd, 2009, 07:14 AM
We love to help noobs, but like Viggy said, your question must be specific.

Posting homework without thinking is a no-go.