SlideShare a Scribd company logo
Shared Memory 
& 
Semaphore 
By 
Venkata Durga Prasad. B 
Email: venkata.prasad@vvdntech.in
Shared Memory 
 It allows two unrelated processes to access the same logical 
memory. 
 Shared memory is a special range of addresses that is created 
by IPC for one process and appears in the address space of 
that process. 
 Other processes can then “attach” the same shared memory 
segment into their own address space.
Creation: 
 shmget() is used to obtain access to a shared memory 
segment. 
 int shmget(key_t key, size_t size, int shmflg); 
 The key argument is a access value associated with the 
semaphore ID. 
 The size argument is the size in bytes of the requested shared 
memory. 
 The shmflg argument specifies the initial access permissions 
and creation control flags. 
 The flag uses IPC_CREAT | 0666 to set permission.
Creation:(Cont'd..) 
 On Success it returns the shared memory segment ID. On 
failure, it returns -1 
 This call is also used to get the ID of an existing shared 
segment.
Attachment: 
 To enable access to the shared memory, you must attach it to 
the address space of a process. 
 void *shmat(int shm_id, const void *shm_addr, int shmflg); 
 shm_id is the shared memory ID returned by shmget(). 
 The second parameter, shm_addr, is the address at which the 
shared memory is to be attached to the current process. 
 The third parameter, shmflg, is a bitwise flag. 
 SHM_RDONLY, which makes the attached memory read-only.
Attachment:(cont'd...) 
 On success shmat() returns a pointer to the first byte of shared 
memory. 
 On failure, it returns -1.
Detachment: 
 The shmdt function detaches the shared memory from the 
current process. It takes a pointer to the address returned by 
shmat. 
 Int shmdt(shm_ptr); 
 On success shmdt() returns 0, 
 On error -1 is returned.
Controlling: 
 shmctl() performs the control operation specified by cmd on 
the shared memory segment whose identifier is given in shmid. 
 int shmctl(int shmid, int cmd, struct shmid_ds *buf); 
 The first parameter, shm_id, is the identifier returned from 
shmget. 
 The second parameter, command, is the action to take. It can 
take three values.
Controlling:(cont'd...) 
 IPC_STAT Sets the data in the shmid_ds structure to reflect 
the values associated with the shared memory. 
 IPC_SET Sets the values associated with the shared memory 
to those provided in the shmid_ds data structure, if the process 
has permission to do so. 
 IPC_RMID Deletes the shared memory segment. 
 The third parameter, buf, is a pointer to the structure containing 
the modes and permissions for the shared memory. 
 On success, it returns 0, on failure, –1.
NOTE: 
 If you did not remove your shared memory segments they 
will be in the system forever. This will degrade the system 
performance. 
 Use the ipcs command to check if you have shared 
memory segments left in the system. 
 Use the ipcrm command to remove your shared memory 
segments.
Semaphore 
 A semaphore is a special type of variable that can be 
incremented or decremented, 
 But crucial access to the variable is guaranteed to be atomic, 
even in a multi- threaded program.
Types of semaphore: 
There are two types of semaphores. They are 
 Unnamed semaphore 
 Named semaphore
1.unnamed semaphore: 
 The semaphore is placed in a region of memory that is shared 
between multiple threads. 
 This semaphore also termed as binary semaphore. 
 a binary semaphore that takes only values 0 or 1. 
 Binary semaphores are used to protect a piece of code so that 
only one thread of execution can run it at any one time.
Initialization: 
 sem_init() initializes the unnamed semaphore at the address 
pointed to by sem. The value argument specifies the initial 
value for the semaphore. 
 int sem_init(sem_t *sem, int pshared, unsigned int value); 
 The pshared parameter controls the type of semaphore. 
 If the value of pshared is 0, the semaphore is local to the 
current process. 
 Otherwise, the semaphore may be shared between 
processes.
Controlling: 
 int sem_wait(sem_t *sem); 
 sem_wait() decrements (locks) the semaphore pointed to by 
sem. 
 If the semaphore's value is greater than zero, then the 
decrement proceeds, and the function returns, immediately. 
 If the semaphore currently has the value zero, then the call 
blocks until either it becomes possible to perform the 
decrement. 
 On success it returns 0, and on failure -1.
Controlling:(cont'd...) 
 int sem_post(sem_t * sem); 
 sem_post() increments (unlocks) the semaphore pointed to 
by sem. 
 If the semaphore's value consequently becomes greater than 
zero, 
 Then another process or thread blocked in a sem_wait call will 
be woken up and proceed to lock the semaphore. 
 On success it returns 0, and on failure -1.
Destroy( ): 
 int sem_destroy(sem_t *sem); 
 sem_destroy() destroys the unnamed semaphore at the 
address pointed to by sem. 
 Only a semaphore that has been initialized by sem_init should 
be destroyed using sem_destroy(). 
 Destroying a semaphore that other processes or threads are 
currently blocked on (in sem_wait) produces undefined 
behavior. 
 sem_destroy() returns 0 on success, on error, -1 is returned.
2.Named semaphore: 
 A named semaphore is identified by a name of the form 
/some-name, consisting of an initial slash, followed by one or 
more characters, none of which are slashes. 
 Two processes can operate on the same named semaphore by 
passing the same name to sem_open.
Creation: 
 sem_t *sem_open(const char *name, int oflag, mode_t mode, 
unsigned int value); 
 sem_open() creates a new POSIX semaphore or opens an 
existing semaphore. 
 The semaphore is identified by name. 
 The oflag argument specifies flags that control the operation of 
the call. 
 If O_CREAT is specified in oflag, then the semaphore is 
created if it does not already exist.
Creation:(cont'd...) 
 The mode argument specifies the permissions to be placed on 
the new semaphore. 
 The value argument specifies the initial value for the new 
semaphore. 
 If O_CREAT is specified, and a semaphore with the given 
name already exists, then mode and value are ignored. 
 On success, sem_open() returns the address of the new 
semaphore. 
 On error, sem_open() returns SEM_FAILED.
Destroy: 
 int sem_unlink(const char *name); 
 sem_unlink() removes the named semaphore referred to 
by name. 
 On success sem_unlink() returns 0. 
 On error, -1 is returned.
NOTE: 
 Programs using the POSIX semaphores API must be compiled 
with cc -lrt to link against the real-time library. 
 Otherwise compile with cc -pthread.
Any 
queries? 
???

More Related Content

What's hot

Case Study 1: Linux
Case Study 1: LinuxCase Study 1: Linux
Case Study 1: Linux
Munazza-Mah-Jabeen
 
CACHE MEMORY
CACHE MEMORYCACHE MEMORY
CACHE MEMORY
VENNILAV6
 
Combined paging and segmentation
Combined paging and segmentationCombined paging and segmentation
Combined paging and segmentationTech_MX
 
Message passing in Distributed Computing Systems
Message passing in Distributed Computing SystemsMessage passing in Distributed Computing Systems
Message passing in Distributed Computing Systems
Alagappa Govt Arts College, Karaikudi
 
OS Unit 3 - Interprocess Communication
OS Unit 3 - Interprocess CommunicationOS Unit 3 - Interprocess Communication
OS Unit 3 - Interprocess Communication
Gyanmanjari Institute Of Technology
 
SCSI(small computer system interface)
SCSI(small computer system interface)SCSI(small computer system interface)
SCSI(small computer system interface)
Niraj Lamichhane
 
Base-64 Presentation
Base-64 PresentationBase-64 Presentation
Base-64 Presentation
Ganesh Bagaria
 
Memory management
Memory managementMemory management
Memory management
Muhammad Fayyaz
 
CCNA 2 Routing and Switching v5.0 Chapter 8
CCNA 2 Routing and Switching v5.0 Chapter 8CCNA 2 Routing and Switching v5.0 Chapter 8
CCNA 2 Routing and Switching v5.0 Chapter 8
Nil Menon
 
Chord Algorithm
Chord AlgorithmChord Algorithm
Chord Algorithm
Sijia Lyu
 
Hash function
Hash function Hash function
Hash function
Salman Memon
 
Ipsec
IpsecIpsec
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
AES by example
AES by exampleAES by example
AES by example
Shiraz316
 
OS Unit 5 - Memory Management
OS Unit 5 - Memory ManagementOS Unit 5 - Memory Management
OS Unit 5 - Memory Management
Gyanmanjari Institute Of Technology
 
Distributed system
Distributed systemDistributed system
Distributed system
Syed Zaid Irshad
 
The basic concept of Linux FIleSystem
The basic concept of Linux FIleSystemThe basic concept of Linux FIleSystem
The basic concept of Linux FIleSystem
HungWei Chiu
 

What's hot (20)

Case Study 1: Linux
Case Study 1: LinuxCase Study 1: Linux
Case Study 1: Linux
 
CACHE MEMORY
CACHE MEMORYCACHE MEMORY
CACHE MEMORY
 
Combined paging and segmentation
Combined paging and segmentationCombined paging and segmentation
Combined paging and segmentation
 
Message passing in Distributed Computing Systems
Message passing in Distributed Computing SystemsMessage passing in Distributed Computing Systems
Message passing in Distributed Computing Systems
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
OS Unit 3 - Interprocess Communication
OS Unit 3 - Interprocess CommunicationOS Unit 3 - Interprocess Communication
OS Unit 3 - Interprocess Communication
 
SCSI(small computer system interface)
SCSI(small computer system interface)SCSI(small computer system interface)
SCSI(small computer system interface)
 
Addressing
AddressingAddressing
Addressing
 
Base-64 Presentation
Base-64 PresentationBase-64 Presentation
Base-64 Presentation
 
Memory management
Memory managementMemory management
Memory management
 
CCNA 2 Routing and Switching v5.0 Chapter 8
CCNA 2 Routing and Switching v5.0 Chapter 8CCNA 2 Routing and Switching v5.0 Chapter 8
CCNA 2 Routing and Switching v5.0 Chapter 8
 
Chord Algorithm
Chord AlgorithmChord Algorithm
Chord Algorithm
 
Hash function
Hash function Hash function
Hash function
 
13. multiprocessing
13. multiprocessing13. multiprocessing
13. multiprocessing
 
Ipsec
IpsecIpsec
Ipsec
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
AES by example
AES by exampleAES by example
AES by example
 
OS Unit 5 - Memory Management
OS Unit 5 - Memory ManagementOS Unit 5 - Memory Management
OS Unit 5 - Memory Management
 
Distributed system
Distributed systemDistributed system
Distributed system
 
The basic concept of Linux FIleSystem
The basic concept of Linux FIleSystemThe basic concept of Linux FIleSystem
The basic concept of Linux FIleSystem
 

Similar to Shared memory and semaphore? And how to use them? An explanation about those system calls.

Process management
Process managementProcess management
Process management
Utkarsh Kulshrestha
 
Semaphore in Java with Example.pdf
Semaphore in Java with Example.pdfSemaphore in Java with Example.pdf
Semaphore in Java with Example.pdf
SudhanshiBakre1
 
Safe Clearing of Private Data
Safe Clearing of Private DataSafe Clearing of Private Data
Safe Clearing of Private Data
PVS-Studio
 
Introduction to OpenMP
Introduction to OpenMPIntroduction to OpenMP
Introduction to OpenMP
Akhila Prabhakaran
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPT
Sowmya Jyothi
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationMohd Tousif
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
ssuser1c8ca21
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorialhughpearse
 
memory
memorymemory
memory
teach4uin
 
Different type of shells In Netapp Cluster mode 8.X and how to access them t...
Different type of shells In Netapp Cluster mode 8.X  and how to access them t...Different type of shells In Netapp Cluster mode 8.X  and how to access them t...
Different type of shells In Netapp Cluster mode 8.X and how to access them t...
Saroj Sahu
 
OverviewAssignment 1 Hypothetical Machine SimulatorCSci 43.docx
OverviewAssignment 1  Hypothetical Machine SimulatorCSci 43.docxOverviewAssignment 1  Hypothetical Machine SimulatorCSci 43.docx
OverviewAssignment 1 Hypothetical Machine SimulatorCSci 43.docx
honey690131
 
Programming using Open Mp
Programming using Open MpProgramming using Open Mp
Programming using Open MpAnshul Sharma
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
Neeraj Kaushik
 
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
wkyra78
 
OGRE v2.1 manual - Technical Overview
OGRE v2.1 manual - Technical OverviewOGRE v2.1 manual - Technical Overview
OGRE v2.1 manual - Technical Overview
River Wang
 
P3.docx
P3.docxP3.docx
P3.docx
IsaacMwangi25
 
Valgrind debugger Tutorial
Valgrind debugger TutorialValgrind debugger Tutorial
Valgrind debugger Tutorial
Anurag Tomar
 
unix interprocess communication
unix interprocess communicationunix interprocess communication
unix interprocess communicationguest4c9430
 
ch 7 POSIX.pptx
ch 7 POSIX.pptxch 7 POSIX.pptx
ch 7 POSIX.pptx
sibokac
 

Similar to Shared memory and semaphore? And how to use them? An explanation about those system calls. (20)

Lab8
Lab8Lab8
Lab8
 
Process management
Process managementProcess management
Process management
 
Semaphore in Java with Example.pdf
Semaphore in Java with Example.pdfSemaphore in Java with Example.pdf
Semaphore in Java with Example.pdf
 
Safe Clearing of Private Data
Safe Clearing of Private DataSafe Clearing of Private Data
Safe Clearing of Private Data
 
Introduction to OpenMP
Introduction to OpenMPIntroduction to OpenMP
Introduction to OpenMP
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPT
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorial
 
memory
memorymemory
memory
 
Different type of shells In Netapp Cluster mode 8.X and how to access them t...
Different type of shells In Netapp Cluster mode 8.X  and how to access them t...Different type of shells In Netapp Cluster mode 8.X  and how to access them t...
Different type of shells In Netapp Cluster mode 8.X and how to access them t...
 
OverviewAssignment 1 Hypothetical Machine SimulatorCSci 43.docx
OverviewAssignment 1  Hypothetical Machine SimulatorCSci 43.docxOverviewAssignment 1  Hypothetical Machine SimulatorCSci 43.docx
OverviewAssignment 1 Hypothetical Machine SimulatorCSci 43.docx
 
Programming using Open Mp
Programming using Open MpProgramming using Open Mp
Programming using Open Mp
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
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
 
OGRE v2.1 manual - Technical Overview
OGRE v2.1 manual - Technical OverviewOGRE v2.1 manual - Technical Overview
OGRE v2.1 manual - Technical Overview
 
P3.docx
P3.docxP3.docx
P3.docx
 
Valgrind debugger Tutorial
Valgrind debugger TutorialValgrind debugger Tutorial
Valgrind debugger Tutorial
 
unix interprocess communication
unix interprocess communicationunix interprocess communication
unix interprocess communication
 
ch 7 POSIX.pptx
ch 7 POSIX.pptxch 7 POSIX.pptx
ch 7 POSIX.pptx
 

Recently uploaded

Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
JezreelCabil2
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 

Shared memory and semaphore? And how to use them? An explanation about those system calls.

  • 1. Shared Memory & Semaphore By Venkata Durga Prasad. B Email: venkata.prasad@vvdntech.in
  • 2. Shared Memory  It allows two unrelated processes to access the same logical memory.  Shared memory is a special range of addresses that is created by IPC for one process and appears in the address space of that process.  Other processes can then “attach” the same shared memory segment into their own address space.
  • 3. Creation:  shmget() is used to obtain access to a shared memory segment.  int shmget(key_t key, size_t size, int shmflg);  The key argument is a access value associated with the semaphore ID.  The size argument is the size in bytes of the requested shared memory.  The shmflg argument specifies the initial access permissions and creation control flags.  The flag uses IPC_CREAT | 0666 to set permission.
  • 4. Creation:(Cont'd..)  On Success it returns the shared memory segment ID. On failure, it returns -1  This call is also used to get the ID of an existing shared segment.
  • 5. Attachment:  To enable access to the shared memory, you must attach it to the address space of a process.  void *shmat(int shm_id, const void *shm_addr, int shmflg);  shm_id is the shared memory ID returned by shmget().  The second parameter, shm_addr, is the address at which the shared memory is to be attached to the current process.  The third parameter, shmflg, is a bitwise flag.  SHM_RDONLY, which makes the attached memory read-only.
  • 6. Attachment:(cont'd...)  On success shmat() returns a pointer to the first byte of shared memory.  On failure, it returns -1.
  • 7. Detachment:  The shmdt function detaches the shared memory from the current process. It takes a pointer to the address returned by shmat.  Int shmdt(shm_ptr);  On success shmdt() returns 0,  On error -1 is returned.
  • 8. Controlling:  shmctl() performs the control operation specified by cmd on the shared memory segment whose identifier is given in shmid.  int shmctl(int shmid, int cmd, struct shmid_ds *buf);  The first parameter, shm_id, is the identifier returned from shmget.  The second parameter, command, is the action to take. It can take three values.
  • 9. Controlling:(cont'd...)  IPC_STAT Sets the data in the shmid_ds structure to reflect the values associated with the shared memory.  IPC_SET Sets the values associated with the shared memory to those provided in the shmid_ds data structure, if the process has permission to do so.  IPC_RMID Deletes the shared memory segment.  The third parameter, buf, is a pointer to the structure containing the modes and permissions for the shared memory.  On success, it returns 0, on failure, –1.
  • 10. NOTE:  If you did not remove your shared memory segments they will be in the system forever. This will degrade the system performance.  Use the ipcs command to check if you have shared memory segments left in the system.  Use the ipcrm command to remove your shared memory segments.
  • 11. Semaphore  A semaphore is a special type of variable that can be incremented or decremented,  But crucial access to the variable is guaranteed to be atomic, even in a multi- threaded program.
  • 12. Types of semaphore: There are two types of semaphores. They are  Unnamed semaphore  Named semaphore
  • 13. 1.unnamed semaphore:  The semaphore is placed in a region of memory that is shared between multiple threads.  This semaphore also termed as binary semaphore.  a binary semaphore that takes only values 0 or 1.  Binary semaphores are used to protect a piece of code so that only one thread of execution can run it at any one time.
  • 14. Initialization:  sem_init() initializes the unnamed semaphore at the address pointed to by sem. The value argument specifies the initial value for the semaphore.  int sem_init(sem_t *sem, int pshared, unsigned int value);  The pshared parameter controls the type of semaphore.  If the value of pshared is 0, the semaphore is local to the current process.  Otherwise, the semaphore may be shared between processes.
  • 15. Controlling:  int sem_wait(sem_t *sem);  sem_wait() decrements (locks) the semaphore pointed to by sem.  If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately.  If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement.  On success it returns 0, and on failure -1.
  • 16. Controlling:(cont'd...)  int sem_post(sem_t * sem);  sem_post() increments (unlocks) the semaphore pointed to by sem.  If the semaphore's value consequently becomes greater than zero,  Then another process or thread blocked in a sem_wait call will be woken up and proceed to lock the semaphore.  On success it returns 0, and on failure -1.
  • 17. Destroy( ):  int sem_destroy(sem_t *sem);  sem_destroy() destroys the unnamed semaphore at the address pointed to by sem.  Only a semaphore that has been initialized by sem_init should be destroyed using sem_destroy().  Destroying a semaphore that other processes or threads are currently blocked on (in sem_wait) produces undefined behavior.  sem_destroy() returns 0 on success, on error, -1 is returned.
  • 18. 2.Named semaphore:  A named semaphore is identified by a name of the form /some-name, consisting of an initial slash, followed by one or more characters, none of which are slashes.  Two processes can operate on the same named semaphore by passing the same name to sem_open.
  • 19. Creation:  sem_t *sem_open(const char *name, int oflag, mode_t mode, unsigned int value);  sem_open() creates a new POSIX semaphore or opens an existing semaphore.  The semaphore is identified by name.  The oflag argument specifies flags that control the operation of the call.  If O_CREAT is specified in oflag, then the semaphore is created if it does not already exist.
  • 20. Creation:(cont'd...)  The mode argument specifies the permissions to be placed on the new semaphore.  The value argument specifies the initial value for the new semaphore.  If O_CREAT is specified, and a semaphore with the given name already exists, then mode and value are ignored.  On success, sem_open() returns the address of the new semaphore.  On error, sem_open() returns SEM_FAILED.
  • 21. Destroy:  int sem_unlink(const char *name);  sem_unlink() removes the named semaphore referred to by name.  On success sem_unlink() returns 0.  On error, -1 is returned.
  • 22. NOTE:  Programs using the POSIX semaphores API must be compiled with cc -lrt to link against the real-time library.  Otherwise compile with cc -pthread.