semget() problem - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

semget() problem

ThemisThemis Posts: 73Member
[b][red]This message was edited by Themis at 2004-8-30 3:6:0[/red][/b][hr]
Hello, I 'm trying to write a program that needs semaphores in Linux. I 'm trying to use semget() to get a semaphore but the call fails:
[code]
if ((((struct tobuffer_data *)data)->semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0644)) != 0)
perror("semget()");
[/code]

What I get is:
semget(): Illegal seek

Any idea why this happens?

Thanks, Themis


Comments

  • johnsonmejohnsonme Posts: 57Member
    /*Allocating and Deallocating a Binary Semaphore*/
    #include
    #include
    #include
    /* We must define union semun ourselves. */
    union semun {
    int val;
    struct semid_ds *buf;
    unsigned short int *array;
    struct seminfo *__buf;
    };
    /* Obtain a binary semaphores ID, allocating if necessary. */
    int binary_semaphore_allocation (key_t key, int sem_flags)
    {
    return semget (key, 1, sem_flags);
    }
    /* Deallocate a binary semaphore. All users must have finished their
    use. Returns -1 on failure. */
    int binary_semaphore_deallocate (int semid)
    {
    union semun ignored_argument;
    return semctl (semid, 1, IPC_RMID, ignored_argument);
    }

    /**********************************************************************/
    /*Wait and Post Operations for a Binary Semaphore*/
    #include
    #include
    #include
    /* Wait on a binary semaphore. Block until the semaphore value is positive, then
    decrement it by 1. */
    int binary_semaphore_wait (int semid)
    {
    struct sembuf operations[1];
    /* Use the first (and only) semaphore. */
    operations[0].sem_num = 0;
    /* Decrement by 1. */
    operations[0].sem_op = -1;
    /* Permit undoing. */
    operations[0].sem_flg = SEM_UNDO;
    return semop (semid, operations, 1);
    }
    /* Post to a binary semaphore: increment its value by 1.
    This returns immediately. */
    int binary_semaphore_post (int semid)
    {
    struct sembuf operations[1];
    /* Use the first (and only) semaphore. */
    operations[0].sem_num = 0;
    /* Increment by 1. */
    operations[0].sem_op = 1;
    /* Permit undoing. */
    operations[0].sem_flg = SEM_UNDO;
    return semop (semid, operations, 1);
    }
Sign In or Register to comment.