0% found this document useful (0 votes)
7 views3 pages

6th Os

The document presents a C program demonstrating mutual exclusion using semaphores for a producer-consumer scenario. It initializes semaphores and shared memory, allowing a producer to generate characters and a consumer to consume them while ensuring synchronization. The output shows the sequence of produced and consumed characters.

Uploaded by

astrophileion369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

6th Os

The document presents a C program demonstrating mutual exclusion using semaphores for a producer-consumer scenario. It initializes semaphores and shared memory, allowing a producer to generate characters and a consumer to consume them while ensuring synchronization. The output shows the sequence of produced and consumed characters.

Uploaded by

astrophileion369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX.6: MUTUAL EXCLUSION BY SEMAPHORE.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/wait.h>
#define BUFFER_SIZE 5
#define SEM_MUTEX 0
#define SEM_EMPTY 1
#define SEM_FULL 2

union semun { int val; struct semid_ds *buf; unsigned short *array; };
struct sembuf P(int sem_num) { return (struct sembuf){sem_num, -1, 0}; }
struct sembuf V(int sem_num) { return (struct sembuf){sem_num, 1, 0}; }
int semid, shmid;
char *buffer;
void initialize() {
semid = semget(IPC_PRIVATE, 3, 0666 | IPC_CREAT);
semctl(semid, SEM_MUTEX, SETVAL, (union semun){1});
semctl(semid, SEM_EMPTY, SETVAL, (union semun){BUFFER_SIZE});
semctl(semid, SEM_FULL, SETVAL, (union semun){0});
shmid = shmget(IPC_PRIVATE, BUFFER_SIZE, 0666 | IPC_CREAT);
buffer = shmat(shmid, NULL, 0);
}
void producer() {
int in = 0;
for (int i = 0; i < 10; i++) {
semop(semid, &P(SEM_EMPTY), 1);
semop(semid, &P(SEM_MUTEX), 1);
buffer[in] = 'A' + (i % 26);
printf("Produced: %c\n", buffer[in]);
in = (in + 1) % BUFFER_SIZE;
semop(semid, &V(SEM_MUTEX), 1);
semop(semid, &V(SEM_FULL), 1);
sleep(1);
}}
void consumer() {
int out = 0;
for (int i = 0; i < 10; i++) {
semop(semid, &P(SEM_FULL), 1);
semop(semid, &P(SEM_MUTEX), 1);
printf("Consumed: %c\n", buffer[out]);
out = (out + 1) % BUFFER_SIZE;
semop(semid, &V(SEM_MUTEX), 1);
semop(semid, &V(SEM_EMPTY), 1);
sleep(1);
}}
int main() {
initialize();
if (fork() == 0) {
consumer();
exit(0);
} else {
producer();
wait(NULL);
shmctl(shmid, IPC_RMID, NULL);
semctl(semid, 0, IPC_RMID);
}
return 0;
}

OUTPUT:
Produced: A
Consumed: A
Produced: B
Consumed: B
Produced: C
Consumed: C
Produced: D
Consumed: D
Produced: E
Consumed: E
Produced: F
Consumed: F
Produced: G
Consumed: G
Produced: H
Consumed: H
Produced: I
Consumed: I
Produced: J
Consumed: J

You might also like