Thread: question about semaphores

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    41

    Post question about semaphores

    Code:
    typedef struct {
        int buf[BUFF_SIZE];   /* shared var */
        int in;         	  /* buf[in%BUFF_SIZE] is the first empty slot */
        int out;        	  /* buf[out%BUFF_SIZE] is the first full slot */
        sem_t full;     	  /* keep track of the number of full spots */
        sem_t empty;    	  /* keep track of the number of empty spots */
        sem_t mutex;    	  /* enforce mutual exclusion to shared data */
    } sbuf_t;
    
    sbuf_t shared;
    
    
    
    int main()
    {
        pthread_t idP, idC;
        int index;
    
        sem_init(&shared.full, 0, 0);
        sem_init(&shared.empty, 0, BUFF_SIZE);
    At the red lines,I dont understand why the value of shared.full is 0 and the value of shared.empty is BUFF_SIZE. I need some explanations. Thanks

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Well sem_init full is a counter. I believe your comment for this was enough, so I am not sure where your confusion may be with that. This is just the initial value being used. As far as sem_init() making use of the constant BUFF_SIZE, this may be due to the fact that the function call is using this value argument and subtracting from this.


    Have you read up on this call?
    sem_init

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    Quote Originally Posted by slingerland3g View Post
    Well sem_init full is a counter. I believe your comment for this was enough, so I am not sure where your confusion may be with that. This is just the initial value being used. As far as sem_init() making use of the constant BUFF_SIZE, this may be due to the fact that the function call is using this value argument and subtracting from this.


    Have you read up on this call?
    sem_init
    but why dont we do another way around like
    Code:
    sem_init(&shared.full, 0, BUFF_SIZE);
    sem_init(&shared.empty, 0, 0);
    I think there is another reason for that but I can not get it...
    Last edited by thungmail; 12-01-2009 at 07:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores, need advice on implementation.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2009, 01:54 AM
  2. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM