operating system programs

25
1. Program to implement FIRST COME FIRST SERVE SCHEDULING ALGORITHM and hence calculate average waiting time as well as turnaround time for each process. #include<stdio.h> #include<conio.h> #define size 10 void main() { int a[size], b[size], t[size], avg[size], i , n, time; float avg_wt, sum = 0.0, avg_ta, ta =0.0; a[0]=0; clrscr(); printf(“Enter the no. of processes\t”); scanf(“%d”, &n); printf(“Enter the arrival and burst time for corresponding processes\n”); for(i=1;i<=n;i++) { printf(“\n For process %d\n arrival time = \t”,i); scanf(“%d”, &a[i]); while(a[i]<a[i-1]) { printf(“\n The arrival time should be ascending order.”); printf(“\n Enter the arrival time for %d again\t, i); scanf(“%d”, &a[i]); } printf(“burst time= \t”); scanf(“%d”,&b[i]); } printf(“\n\n FIRST COME FIRST SERVE TECHNIQUE \n\n”); avg[0] = time = 0; t[0] = 0; b[0] = 0; for(i = 1; i<=n ; i++) { if(time <= a[i]) time = a[i] 1

Upload: close2dwivedi

Post on 06-Apr-2015

81 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Operating System Programs

1. Program to implement FIRST COME FIRST SERVE SCHEDULING ALGORITHM and hence calculate average waiting time as well as turnaround time for each process.

#include<stdio.h>#include<conio.h>#define size 10void main(){

int a[size], b[size], t[size], avg[size], i , n, time;float avg_wt, sum = 0.0, avg_ta, ta =0.0;a[0]=0;clrscr();printf(“Enter the no. of processes\t”);scanf(“%d”, &n);printf(“Enter the arrival and burst time for corresponding processes\n”);for(i=1;i<=n;i++){

printf(“\n For process %d\n arrival time = \t”,i);scanf(“%d”, &a[i]);while(a[i]<a[i-1]){

printf(“\n The arrival time should be ascending order.”);printf(“\n Enter the arrival time for %d again\t, i);scanf(“%d”, &a[i]);

}printf(“burst time= \t”);scanf(“%d”,&b[i]);

}printf(“\n\n FIRST COME FIRST SERVE TECHNIQUE \n\n”);avg[0] = time = 0;t[0] = 0;b[0] = 0;for(i = 1; i<=n ; i++){

if(time <= a[i])time = a[i]

avg[i] = time_a[i];t[i] = avg[i] + b[i];time += b[i];

}for(i = 1; i<n; i++) {

sum = sum + avg[i];ta = ta + t[i];

}

1

Page 2: Operating System Programs

avg_wt = sum/n;avg_ta = ta/n;printf(“Process \t Arrival time \t Burst time \t Waiting time \t Turnaround time \n”);for(i = 1; i<=n; i++)printf(“\n %d\t %d\t %d\t %d\t %d”, i, a[i],b[i],avg[i],t[i]);printf(“\n Average waiting time : %f”, avg_wt);printf(“\n Average waiting time : %f”, avg_ta);getch();

}

OUTPUT:-

Enter the no. of process : 4Enter the arrival and burst time for corresponding process

For process 1arrival time = 0burst time = 8

For process 2arrival time = 4burst time = 4

For process 3arrival time = 5burst time = 9

For process 4arrival time = 7burst time = 5

FIRST COME FIRST SERVE TECHNIQUE

Process Arrival time Burst time Waiting time Turnaround time1 0 8 0 82 4 4 4 83 5 9 7 164 7 5 14 19

Average waiting time = 2.750000Average waiting time = 8.000000

2

Page 3: Operating System Programs

2. Program to implement SHORTEST JOB FIRST SCHEDULING ALGORITHM (NON PREEMPTIVE) and hence calculate average waiting time as well as turn-around time for each process.

#include<stdio.h>#include<conio.h>#define size 10void main(){

