Академический Документы
Профессиональный Документы
Культура Документы
Apurba Sarkar
IIEST Shibpur
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 1 / 16
Outline
1 Semaphore
Motivation
Few important structures
System calls needed
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 2 / 16
Semaphore
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 3 / 16
Semaphore
Uses of Semaphores
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 4 / 16
Semaphore Motivation
Things to be discussed
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 5 / 16
Semaphore Few important structures
Structures to be used
semid ds
struct semid_ds {
. . .
struct sem *sem_base; /*pointer to first semaphore in set*/
ushort_t sem_nsems; /* number of semaphores in set */
};
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 6 / 16
Semaphore Few important structures
structures to be used
Structure sem
struct sem {
ushort semval; /*semaphore value*/
pid_t sempid; /*pid of last operation */
ushort semcnt; /* # waiting for increase in semval */
ushort semzcnt; /* # waiting for semval == 0 */
};
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 7 / 16
Semaphore System calls needed
Function of semget
To create a semaphore, or gain access to one that exists.
to include <sys/types.h> <sys/ipc.h> <sys/sem.h>
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 8 / 16
Semaphore System calls needed
semget notes
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 9 / 16
Semaphore System calls needed
semget
struct semid ds
semval [0]
semid
sem perm sempid [0]
structure semcnt [0]
semzcnt [0]
kernel
sem base semval [1]
sem nsems sempid [1]
sem otime semcnt [1]
sem ctime semzcnt [1]
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 10 / 16
Semaphore System calls needed
System Call: int semop (int semid, struct sembuf *sops, size t
nsops);
Return values:
On Success: 0
On failure: −1 and Sets errno
Arguments
int semid: semaphore identifier.
struct sembuf *sops: a reference to the address of an array
of semaphore operations that will be performed on the semaphore
set denoted by the semid value.
size t nsops: the number of elements in the array of
semaphore operations.
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 11 / 16
Semaphore System calls needed
struct sembuf {
ushort sem_num; /* semaphore # */
short sem_op; /*semaphore operation*/
short sem_flg; /*operation flags*/
};
ushort sem num: semaphore number (the index into the array of sem structures
referenced by the sem base) .
short sem op: operation to be performed on the semaphore.
short sem flg:
IPC NOWAIT: if the semaphore operation can not be performed,
the call will return immediately.
SEM UNDO: allows an operation to be undone if a blocked operation
subsequently fails.
0: process blocks until semaphore available S
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 12 / 16
Semaphore System calls needed
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 13 / 16
Semaphore System calls needed
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 15 / 16
Semaphore System calls needed
Apurba Sarkar (IIEST Shibpur) Semaphores and Related System Calls February 18, 2019 16 / 16