0% found this document useful (0 votes)
4 views

A1_09 OS Lab Pract_6

The document outlines an experiment focused on implementing threads and semaphores for process synchronization in C programming. It includes source code for a multi-threading demonstration and a producer-consumer problem, showcasing how to create threads for distinct computations and manage shared resources with semaphores. The inference highlights the importance of synchronization mechanisms in preventing race conditions and ensuring stable execution of concurrent processes.

Uploaded by

thunderkinggamer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

A1_09 OS Lab Pract_6

The document outlines an experiment focused on implementing threads and semaphores for process synchronization in C programming. It includes source code for a multi-threading demonstration and a producer-consumer problem, showcasing how to create threads for distinct computations and manage shared resources with semaphores. The inference highlights the importance of synchronization mechanisms in preventing race conditions and ensuring stable execution of concurrent processes.

Uploaded by

thunderkinggamer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

========================================================================

EXPERIMENT NO: 06

AIM:
Write C programs to implement threads and/or semaphores for process synchronization.

Author: Ajatshatru Arun Kaushik


Date: 17-03-2025
Source: threads.c, producer_consumer.c
========================================================================
Problem Statement:

[1] Write a program to demonstrate threads. The demonstration may create 2 or more threads,
where each thread will proceed with a distinct computation using a function or separate
program. The main flow will continue as normal.

[2] Write a program to implement producer-consumer problem using semaphores.

========================================================================
SOURCE CODE
========================================================================
[1] Filename: threads.c

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

void *add(void *val) {


​ int num = *((int *)val);
​ printf("\tAdd Num to Self: %d\n", num + num);
​ sleep(1);
​ printf("\tIncrement by 11: %d\n", num + num + 11);
​ return NULL;
}

void *mul(void *val) {


​ int num = *((int *)val);
​ printf("\tMultiply Num By Self: %d\n", num * num);
​ sleep(1);
​ printf("\tMultiply Product By 2: %d\n", num * num * 2);
​ return NULL;
}

int main() {
​ pthread_t t[5];
​ int i, val[5], knt, choice;
​ printf("How many Threads (Max 5): ");
​ scanf("%d", &knt);

​ if (knt > 5) {
​ printf("Maximum 5 threads allowed.\n");
​ return 1;
​ }

​ for (i = 0; i < knt; i++) {


​ printf("\nEnter Value for Thread-%d: ", i + 1);
​ scanf("%d", &val[i]);

​ printf("\nMenu...\n[1] Addition [2] Multiplication\n");
​ printf("Your Choice: ");
​ scanf("%d", &choice);

​ if (choice == 1) {
​ pthread_create(&t[i], NULL, add, &val[i]);
​ printf("\tThread-%d created for addition.\n", i + 1);
​ } else if (choice == 2) {
​ pthread_create(&t[i], NULL, mul, &val[i]);
​ printf("\tThread-%d created for multiplication.\n", i + 1);
​ } else {
​ printf("Invalid choice. Try again.\n");
​ i--;
​ }
​ }

​ for (i = 0; i < knt; i++) {


​ pthread_join(t[i], NULL);
​ }

​ return 0;
}

[2] Filename: producer_consumer.c

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

#define BUFFER_SIZE 5

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

void *producer(void *arg) {


​ int item, i = 0;
​ while (i < 10) {
​ item = rand() % 100;
​ sem_wait(&empty);
​ pthread_mutex_lock(&mutex);

​ buffer[count] = item;
​ printf("Producer produced: %d\n", item);
​ count++;

​ pthread_mutex_unlock(&mutex);
​ sem_post(&full);

​ sleep(1);
​ i++;
​ }
​ return NULL;
}

void *consumer(void *arg) {


​ int item, i = 0;
​ while (i < 10) {
​ sem_wait(&full);
​ pthread_mutex_lock(&mutex);

​ item = buffer[count - 1];


​ printf("Consumer consumed: %d\n", item);
​ count--;

​ pthread_mutex_unlock(&mutex);
​ sem_post(&empty);

​ sleep(1);
​ i++;
​ }
​ return NULL;
}

int main() {
​ pthread_t prodThread, consThread;

​ sem_init(&empty, 0, BUFFER_SIZE);
​ sem_init(&full, 0, 0);
​ pthread_mutex_init(&mutex, NULL);
​ pthread_create(&prodThread, NULL, producer, NULL);
​ pthread_create(&consThread, NULL, consumer, NULL);

​ pthread_join(prodThread, NULL);
​ pthread_join(consThread, NULL);

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

​ return 0;
}

========================================================================
OUTPUT
========================================================================
[1] Filename: threads.c

ajatshatru-kaushik@Linux:~/Desktop/CS4A09/OS Lab Experiments/Pract_7$ gcc threads.c -o threads

ajatshatru-kaushik@Linux:~/Desktop/CS4A09/OS Lab Experiments/Pract_5$ ./threads

How many Threads (Max 5): 3

Enter Value for Thread-1: 2

Menu...
[1] Addition [2] Multiplication
Your Choice: 1
​ Thread-1 created for addition.

Enter Value for Thread-2: ​ Add Num to Self: 4


​ Increment by 11: 15
1

Menu...
[1] Addition [2] Multiplication
Your Choice: 2
​ Thread-2 created for multiplication.

Enter Value for Thread-3: ​ Multiply Num By Self: 1


​ Multiply Product By 2: 2
4

Menu...
[1] Addition [2] Multiplication
Your Choice: 1
​ Thread-3 created for addition.
​ Add Num to Self: 8
​ Increment by 11: 19

[2] Filename: producer_consumer.c

ajatshatru-kaushik@Linux:~/Desktop/CS4A09/OS Lab Experiments/Pract_7$ gcc


producer_consumer.c -o pc

ajatshatru-kaushik@Linux:~/Desktop/CS4A09/OS Lab Experiments/Pract_5$ ./pc

Producer produced: 83
Consumer consumed: 83
Producer produced: 86
Consumer consumed: 86
Producer produced: 77
Consumer consumed: 77
Producer produced: 15
Consumer consumed: 15
Producer produced: 93
Consumer consumed: 93
Producer produced: 35
Consumer consumed: 35
Producer produced: 86
Consumer consumed: 86
Producer produced: 92
Consumer consumed: 92
Producer produced: 49
Consumer consumed: 49
Producer produced: 21
Consumer consumed: 21

========================================================================
INFERENCE
========================================================================

The multi-threading demonstration problem focuses on creating and managing multiple threads that
perform distinct computations concurrently, such as addition and multiplication, while ensuring
synchronization using pthreads. The main thread continues execution while the created threads process
their tasks independently. The producer-consumer problem, on the other hand, addresses
synchronization using semaphores to manage shared resources efficiently. The producer generates items
and adds them to a buffer, while the consumer removes them, ensuring that neither overproduction nor
underutilization occurs. Proper synchronization mechanisms like semaphores and mutex locks prevent
race conditions and maintain system stability, ensuring controlled execution of concurrent processes.

You might also like