int flag[size], b[size], i, j, n, smallest;int turn[size], index, wait[size], tat[size];float sum=00.0, ta=0.0, avg_wt, avg_ta;clrscr();printf(“Enter the burst time for corresponding processes\t”);scanf(“%d”,&n);printf(“Enter he burst time for corresponding processes\n”);for(i=1;i<=n;i++){

printf(“\nFor process %d\nburst time = \t”);scanf(“%d”,&b[i]);flag[i]=1;

}printf(“\n\nSHORTEST TIME FIRST\n\n);smallest=b[1];index=1;for(j=1;j<=n;j++){

for(i=01;i<=n;i++){

if((b[i]<smallest)&&flag[i]){

smallest=b[i];index=I;

}}flag[index]=0;turn[j]=indez;smallest=1000;

}wait[turn[1]]=0;tat[turn[1]]=b[turn[1]];for(i=2;i<=n;i++){

wait[turn[i]]=wait[turn[i-1]] + b[turn[i-1]];tat[turn[i]]=wait[turn[i]] + b[turn[i]];

3

Page 4: Operating System Programs

for(i=1;i<=n;i++){

sum=sum+wait[i];ta=ta+tat[i];

}avg_wt=sum/n;avg_ta=ta/n;printf("Process\t\tBurst Time\t\tAvg. Waiting Time\t\tTurn Around Time\n\n");for(i=1;i<=n;i++)

printf(" %d\t\t %d\t\t %d\t\t %d\n\n", i, b[i], wait[i], tat[i]);printf("Average waiting time: %2f", avg_wt);printf("\nAverage Turn Around Time: %2f" avg_tat);

}getch();

}

OUTPUT:-

Enter the no. of processes: 5Enter the burst time for corresponding processes

For process 1Burst Time = 10

For process 2Burst Time = 1

For process 3Burst Time=2

For process 4Burst Time = 4

For process 5Burst Time = 5

Process Burst Time Avg. Waiting Time Turn Around Time

1 10 9 192 1 0 13 2 2 44 1 1 25 5 4 9

4

Page 5: Operating System Programs

3. Program to implement SHORTEST JOB FIRST SCHEDULING (PRE-EMPTIVE) algorithm, and hence calculate average waiting time as well as turn-around time for each process

#include<stdio.h>#include<conio.h>void main(){

int a[20], i, n, j, w[20], ta[20], ar[20];int temp;float avg=0;clrscr();printf("Enter the total number of processes");scanf("%d",&n);printf("Enter the burst time and arrival time of the processes");for(i=1;i<=n:i++)scanf("%d%d", &a[i], &ar[i]);for(i=1;i<=n;i++){

for(j=0;j<n-i;j++){

if(ar[j+1] < ar[j]){

temp=ar[j];ar[j]=ar[j+1];ar[j+1]=temp;

}}

}k=0;i=0;for(i=0;i<n;i++){

if(ar[i]==k){

b[1]=i;i++;

}}for(i=0;i<n;i++)

printf("%d\n",ar[i]);w[0]=0;for(i=0;i<n;i++){

w[i+1]=w[i] + a[i];}

5

Page 6: Operating System Programs

printf("\nWaiting Time:");for(i=0;i<n;i++)

avg=avg + w[i];printf("\navg=%f", avg);printf("\nTurn Around Time is:\n");for(i=0;i<n;i++)

ta[i]==a[i] + w[i];avg=0;for(i=0;i<n;i++){

printf("%d\n", ta[i]);avg=avg + ta[i];

}printf("Average turnaround time is:");printf("%2f",avg/n);

}

6

Page 7: Operating System Programs

4. Program to Implement ROUND ROBIN SCHEDULING ALGORITHM and calculate average waiting time as well as turnaround time for each process.

#include<stdio.h>#include<conio.h>void main() {

