Os Lab6
Os Lab6
Os Lab6
Enroll: 9918103141
Batch: F5
LAB 6(OS)
1. To avoid deadlock in dining philosophers’ problem use a possible solution as the odd
numbered philosophers grab the right and then the left. Implement this solution using
pthread mutual exclusion lock.
#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);
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);
}
2. Using condition variables to implement a producer-consumer algorithm. Define two
threads: one producer and one consumer. The producer reads characters one by one from
a string stored in a file named “string.txt”, then writes sequentially these characters into a
circular queue. Meanwhile, the consumer reads sequentially from the queue and prints
them in the same order. The diagram illustrates the process. Upon completion of running
the program, “Hello! World.” is printed on the screen. In the program, use #define to
specify the size of the queue. For example, #define QUEUE_SIZE 5. Make sure to test
your program with different queue sizes, including 1.
#define QUEU_SIZE 5
char queue [QUEU_SIZE] ;
int front = 0 , rear = 0 ; // pointrs to front and rear of the queu
int items = 0 ; // number of items in queu
int done = 1 ; // to show that the input file is finished to read
pthread_mutex_t mutex ;
pthread_cond_t item_available ; // condition variable to show producer put a word in queu
pthread_cond_t space_available ; // condition variable to show consumer delete a word from queu
return 0;
}
void * p_thread()
{
FILE *fp = fopen("message.txt","r");
char c ;
c = getc(fp);
while (c != EOF) {
//1
sleep(1);
pthread_mutex_lock (&mutex) ;
//2
printf (" front<%d> rear<%d> items <%d>\n", front , rear , items ) ;
while ( items >= QUEU_SIZE ) // while there is no space in queu to write
pthread_cond_wait ( &space_available , &mutex ) ;
//3
printf (" front<%d> rear<%d> items <%d>\n", front , rear , items ) ;
pthread_mutex_unlock (&mutex) ;
sleep (1);
c = getc(fp);
}
pthread_mutex_lock (&mutex) ;
done = 0 ; // we should tell the consumer that the file is finished
pthread_cond_signal(&item_available);
pthread_mutex_unlock (&mutex) ;
fclose (fp);
void * c_thread()
{
FILE* output = fopen ("message_result.txt" , "w") ;
//4
while ( done != 0 ) { // while there is something to read
pthread_mutex_lock (&mutex) ;
//5
printf ("front<%d> rear<%d> items <%d>\n", front , rear , items ) ;
while ( items <= 0 && done!= 0 ) // while there is no character in queu to read
we should wait
pthread_cond_wait ( &item_available , &mutex ) ;
//6
printf ("front<%d> rear<%d> items <%d>\n", front , rear , items ) ;
pthread_mutex_unlock (&mutex) ;
}
fclose (output) ;
//return NULL;
pthread_exit (0);
}
3. Consider a system with: five processes, P0 P4, three resource types, A, B, and C. Type
A has 10 instances, B has 5 instances, C has 7 instances. At time T0 the following
snapshot of the system is taken.
Write a Linux C program to check weather system is in safe state or not. Also
demonstrate the unsafe sequence by modifying any resource allocation.
// Banker's Algorithm
#include <iostream>
using namespace std;
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 = 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;
}
}
}
}
return (0);
}
4. Write a Linux C program to demonstrate that deadlock and starvation are opposite
problems. Solution to deadlock problem cause more starvation and solution to the
starvation problem can cause deadlock.
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
//a change here
#define HOLDRIGHT 4
#define HOLDLEFT 3
#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];
state[phnum] = EATING;
sleep(2);
sem_post(&S[phnum]);
}
//a change here
else{
printf("Philosopher %d Cant Eat\n", phnum + 1);
state[phnum] = HUNGRY;
sleep(2);
sem_post(&S[phnum]);
}
}
if (state[phnum] == HUNGRY
&& state[LEFT] != HOLDRIGHT && state[LEFT] != EATING) {
// state that eating
state[phnum] = HOLDLEFT;
testright(phnum);
}
}
// take up chopsticks
void take_fork(int phnum)
{
sem_wait(&mutex);
sem_post(&mutex);
sleep(1);
}
sem_wait(&mutex);
testleft(LEFT);
testleft(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];
printf("*******Create starvation******\n");
sleep(1);
sem_init(&S[i], 0, 0);
pthread_join(thread_id[i], NULL);
}