Please enable JavaScript.
Coggle requires JavaScript to display documents.
Semaphores, Shared Memory, and Message Queues (Shared Memory (shmget (As…
Semaphores, Shared Memory, and Message Queues
Semaphores
To prevent problems caused by more than one program simultaneously accessing a shared resource, you need a way of generating and using a token that grants access to only one thread of execution in a critical section at a time.
Is a special variable on which only two operations are allowed; these operations are officially termed wait and signal.
A binary semaphore is a variable that can take only the values 0 and 1. Semaphores that can take many positive values are called general semaphores.
semget
-
int semget(key_t key, int num_sems, int sem_flags); . The key is an integral value used to allow unrelated processes to access the same semaphore. The num_sems is the number of semaphores required. Returns non-zero value on success and -1 on error.
semop
-
int semop(int sem_id, struct sembuf *sem_ops, size_t num_sem_ops); . SEM_ID is the semaphore identifier. SEM_OPS is a pointer to an array of structures.
semctl
-
int semctl( int sem_id, int sem_num, int command, ..); . SEM_ID is the semaphore identifier. SEM_NUM is the semaphore number. COMMAND is the action to take.
Shared Memory
It allows two unrelated processes to access the same logical memory. It is a very efficient way of transferring data between two running processes.
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. If one process writes to the shared memory, the changes immediately become visible to any other process that has access to that shared memory.
It provides an efficient way of sharing and passing data between multiple processes. Since it does not provide any synchronization facility you need to use some other mechanism to synchronize access to the shared memory.
shmget
As with semaphores, the function provides key, which effectively names the shared memory segment, and the shmget function returns a shared memory identifier.
shmat
To enable access to the shared memory, you must attach it to the address space of a process with shmat
-
shmdt
The shmdt function detaches the shared memory from the current process. It takes a pointer to the address returned by shmat.
Message Queues
Message queues are like named pipes, but without the complexity associated with opening and closing the pipe.
Message queues provide a reasonably easy and efficient way of passing data between two unrelated processes.
Message queues provide a way of sending a block of data from one process to another. Each block of data is considered to have a type, and a receiving process receives blocks of data having different type values independently.
msgget
You create and access a message queue using the msgget function. It returns a positive number, the queue identifier, on success or –1 on error.
msgsnd
The msgsnd function allows you to add a message to a message queue. On success, the function returns 0, on failure –1. If the call is successful, a copy of the message data has been taken and placed on the message queue.
msgrcv
int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg); .
The msgrcv function retrieves messages from a message queue. On success, msgrcv returns the number of bytes placed in the receive buffer. the message is copied into the user-allocated buffer pointed to by msg_ptr.
msgctl
The final message queue function is msgctl, which is very similar to that of the control function for shared memory. 0 is returned on success, –1 on failure. If a message queue is deleted while a process is waiting in a msgsnd or msgrcv function, the send or receive function will fail.
IPC Status Commands
Linux systems provide a set of commands that allow command-line access to IPC information, and to tidy up stray IPC facilities.
One of the stress situations of the IPC facilities is that a poorly written program, or a program that fails for some reason, can leave its IPC resources loitering on the system long after the program completes.
Semaphore Status
To examine the state of semaphores on the system, use the ipcs -s command. You can use the ipcrm command to remove any semaphores accidentally left by programs.
Shared Memory Status
To access the details of shared memory you can use ipcs -m and ipcrm -m <id>. The ipcrm -m <id> command allows shared memory to be removed.