int et[10],wt[10],timer,count,pt[10],rt,i,j,totwt=0,t,n,found=0,m;float avgwt;printf("\nEnter the no. of processes: ");scanf("%d",&n);printf("\nEnter the time slice : ");scanf("%d",&timer);for(i=0;i<n;i++){

printf("\nEnter the processing time for process %d :",i+1);scanf("%d",&pt[i]);

}m=n;wt[0]=0;i=0;do{

if(pt[i]>timer){

rt=pt[i]-timer;pt[n]=rt;et[i]=timer;n++;

}else{

et[i]=pt[i];}i++;wt[i]=wt[i-1]+et[i-1];

}while(i<n);count=0;for(i=0;i<m;i++){

for(j=i+1;j<=n;j++){

if(i==j){

count++;found=j;

}}if(found!=0){

wt[i]=wt[found]-(count*timer);count=0;

7

Page 8: Operating System Programs

found=0;}

}for(i=0;i<m;i++){

totwt+=wt[i];}avgwt=(float)totwt/m;for(i=0;i<m;i++){

printf("\nProcess %d\t%d\t%d",i+1,pt[i],wt[i]);}printf("\nTotal waiting time %d\n",totwt);printf("\nTotal avgtime %f",avgwt);getch();

}

OUTPUT:-

Enter the no. of processes: 3

Enter the time quantum: 10

Enter the processing time for process 1 : 24

Enter the processing time for process 2 : 3

Enter the processing time for process 3 : 3

Process 1 24 0

Process 2 3 10

Process 3 3 13

Total Waiting Time 23

Total avg time 7.666667

8

Page 9: Operating System Programs

5. Program to implement PRIORITY SCHEDULING ALGORITHM and hence calculate average waiting time as well turn around time for each process.

#include<stdio.h>#include<conio.h>void main(){

int flag[size], b[size], I, j,n, smallest, turn[size], index, wait[size], tat[size], p[size];float sum=0.0, ta=0.0, avg_wt, avg_ta;clrscr();printf(“Enter the no. of processes \t”);scanf(“%d”, &n);printf(“Enter the burst time and priority for corresponding processes\n”);printf(“\n\n\t\t Priority should be in the range of 0 to 10 with 0 highest\n”);for(i=1;i<=n;i++){

printf(“\n\nFor process %d\nburst time=\t”, i);scanf(“%d”, &b[i]);printf(“Priority=\t”);scanf(“%d”, &p[i]);flag[i]=1;

}printf(“\n\n PRIORITY SCHEDULING\n\n ”);smallest=p[1];index=1:for(j=1;j<=n;j++){

for(i=1;i<=n;i++){

if((p[i]<smallest) && flag[i]){

smallest=p[i];index=i:

}}flag[index]=0;turn[j]=index;smallest=1000;

}wait[turn[1]]=0;tat[turn[1]]=b[turn[1]];for (i=2;i<=n:i++){

wait[turn[i]]=wait[turn[i-1]] + b[turn[i-1]];tat[turn[i]]=wait[turn[i]] + b[turn[i]];

}

9

Page 10: Operating System Programs

for(i=1;i<=n;i++){

sum=sum+ wait[i];ta=ta+tat[i];

}avg_wt=sum/n;avg_ta=ta/n;printf(“ Process \t\t Burst time\t Priority\t Wt time\t turn Around Time\n\n”);for (i=1;i<=n;i++)

printf(“%d\t\t%d\t\t%d\t\t%d\t\t%d\n\n”, I, b[i], p[i], wait[i],tat[i]);printf(“\n Average waiting time is : %2 f, avg_wt”);printf(“\n Average turn around time is : %2 f, avg_ta”);getch();

}

OUTPUT:-

Enter the no. of processes: 3Enter the burst time and priority for corresponding processes :Priority should be in the range of 0 to 10 with 0 highest

For process 1Burst Time= 2

Priority= 3

For process 2Burst Time= 10Priority= 1

For process 3Burst Time=6 Priority= 5

PRIORITY SCHEDULING

Process Burst Time Priority WT Time Turn Around Time

1 2 3 10 12

2 10 1 0 10

3 6 5 12 18

Average waiting time is: 7.333333

Average turn around time is: 13.333333

