Professional Documents
Culture Documents
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
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:
#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;
while (k<=3)
{ sem_wait(&mutex);
readers++; if (readers
== 1) {
sem_wait(&rw_mutex);
}
sem_post(&mutex);
sem_wait(&mutex);
readers--; if
(readers == 0) {
sem_post(&rw_mutex);
}
sem_post(&mutex);
sleep(2);
k++;
}
return NULL;
}
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_destroy(&rw_mutex); sem_destroy(&mutex);
return 0;
}
Output:
4
5
5
6
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 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.
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.