You are on page 1of 8

1

1. Dining Philosophers Problem using mutex and semaphore in C language.

Source Code:

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

#define N 5 sem_t bowl; sem_t


chopsticks[N]; void
*philosopher(void *num); void
eat(int);

void *philosopher(void *num)


{
int phil = *((int*)num); sem_wait(&bowl);
printf("\nPhilospher %d has taken the bowl", phil);
sem_wait(&chopsticks[phil]);
sem_wait(&chopsticks[(phil+1)%N]);
eat(phil);
sleep(2);
printf("\nPhilospher %d has finished eating", phil);
sem_post(&chopsticks[(phil+1)%N]);
sem_post(&chopsticks[phil]); sem_post(&bowl);
}

void eat(int phil)


{
printf("\nPhilosopher %d is eating", phil);
}

int main()
{
int i, a[N]; pthread_t tid[N];
sem_init(&bowl, 0, 1);
for(i=0;i<N;i++)
sem_init(&chopsticks[i], 0, 1);
while(1) {
for(i=0;i<N;i++)
{
a[i]=i;
pthread_create(&tid[i], NULL, philosopher, (void *)&a[i]);
}

1
2

for(i=0;i<N;i++) pthread_join(tid[i],
NULL);
}
}

Output:

2.Readers-Writers Problem using mutex and semaphore in C language.


Source Code:

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

2
3

#define READERS_COUNT 5
#define WRITERS_COUNT 2
sem_t rw_mutex, mutex; int
readers = 0;

void *reader(void *arg) {


int id = *(int *)arg; int
k=1;

while (k<=3)
{ sem_wait(&mutex);
readers++; if (readers
== 1) {
sem_wait(&rw_mutex);
}
sem_post(&mutex);

printf("Reader %d is reading.\n", id); sleep(1);

sem_wait(&mutex);
readers--; if
(readers == 0) {
sem_post(&rw_mutex);
}
sem_post(&mutex);

sleep(2);
k++;
}

return NULL;
}

void *writer(void *arg) {


int id = *(int *)arg; int
k=1;
while (k<=3) { sem_wait(&rw_mutex);

printf("\nWriter %d is writing.\n\n", id);


sleep(1); sem_post(&rw_mutex);

sleep(2);
k++;
}

return NULL;
}

3
4

int main() {
pthread_t readers_threads[READERS_COUNT],
writers_threads[WRITERS_COUNT];
int i, readers_ids[READERS_COUNT], writers_ids[WRITERS_COUNT];

sem_init(&rw_mutex, 0, 1); sem_init(&mutex, 0, 1);

for (i = 0; i < READERS_COUNT; i++) {


1. readers_ids[i] = i;
pthread_create(&readers_threads[i], NULL, reader, &readers_ids[i]);
}

for (i = 0; i < WRITERS_COUNT; i++) { writers_ids[i] = i;


pthread_create(&writers_threads[i], NULL, writer, &writers_ids[i]);
}

for (i = 0; i < READERS_COUNT; i++) {


pthread_join(readers_threads[i], NULL);
}

for (i = 0; i < WRITERS_COUNT; i++) {


pthread_join(writers_threads[i], NULL);
}

sem_destroy(&rw_mutex); sem_destroy(&mutex);

return 0;
}
Output:

4
5

5
6

8. Banker’s Algorithm using C programming Language.

Source Code:

#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k; n = 5; // Number of
processes m = 3; // Number of
resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4

int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix


{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[n], ans[n], ind = 0; for (k = 0; k < n; k++) { f[k] = 0; } int


need[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0; for (k = 0; k < 5; k++) { for
(i = 0; i < n; i++) { if (f[i] == 0) {

int flag = 0; for (j = 0; j < m; j++) { if


(need[i][j] > avail[j]){ flag = 1; break;
}
}

if (flag == 0) { ans[ind++] = i; for (y = 0; y < m;


y++) avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}

int flag = 1;

for(int i=0;i<n;i++)

6
7

{
if(f[i]==0)
{ flag=0;
printf("The following system is not safe"); break;
}}

if(flag==1) {
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++) printf(" P%d ->",
ans[i]);
printf(" P%d", ans[n - 1]);
}
printf("\n"); return

(0);

}
Output:

Producer-Consumer
---
The Producer-Consumer problem involves two entities: a producer and a consumer, who share a common
buffer or queue. The producer generates data items and adds them to the buffer, while the consumer
consumes or removes items from the buffer. The goal is to ensure that the producer and consumer can
work concurrently without synchronization issues.

To solve the problem, synchronization mechanisms are employed:

Mutual Exclusion: The shared buffer is protected using locks or mutexes to enforce mutual exclusion. Only
one entity can access the buffer at a time to avoid data corruption.

Empty and Full Buffer Conditions: The producer checks if the buffer is full before adding new items. If the
buffer is full, the producer waits until space becomes available. The consumer checks if the buffer is empty
before consuming items. If the buffer is empty, the consumer waits until items are produced.

Signaling: Condition variables or semaphores are used for signaling between the producer and consumer.
The producer signals the consumer when new items are added to the buffer, indicating that it can start

7
8

consuming. The consumer signals the producer when it has consumed an item or when space becomes
available in the buffer.

By coordinating the actions of the producer and consumer using synchronization mechanisms, the
Producer-Consumer problem can be solved effectively. This ensures that the producer and consumer can
work concurrently without issues such as buffer overflows or underflows, and it maintains the integrity and
synchronization of the shared buffer.

You might also like