Linux MSGQueues SharedMemory Semaphores
Linux MSGQueues SharedMemory Semaphores
Engineering &Technology
SUBJECT: LinuxProgramming
Subject Code: 19PC1C08
G. Laxmi Deepthi
Assistant Professor
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 1
Agenda
§ Message Queues
§ Kernel support for messages
§ APIs for message queues
§ Client/Server example
§ semaphores
§ Kernel support for semaphores
§ APIs for semaphores
§ file locking with semaphores
§ Shared Memory
§ Kernel support for Shared Memory
§ APIs for Shared Memory
§ Shared Memory example
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 2
Message Queues, Semaphores, Shared Memory
§ Why to use messages?
There are some deficiencies using pipes ( both named and unnamed pipes )
1. Difficulty in multiprocessing
2. If one end of the pipe is closed then the process using another end unable to complete the work
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 3
Message Queues
§ The IPC Methods supported by UNIX System V are:
o Messages
o Semaphores
o Shared memory
o Transport Level Interface
Messages
o It allows multiple processes on the same machine to communicate.
o Each message has an integer message type assigned to it by a sender process.
o So the recipient process can select messages based on type.
o Ex: mail box
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 4
Message Queues
§ Kernel support for Messages
o Message queue table will be maintained in a kernel address space.
o MQT : it keeps track of all message queues created in a system.
o Each entry of the message table stores the following data for one message queue,
• A name (integer ID)
• Creator UID and group ID
• Assigned owner user ID and group ID
• Access permissions
• Time stamp & PID ( last process sent a msg)
• Time stamp & PID ( last process read a msg)
• Pointer to message record
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 5
Message Queues
A Message Queue
Message record
Message
table
Kernel data structure for message queues
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 6
Message Queues
§ System limits on the manipulation of messages
MSGMNI
MSGMAX
MSGMNB
MSGTQL
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 7
Message Queues
§ APIs for Message Queues
<sys/message.h>, the data fields of the structure it stores are:
Msg_perm
Msg_first
Msg_last
Msg_cbyte
Msg_qnum
Msg_qbyte
Msg_lspid
Msg_lrpid
Msg_stime
Msg_rtime
Msg_ctime
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 8
Message Queues
There are four APIs for message manipulation
1. msgget : open and create if needed, a message queue for access
2. Msgsnd: send a message to a message queue
3. Msgrcv: receive a message from message queue
4. Msgctl: manipulate the control data of a message queue.
Msgget
The function prototype of the msgget API is,
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 9
Message Queues
§ msgget
IPC_PRIVATE
IPC_CREAT
Error values
ENOSPC
EEXIST
EACCESS
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 10
Message Queues
§ Msgsnd
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgsnd(int msqfd, const void *msgptr, int len, int flg);
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 11
Message Queues
§ msgrcv
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgrcv(int msqfd, const void *msgptr, int len, int mtype, int flag);
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 12
Message Queues
§ Msgctl
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
Cmd value
IPC_STAT
IPC_SET
IPC_RMID
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 13
Semaphores
§ Semaphores
Semaphores provide a method to synchronize the execution of multiple processes.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 14
Semaphores
§ UNIX Kernel Support for semaphores
o In UNIX system, there is a semaphore table in the kernel address space that keeps track of all semaphore
sets created in the system.
o Each entry of the semaphore table stores the following data for one semaphore set,
• Name
• Creator user ID and group ID
• Assigned owner userID and groupID
• RW access Permissions
• No. of Semaphores in the set
• Time – semaphore values changed by the last process
• Time – control data set values changed by the last process
• pointer
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 15
Semaphores
§ Each semaphore stores the following data:
o Semaphore’s value
o The process ID
o The No. of processes blocked
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 16
Semaphores
§ Kernel data structure for semaphores
Semaphore set
Semaphore
table
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 17
Semaphores
§ System limit
SEMMNI
SEMMNS
SEMMSL
SEMOPM
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 18
Semaphores
§ Semget : get a semaphore
The function prototype is,
#include <sys/sem.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 19
Semaphores
§ Semop :semaphore operations
The function prototype is,
#include <sys/sem.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 20
Semaphores
§ Semctl : control operations
the function prototype is,
#include <sys/sem.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 21
Semaphores
§ File Locking with Semaphores
If a program needs to access a resource, it will have to obtain a lock on the semaphore file.
o Open semaphore for reading
o Lock the semaphore file
o Open data for reading
o Read the data
o Close data file
o Open file for update
o Write updated data to file
o Close data file
o Close semaphore file
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 22
Shared Memory
§ Shared Memory
o it is a technique where two or more process can access the common memory.
o communication is done via this shared memory where changes made by one process can be viewed by
another process.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 23
Shared Memory
§ Kernel support for Shared Memory
o Shared memory table will be maintained in the kernel address space
o It keep tracks of all shared memory regions created in the system.
o Each entry of the table stores the following data for one shared memory region
• A name
• Creator user and group ID
• Assigned owner user and group ID
• Access permission
• The size
• Time
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 24
Shared Memory
§ Kernel data structure for shared memory
Process table
Shared memory table
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 25
Shared Memory
§ There are some limits imposed on the manipulation of shared memory.
§ System limit
SHMMNI
SHMMIN
SHMMAX
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 26
Shared Memory
§ API for shared memory
Shmget
Shmat
Shmdt
shmctl
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 27
Shared Memory
§ Shmget
The function prototype is,
#include<sys/types.h>
#include<sys/ipc.h>
#include <sys/shm.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 28
Shared Memory
§ Shmat
the function prototype is,
#include<sys/types.h>
#include<sys/ipc.h>
#include <sys/shm.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 29
Shared Memory
§ Shmdt
The function prototype is,
#include<sys/types.h>
#include<sys/ipc.h>
#include <sys/shm.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 30
Shared Memory
§ Shmctl
The function prototype is,
#include<sys/types.h>
#include<sys/ipc.h>
#include <sys/shm.h>
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 31
Shared Memory
§ Semaphore and Shared Memory Example
o The shared memory provides a kernel memory region to store any messages sent to the queue.
o The semaphores control which process can access the shared memory at any one time.
o The design of semop calls must ensure that
• Either they are all blocked while the server is actively accessing the shared memory
• Or only one of the client processes is actively accessing the shared memory.
• Two semaphores will be used to meet the above criteria.
• The semaphore values and its usage is,
Semaphore0 Semaphore1 Usage
0 1 Server is waiting for a client to send message
1 0 Client’s message is ready for server to read
1 1 Server’s response data are ready for a client
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 32
Shared Memory
§ The interaction of client processes and the server process to the semaphore set is:
o The server initially creates the semaphore set and a shared memory.
o The server waits for a client to send a request to the shared memory
o When one or more clients attempt to send a message to the shared memory
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 33
Shared Memory
o The client process that succeeds in changing the semaphore set value can now write a service request
command and its PID to the shared memory.
o Once the service is unblocked it will read the client service request from the shared memory.
o The client that is unblocked by the server sets the semaphore set value to 0,0 and reads the server’s
response data.
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 34
Shared Memory
§ Client/Server interaction using semaphores/shared memory
Server is ready for a client to send message
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 35
THANK YOU
Department of Computer Science & Engineering, VNRVJIET, Hyderabad May 18, 2023 36