-
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;
}
-
Im back!
Here's one. You'll find many more Here.
-
Off the subject a little, where are you doing this course, Im currently doing an OS course at USQ on Nachos.
-
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