OS Lab Practical File 073
OS Lab Practical File 073
UNIVERSITY
void lock_init()
{
flag[0]=flag[1]=0; turn = 0;
}
void lock(int self)
{
flag[self]=1; turn = 1-self;
while(flag[1-self]==1 && turn == 1-self);
}
void unlock(int self)
{
flag[self]=0;
}
void* func(void *s)
{
int i=0;
int *limitptr = (int*) s; int self =
*limitptr;
printf("Thread %d in queue for critical section\n",self); lock(self);
printf("Thread %d in critical section\n",self);
for(i=0;i<MAX;i++)
{
ans++;
}
printf("Thread %d done counting\n",self); printf("Thread %d is
exiting critical section\n",self); unlock(self);
}
int main()
{
pthread_t p1, p2; int a=0,b=1;
lock_init();
pthread_create(&p1, NULL, func, &a);
pthread_create(&p2, NULL, func, &b);
pthread_join(p1, NULL); pthread_join(p2, NULL);
printf("Exiting Main\n");
return 0;
}
Experiment : 3
#include<stdio.h>
#include<stdlib.h>
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x); x--;
mutex=signal(mutex);
}
Experiment : 4
temp=p[i]; p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0; for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time printf("\n\nAverage
Waiting Time=%d",avg_wt); printf("\nAverage Turnaround
Time=%d\n",avg_tat); return 0;
}
Experiment : 6
#include <stdio.h>
int main()
{
printf("\nEnter number of processes: "); scanf("%d",
&processes);
for (i = 0; i < processes; i++)
{
running[i] = 1; counter++;
}
printf("\nEnter number of resources: "); scanf("%d",
&resources); printf("\nEnter Claim Vector:");
for (i = 0; i < resources; i++)
{
scanf("%d", &maxres[i]);
}
printf("\nEnter Allocated Resource Table:\n"); for (i = 0; i
< processes; i++)
{
for(j = 0; j < resources; j++) scanf("%d",
¤t[i][j]);
}
printf("\nEnter Maximum Claim Table:\n"); for (i = 0; i
< processes; i++)
for(j = 0; j < resources; j++)
scanf("%d", &maximum_claim[i][j]);
printf("\nThe Claim Vector is: ");
for (i = 0; i < resources; i++)
{
printf("\t%d", maxres[i]);
}
printf("\nThe Allocated Resource Table:\n"); for (i = 0; i
< processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", current[i][j]);
}
printf("\n");
}
printf("\nThe Maximum Claim Table:\n"); for (i = 0; i
< processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", maximum_claim[i][j]);
}
printf("\n");
}
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
allocation[j] += current[i][j];
}
}
printf("\nAllocated resources:"); for (i = 0; i
< resources; i++)
{
printf("\t%d", allocation[i]);
}
for (i = 0; i < resources; i++)
{
available[i] = maxres[i] - allocation[i];
}
printf("\nAvailable resources:"); for (i = 0; i
< resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
while (counter != 0)
{
safe = 0;
for (i = 0; i < processes; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] > available[j])
{
exec = 0; break;
}
}
if (exec)
{
printf("\nProcess%d is executing\n", i + 1); running[i] =
0;
counter--; safe = 1;
for (j = 0; j < resources; j++)
{
available[j] += current[i][j];
}
break;
}
}
}
if (!safe)
{
printf("\nThe processes are in unsafe state.\n"); break;
}
else
{
printf("\nThe process is in safe state"); printf("\nAvailable
vector:");
for (i = 0; i < resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
}
}
return 0;
}
Experiment : 7
#include<stdio.h>
#include<stdio.h>
#include<math.h> int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp; temp1++;
}
else
{
queue2[temp2]=temp; temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
{
queue[i]=queue1[j];
queue[i]=max; queue[i+1]=0;
}
for(i=temp1+3,j=0;j<temp2;i++,j++)
{
queue[i]=queue2[j];
queue[0]=head;
}
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]); seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",
queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg); return 0;
}
Experiment : 9
}
else
{
}
printf(” The file is
allocated to disk\n");
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)"); scanf("%d",
&c);
if(c==1)
goto x; else
exit();
}
getch();
}
Experiment : 10
Objective : Write a program for MFT and MVT first fit and best fit .
Program : First Fit .