10

Page 11: Operating System Programs

6. Write a program to implement NEED MATRIX.

#include<stdio.h>#include<conio.h>void main(){

int i,n,A[10],B[10],C[10], Am[10],Bm[10],Cm[10], An[10],Bn[10],Cn[10],Aa,Ba,Ca, seq[10],count,c ;

clrscr();printf(“\nEnter the number of processes: ”);scanf(“%d”,&n);printf(“\nEnter the allocated resources for A,B & C: ”);for (i=0;i<n;i++){

printf(“\nP%d: ”,i);scanf(“%d%d%d”, &A[i],&B[i],&C[i]);seq[i]=-1;

}printf(“\nEnter the maximum required resources: ”);for (i=0;i<n;i++){

printf(“\nP%d: ”,i);scanf(“%d%d%d”, &Am[i],&Bm[i],&Cm[i]);

}printf(“\nEnter the available resources: ”);scanf(“%d%d%d”, &Aa,&Ba,&Ca);for (i=0;i<n;i++){

An[i]=Am[i]-A[i];Bn[i]=Bm[i]-B[i];Cn[i]=Cm[i]-C[i];

}count=0; i=0; j=0; c=0;while(count<n){

c=0:while (c<=j){

if(seq[c]==i)i++;c++;

}if((An[i]<=Aa) && (Bn[i]<=Ba) && (Cn[i]<=Ca)){

11

Page 12: Operating System Programs

count++;seq[j]=I;j++;Aa+=A[i];Ba+=B[i];Ca+=c[i];

}i++;if(i==n)i=0;

}//Printing Beginsprintf(“\nThe need matrix is: ”);for (i=0;i<n;i++){

printf(“\n”);printf(“\n%d%d%d”, An[i],Bn[i],Cn[i]);

}printf(“\nThe sequence of execution is: “);for(i=0;i<n;i++){

printf(“%d”, seq[i]);}getch();

}

OUTPUT:-

Enter the number of processes: 5Enter the allocated resources for A, B &C: 0 1 02 0 03 0 22 1 10 0 2Enter the max required resources: 7 5 33 2 29 0 22 2 24 3 3Enter the available resources: 3 3 27 4 31 2 26 0 00 1 14 3 1The sequence of execution is: 134

12

Page 13: Operating System Programs

0

7. Program to implement MUTUAL EXCLUSION.

#include <stdio.h>#include<conio.h>int criticalsection(int, int);int flagp1=0,flagp2=0;void main(){

int p;clrscr();printf(“Name process to execute first enter 1 or 2 as process name”);scanf(“%d”,&p);if(p==1){

flagp1=1;flag p1=criticalsection(p,flagp1);

}else{

flagp2=1;flagp2=criticalsection(p,flagp2);

}getch();

}

int criticalsection(int p,int flag);{

if(p==1&&flag==1){

printf("\n Process 1 is going on and process 2 is waiting");flag=0;return flag;

}else{

printf(\n process 2 is going on and process 1 is waiting);flag=0;return flag;

}}

OUTPUT:-

Name process to execute first enter 1 or 2 as process name: 2

13

Page 14: Operating System Programs

Process 2 is going on and process 1 is waiting8. Program to implement BANKER'S ALGORITHM.

#include<stdio.h>#include<conio.h>void main(){

