Thread: Problem about C semaphore functions

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Smile Problem about C semaphore functions

    Hello,

    I am studying on semaphores in C about 5-6 days. I found a java code piece and wanted to turn that to C language but couldn't find one function in C.

    I wanna find what should I use in C for that or is there anything for this function.

    In Java: semap.availablePermits() In C : ?

    Is it true ?

    In Java: semap.acquire() In C: WaitForSingleObject(semap, INFINITE) ?


    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    south florida
    Posts
    1
    I'm on Sun Solaris, but most of this should be the same regardless of OS. I hope this helps.
    - LT

    semaphore research in the MAN pages:
    man semop
    man semctl
    man semget
    man ipcs
    man ipcrm

    the include files:
    Code:
        #include <sys/types.h>
        #include <sys/ipc.h>
        #include <sys/sem.h>
    A unique key and semaphore struct:
    Code:
         key_t semkey = 2012;
    
         union semun {
            int val;
            struct semid_ds *buf;
            ushort *array;
         } arg;
    get semaphore id for use:
    Code:
         int  semflg = IPC_CREAT | 0666;  /* create with full access */
         int  nsems = 1;  /* number of concurrent processes to allow */
         id = semget(semkey, nsems, semflg); /* "id" is used in the semctl() and semop() functions */

    create a semaphore:
    Code:
         id = semget(semkey, nsems, semflg);
         arg.val = 1;
         if (semctl(id, 0, SETVAL, arg) < 0)
                    fprintf(stderr, "semctl create() failed\n");
    delete a semaphore:
    Code:
         id = semget(semkey, nsems, semflg);
          if ( semctl(id, 0, IPC_RMID, arg) == -1)
                    fprintf(stderr, "semctl delete() failed\n");
    lock semaphore:
    Code:
          struct sembuf sbuf;
          sbuf.sem_num = 0;
          sbuf.sem_op  = -1;  /* lock it */
          sbuf.sem_flg = SEM_UNDO;
          if ( semop(id, &sbuf, 1) == -1)
                    fprintf(stderr, "semop lock() failed\n");
    unlock semaphore:
    Code:
          struct sembuf sbuf;
          sbuf.sem_num = 0;
          sbuf.sem_op  = 1;  /* UNLOCK IT */
          sbuf.sem_flg = SEM_UNDO;
          if ( semop(id, &sbuf, 1) == -1)
                    fprintf(stderr, "semop unlock() failed\n");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with functions in inherited classes
    By cableguy414 in forum C++ Programming
    Replies: 15
    Last Post: 08-30-2009, 10:56 AM
  2. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  3. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 05:42 PM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM