A1_09 OS Lab Pract_6
A1_09 OS Lab Pract_6
EXPERIMENT NO: 06
AIM:
Write C programs to implement threads and/or semaphores for process synchronization.
[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.
========================================================================
SOURCE CODE
========================================================================
[1] Filename: threads.c
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
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;
}
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--;
}
}
return 0;
}
#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;
buffer[count] = item;
printf("Producer produced: %d\n", item);
count++;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
i++;
}
return NULL;
}
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
Menu...
[1] Addition [2] Multiplication
Your Choice: 1
Thread-1 created for addition.
Menu...
[1] Addition [2] Multiplication
Your Choice: 2
Thread-2 created for multiplication.
Menu...
[1] Addition [2] Multiplication
Your Choice: 1
Thread-3 created for addition.
Add Num to Self: 8
Increment by 11: 19
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.