Professional Documents
Culture Documents
LAB 4 2024 NDJ 20703 G1[1](1)
LAB 4 2024 NDJ 20703 G1[1](1)
Objectives:
a. To describe how to implement the Producer & Consumer Problem in an operating system
b. To understand synchronization in an operating system
c. To apply semaphore in operating system.
AIM:
Semaphores
Semaphores are meant for synchronizing access to multiple processes. When one process wants to
access the memory (for reading or writing), it needs to be locked (or protected) and released when
the access is removed. This needs to be repeated by all the processes to secure data.
Signals
Signal is a mechanism to communication between multiple processes by way of signalling. This means
a source process will send a signal (recognized by number) and the destination process will handle it
accordingly.
(Semaphore)
DESCRIPTION:
ALGORITHM:
ii) If there is empty space in the buffer check the mutex value for enter into the critical section.
iii) If the mutex value is 0, allow the producer to add value in the temporary variable to the buffer.
ii) If there is any item in the buffer check for mutex value, if the mutex==0,
iii) Signal the mutex value and reduce the empty value by 1.
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
#define BUFFERSIZE 10
int buffer[20];
int in = 0, out = 0;
(*s)--;
{ (*s)++;
void producer()
{ int item;
NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)
do {
wait(&empty);
wait(&mutex);
printf("\nEnter an item:");
scanf("%d", &item);
buffer[in] = item;
signal(&mutex);
signal(&full);
} while (1);
void consumer()
{ int item1;
do {
wait(&full);
wait(&mutex);
item1 = buffer[out];
signal(&mutex);
signal(&empty);
} while (1);
int main() {
scanf("%d", &n);
empty = n;
NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)
// Create producer and consumer threads/processes here
producer();
consumer();
return 0;
OUTPUT:
$ cc prco.c
$ a.out
consumed item=2
consumed item=5
consumed item=9
NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)
LABORATORY ASSESSMENT
1. Define Semaphore?
The "wait" function is used to get a semaphore, decreasing its value. The "signal" function is used to
release a semaphore, incrementing its value. Both together, help each other coordinate access to
shared resources and avoid race conditions.
Mutual exclusion tells about the concept of ensuring that only one process at a time can access a
shared resource, preventing conflicts and maintaining data integrity in concurrent programming.
The producer-consumer problem involves two types of processes, producers and consumers and they
share something which is the fixed-size buffer. Producers add items to the buffer, and consumers
remove the items. The challenge is to ensure synchronization so that the buffer is accessed correctly.
Process synchronization is needed to coordinate the execution of multiple processes to avoid race
conditions, maintain data consistency, and ensure correct program execution in concurrent
environments.
In a producer-consumer problem with bounded buffers, there is a limited capacity for items in the
buffer. This will lead to issues for example buffer overflow if the producer produces too quickly or
buffer underflow if the consumer consumes too quickly. Unbounded buffers have no such limitations
but may lead to resource exhaustion.
7. Can producer and consumer processes access the shared memory concurrently? If not, which
technique provides such a benefit?
No, producer and consumer processes must not access shared memory concurrently in order to
avoid data corruption. Techniques like semaphores or mutexes (mutual exclusion) are used to
enforce exclusive access to shared resources, ensuring proper synchronization.
NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)
1. Start
4. Producer generates an item and sends it to the consumer through the message queue.
6. End.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// Message structure
struct msg_buffer {
long msg_type;
int item;
};
// Function prototypes
int main() {
if (fork() == 0) {
producer(msgqid);
} else {
consumer(msgqid);
return 0;
message.msg_type = 1;
message.item = i;
sleep(1);
sleep(1);