C Programm Files

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Prof. Dr.

SK DHAMEJA, HEAD, EDIC DEPARTMENT,


NITTER, SECTOR 26, CHANDIGARH-160 019

PRACTICAL FILE
ON
ADVANCED OPEREATING SYSTEM

SUBMITTED BY SUBMITTED TO
Faizul Raza Ass. Prof. Champion Mahipal

Roll No. 11 (Deptt. of Computer

M. Tech (CSE) 2nd Sem. Science & Application)

1
INDEX
SR PROGRAMS PAGE REMARKS
NO. NO.

1. WRITE A C PROGRAM TO IMPLEMENT FCFS 3-5


PROCESS SCHEDULING

2. WRITE A C PROGRAM TO IMPLEMENT 6-8


ROUND ROBIN PROCESS SCHEDULING

3. WRITE A C PROGRAM TO IMPLEMENT 9-11


SHORTEST JOB FIRST PROCESS
SCHEDULING

4. WRITE A C PROGRAM TO IMPLEMENT FIRST 12-14


FIT ALGORITHM OF MEMORY
MANAGEMENT

5. WRITE A C PROGRAM TO IMPLEMENT BEST 15-17


FIT ALGORITHM OF MEMORY
MANAGEMENT

6. WRITE A C PROGRAM TO IMPLEMENT FCFS 18-19


OF DISK SCHEDULING

7. WRITE A C PROGRAM TO IMPLEMENT 20-22


SHORTEST SEEK TIME FIRST OF DISK
SCHEDULING

8. WRITE A C PROGRAM TO IMPLEMENT 23-25


LAMPART LOGICAL CLOK

9. WRITE A C PROGRAM TO IMPLEMENT 26-30


DEADLOCK

10. WRITE A C PROGRAM TO IMPLEMENT 31-34


MUTUAL EXCLUSION

2
PRACTICAL 1

AIM: - WRITE A C PROGRAM TO IMPLEMENT FCFS PROCESS


SCHEDULING.

#include<stdio.h>
#include<conio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("\nEnter Process Burst Time\n");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}

wt[0]=0; //waiting time for first process is 0

//calculating waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculating turnaround time


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

3
getch();

return 0;

4
OUTPUT

5
PRACTICAL 2

AIM: - WRITE A C PROGRAM TO IMPLEMENT ROUND ROBIN


PROCESS SCHEDULING
.
#include<stdio.h>
#include<conio.h>
int main()
{

int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;

6
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
getch();
return 0;
}

7
OUTPUT

8
PRACTICAL 3

AIM: - WRITE A C PROGRAM TO SHORTEST JOB FIRST PROCESS


SCHEDULING
#include<stdio.h>
#include<conio.h>

void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}

//sorting burst time in ascending order using selection sort


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time


for(i=1;i<n;i++)
9
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)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=(float)total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
getch();
}

10
OUTPUT

11
PRACTICAL 4

AIM: - WRITE A C PROGRAM TO IMPLEMENT FIRST FIT


ALGORITHM OF MEMORY MANAGEMENT

#include<stdio.h>

Int main()
{
Static int block_arr[10], file_arr[10];
Int fragments[10] ,blocks[10], files[10];
Int m,n, number_of_blocks,number_of_files,temp;
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m=0;m<number_of_blocks;m++)
{
printf("Block No.[%d]:\t",m+1);
scanf("%d",&blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m=0;m<number_of_files;m++)
{
printf("File No.[%d]:\t",m+1);
scanf("%d",&files[m]);
}
for(m=0;m<number_of_files;m++)
{
for(n=0;n<number_of_blocks;n++)
{
if(block_arr[n]!=1)
{
temp=blocks[n]-files[m];
if(temp>=0)
{
file_arr[m]=n;
break;
}
}
}
fragments[m]=temp;
block_arr[file_arr[m]]=1;
}

12
printf("\nFile Number\tBlock Number\tFile Size\tBlock Size\tFragment");
for(m=0;m<number_of_files;m++)
{

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",m,file_arr[m],files[m],blocks[file_arr[m]],fragmen
ts[m]);
}
printf("\n");
return0;
}

13
OUTPUT

14
PRACTICAL 5

AIM: - WRITE A C PROGRAM TO IMPLEMENT BEST FIT


ALGORITHM OF MEMORY MANAGEMENT

#include<stdio.h>

void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];

printf("\n\t\t\tMemory Management Scheme - Best Fit");


printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);

printf("\nEnter the size of the blocks:-\n");


for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}

printf("\nEnter the size of the processes :-\n");


for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}

for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}

15
}
}

fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}

printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}

16
OUTPUT

17
PRACTICAL 6

AIM: - WRITE A C PROGRAM TO IMPLEMENT FCFS OF DISK


SCHEDULING
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
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",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
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;
}

18
OUTPUT

19
PRACTICAL 7

AIM: - WRITE A C PROGRAM TO IMPLEMENT SHORTEST SEEK


