pthreads + "private" semaphores issue

This is a discussion on pthreads + "private" semaphores issue within the C Programming forums, part of the General Programming Boards category; Posted this over on stack overflow, but they were no help. This is an assignment i'm working on, it must ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    11

    pthreads + "private" semaphores issue

    Posted this over on stack overflow, but they were no help.

    This is an assignment i'm working on, it must use semaphores, not mutex.

    Code:
    #include <stdio.h> 
    #include <pthread.h> 
    #include <assert.h> 
    #include <unistd.h> 
    #include <semaphore.h> 
    #include <fcntl.h>
    sem_t *ab, *ac, *ad, *de, *ce, *bf, *ef; 
    
    void *a(void *arg) {
    	printf("Entering A...\n");
    	sleep(1);
    	printf("Exiting A...\n");
    	assert(sem_post(ab)==0);
    	assert(sem_post(ac)==0);
    	assert(sem_post(ad)==0);
    	pthread_exit((void *)99);
    }
    
    void *b(void *arg) {
    	assert(sem_wait(ab)==0);
    	printf("Entering B...\n");
    	sleep(1);
    	printf("Exiting B...\n");
    	assert(sem_post(bf)==0);
    	pthread_exit((void *)99);
    }
    
    void *c(void *arg) {
    	assert(sem_wait(ac)==0);
    	printf("Entering C...\n");
    	sleep(1);
    	printf("Exiting C...\n");
    	assert(sem_post(ce)==0);
    	pthread_exit((void *)99);
    }
    
    void *d(void *arg) {
    	assert(sem_wait(ad)==0);
    	printf("Entering D...\n");
    	sleep(1);
    	printf("Exiting D...\n");
    	assert(sem_post(de)==0);
    	pthread_exit((void *)99);
    }
    
    void *e(void *arg) {
    	assert(sem_wait(ce)==0);
    	assert(sem_wait(de)==0);
    	printf("Entering E...\n");
    	sleep(1);
    	printf("Exiting E...\n");
    	assert(sem_post(ef)==0);
    	pthread_exit((void *)99);
    }
    
    void *f(void *arg) {
    	assert(sem_wait(bf)==0);
    	assert(sem_wait(ef)==0);
    	printf("Entering F...\n");
    	sleep(1);
    	printf("Exiting F...\n");
    	pthread_exit((void *)99);
    }
    
    
    int main() { 
    	pthread_t _a, _b, _c, _d, _e, _f;
     	int r1, r2, r3, r4, r5, r6;
    
    	ab=sem_open("foobar", O_CREAT, 0700, 0);
    	ac=sem_open("foobar", O_CREAT, 0700, 0);
    	ad=sem_open("foobar", O_CREAT, 0700, 0);
    	ce=sem_open("foobar", O_CREAT, 0700, 0);
    	de=sem_open("foobar", O_CREAT, 0700, 0);
    	ef=sem_open("foobar", O_CREAT, 0700, 0);
    	bf=sem_open("foobar", O_CREAT, 0700, 0);
    
    	assert(pthread_create(&_a, NULL, a, &r1) == 0);
    	assert(pthread_create(&_b, NULL, b, &r2) == 0);
    	assert(pthread_create(&_c, NULL, c, &r3) == 0);
    	assert(pthread_create(&_d, NULL, d, &r4) == 0);
    	assert(pthread_create(&_e, NULL, e, &r5) == 0);
    	assert(pthread_create(&_f, NULL, f, &r6) == 0);
    
    	assert(pthread_join(_a, NULL) == 0);
    	assert(pthread_join(_b, NULL) == 0);
    	assert(pthread_join(_c, NULL) == 0);	
    	assert(pthread_join(_d, NULL) == 0);
    	assert(pthread_join(_e, NULL) == 0);
    	assert(pthread_join(_f, NULL) == 0);
    
    	assert( sem_close(ab)==0 ); 
    	assert( sem_close(ac)==0 ); 
    	assert( sem_close(ad)==0 ); 
    	assert( sem_close(ce)==0 );
    	assert( sem_close(de)==0 ); 
    	assert( sem_close(bf)==0 );
    	assert( sem_close(ef)==0 ); 
     
    	return 0; 
    }
    Its pretty simple, but for some reason its not executing in the right order. It is far from consistent, but always incorrect Here is one output:

    Entering A...
    Entering B... <----sem_post(ab) has not even been called yet
    Exiting A...
    Entering C...
    Entering D...
    Exiting B...
    Exiting D...
    Exiting C...
    Entering E...
    Entering F...
    Exiting F...
    Exiting E...

    The proper order should follow these:

    A->B, A->C, A->D, C->E, D->E, B->F, E->F

    (i.e A->B means B must finish before A can start)

    I feel like i'm missing something regarding the semaphores but I can't figure out what. Any help on this would be greatly appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So are you in the same class as this guy: Semaphore noob - sem_open ok, but sem_init not
    Or did you just make a new account so that you could pretend I didn't already tell you to check your return values?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    11
    Didn't see that post, it is the same class tho im sure of it.

    What return values are you referring to that I have not already used an assert on?

    The program works but the threads aren't executing correctly(and im guessing the other guys isnt either, it looks the same as mine.
    I changed it a bit heres the updated code:

    Code:
    #include <stdio.h> 
    #include <pthread.h> 
    #include <assert.h> 
    #include <unistd.h> 
    #include <semaphore.h> 
    #include <fcntl.h>
    sem_t ab, ac, ad, de, ce, bf, ef; 
    
    void *a(void *arg) {
    	printf("Entering A...\n");
    	//sleep(1);
    	printf("Exiting A...\n");
    	assert(sem_post(&ab)==0);
    	assert(sem_post(&ac)==0);
    	assert(sem_post(&ad)==0);
    	pthread_exit((void *)99);
    }
    
    void *b(void *arg) {
    	assert(sem_wait(&ab)==0);
    	printf("Entering B...\n");
    	//sleep(1);
    	printf("Exiting B...\n");
    	assert(sem_post(&bf)==0);
    	pthread_exit((void *)99);
    }
    
    void *c(void *arg) {
    	assert(sem_wait(&ac)==0);
    	printf("Entering C...\n");
    	//sleep(1);
    	printf("Exiting C...\n");
    	assert(sem_post(&ce)==0);
    	pthread_exit((void *)99);
    }
    
    void *d(void *arg) {
    	assert(sem_wait(&ad)==0);
    	printf("Entering D...\n");
    	//sleep(1);
    	printf("Exiting D...\n");
    	assert(sem_post(&de)==0);
    	pthread_exit((void *)99);
    }
    
    void *e(void *arg) {
    	assert(sem_wait(&ce)==0);
    	assert(sem_wait(&de)==0);
    	printf("Entering E...\n");
    	//sleep(1);
    	printf("Exiting E...\n");
    	assert(sem_post(&ef)==0);
    	pthread_exit((void *)99);
    }
    
    void *f(void *arg) {
    	assert(sem_wait(&bf)==0);
    	assert(sem_wait(&ef)==0);
    	printf("Entering F...\n");
    	//sleep(1);
    	printf("Exiting F...\n");
    	pthread_exit((void *)99);
    }
    
    int main() { 
    	pthread_t _a, _b, _c, _d, _e, _f;
     	int r1, r2, r3, r4, r5, r6;
    
    	sem_init(&ab,0,1);
       	sem_init(&ac,0,1);
    	sem_init(&ad,0,1);
    	sem_init(&ce,0,1);
    	sem_init(&de,0,1);
    	sem_init(&ef,0,1);
    	sem_init(&bf,0,1);
    
    	assert(pthread_create(&_a, NULL, a, &r1) == 0);
    	assert(pthread_create(&_b, NULL, b, &r2) == 0);
    	assert(pthread_create(&_c, NULL, c, &r3) == 0);
    	assert(pthread_create(&_d, NULL, d, &r4) == 0);
    	assert(pthread_create(&_e, NULL, e, &r5) == 0);
    	assert(pthread_create(&_f, NULL, f, &r6) == 0);
    
    	assert(pthread_join(_a, NULL) == 0);
    	assert(pthread_join(_b, NULL) == 0);
    	assert(pthread_join(_c, NULL) == 0);	
    	assert(pthread_join(_d, NULL) == 0);
    	assert(pthread_join(_e, NULL) == 0);
    	assert(pthread_join(_f, NULL) == 0);
    
    	assert( sem_destroy(&ab)==0 );
    	assert( sem_destroy(&ac)==0 );
    	assert( sem_destroy(&ad)==0 );
    	assert( sem_destroy(&ce)==0 );
    	assert( sem_destroy(&de)==0 );
    	assert( sem_destroy(&bf)==0 );
    	assert( sem_destroy(&ef)==0 );
     
    	return 0; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pthreads Error "_imp_pthread_create"
    By Nico N in forum C Programming
    Replies: 3
    Last Post: 08-10-2009, 07:34 AM
  2. Replies: 8
    Last Post: 01-17-2006, 05:39 PM
  3. how to access "private or protected " in Class
    By johnnypiere in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2002, 02:33 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21