Thread: semaphore

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    6

    semaphore

    Please i found some examples of semaphore but i don't understand what they do?
    if someone can explain to me what they do with details thank you!
    Code:
    pv.h
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    #include <errno.h>
    
    extern int errno;
    
    #define SEMPERM 0600
    #define TRUE 1
    #define FALSE 0
    #define SEMKEY 0*200
    #define SEMKEYSTR "0*200"
    
    
    union _semun{
     int val;
     struct semid_ds *stat;
     unsigned short *array;
    } 
    
    int p (int semid);
    int v (int semid);
    int initsem (key_t semkey);
    void handlesem (key_t semkey);
    
    ***********************
    /* initsem.c -- semaphore initialisation */
    
    #include "pv.h"
    
    int initsem(key_t semkey)
    {
        int status = 0, semid;
        union semun arg;
        arg.val = 1;
    
        if((semid = semget(semkey, 1, SEMPERM|IPC_CREAT|IPC_EXCL)) == -1)
        {
    	if (errno ==EEXIST)
    	    semid = semget(semkey, 1, 0);
        }
        else /* if created ... */
        {
    	status = semctl(semid, 0, SETVAL, arg);
        }
    
        if (semid == -1 || status == -1)
        {
    	perror("initsem failed");
    	return(-1);
        }
    
        /* all okay */
        else
        return(semid);
    }
    void removesem (char *key)
    {
        execlp ("ipcrm", "ipcrm", "-S", key,(char *) 0);
    
    *************************
    pc.c
    #include "pv.h"
    int p(int semid){
        struct sembuf p_buf;
    
        p_buf.sem_num = 0;
        p_buf.sem_op = -1;
        p_buf.sem_flg = SEM_UNDO; /* undoes operation if process exits! */
    
        if (semop(semid, &p_buf, 1) == -1)
        {
    	perror("p(semid) failed");
    	exit(1);
        } 
    else
    	return (0);
    }
    //p()
    
    ***************************
    vc.c
    #include "pv.h"
    int v(int semid){
        struct sembuf v_buf;
    
        v_buf.sem_num = 0;
        v_buf.sem_op = 1;
        v_buf.sem_flg = SEM_UNDO;
    
        if (semop(semid, &v_buf, 1) == -1){
    	perror("v(semid) failed");
    	exit(1);
        } else
    	return(0);
    }//v()
    
    ****************************
    testsem.c
    /* program for testing p() and v() -- gets mutual exclusion */
    
    #include "pv.h"
    
    int main(void){
      key_t semkey = SEMKEY;
    
      if(fork() == 0) handlesem(semkey);
      else if(fork() == 0) handlesem(semkey);
      else if(fork() == 0) handlesem(semkey);
      
      while (wait ((int *) 0) >=0)
      removesem (semkeystr);
      return 0;
      }//main()
    
    void handlesem(key_t skey){
      int semid, pid = getpid();
    
      /* initialize the semaphore if we just created it */
      
      if ((semid = initsem(skey)) < 0) exit(1);
    
      printf("\nprocess %d BEFORE critical section\n", pid);
      
      p(semid);
    
      printf("\nprocess %d in critical section \n", pid);
    
      sleep(10);
       
      printf("\nprocess %d leaving critical section \n", pid);
      
      v(semid); /* call semaphore SIGNAL operation */
      
      printf("\nprocess %d is OUTSIDE critical section and quitting!\n", pid);
      exit(0);
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Ok ok No one can answer me please!!!!!!!!!!!!!!!!!!!!!!

  3. #3

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    I know that i'm a beginner about semaphore i train myself with a little exercises and i just want to know what this code do sorry to disturb you!!!!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Then ask a specific question about specific lines of code.

    Don't just dump the whole thing and expect a complete explanation(*). Show what you've managed to figure out for yourself, then perhaps we'll help with the next step.

    (*) Any explanation would suffer from the "3 bears" problem of either being
    - too hard for you to understand
    - tell you things you already know
    - just right.
    A 66% of wasting our time isn't productive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Salam salem
    Thanks a lot to reply me
    actually i don't want you to describe all the line
    i just want an explaination of what they do this code and in what case we can use it?
    Thank you and sorry for the few details

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, so what do you expect from us which isn't in the manual pages for those functions?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    What i understand:

    critical section created by p and v
    3 childs created
    only 1 child can access to the critical section between p and v

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, that is what a semaphore is designed to do - ensure restricted access to a critical resource.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    http://www.ecst.csuchico.edu/~beej/guide/ipc/

    heres a link to a good semaphore page.....I honestly don't know how I still have this, this is a link from the class I leared semaphores from and use them all the time. May be outdated from 5 years ago.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    This is excellent for anyone trying to learn too :
    http://www.cs.mtu.edu/~shene/NSF-3/e-Book/index.html

    I've found its explainations better than most so far.
    we are one

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    Code:
      if(fork() == 0)
    fork makes me smile
    we are one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Kernel: Legal to kfree() a down()'ed semaphore
    By Kennedy in forum Linux Programming
    Replies: 1
    Last Post: 06-05-2009, 09:33 AM
  2. semaphore
    By menzeli in forum C Programming
    Replies: 1
    Last Post: 03-24-2007, 11:34 AM
  3. clueless about the semaphore object
    By y_cant_i_C in forum Windows Programming
    Replies: 1
    Last Post: 10-25-2006, 01:03 PM
  4. Semaphore Question
    By azamsharp1 in forum C Programming
    Replies: 4
    Last Post: 10-30-2005, 09:01 AM
  5. CreateSemaphore/ReleaseSemaphore
    By nrieger in forum Windows Programming
    Replies: 2
    Last Post: 08-03-2005, 06:57 AM