Professional Documents
Culture Documents
Vinay Os
Vinay Os
#include<stdio.h>
wt[0] = 0;
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
printf(" %d ",(i+1));
printf(" %d",wt[i]);
printf(" %d\n",tat[i]);
printf("\n");
// Driver code
int main()
//process id's
findavgTime(processes, n, burst_time);
return 0;
Output:-
1 30 0 30
2 6 30 36
3 8 36 44
2.
#include <stdio.h>
int main()
// Matrix for storing Process Id, Burst Time, Average Waiting Time & Average Turn Around
Time.
int A[100][4];
scanf("%d", &n);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
index = i;
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
A[0][2] = 0;
A[i][2] = 0;
A[i][2] += A[j][1];
total += A[i][2];
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
avg_tat = (float)total / n;
Output
P1: 2
P2: 3
P3: 7
P4: 9
P5: 2
P BT WT TAT
P1 2 0 2
P5 2 2 4
P2 3 4 7
P3 7 7 14
P4 9 14 23
3.
#include <stdio.h>
int main()
// Matrix for storing Process Id, Burst Time, Average Waiting Time & Average Turn Around
Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
scanf("%d", &n);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
index = i;
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
A[0][2] = 0;
A[i][2] = 0;
A[i][2] += A[j][1];
total += A[i][2];
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
total += A[i][3];
avg_tat = (float)total / n;
Output
P1: 2
P2: 464
P3:
P BT WT TAT
P1 2 0 2
P2 4 2 6
P3 46 6 52
4.
int main()
//Input no of processed
int n;
scanf("%d", &n);
int x = n;
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
int time_slot;
scanf("%d", &time_slot);
for(total=0, i = 0; x!=0; )
temp_burst_time[i] = 0;
counter=1;
total += time_slot;
wait_time = wait_time+total-arr_time[i]-burst_time[i];
counter =0;
if(i==n-1)
i=0;
else if(arr_time[i+1]<=total)
i++;
else
i=0;
}
return 0;
5.
#include<stdio.h>
struct priority_scheduling {
char process_name;
int burst_time;
int waiting_time;
int turn_around_time;
// priority of the process
int priority;
};
int main() {
int number_of_process;
int total = 0;
// swapping position
int position;
float average_waiting_time;
float average_turnaround_time;
ASCII_number++;
position = i;
position = j;
temp_process = process[i];
process[i] = process[position];
process[position] = temp_process;
// First process will not have to wait and hence has a waiting time of 0
process[0].waiting_time = 0;
process[i].waiting_time = 0;
process[i].waiting_time += process[j].burst_time;
total += process[i].waiting_time;
total = 0;
printf("------------------------------------------------------------\n");
for (int i = 0; i < number_of_process; i++) {
total += process[i].turn_around_time;
printf("\n-----------------------------------------------------------\n");
return 0;
7.
#include <stdio.h>
#include <stdlib.h>
// Initialize a mutex to 1
int mutex = 1;
int full = 0;
void producer()
--mutex;
++full;
--empty;
// Item produced
x++;
++mutex;
void consumer()
{
--mutex;
--full;
++empty;
x--;
++mutex;
// Driver Code
int main()
int n, i;
printf("\n1. Press 1 for Producer" "\n2. Press 2 for Consumer" "\n3. Press 3 for Exit");
// Using '#pragma omp parallel for' can give wrong value due to synchronization issues.
// 'critical' specifies that code is executed by only one thread at a time i.e., only one thread enters
the critical section at a given time
{
printf("\nEnter your choice:");
scanf("%d", &n);
// Switch Cases
switch (n)
case 1:
producer();
else
printf("Buffer is full!");
break;
case 2:
consumer();
printf("Buffer is empty!");
break;
// Exit Condition
case 3:
exit(0);
break;
8.
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };
sem_t mutex;
sem_t S[N];
void test(int phnum)
if (state[phnum] == HUNGRY
state[phnum] = EATING;
sleep(2);
// during takefork
// during putfork
sem_post(&S[phnum]);
// take up chopsticks
sem_wait(&mutex);
state[phnum] = HUNGRY;
printf("Philosopher %d is Hungry\n", phnum + 1);
test(phnum);
sem_post(&mutex);
sem_wait(&S[phnum]);
sleep(1);
sem_wait(&mutex);
state[phnum] = THINKING;
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(&mutex, 0, 1);
sem_init(&S[i], 0, 0);
for (i = 0; i < N; i++) {
pthread_create(&thread_id[i], NULL,
philosopher, &phil[i]);
pthread_join(thread_id[i], NULL);
9.
/* Deadlock Avoidance*/
#include<stdio.h>
int main()
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
scanf("%d",&n);
scanf("%d",&r);
scanf("%d",&totext[k]);
scanf("%d",&resalloc[i][k]);
scanf("%d",&p);
scanf("%d",&newreq[k]);
if(i!=p)
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
block[p]=0;
run[p]=0;
j=0;
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
if(totext[k]-totalloc[k]<0)
u=1;
break;
if(u==0)
simalloc[k]=totalloc[k];
if(active[i]==1)
j=0;
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;
break;
if(j==0)
active[i]=0;
simalloc[k]=resalloc[i][k];
m=0;
resreq[p][k]=newreq[k];
else
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
return 0;
10.
/* deadlock Prevention*/
#include<stdio.h>
#include<conio.h>
void main()
int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15];
int pno,rno,i,j,prc,count,t,total;
count=0;
//clrscr();
scanf("%d",&pno);
scanf("%d",&rno);
for(i=1;i<=pno;i++)
flag[i]=0;
for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
for(i=1;i<= pno;i++)
for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}
printf("\n Enter allocated resources for each process:");
for(i=1;i<= pno;i++)
for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);
for(j=1;j<= rno;j++)
avail[j]=0;
total=0;
for(i=1;i<= pno;i++)
total+=allocated[i][j];
avail[j]=tres[j]-total;
work[j]=avail[j];
printf(" %d \t",work[j]);
do
for(i=1;i<= pno;i++)
for(j=1;j<= rno;j++)
need[i][j]=max[i][j]-allocated[i][j];
}
printf("\n Allocated matrix Max need");
for(i=1;i<= pno;i++)
printf("\n");
for(j=1;j<= rno;j++)
printf("%4d",allocated[i][j]);
printf("|");
for(j=1;j<= rno;j++)
printf("%4d",max[i][j]);
printf("|");
for(j=1;j<= rno;j++)
printf("%4d",need[i][j]);
prc=0;
for(i=1;i<= pno;i++)
if(flag[i]==0)
prc=i;
for(j=1;j<= rno;j++)
if(work[j]< need[i][j])
{
prc=0;
break;
if(prc!=0)
break;
if(prc!=0)
count++;
for(j=1;j<= rno;j++)
work[j]+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(" %d",work[j]);
while(count!=pno&&prc!=0);
if(count==pno)
else
11.
/FIFO/
#include<stdio.h>
int main()
int i,j,n,a[50],frame[10],no,k,avail,count=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
for(i=1;i<=n;i++)
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
printf("\n");
return 0;
12.
/FIFO/
#include<stdio.h>
int main()
int i,j,n,a[50],frame[10],no,k,avail,count=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
for(i=1;i<=n;i++)
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
printf("\n");
return 0;
13.
#include<stdio.h>
int frames[n];
frames[i]=-1;
int index=-1;
//Counters
int page_miss=0;
int page_hits=0;
int full=0;
int symbol=string[i];
int flag=0;
for(int j=0;j<n;j++)
if (symbol==frames[j])
flag=1;
break;
if (flag==1)
printf("%d ",frames[j]);
page_hits+=1;
else
if (full==0)
{
index=(index+1)%n;
frames[index]=symbol;
page_miss+=1;
printf("%d ",frames[j]);
if (i==n-1)
full=1;
else
int pos=-1;
int index=-1;
for(int j=0;j<n;j++)
int found=0;
if (frames[j]==string[k])
found=1;
if (pos<k)
pos=k;
index=j;
break;
if (found==0)
pos=size;
index=j;
frames[index]=symbol;
printf("%d ",frames[j]);
//Main function
int main(void)
{
int no_frames=3;
int size=sizeof(string)/sizeof(int);
optimal(string,no_frames,size);
return 0;