MentOS  0.8.0
The Mentoring Operating System
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Classes | Macros | Functions | Variables
sem.c File Reference

Classes

struct  sem_info_t
 Semaphore management structure. More...
 

Macros

#define __DEBUG_HEADER__   "[IPCsem]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 

Functions

static sem_info_t__sem_info_alloc (key_t key, int nsems, int semflg)
 Allocates the memory for semaphore management structure. More...
 
static void __sem_info_dealloc (sem_info_t *sem_info)
 Frees the memory of a semaphore management structure. More...
 
static sem_info_t__list_find_sem_info_by_id (int semid)
 Searches for the semaphore with the given id. More...
 
static sem_info_t__list_find_sem_info_by_key (key_t key)
 Searches for the semaphore with the given key. More...
 
static void __list_add_sem_info (sem_info_t *sem_info)
 Adds the structure to the global list. More...
 
static void __list_remove_sem_info (sem_info_t *sem_info)
 Removes the structure from the global list. More...
 
int sem_init (void)
 Iinitializes the semaphore system. More...
 
long sys_semget (key_t key, int nsems, int semflg)
 
long sys_semop (int semid, struct sembuf *sops, unsigned nsops)
 
long sys_semctl (int semid, int semnum, int cmd, union semun *arg)
 
ssize_t procipc_sem_read (vfs_file_t *file, char *buf, off_t offset, size_t nbyte)
 Read function for the proc system. More...
 

Variables

int __sem_id = 0
 A value to compute the semid value.
 
list_head semaphores_list
 List of all current active semaphores.
 

Detailed Description

03/04/2023

At the moment we have various functions with their description (see comments). The first time that the function semget is called, we are going to generate the list and the first semaphore set with the assumption that we are not given a IPC_PRIVATE key (temporary ofc). We are able to create new semaphores set and generate unique keys (IPC_PRIVATE) with a counter that searches for the first possible key to assign. Temporary idea:

11/04/2023

Right now we have a first version of working semaphores in MentOS. We have completed the semctl function and we have implemented the first version of the semop function both user and kernel side. The way it works is pretty straightforward, the user tries to perform an operation and based on the value of the semaphore the kernel returns certain values. If the operation cannot be performed then the user will stay in a while loop. The cycle ends with a positive return value (the operation has been taken care of) or in case of errors. For testing purposes -> you can try the t_semget and the t_sem1 tests. They both use semaphores and blocking / non blocking operations. t_sem1 is also an exercise that was assingned by Professor Drago in the OS course.

Function Documentation

◆ __list_add_sem_info()

static void __list_add_sem_info ( sem_info_t sem_info)
inlinestatic

Adds the structure to the global list.

Parameters
sem_infothe structure to add.

◆ __list_find_sem_info_by_id()

static sem_info_t* __list_find_sem_info_by_id ( int  semid)
inlinestatic

Searches for the semaphore with the given id.

Parameters
semidthe id we are searching.
Returns
the semaphore with the given id.

◆ __list_find_sem_info_by_key()

static sem_info_t* __list_find_sem_info_by_key ( key_t  key)
inlinestatic

Searches for the semaphore with the given key.

Parameters
keythe key we are searching.
Returns
the semaphore with the given key.

◆ __list_remove_sem_info()

static void __list_remove_sem_info ( sem_info_t sem_info)
inlinestatic

Removes the structure from the global list.

Parameters
sem_infothe structure to remove.

◆ __sem_info_alloc()

static sem_info_t* __sem_info_alloc ( key_t  key,
int  nsems,
int  semflg 
)
inlinestatic

Allocates the memory for semaphore management structure.

Parameters
keyIPC_KEY associated with the set of semaphores.
nsemsnumber of semaphores to initialize.
semflgflags used to create the set of semaphores.
Returns
a pointer to the allocated semaphore management structure.

◆ __sem_info_dealloc()

static void __sem_info_dealloc ( sem_info_t sem_info)
inlinestatic

Frees the memory of a semaphore management structure.

Parameters
sem_infopointer to the semaphore management structure.

◆ procipc_sem_read()

ssize_t procipc_sem_read ( vfs_file_t file,
char *  buf,
off_t  offset,
size_t  nbyte 
)

Read function for the proc system.

Parameters
fileThe file.
bufBuffer where the read content must be placed.
offsetOffset from which we start reading from the file.
nbyteThe number of bytes to read.
Returns
The number of red bytes.

◆ sem_init()

int sem_init ( void  )

Iinitializes the semaphore system.

Returns
0 on success, 1 on failure.