int i,n,A[10],B[10],c[10],Am[10],Bm[10],Cm[10],An[10],Bn[10],Cn[10],Aa,Ba,Ca, seq[10],count,j;clrscr();printf("\n Enter the number of processes : " );scanf("%d",&n);printf(" \n Enter the allocated resources for A,B and C: ");for(i=0;i<n;i++){

printf(" \nP%d: ",i);scanf("%d%d%d",&a[i],&b[i],&c[i]);seq[i]=-1;

}printf("\n\n Enter the maximum required resources: ");for(i=0;i<n;i++){

printf("\nP%d :",i);scanf("%d%d%d",&Am[i],&Bm[i],&Cm[i]);

}printf("Enter the available process :");scanf("%d%d%d",&Aa,&Ba,&Ca);for(i=0;i<n;i++){

An[i]=Am[i]-A[i];Bn[i]=Bm[i]-B[i];Cn[i]=Cm[i]-C[i];

}count=0;i=0,j=0,c=0;while(count){

c=0;while(c<=j){

if(seq[c]==1)i++ ;

c++ ;}if((An[i]<=Aa)&&(Bn[i]<=Ba)&&(Cn[i]<=Ca)){

count ;seq[j]=i;j++ ;Aa =A[i];Ba =B[i];Ca =C[i];

}

14

Page 15: Operating System Programs

i++ ;if(i==n)i=0;

}printf("\n The Matrix is: ");for(i=0;i<n;i++){

printf(" \n ");printf("%d%d%d",&An[i],&Bn[i],&Cn[i]);

}printf(" \n The sequence of execution is: ");for(i=0;i<n;i++){

printf("%d",seq[i]);}getch();

}

OUTPUT:-

Enter the number of processes: 5Enter the allocated resources for A,B and C: 0 1 02 0 03 0 22 1 1 0 0 2Enter the max required resources: 7 5 33 2 2 9 0 22 2 24 3 3 Enter the available resources: 3 2 2The need matrix is:7 4 3 1 2 2 6 0 00 1 14 3 1

The sequence of execution is: 13 402

15

Page 16: Operating System Programs

9. Write a program to implement READER WRITER'S PROBLEM.

#include<pthread.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>#define MAXCOUNT 5#define READER1 50000#define READER2 100000#define READER3 800000#define READER4 150000type def strcut {

pthread_mutex_t *mut;int writers;int readers;int waiting;pthread_cond_t *writeOK,*readOK;} rwl;

rwl *initlock(void);void readlock(rwl *lock,int d);void writelock(rwl *lock,int d);void readunlock(rwl *lock);void writeunlock(rwl *lock);void deletelock(rwl *lock);type def struct {

rwl *lock;int id;long delay;} rwargs;