TIME FIRST OF DISK SCHEDULING

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
// clrscr();
printf("*** SSTF Disk Scheduling Algorithm ***\n");
printf("Enter the size of Queue\t");
scanf("%d",&n);
printf("Enter the Queue\t");
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position\t");
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(t[i]>t[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
seek=seek+abs(head-queue[i]);
head=queue[i];
}

20
printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
getch();
}

21
OUTPUT

22
PRACTICAL 8

AIM: - WRITE A C PROGRAM TO IMPLEMENT LAMPART LOGICAL


CLOK

#include<stdio.h>
#include<conio.h>
int max1(int a, int b) //to find the maximum timestamp between two events
{
if (a>b)
return a;
else
return b;
}

int main()
{
int i,j,k,p1[20],p2[20],e1,e2,dep[20][20];
printf("enter the events : ");
scanf("%d %d",&e1,&e2);
for(i=0;i<e1;i++)
p1[i]=i+1;
for(i=0;i<e2;i++)
p2[i]=i+1;
printf("enter the dependency matrix:\n");
printf("\t enter 1 if e1->e2 \n\t enter -1, if e2->e1 \n\t else enter 0 \n\n");
for(i=0;i<e2;i++)
printf("\te2%d",i+1);
for(i=0;i<e1;i++)
{
printf("\n e1%d \t",i+1);
for(j=0;j<e2;j++)
scanf("%d",&dep[i][j]);
}

for(i=0;i<e1;i++)
{
for(j=0;j<e2;j++)
{
if(dep[i][j]==1) //change the timestamp if dependency exist
{ p2[j]=max1(p2[j],p1[i]+1);
for(k=j;k<e2;k++)
p2[k+1]=p2[k]+1;
}
if(dep[i][j]==-1) //change the timestamp if dependency exist

23
{
p1[i]=max1(p1[i],p2[j]+1);
for(k=i;k<e1;k++)
p2[k+1]=p1[k]+1;
}

}
}
printf("P1 : "); //to print the outcome of Lamport Logical Clock
for(i=0;i<e1;i++)
{
printf("%d",p1[i]);
}
printf("\n P2 : ");
for(j=0;j<e2;j++)
printf("%d",p2[j]);

getch();
return 0 ;
}

24
OUTPUT

25
PRACTICAL 9

AIM: - WRITE A C PROGRAM TO IMPLEMENT IMPLEMENT


DEADLOCK

#include<stdio.h>
#include<conio.h>
int max[100][100];
intalloc[100][100];
int need[100][100];
int avail[100];
intn,r;
void input();
void show();
void cal();
int main()
{
inti,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
inti,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{

26
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
inti,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
inti,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{

27
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
//printf("\nP%d",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)

28
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}
}

29
OUTPUT

30
PRACTICAL 10

AIM: - WRITE A C PROGRAM TO IMPLEMENT MUTUAL EXCLUSION

#include <stdio.h>
#include <pthread.h>
#include"mythreads.h"
int flag[2];
int turn;
const int MAX = 1e9;
int ans = 0;
void lock_init()
{
// Initialize lock by
reseting the desire of
// both the threads to
acquire the locks.
// And, giving turn to
one of them.
flag[0] = flag[1] = 0;
turn = 0;
}
// Executed before
entering critical section
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 self = (int *)s;
printf("Thread Entered
: %d\n", self);
lock(self);
for (i=0; i<MAX; i++)
ans++;
unlock(self);
}

31
// Driver code
int main()
{
// Initialized the
lock then fork 2 threads
pthread_t p1, p2;
lock_init();
// Create two threads
(both run func)
pthread_create(&p1,NULL, func, (void*)0);
pthread_create(&p2,NULL, func, (void*)1);
// Wait for the
threads to end.
pthread_join(p1, NULL);
pthread_join(p2, NULL);
printf("Actual Count:%d | Expected Count: %d\n", ans, MAX*2);
return 0;
}
// mythread.h (A wrapper
header file with assert
// statements)
#ifndef _MYTHREADS_h_
#define _MYTHREADS_h_
#include <pthread.h>
#include <assert.h>
#include <sched.h>
void Pthread_mutex_lock(pthread_mutex_t *m)
{
int rc = pthread_mutex
_lock(m);
assert(rc == 0);
}
void Pthread_mutex_unlock(
pthread_mutex_t *m)
{
int rc = pthread_mutex
_unlock(m);
assert(rc == 0);
}
void Pthread_create(
pthread_t *thread, const
pthread_attr_t *attr,
void (
start_routine)(void*),
void *arg)
{

32
int rc = pthread_
create(thread, attr, start
_routine, arg);
assert(rc == 0);
}
void Pthread_join(pthread_
t thread, void **value_ptr
)
{
int rc = pthread_join(
thread, value_ptr);
assert(rc == 0);
}

33
OUTPUT

34

You might also like