Professional Documents
Culture Documents
Name: Pranav Chaurasia REG. NO.: 18BCE0216 Os Lab Assessment-3 SLOT: L41 + L42 Faculty: Geraldine Bessie Ma'Am
Name: Pranav Chaurasia REG. NO.: 18BCE0216 Os Lab Assessment-3 SLOT: L41 + L42 Faculty: Geraldine Bessie Ma'Am
CODE:
#include<stdio.h>
#include<sys/types.h>
#include<pthread.h>
#include<semaphore.h>
sem_t mutex;
sem_t full;
sem_t empty;
char buffer[100];
buffer[index]=i+64;
printf("Producer added %c to buffer ", buffer[index]);
sem_post(&full);
sem_post(&mutex);
if(++index==10)index=0;
}}
sem_post(&empty);
sem_post(&mutex);
if(++index==10) index=0;
}}
int main()
{
pthread_t tid1,tid2;
sem_init(&mutex,0,1);
sem_init(&empty,0,10); // 0 indicates its not shared memory but a buffer
sem_init(&full,0,0);
pthread_create(&tid1,NULL,producer,NULL);
pthread_create(&tid2,NULL,consumer,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
sem_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);
return 0;
}
Output:
2. READER-WRITER Problem
CODE:
#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#define MAXTHREAD 10 /* define # readers */
void access_database(); /* prototypes */
void non_access_database();
void* reader(void*);
void* writer(void*);
sem_t q; /* establish que */
int rc = 0; /* number of processes reading or wanting to */
int wc = 0;
int write_request = 0;
int main()
{
pthread_t readers[MAXTHREAD],writerTh;
int index;
int ids[MAXTHREAD]; /* readers and initialize mutex, q and db-set them to 1
*/
sem_init (&q,0,1);
for(index = 0; index < MAXTHREAD; index ++)
{
ids[index]=index+1;
if(pthread_create(&readers[index],0,reader,&ids[index])!=0){
perror("Cannot create reader!");
exit(1);
}
}
if(pthread_create(&writerTh,0,writer,0)!=0){
perror("Cannot create writer");
exit(1);
}
pthread_join(writerTh,0);
sem_destroy (&q);
return 0;
}
Output:
3. DINING PHILLOSOPHER Problem
CODE:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N
int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };
sem_t mutex;
sem_t S[N];
sleep(2);
// take up chopsticks
void take_fork(int phnum)
{
sem_wait(&mutex);
sem_post(&mutex);
sleep(1);
}
sem_wait(&mutex);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
while (1) {
int* i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}
int main()
{
int i;
pthread_t thread_id[N];
sem_init(&S[i], 0, 0);
pthread_join(thread_id[i], NULL);
}
Output:
4. BANKER’S Algorithm:
CODE:
#include <stdio.h>
int main()
{
int alloc[5][3],i,j;
int max[5][3];
int avail[3];
int n, m, k;
n = 5; // Number of processes
m = 3; // Number of resources
printf("\nEnter the allocation table for 5 processes in 5X3 matrix: \n ");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\nEnter max table(5X3):\n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\nEnter available resources:\n");
for(i=0;i<3;i++)
{
scanf("%d",&avail[i]);
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}}
}}
// P0, P1, P2, P3, P4 are the Process names here
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]);
return (0);
}
Output: