Unit_4_UNIX System V
Unit_4_UNIX System V
UNIX System V
Unix system 5 release 1 in 1983
System V
► System V ("System Five") is one of the major versions of the Unix operating
system.
► System V became one of the most widely used and influential Unix variants,
with many features and concepts that have been adopted into other Unix-like
systems.
► UNIX System V messages are part of the System V inter-process
communication (IPC) mechanisms, which allow processes to exchange data
and synchronize their activities.
These mechanisms include
► Message queues,
► semaphores, and
► shared memory.
Key features of System V
Key features of System V
Key features of System V
Versions and Evolutions
Versions and Evolutions
Message Queues
► Message queues in the UNIX kernel allow processes to communicate with each
other by sending and receiving messages.
Unix APIs for messages
► Proper understanding and use of these APIs can significantly enhance the
capabilities of applications requiring inter-process communication.
Key functions for messages
Client server Example
Client server Example
► key = ftok("msgqueue", 65); // Generate a unique key for the message queue
► msgid = msgget(key, 0666); // Get the identifier for the message queue
► if (msgid == -1) {
► perror("msgget");
► exit(1);
► }
► // Prepare the message to be sent to the server
message.mtype = 1; // Message type for the client to server message
► snprintf(message.mtext, MSGSZ, "Hello from client");
if (msgsnd(msgid, &message, sizeof(message.mtext), 0) == -1) {// Send the message to the server
► perror("msgsnd");
► exit(1);
► }
► printf("Client sent: %s\n", message.mtext);
► if (msgrcv(msgid, &message, sizeof(message.mtext), 2, 0) == -1) {// Receive the response message from the server
► perror("msgrcv");
► exit(1);
► }
► printf("Client received response: %s\n", message.mtext);
Semaphore
► semaphores are a mechanism for synchronizing processes.
1.System V Semaphores
2) POSIX Semaphores
► struct sembuf {
► unsigned short sem_num; // Semaphore number in the set
► short sem_op; // Operation to be performed
► short sem_flg; // Operation flags
► };
Example
► int semid = semget(key, 1, 0666 | IPC_CREAT); // Create a semaphore set with one semaphore
► if (semid == -1) {
► perror("semget");
► exit(1);
► }
► struct sembuf sem_op; // Perform a wait (P) operation: decrement the semaphore by 1
► perror("semop");
► exit(1);
► }
► sem_op.sem_op = 1; // Increment by 1
► perror("semop");
► exit(1);
► }
► printf("Semaphore incremented\n");
► perror("semctl");
► exit(1);
► }
► return 0;
► }
Removing a Semaphore Set
► First, you need to create a semaphore using the ‘semget’ system call. This
semaphore will be used to control access to the shared file.
► #include <sys/sem.h>
► #include <sys/types.h>
► #include <sys/ipc.h>
► #include <stdio.h>
► #include <stdlib.h>
► #define SEM_KEY 1234 // Example semaphore key
► #define SEM_PERMS 0666 // Permissions for semaphore
► int semaphore_id;
► void init_semaphore() {
► semaphore_id = semget(SEM_KEY, 1, IPC_CREAT | SEM_PERMS);
► if (semaphore_id == -1) {
► perror("Failed to create semaphore");
► exit(1);
► }
► }
► void set_semaphore_value(int value) {
► union semun {
► int val;
► struct semid_ds *buf;
► unsigned short *array;
► } semarg;
► semarg.val = value;
► if (semctl(semaphore_id, 0, SETVAL, semarg) == -1) {
► perror("Failed to set semaphore value");
► exit(1);
► }
► }
2. Locking the File
To lock the file using the semaphore, you typically decrease (P operation) the semaphore value.
This indicates that the file is in use and prevents other processes from accessing it concurrently.
c
► void semaphore_lock() {
► struct sembuf sem_op;
► sem_op.sem_num = 0;
► sem_op.sem_op = -1; // P operation (decrement)
► sem_op.sem_flg = SEM_UNDO;
► To unlock the file, you increase (V operation) the semaphore value, allowing
other processes to access the file.
► void semaphore_unlock() {
► struct sembuf sem_op;
► sem_op.sem_num = 0;
► sem_op.sem_op = 1; // V operation (increment)
► sem_op.sem_flg = SEM_UNDO;
•shmat(): Attaches the shared memory segment to the process's address space.
•shmdt(): Detaches the shared memory segment from the process's address space.
•shmctl(): Controls operations on the shared memory segment, like marking it for deletion.
Using the Shared Memory:
Creating a Shared Memory Segment:
► Once attached, the shared memory can be accessed like any other part of the
process's memory.
Detaching the Shared Memory
Segment: