SlideShare a Scribd company logo
1 of 12
Semaphore
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.
#include <sys/types.h>
#include <sys/ipc.h>
#include<stdio.h>
void main()
{
int semid,key,nsem;
key = (key_t)0x21;
nsem = 1;
semid=
semget(key,nsem,IPC_CREAT|0666);
printf("Created Semaphore with
ID:%dn",semid);
}
Example
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).
#include <sys/types.h>
#include <sys/ipc.h>
#include<stdio.h>
void main()
{
int semid;
semid= 1;
semctl(semid,0,IPC_RMID,0);
printf("Semaphore with ID:%dn
deleted.",semid);
}
Example
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.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include<stdio.h>
void main()
{
int retval;
retval= semctl(2, 0, GETVAL, 0);
printf("Value returned is %dn", retval);
}
Example – Getting Value
#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
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.
#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
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.
#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

More Related Content

Similar to Semaphore.pptx

Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxSoumen Santra
 
The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210Mahmoud Samir Fayed
 
Alexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for DevelopersAlexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for DevelopersDevDay Dresden
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docxwkyra78
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionSveta Smirnova
 
Assignment no39
Assignment no39Assignment no39
Assignment no39Jay Patel
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
Instruction1. Please read the two articles. (Kincheloe part 1 &.docxInstruction1. Please read the two articles. (Kincheloe part 1 &.docx
Instruction1. Please read the two articles. (Kincheloe part 1 &.docxcarliotwaycave
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.pptMahyuddin8
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfarri2009av
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxjacksnathalie
 
Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6scuhurricane
 
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QACreating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QAarchwisp
 
fog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloudfog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the CloudWesley Beary
 

Similar to Semaphore.pptx (20)

What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with Linux
 
The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210
 
Alexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for DevelopersAlexander Reelsen - Seccomp for Developers
Alexander Reelsen - Seccomp for Developers
 
Systemcall
SystemcallSystemcall
Systemcall
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docx
 
Array Cont
Array ContArray Cont
Array Cont
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in action
 
Assignment no39
Assignment no39Assignment no39
Assignment no39
 
Unit 4
Unit 4Unit 4
Unit 4
 
Function
FunctionFunction
Function
 
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
Instruction1. Please read the two articles. (Kincheloe part 1 &.docxInstruction1. Please read the two articles. (Kincheloe part 1 &.docx
Instruction1. Please read the two articles. (Kincheloe part 1 &.docx
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
 
bluespec talk
bluespec talkbluespec talk
bluespec talk
 
Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6
 
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QACreating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
 
fog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloudfog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloud
 
Bluespec @waseda
Bluespec @wasedaBluespec @waseda
Bluespec @waseda
 

Recently uploaded

The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryEugene Lysak
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesashishpaul799
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Celine George
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Mark Carrigan
 
Behavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfBehavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfaedhbteg
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptxmanishaJyala2
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPragya - UEM Kolkata Quiz Club
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTechSoup
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/siemaillard
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...Nguyen Thanh Tu Collection
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Celine George
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Celine George
 

Recently uploaded (20)

The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
 
Behavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfBehavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdf
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 

Semaphore.pptx

  • 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.
  • 3. #include <sys/types.h> #include <sys/ipc.h> #include<stdio.h> void main() { int semid,key,nsem; key = (key_t)0x21; nsem = 1; semid= semget(key,nsem,IPC_CREAT|0666); printf("Created Semaphore with ID:%dn",semid); } Example
  • 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).
  • 5. #include <sys/types.h> #include <sys/ipc.h> #include<stdio.h> void main() { int semid; semid= 1; semctl(semid,0,IPC_RMID,0); printf("Semaphore with ID:%dn deleted.",semid); } Example
  • 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.
  • 7. #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include<stdio.h> void main() { int retval; retval= semctl(2, 0, GETVAL, 0); printf("Value returned is %dn", retval); } Example – Getting Value
  • 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