0% found this document useful (0 votes)
8 views8 pages

Cse4a 25 Prac6

This document outlines an experiment aimed at implementing threads and semaphores for process synchronization in C programming. It includes code for a Producer-Consumer problem and a Reader-Writer problem, demonstrating the use of semaphores and mutexes to manage concurrent access to shared resources. The program also provides a menu for users to choose between different synchronization demonstrations.

Uploaded by

thunderkinggamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Cse4a 25 Prac6

This document outlines an experiment aimed at implementing threads and semaphores for process synchronization in C programming. It includes code for a Producer-Consumer problem and a Reader-Writer problem, demonstrating the use of semaphores and mutexes to manage concurrent access to shared resources. The program also provides a menu for users to choose between different synchronization demonstrations.

Uploaded by

thunderkinggamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment 6

Aim: Write C programs to implement threads and/or semaphores for


process synchronization.
Upload PDF of Program Listing ( Document compiled for Source Code &
Execution Trace)
Name:Anuj Chandrakar
Section:A
Roll no.:25
Batch:A2
Semester:IV

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>

#define BUFFER_SIZE 5

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

void *add(void *val) {


int num = *((int *)val);
printf("\t\tAdd Num To Self: %d\n", num + num);
sleep(1);
printf("\t\tIncrement by 11: %d\n", num + num + 11);
free(val); // Free memory inside the thread function
return NULL;
}

void *mul(void *val) {


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

void *producer(void *arg) {


for (int i = 0; i < 10; i++) {
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[count++] = i;
printf("Produced: %d\n", i);
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
return NULL;
}

void *consumer(void *arg) {


for (int i = 0; i < 10; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
int item = buffer[--count];
printf("Consumed: %d\n", item);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(1);
}
return NULL;
}

int main() {
pthread_t t[5], prodThread, consThread;
int choice, knt, val;

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

while (1) {
printf("\nMENU:\n[1] Thread Demonstration\n[2] Producer-
Consumer Problem\n[3] Reader-Writer Problem (IPC)\n[4] Exit\nYour
Choice: ");
scanf("%d", &choice);

if (choice == 1) {
printf("How Many Threads: ");
scanf("%d", &knt);

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


printf("\nEnter Value-%d: ", i + 1);
scanf("%d", &val);
int *num = malloc(sizeof(int));
*num = val;

printf("\n[1] Addition\n[2] Multiplication\nYour


Choice: ");
scanf("%d", &choice);

if (choice == 1)
pthread_create(&t[i], NULL, add, num);
else if (choice == 2)
pthread_create(&t[i], NULL, mul, num);
else
printf("Invalid Choice\n");

pthread_join(t[i], NULL);
}
} else if (choice == 2) {
pthread_create(&prodThread, NULL, producer, NULL);
pthread_create(&consThread, NULL, consumer, NULL);
pthread_join(prodThread, NULL);
pthread_join(consThread, NULL);
} else if (choice == 3) {
int shmid = shmget(1234, sizeof(int), IPC_CREAT | 0666);
if (shmid == -1) {
perror("shmget failed");
exit(1);
}

int *data = (int *)shmat(shmid, NULL, 0);


if (data == (int *)-1) {
perror("shmat failed");
exit(1);
}
*data = 0;
sem_init(&readWriteLock, 1, 1);

if (fork() == 0) {
sem_wait(&readWriteLock);
*data = rand() % 100;
printf("Writer wrote data: %d\n", *data);
sem_post(&readWriteLock);
shmdt(data);
exit(0);
} else {
wait(NULL);
if (fork() == 0) {
sem_wait(&readWriteLock);
printf("Reader read data: %d\n", *data);
sem_post(&readWriteLock);
shmdt(data);
exit(0);
}
wait(NULL);
}

shmdt(data);
shmctl(shmid, IPC_RMID, NULL);
} else if (choice == 4) {
break;
} else {
printf("Invalid Choice!\n");
}
}

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

return 0;
}
Output:
root@Anuj:nano practical6.c
root@Anuj:gcc practical6.c
root@Anuj:./a.out

MENU:
[1] Thread Demonstration
[2] Producer-Consumer Problem
[3] Reader-Writer Problem (IPC)
[4] Exit
Your Choice: 1
How Many Threads: 3

Enter Value-1: 23

[1] Addition
[2] Multiplication
Your Choice: 1
Add Num To Self: 46
Increment by 11: 57

Enter Value-2: 24

[1] Addition
[2] Multiplication
Your Choice: 2
Multiply Num By Self: 576
Multiply Product By 2: 1152

Enter Value-3: 98

[1] Addition
[2] Multiplication
Your Choice: 2
Multiply Num By Self: 9604
Multiply Product By 2: 19208

MENU:
[1] Thread Demonstration
[2] Producer-Consumer Problem
[3] Reader-Writer Problem (IPC)
[4] Exit
Your Choice: 2
Produced: 0
Consumed: 0
Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
Produced: 4
Consumed: 4
Produced: 5
Consumed: 5
Produced: 6
Consumed: 6
Produced: 7
Consumed: 7
Produced: 8
Consumed: 8
Produced: 9
Consumed: 9

MENU:
[1] Thread Demonstration
[2] Producer-Consumer Problem
[3] Reader-Writer Problem (IPC)
[4] Exit
Your Choice: 3
Writer wrote data: 83
Reader read data: 83

MENU:
[1] Thread Demonstration
[2] Producer-Consumer Problem
[3] Reader-Writer Problem (IPC)
[4] Exit
Your Choice: 4

Inference: This program demonstrates process synchronization using


threads and semaphores. The Producer-Consumer problem ensures safe
buffer access, while the Reader-Writer problem synchronizes shared
memory access. Proper locking mechanisms prevent race conditions and
data inconsistency.

You might also like