0% found this document useful (0 votes)
31 views12 pages

Lab Assignment-5_DOS(CSE3249) (1)

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)
31 views12 pages

Lab Assignment-5_DOS(CSE3249) (1)

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/ 12

Laboratory Assignments

Subject: Design Principles of Operating Systems

Subject code: CSE 3249

Assignment 5: Implementation of synchronization using semaphore:

Objective of this Assignment:


• To implement the concept of multi-threading in a process.
• To learn the use of semaphore i.e., to control access to shared resources.

1. Producer-Consumer problem

Problem: Write a C program to implement the producer-consumer program where:

• Producer generates integers from 1 to 100.

• Consumer processes the numbers.

Requirements:

• Use a shared buffer with a maximum size of 10.


• Use semaphores and mutex to ensure thread-safe access to the buffer.
• Print the number that producer is producing and consumer is consuming.

• Both producer and consumer will continue for 20 iterations

CODE:

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define BUFFER_SIZE 10
#define PRODUCER_ITERATIONS 20
#define CONSUMER_ITERATIONS 20

int buffer[BUFFER_SIZE];
int in = 0, out = 0;
sem_t empty, full;
pthread_mutex_t mutex;

void* producer(void* arg) {


for (int i = 1; i <= PRODUCER_ITERATIONS; i++) {
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = i;
printf("Producer produced: %d\n", i);
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full);
usleep(100000);
}
return NULL;
}

void* consumer(void* arg) {


for (int i = 0; i < CONSUMER_ITERATIONS; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
int item = buffer[out];
printf("Consumer consumed: %d\n", item);
out = (out + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
usleep(150000);
}
return NULL;
}

int main() {
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);

pthread_t prod_thread, cons_thread;

pthread_create(&prod_thread, NULL, producer, NULL);


pthread_create(&cons_thread, NULL, consumer, NULL);

pthread_join(prod_thread, NULL);
pthread_join(cons_thread, NULL);

sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);

return 0;
}
OUTPUT:
2. Alternating Numbers with Two Threads

Problem: Write a program to print 1, 2, 3 … upto 20. Create threads where two threads print numbers
alternately.

• Thread A prints odd numbers: 1, 3, 5 ...


• Thread B prints even numbers: 2, 4, 6 ...

Requirements:

• Use semaphores to control the order of execution of the threads.

• Ensure no race conditions occur.


CODE:

#include <stdio.h>

#include <pthread.h>

#include <semaphore.h>

sem_t sem_a, sem_b;

void* print_odd(void* arg) {

for (int i = 1; i <= 19; i += 2) {

sem_wait(&sem_a);

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

sem_post(&sem_b);

return NULL;

void* print_even(void* arg) {

for (int i = 2; i <= 20; i += 2) {

sem_wait(&sem_b);

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

sem_post(&sem_a);

return NULL;

int main() {

sem_init(&sem_a, 0, 1);

sem_init(&sem_b, 0, 0);

pthread_t thread_a, thread_b;


pthread_create(&thread_a, NULL, print_odd, NULL);

pthread_create(&thread_b, NULL, print_even, NULL);

pthread_join(thread_a, NULL);

pthread_join(thread_b, NULL);

sem_destroy(&sem_a);

sem_destroy(&sem_b);

return 0;

OUTPUT:

3. Alternating Characters

Problem: Write a program to create two threads that print characters (A and B) alternately such as
ABABABABA…. upto 20. Use semaphores to synchronize the threads.

• Thread A prints A.

• Thread B prints B.

Requirements:

• Use semaphores to control the order of execution of the threads.


• Ensure no race conditions occur.

CODE:

#include <stdio.h>

#include <pthread.h>

#include <semaphore.h>

sem_t sem_a, sem_b;

void* print_A(void* arg) {

for (int i = 0; i < 10; i++) {

sem_wait(&sem_a);

printf("A");

sem_post(&sem_b);

return NULL;

void* print_B(void* arg) {

for (int i = 0; i < 10; i++) {

sem_wait(&sem_b);

printf("B");

sem_post(&sem_a);

return NULL;

int main() {

sem_init(&sem_a, 0, 1);

sem_init(&sem_b, 0, 0);

pthread_t thread_a, thread_b;


pthread_create(&thread_a, NULL, print_A, NULL);

pthread_create(&thread_b, NULL, print_B, NULL);

pthread_join(thread_a, NULL);

pthread_join(thread_b, NULL);

sem_destroy(&sem_a);

sem_destroy(&sem_b);

return 0;

OUTPUT:

4. Countdown and Countup

Problem: Write a program create two threads where:

• Thread A counts down from 10 to 1.

• Thread B counts up from 1 to 10.

Both threads should alternate execution.

Requirements:

• Use semaphores to control the order of execution of the threads.

• Ensure no race conditions occur.

CODE:

#include <stdio.h>

#include <pthread.h>

#include <semaphore.h>

sem_t sem_a, sem_b;

void* countdown(void* arg) {

for (int i = 10; i >= 1; i--) {


sem_wait(&sem_a);

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

sem_post(&sem_b);

return NULL;

void* countup(void* arg) {

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

sem_wait(&sem_b);

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

sem_post(&sem_a);

return NULL;

int main() {

sem_init(&sem_a, 0, 1);

sem_init(&sem_b, 0, 0);

pthread_t thread_a, thread_b;

pthread_create(&thread_a, NULL, countdown, NULL);

pthread_create(&thread_b, NULL, countup, NULL);

pthread_join(thread_a, NULL);

pthread_join(thread_b, NULL);

sem_destroy(&sem_a);

sem_destroy(&sem_b);

return 0;

}
OUTPUT:

5. Sequence Printing using Threads

Problem: Write a program that creates three threads: Thread A, Thread B, and Thread C. The threads must print
numbers in the following sequence: A1, B2, C3, A4, B5, C6 … upto 20 numbers.

• Thread A prints A1, A4, A7, …


• Thread B prints B2, B5, B8, …

• Thread C prints C3, C6, C9, ...

Requirements:

• Use semaphores to control the order of execution of the threads.

• Ensure no race conditions occur.

CODE:

#include <stdio.h>

#include <pthread.h>

#include <semaphore.h>
sem_t sem_a, sem_b, sem_c;

void* threadA(void* arg) {

for (int i = 1; i <= 20; i += 3) {

sem_wait(&sem_a);

printf("A%d\n", i);

sem_post(&sem_b);

return NULL;

void* threadB(void* arg) {

for (int i = 2; i <= 20; i += 3) {

sem_wait(&sem_b);

printf("B%d\n", i);

sem_post(&sem_c);

return NULL;

void* threadC(void* arg) {

for (int i = 3; i <= 20; i += 3) {

sem_wait(&sem_c);

printf("C%d\n", i);

sem_post(&sem_a);

return NULL;

int main() {
// Initialize semaphores

sem_init(&sem_a, 0, 1);

sem_init(&sem_b, 0, 0);

sem_init(&sem_c, 0, 0);

pthread_t tA, tB, tC;

pthread_create(&tA, NULL, threadA, NULL);

pthread_create(&tB, NULL, threadB, NULL);

pthread_create(&tC, NULL, threadC, NULL);

pthread_join(tA, NULL);

pthread_join(tB, NULL);

pthread_join(tC, NULL);

sem_destroy(&sem_a);

sem_destroy(&sem_b);

sem_destroy(&sem_c);

return 0;

OUTPUT:

You might also like