Semaphor ???

This is a discussion on Semaphor ??? within the Linux Programming forums, part of the Platform Specific Boards category; Okay I'm doing Operating System and I'm suppose to explain this program. I know the basic of semaphor but understood ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    37

    Semaphor ???

    Okay I'm doing Operating System and I'm suppose to explain this program.

    I know the basic of semaphor but understood nothing from this program, anyone know any sites i can read more about semaphors to be able to explain that disgusting thing below ! =)



    /****************
    * *
    * semaphore.c *
    * *
    ****************/


    #include <assert.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>

    #include "semaphore.h"

    static int sem_id;

    static union semun
    {
    int val;
    struct semid_ds *buf;
    ushort array[1];
    } sem_attr;

    static struct sembuf asem[1];
    static key_t s_key;

    int sem_config(int event, int init_val)
    {
    int x;

    s_key = ftok(SEM_NAME);

    if ( -1 == (sem_id = semget(s_key, SEM_MAX, 0666|IPC_CREAT ) ) )
    {
    perror("semget");
    return -1;
    }

    if ( event == NO_EVENT )
    return 0; /*locate semaphore only*/

    sem_attr.val = init_val;
    if ( -1 == semctl(sem_id, event, SETVAL, sem_attr ) )
    {
    perror("semctl SETVAL");
    return -1;
    }

    if ( -1 == ( x = semctl(sem_id, event, GETVAL, sem_attr ) ) )
    {
    perror("semctl GETVAL");
    return -1;
    }
    assert( x == init_val );

    return 0;
    }

    int sem_wait(int event, int nwaits)
    {

    asem[0].sem_num = event;
    asem[0].sem_op = nwaits * S_WAIT;
    asem[0].sem_flg = 0;

    if ( event == NO_EVENT ) /*remove semaphore set*/
    if ( -1 == semctl(sem_id, 0, IPC_RMID, sem_attr ) )
    {
    perror("semctl IPC_RMID");
    return -1;
    }
    else
    return 0;


    if ( -1 == semop(sem_id, asem, 1 ) )
    {
    perror("semop");
    return -1;
    }


    return 0;

    }

    int sem_signal(int event, int nsignals)
    {

    asem[0].sem_num = event;
    asem[0].sem_op = nsignals * S_SIGNAL;
    asem[0].sem_flg = 0;

    if ( event == NO_EVENT )
    if ( -1 == semctl(sem_id, 0, IPC_RMID, sem_attr ) )
    {
    perror("semctl IPC_RMID");
    return -1;
    }
    else
    return 0;


    if ( -1 == semop(sem_id, asem, 1 ) )
    {
    perror("semop");
    return -1;
    }


    return 0;

    }

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Here's one. You'll find many more Here.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Off the subject a little, where are you doing this course, Im currently doing an OS course at USQ on Nachos.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    4
    also OT but i'm also doing an OS course at my uni (NUS)

    anyway, i found the man pages really quite useful (contrary to 'popular' belief )... do a man stuffyoudunget like man semget if there's anything you see but don't get...

    if that fails, try a man -K stuffyoudunget to search thru the man pages...

Popular pages Recent additions subscribe to a feed

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