Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

my_sem.h File Reference

Go to the source code of this file.

Defines

#define SEM_PERMISSION   0x000001C0
 This macro represents the default semaphore's premission (applied at creation time). This means that we grant all access rights (Read/Write/Execute) to the process' user. In binary, 0x1C0, is 111 000 000.

#define CREATE_SEMAPHORE_OK   0
 Return value for the function create_semaphore_set(). The function was successful.

#define CREATE_SEMAPHORE_CREATE_ERROR   -1
 Return value for the function create_semaphore_set(). An error occured while creating the set of semaphores.

#define CREATE_SEMAPHORE_SET_VAL_ERROR   -2
 Return value for the function create_semaphore_set(). An error occured while setting the semaphores' values.

#define GET_SEM_OK   0
 Return value for the function get_semaphore_set(). The function was successful.

#define GET_SEM_ALREADY_GET   -1
 Return value for the function get_semaphore_set(). The process already got the set of semaphores.

#define GET_SEM_ERROR   -2
 Return value for the function get_semaphore_set(). An error occured while getting the set of semaphores.

#define SET_SEM_VALUES_OK   0
 Return value for the function set_semaphore_values(). The function was successful.

#define SET_SEM_VALUES_SEM_NOT_CREATED   -1
 Return value for the function set_semaphore_values(). The semaphore set was not created (or get).

#define SET_SEM_VALUES_SET_ERROR   -2
 Return value for the function set_semaphore_values(). An error occured while setting the semaphores' values.


Functions

void change_sem_access_permission (int new_permission)
 Set default access permissions for the set of semaphores.

int create_semaphore_set (key_t ipc_key, int number_of_sem)
 Create a set of semaphores.

int set_semaphore_values (int *values)
 Set semaphores' values (this update the entire set of semaphores).

int set_semaphore_value (int value, int sem_number)
 Set the value of one semaphore (and only one of the set).

int take_sem (int sem_idx)
 Take one semaphore. This action decrements the semaphore's counter by 1.

int release_sem (int sem_idx)
 Release one semaphore. This action increments the semaphore's counter by 1.

int destroy_semaphore_set ()
 Destroy the set of semaphores.

int get_semaphore_set ()


Detailed Description

Header file for sem.c.

Definition in file my_sem.h.


Function Documentation

void change_sem_access_permission int    new_permission
 

Set default access permissions for the set of semaphores.

Parameters:
new_permission New permissions.
Warning:
By default, the set of semaphores is created with the persmission mask 0x000001C0, that is, in binary: 111 000 000. This grants all right to the process' user but nothing for other users (just like file permissions).

Definition at line 23 of file sem.c.

References permission.

int create_semaphore_set key_t    ipc_key,
int    number_of_sem
 

Create a set of semaphores.

Parameters:
ipc_key IPC key for the semaphore set. If you have a well known key (it should be a number greater or equal to zero), then you can specify it. Otherwise, if you want the kernel to find the first available key, then you must specify the value (key_t)-1.
number_of_sem Number of semaphores in the set.
Returns:
This function may return one of the followinf values:
  • CREATE_SEMAPHORE_OK: The operation was successful.
  • CREATE_SEMAPHORE_CREATE_ERROR: A system error occured while creating the set of semaphores.
  • CREATE_SEMAPHORE_SET_VAL_ERROR: A system error occured while setting semaphores' values to 1.
Warning:
By default, semaphores are created with the value 1 (like mutexes). You can change that later by using the function change_sem_access_permission().

Definition at line 40 of file sem.c.

References CREATE_SEMAPHORE_CREATE_ERROR, CREATE_SEMAPHORE_OK, CREATE_SEMAPHORE_SET_VAL_ERROR, permission, semaphore_set, and set_size.

int destroy_semaphore_set  
 

Destroy the set of semaphores.

Returns:
Upon successful completion, a positive value is returned. Otherwise, -1 is returned.

Definition at line 215 of file sem.c.

References semaphore_set.

int release_sem int    sem_idx
 

Release one semaphore. This action increments the semaphore's counter by 1.

Parameters:
sem_idx Index of the semaphore to take. The index of the first semaphore is 0.
Returns:
Upon successful completion, 0 is returned. Otherwise, -1 is returned.
Warning:
If the set of semaphores you created contains N semaphores, then "0 <= sem_idx < N". Please note that we do NOT specify the semaphore's flag SEM_UNDO. If you specify this flag, then you will have the following behaviour: when the program exits, the operation will be canceled. Here the operation is "release the semaphore". So, if we set SEM_UNDO, and if the process exists, then the operation will be canceled: in other words, the semaphore will be taken (the oposite of released).

Definition at line 197 of file sem.c.

References semaphore_set.

int set_semaphore_value int    value,
int    sem_number
 

Set the value of one semaphore (and only one of the set).

Parameters:
value Integer that represents the semaphore's new value. Integer that represents the semaphore's index (within the set of semaphores).
Returns:
The function may return one of the followinf values:
  • SET_SEM_VALUES_OK: The operation was sucessful.
  • SET_SEM_VALUES_SEM_NOT_CREATED: You did not create (or get) the set of semaphores.
  • SET_SEM_VALUES_SET_ERROR: An error occured while setting the semaphores' values.
Please note that the functions set_semaphore_value() and set_semaphore_values() share the same set of return values.

Definition at line 149 of file sem.c.

References semaphore_set, SET_SEM_VALUES_OK, SET_SEM_VALUES_SEM_NOT_CREATED, SET_SEM_VALUES_SET_ERROR, and set_size.

int set_semaphore_values int *    values
 

Set semaphores' values (this update the entire set of semaphores).

Parameters:
values Pointer to an array of integers that represents semaphores values.
Returns:
The function may return one of the followinf values:
  • SET_SEM_VALUES_OK: The operation was sucessful.
  • SET_SEM_VALUES_SEM_NOT_CREATED: You did not create (or get) the set of semaphores.
  • SET_SEM_VALUES_SET_ERROR: An error occured while setting the semaphores' values.
Warning:
The number of elements of the array pointed by 'values' must be equal to the number of semaphores in the set.

Definition at line 116 of file sem.c.

References semaphore_set, SET_SEM_VALUES_OK, SET_SEM_VALUES_SEM_NOT_CREATED, SET_SEM_VALUES_SET_ERROR, and set_size.

int take_sem int    sem_idx
 

Take one semaphore. This action decrements the semaphore's counter by 1.

Parameters:
sem_idx Index of the semaphore to take. The index of the first semaphore is 0.
Returns:
Upon successful completion, 0 is returned. Otherwise, -1 is returned.
Warning:
If the set of semaphores you created contains N semaphores, then "0 <= sem_idx < N".

Definition at line 172 of file sem.c.

References semaphore_set.


Generated on Thu Apr 3 16:23:46 2003 for Common_C_libraries by doxygen1.3-rc1