rwargs *newRWargs(rwl *l,int i,long d);void *reader(void *args);void *writer(void *args);static int data=1;int main(){

pthread_t r1,r2,r3,r4,w1;rwargs *a1,*a2,*a3, *a4, *a5;rwl *lock;lock=initlock();a1=newRWargs(lock,1,WRITER 1);pthread_create(&w1,NULL,writer,a1);a2=newRWargs(lock 1,READER 1);pthread_create(&rl,NULL,reader,a2);a3=newRWargs(lock,2,READER 2);

16

Page 17: Operating System Programs

pthread_create(&r2,NULL,reader,a3);a4=newRWargs(lock,3,READER3);pthread_create(&r3,NULL,reader,a4);a5=newRWargs(lock,4,READER 4);pthread_create(&r4,NULL,reader,a5);pthread_join(w1,NULL);pthread_join(r1,NULL);pthread_join(r2,NULL);pthread_join(r3,NULL);pthread_join(r4,NULL);free(a1); free(a2); free(a3); free(a4); free(a5);return 0;

}rwargs *newRWargs(rwl *l,int i,long d){

rwargs *args;args=(rwargs*)malloc(sizeof (rwargs));if(args==NULL) return (NULL);args->lock=l; args->id=i; args->delay=d;return(args);

}void *reader(void *args){

rwargs *a;int d;a=(rwargs*)args;do{

readlock(a->lock,a->id);d=data;usleep(a->delay);readunclock(a->lock);printf("Reader %d : Data=%d \n",a->id,d);usleep(a->delay);

} while(d!=0)printf("Reader %d:Finished.\n",a->id);return (NULL);

}void *writer(void *args){

rwargs *a;int i;a=(rwargs*)args;for(i=2;i writelock(a->,a->id);{

17

Page 18: Operating System Programs

data=i;usleep(a->delay);writeunlock(a->lock);printf("Writer %d:Wrote %d \n",a->id,i);usleep(a->delay);

}printf("Writer %d:Finishing..\n",a->id);writelock(a->lock,a->id);data=0;writeunlock(a->lock);printf("Writer %d:Finished.\n",a->id);return(NULL);

}rwl *initlock(void){

rwl *lock;lock=(rwl *)malloc(sizeof (rwl));if(lock==NULL) return(NULL);

lock->mut=(pthread_mutex_t*)malloc(sizeof (pthread_mutex_t));if(lock->mut==NULL) { free(lock); return(NULL);}

lock->writeOK=(pthread_cond_t *)malloc(sizeof (pthread_cond_t));if(lock->writeOK==NULL) { free(lock->mut); free(lock);

return(NULL); }lock->readOK=(pthread_cond_t *)malloc(sizeof (pthread_cond_t));if(lock->writeOK==NULL) { free(lock->mut); free(lock->writeOK);

free(lock); return(NULL); }pthread_mutex_init(lock->mut,NULL);pthread_cond_init(lock->writeOK,NULL);pthread_cond_init(lock->readOK,NULL);lock->readers=0;lock->writers=0;lock->waiting=0;return(lock);

}void readlock(rwl *lock,int d){

pthread_mutex_lock(lock->mut);if(lock->writers || lock->waiting) {

do {

printf("reader %d blocked.\n",d);pthread_cond_wait(lock->readOK,lock->mut);printf("reader %d unblocked.\n,d);

}while(lock->writers);

18

Page 19: Operating System Programs

}lock->readers ;pthread_mutex_unlock(lock->mut);return;

}void writelock(rwl *lock,int d){

pthread_mutex_lock(lock->mut);lock->waiting ;while(lock->readers || lock->writers) {

printf(" writer %d blocked.\n",d);pthread_cond_wait(lock->writeOK,lock->mut);printf("writer %d unblocked.\n",d);

}lock->waiting--;lock->writers ;pthread_mutex_unblock(lock->mut);return;

}

void readunlock(rwl *lock){

pthread_mutex_lock(lock->mut);lock->readers--;pthread_cond_signal(lock->writeOK);pthread_mutex_unlock(lock->mut);

}void writeunlock(rwl *lock){

phread_mutex_lock(lock->mut)lock->writers--;pthread_cond_broadcast(lock->readOK);pthread_mutex_unlock(lock->mut);

}void deletelock(rwl *lock){

pthread_mutex_destroy(lock->mut);pthread_cond_destory(lock->readOK);pthread_cond_destroy(lock->writerOK);free(lock);return;

}

19

Page 20: Operating System Programs

10. Program to implement FIFO PAGING ALGORITHM and find out the number of page faults for any given series of memory references.

#include<stdio.h>#include<conio.h>#define size 20void main(){

int i,j,string[size],pn,page[size],k,fault=0,t;clrscr();printf("How many references to make\t");scanf("%d",&n);printf("\nEnter the string \n\n");for(i=0;i<n;i++){

printf("reference %d : \t",i )scanf("%d",&string[i]);

}printf("\n Size of the page\t");scanf("%d",&t);printf("No. of frames\t");scanf("%d",&pn);j=1;k=1;for(i=0;i page[i]=1;){

for(i=0;i<n;i++){

for(j=1;j<n;j++){

if(string[i]\t==page[j])break;

}if(j=pn 1){

fault ;if(k<=pn)

page[k ]=(string[i]\t);else{

k=k%pn;page[k ]=(string[i]\t);

}}

20

Page 21: Operating System Programs

}}printf("The number of pagefaults=%d",faults);getch();

}

OUTPUT:-

How many references to make: 12

Enter the stringReference 1: 1Reference 2: 2Reference 3: 3Reference 4: 4 Reference 5: 1Reference 6: 2Reference 7: 5Reference 8: 1Reference 9: 2Reference 10: 3Reference 11: 4Reference 12: 5

Size of the page 1No of frames 3The no of page faults=9

21

Page 22: Operating System Programs

22