Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

NDJ 20703 – OPERATING SYSTEM

LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)


FACULTY OF ELECTRONIC ENGINEERING & TECHNOLOGY (FKTEN)

UNIVERSITI MALAYSIA PERLIS

LAB 4: Linux Shell Scripting (Semaphore)

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.

To write a C program to implement the Producer & Consumer Problem

(Semaphore)

DESCRIPTION:

Producer-consumer problem, is a common paradigm for cooperating processes. A producer process


produces information that is consumed by a consumer process. One solution to the producer-
consumer problem uses shared memory. To allow producer and consumer processes to run
concurrently, there must be available a buffer of items that can be filled by the producer and emptied
by the consumer. This buffer will reside in a region of memory that is shared by the producer and
consumer processes. A producer can produce one item while the consumer is consuming another
item. The producer and consumer must be synchronized, so that the consumer does not try to
consume an item that has not yet been produced.

ALGORITHM:

Step 1: The Semaphore mutex, full & empty are initialized.

Step 2: In the case of producer process

i) Produce an item in to temporary variable.

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.

Step 3: In the case of consumer process


NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)
i) It should wait if the buffer is empty

ii) If there is any item in the buffer check for mutex value, if the mutex==0,

remove item from buffer

iii) Signal the mutex value and reduce the empty value by 1.

iv) Consume the item.

Step 4: Print the result

PROGRAM :

#include <stdio.h>

#include <stdlib.h>

#define BUFFERSIZE 10

int mutex, n, empty, full = 0;

int buffer[20];

int in = 0, out = 0;

void wait(int *s)

{ while (*s <= 0) {

// Wait for condition to be met

(*s)--;

void signal(int *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;

in = (in + 1) % n; // Circular buffer

signal(&mutex);

signal(&full);

} while (1);

void consumer()

{ int item1;

do {
wait(&full);

wait(&mutex);

item1 = buffer[out];

printf("\nConsumed item = %d", item1);

out = (out + 1) % n; // Circular buffer

signal(&mutex);

signal(&empty);
} while (1);

int main() {

printf("Enter the value of n:");

scanf("%d", &n);

empty = n;
NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)
// Create producer and consumer threads/processes here

// For simplicity, let's call the functions directly

producer();

consumer();

return 0;

OUTPUT:

$ cc prco.c

$ a.out

Enter the value of n :3

Enter the item:2

Enter the item:5

Enter the item:9

consumed item=2

consumed item=5

consumed item=9
NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)

NAME : BUQRA AN FARDEENA BINTI MOHAMED FAIRUZ

MATRIC No : 222020522 GROUP :1 DATE: 21/2/24

LABORATORY ASSESSMENT

1. Define Semaphore?

A semaphore is a synchronization mechanism used in concurrent programming to control access to a


shared resource. It has two operations: "wait" (decreasing the semaphore) and "signal" (increment
the semaphore).

2. What is use of wait and signal functions?

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.

3. What is mutual exclusion?

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.

4. Define producer consumer problem?

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.

5. What is the need for process synchronization?

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.

6. Discuss the consequences of considering bounded and unbounded buffers in producer-consumer


problem?

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)

8. a. Write algorithm to simulate producer-consumer problem using message-passing system


algorithm

1. Start

2. Initialize message queue for communication in the code.

3. Create the producer and consumer processes.

4. Producer generates an item and sends it to the consumer through the message queue.

5. Consumer receives the item from the message queue.

6. End.

b. Based on algorithm in 8(a), Write C program to simulate producer-consumer problem using


message-passing system.

#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

void producer(int msgqid);

void consumer(int msgqid);

int main() {

// Message queue creation

key_t key = ftok("progfile", 65);

int msgqid = msgget(key, 0666 | IPC_CREAT);


NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)
// Forking processes for producer and consumer

if (fork() == 0) {

// Child process as producer

producer(msgqid);

} else {

// Parent process as consumer

consumer(msgqid);

return 0;

void producer(int msgqid) {

struct msg_buffer message;

// Producer produces items

for (int i = 1; i <= 5; ++i) {

message.msg_type = 1;

message.item = i;

// Sending item to the message queue

msgsnd(msgqid, &message, sizeof(message), 0);

printf("Produced item %d\n", i);

// Adding a delay to simulate production time

sleep(1);

void consumer(int msgqid) {

struct msg_buffer message;


NDJ 20703 – OPERATING SYSTEM
LAB 4 – LINUX SHELL SCRIPTING (MEMORY MANAGEMENT)
// Consumer consumes items

for (int i = 1; i <= 5; ++i) {

// Receiving item from the message queue

msgrcv(msgqid, &message, sizeof(message), 1, 0);

printf("Consumed item %d\n", message.item);

// Adding a delay to simulate consumption time

sleep(1);

c. Run the program and get the output of it.

You might also like