2. Creating Semaphore – semget()
int semget(key_t key, int nsems, int semflg);
• The semget() system call returns the System V semaphore set identifier
associated with the argument key. It may be used either to obtain the identifier
of a previously created semaphore set (when semflg is zero and key does not
have the value IPC_PRIVATE), or to create a new set.
• A new set of nsems semaphores is created if key has the value IPC_PRIVATE or if
no existing semaphore set is associated with key and IPC_CREAT is specified in
semflg.
• RETURN VALUE
• If successful, the return value will be the semaphore set identifier (a nonnegative integer),
otherwise, -1 is returned, with errno indicating the error.
4. Deleting Semaphore – semctl()
int semctl(int semid, int semnum, int cmd, ...);
• semctl() performs the control operation specified by cmd on the System V
semaphore set identified by semid, or on the semnum-th semaphore of that set.
(The semaphores in a set are numbered starting at 0.)
• Value for cmd to delete semaphore :
• IPC_RMID - Immediately remove the semaphore set, awakening all processes
blocked in semop(2) calls on the set (with an error return and errno set to
EIDRM).
6. Getting and Setting Semaphore Values– semctl()
int semctl(int semid, int semnum, int cmd, ...);
• Value for cmd – Getting the Semaphore Value
• GETVAL : Return the value of semval for the semnum-th semaphore of the set. The calling
process must have read permission on the semaphore set.
• Value for cmd – Setting the Semaphore Value
• SETVAL : Set the value of semval to arg.val for the semnum-th semaphore of the set, updating
also the sem_ctime member of the semid_ds structure associated with the set. Undo entries are
cleared for altered semaphores in all processes. If the changes to semaphore values would permit
blocked semop(2) calls in other processes to proceed, then those processes are woken up. The
calling process must have alter permission on the semaphore set.
8. #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include<stdio.h>
void main()
{
int setval,retval;
setval=99;
semctl(2, 0, SETVAL, setval);
printf("Value of Semaphore is addedn");
retval= semctl(2, 0, GETVAL, 0);
printf("Value returned is %dn", retval);
}
Example – Setting Value
9. Getting and Setting Multiple Semaphore Values– semctl()
int semctl(int semid, int semnum, int cmd, ...);
• Value for cmd – Getting the multiple Semaphore Value
• GETALL : Return semval (i.e., the current value) for all semaphores of the set into arg.array. The
argument semnum is ignored. The calling process must have read permission on the semaphore
set.
• Value for cmd – Setting the multiple Semaphore Value
• SETALL : Set semval for all semaphores of the set using arg.array, updating also the sem_ctime
member of the semid_ds structure associated with the set. Undo entries (see semop(2)) are
cleared for altered semaphores in all processes. If the changes to semaphore values would permit
blocked semop(2) calls in other processes to proceed, then those processes are woken up. The
argument semnum is ignored. The calling process must have alter (write) permission on the
semaphore set.
10. #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include<stdio.h>
void main()
{
int semid,key,nsem,setvalmult[3]={1,3,5},
retvalmult[3]={0,0,0},i;
key = (key_t)0x25;
nsem = 3;
semid= semget(key,nsem,0666|IPC_CREAT);
for(i=0;i<nsem;i++)
printf("Semaphore %d is %dn",i,
setvalmult[i]);
semctl(semid, 0, SETALL, setvalmult);
printf("Value of 5 Semaphore is addedn");
semctl(semid, 0, GETALL, retvalmult);
for(i=0;i<nsem;i++)
printf("Value returned for Semaphore
%d is %dn",i, retvalmult[i]);
}
Example – Getting & Setting Multiple Values
11. Getting and Setting Semaphore Values– semop()
(Ensuring Atomicity)
int semop(int semid, struct sembuf *sops, size_t nsops);
• semop() performs operations on selected semaphores in the set indicated by semid. Each of
the nsops elements in the array pointed to by sops is a structure that specifies an operation to
be performed on a single semaphore. The elements of this structure are of type struct sembuf,
containing the following members:
• unsigned short sem_num; /* semaphore number */
• short sem_op; /* semaphore operation */
• short sem_flg; /* operation flags */
• Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO. If an operation specifies
SEM_UNDO, it will be automatically undone when the process terminates.
• If sem_op is a positive integer, the operation adds this value to the semaphore value (sem‐val).
• If sem_op is zero, the process must have read permission on the semaphore set.
• If sem_op is less than zero, the process must have alter permission on the semaphore set.
12. #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include<stdio.h>
void main()
{
int semid,retval;
struct sembuf sop;
sop.sem_num=0;
sop.sem_op=0;
sop.sem_flg=0;
semid= semget(0x30,1,IPC_CREAT|0666);
printf("nBefore semop()n");
retval= semop(semid, &sop, 1);
printf("Value returned by semop is %dn", retval);
}
Example – Getting Value