Skip to content

Stage 22

1) Acquiring a semaphore - Semget 2) Releasing a semaphore - Semrelease 3) Locking a semaphore - SemLock 4) Unlocking a semaphore - SemUnlock

A process maintains record of the semaphores acquired by it in its per-process resource table

eXpOS uses the data structure Semaphore Table.

Pasted image 20260201000034.png

Max - 32 semaphores Default - Process Count - 0, Locking PID - 1

Resource Identifier - Indicates whether the entry corresponds to a file or a semaphore.

Pasted image 20260201000245.png

Semget

Set the MODE_FLAG in the Process Table to 17 and switch to kernel stack.

Find the index of a free entry in the Per Process Resource Table. /* This will be our semaphore descriptor */
If no free entry, then return -1.

Resource Identifier field of the per-process resource table entry is set to 1 to indicate that the resource is a semaphore.

Acquire a semaphore by calling the acquire_semaphore() function in the Resource Manager Module.

/* acquire_semaphore() module function acquires a semaphore by making an entry in the Semaphore Table and 
returns the index of the entry. If there are no free semaphores, it returns -1 */

If there are no free semaphores, return -2.

Store the index of the Semaphore table entry in the Per Process Resource Table entry.   /*Attach the semaphore to the process.*/

Switch back to the user stack by resoring the USER SP from the process table.

Set the MODE_FLAG in the process table entry of the parent process to 0.

Return the Per-process Resource Table entry index.   /* Semaphore Descriptor */

Semrelease

Set the MODE_FLAG in the Process Table to 18 and switch to kernel stack.

If Semaphore descriptor is not valid or the entry in the Per Process Resource Table is not valid, return -1. 
/* The descriptor is invalid if not in the range 0 - 7, or if the resource identifier field of the table entry is not 1 */

Invoke the release_semaphore() function in the Resource Manager Module.

Invalidate the Per-Process resource table entry.   /* Set to -1 */ 

Switch back to the user stack by restoring the USER SP from the process table.

Set the MODE_FLAG in the process table entry of the parent process to 0.

Return 0.

Acquire Semaphore

Find the index of a free entry in Semaphore table. If no free entry, return -1.
/* Free entry is indicated by a Process Count of 0. */ 

Set the PROCESS_COUNT to 1 and LOCKING_PID to -1.

Return the Semaphore table index. /* success */

Release Semaphore

If ( semaphore is locked by the current process) /*Check the Locking PID in the Semaphore table*/
    Set the Locking PID to -1. /* Unlock the semaphore before release */
    loop through the process table{ /*wake up processes blocked by the semaphore */
            if (the process state is ( WAIT_SEMAPHORE, SEMTABLEINDEX ) ){
                Set state of process as (READY , _ )
            } 
    }

Decrement the process count of the semaphore in the semaphore table.
/* When the count becomes 0, the semaphore is free. */

SemLock System Call

Set the MODE_FLAG in the Process Table to 19 and switch to kernel stack.

If Semaphore descriptor is not valid or the entry in the Per Process Resource Table is not valid, return -1. 
/* The descriptor is invalid if not in the range 0 - 7, or if the resource identifier field of the table entry is not 1 */

while the semaphore is locked by a process other than the current process do    /* Check the Locking PID field in the Semaphore table */
              Change the state of the current process to (WAIT_SEMAPHORE, Semaphore table index of the locked semaphore).
              Invoke the switch_context() function in the Scheduler Module.
endwhile

/* Reaches here when the semaphore becomes free for locking */

Change the Locking PID to PID of the current process in the Semaphore Table .

Reset the mode flag in the Process Table to 0 and switch back to the user stack.

Return 0.   /* success */

SemUnlock System Call

Set the MODE_FLAG in the Process Table to 20 and switch to kernel stack.

If Semaphore descriptor is not valid or the entry in the Per Process Resource Table is not valid, return -1. 
/* The descriptor is invalid if not in the range 0 - 7, or if the resource identifier field of the table entry is not 1 */

If semaphore is locked. /* Check the Locking PID in the Semaphore table */

              If current process has not locked the semaphore, return -2.   /* The semaphore is locked by some other process.*/

              Set the Locking PID to -1.   /* Unlock the semaphore. */

              Loop through the process table and change the state to (READY, _ ) for all the processes 
          in the state (WAIT_SEMAPHORE, Semaphore table index of the locked semaphore). 

Reset the MODE_FLAG in the Process Table to 0 and switch back to the user stack. 

Return 0.   /* success */

Modification to Fork

Increment Process Count

Modification to Free User Area Page

While releasing the Free User Area Page, release valid semaphores as well