73
FIRST COME FIRST SERVE Ex. No. : Date : AIM: To implement FCFS CPU scheduling algorithm with different arrival time. ALGORITHM: 1. Start the program. 2. Get the number of process, process name, and burst time of each process. 3. Initially set the waiting time and Average turnaround time depends upon the burst time. 4. Calculate the Average waiting time and Average turnaround time depends upon the burst time. 5. Total turnaround time and total waiting time is calculated using Total turnaround time = turnaround time of all processes. Total waiting time = waiting time of all processes. 6. Average turnaround time and waiting time is

Os Programs

  • Upload
    rijine

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Os Programs

FIRST COME FIRST SERVE

Ex. No. :

Date :

AIM:

To implement FCFS CPU scheduling algorithm with different arrival time.

ALGORITHM:

1. Start the program.

2. Get the number of process, process name, and burst time of each process.

3. Initially set the waiting time and Average turnaround time depends upon the burst

time.

4. Calculate the Average waiting time and Average turnaround time depends upon

the burst time.

5. Total turnaround time and total waiting time is calculated using

Total turnaround time = ∑ turnaround time of all processes.

Total waiting time = ∑ waiting time of all processes.

6. Average turnaround time and waiting time is

Total waiting time

Average waiting time = -------------------------

Number of process

Total turnaround time

Average turnaround time= -------------------------

Number of process

7. Display the process id, process name, waiting time, average waiting time,

turnaround time and average turnaround time for each process.

Page 2: Os Programs

FIRST COME FIRST SERVE

#include<stdio.h>#include<conio.h>main(){int i,np,wt[15],tat[15],rt[15];float aw,at;clrscr();printf("Enter the No.of Process:");scanf("%d",&np);for(i=0;i<np;i++){printf("\nProcess%d:",i+1);scanf("%d",&rt[i]);}for(i=0;i<np;i++){if(i==0){wt[0]=0;tat[0]=rt[0]+wt[0];aw=wt[0];at=tat[0];}else{wt[i]=tat[i-1];tat[i]=rt[i]+wt[i];aw=aw+wt[i];at=at+tat[i];}aw/=np;at/=np;}printf("\nprocessID\tCPUBurstTime\tWaitingtime\tTrunAroundTime\n\n\n");for(i=0;i<np;i++)printf("\n%d\t\t%d\t\t%d\t\t%d\n",i+1,rt[i],wt[i],tat[i]);printf("\n\n\t\tAverage:\t%f\t%f",aw,at);getch();}

Page 3: Os Programs

RESULT

Enter the No.of Process:2

Process1:4

Process2:2

processID CPUBurstTime Waitingtime TrunAroundTime

1 4 0 4

2 2 4 6

Average: 2.000000 4.000000

Page 4: Os Programs

ROUND ROBIN SCHEDULING

Ex. No. :

Date :

AIM:

To implement round Robin CPU scheduling algorithm with different arrival time.

ALGORITHM:

1. Start the program.

2. Get the number of process, process name, burst time and arrival time of each

process.

3. Initially sort the process according to their arrivals times.

4. Insert the sorted processes into the queue.

5. If the burst time of first process is less than or equal to quantum time then add this

burst time into turnaround time and delete the process from the queue else add

quantum to the turnaround time and insert the process into the queue.

6. Calculate the turnaround time and arrival time by subtracting the arrival time.

7. Total turnaround time and total waiting time is calculated using

8. Total turnaround time = ∑ turnaround time of all processes.

9. Total waiting time = ∑ waiting time of all processes.

10. Average turnaround time and waiting time is

i. Total waiting time

Average waiting time = -------------------------

ii. Number of process

1. Total turnaround time

Average turnaround time= -------------------------

2. Number of process

Page 5: Os Programs

11. End the program.

ROUND ROBIN SCHEDULING

#include<stdio.h>#include<conio.h>typedef struct{int pid,etime,wtime,remain,turn;}fcfs;int main(){fcfs fc[10];int n,i,j,sw,stot,se,tq,ts,pid;int c[3][100];int k=0;sw=0;stot=0,se=0,tq=0,ts=0;clrscr();printf("Enter the no.of process;");scanf("%d",&n);for(i=0;i<n;i++){printf("\n Enter the CPU Burst time for process %d's:",i+1);scanf("%d",&fc[i].etime);fc[i].pid=i+1;fc[i].remain=fc[i].etime;}printf("\nEnter the Time Slice:");scanf("%d",&ts);for(i=0;i<n;i++){se=se+fc[i].etime;}while(tq!=se){for(i=0;i<n;i++){if(fc[i].remain>0&&fc[i].remain<=ts){tq=tq+fc[i].remain;fc[i].turn=tq;fc[i].wtime=fc[i].turn-fc[i].etime;fc[i].remain=0;c[0][k]=fc[i].pid;c[1][k]=tq;c[2][k]=fc[i].remain;

Page 6: Os Programs

k++;}else if(fc[i].remain>0){tq+=ts;fc[i].remain==ts;c[0][k]=fc[i].pid;c[1][k]=tq;c[2][k]=fc[i].remain;k++;}}}printf("\nProcessID\tTimeOver\tRemainingTime\n");for(i=0;i<k;i++){printf("\n\t%d\t\t%d\t\t%d",c[0][i],c[1][i],c[2][i]);printf("\n");}printf("\nProcessID\tExecutionTime\tWaitingTime\tTurnAroundTime\n");for(i=0;i<n;i++){printf("\n\n\t%d\t\t%d\t\t%d\t\t%d",fc[i],pid,fc[i].etime,fc[i].wtime,fc[i].turn);sw+=fc[i].wtime;stot+=fc[i].turn;}printf("\n\n\t\t\tAverage:\t%5.3f\t\t%5.3f",(double)sw/n,(double)stot/n);getch();}

RESULT

Enter the no.of process;2

Enter the CPU Burst time for process 1's:6

Enter the CPU Burst time for process 2's:3

Enter the Time Slice:3

ProcessID TimeOver RemainingTime

1 3 6

2 6 0

Page 7: Os Programs

1 9 6

ProcessID ExecutionTime WaitingTime TurnAroundTime

1 6 -23239 6

2 3 3 0

Average: -11618.000 -16072.500

Page 8: Os Programs

SHORTEST JOB FIRST SCHEDULING

Ex. No. :

Date :

AIM:

To implement Shortest Job First (SJF) CPU scheduling algorithm with same

arrival time.

ALGORITHM:

1. Start the program.

2. Get the number of process, process name, process number, burst time of each

process.

3. Sort the process in the ascending order of their burst time.

4. Initialize the turn around time of first process as its burst time and waiting time as

0.

5. Calculate the turn around time and waiting time if remaining process using Turn

around time = Turnaround time of previous process + Burst time.

6. Calculate total turn around time and total waiting time as

Total waiting time = ∑ waiting time of all processes.

Total turnaround time = ∑ turnaround time of all processes.

7. Calculate the average turn around time and waiting time as

Total turnaround time

Average turnaround time= -------------------------

Number of process

Total waiting time

Average waiting time = -------------------------

Number of process

8. End the program.

Page 9: Os Programs

Shortest Job First – Premptive

#include<stdio.h>#include<conio.h>struct process{int pid;int cbt;int arrt;int wt;int tat;int pcompleted;}p[11]={0},o[11]={0},rq[11],t={0},tque[11]={0};int i=0,i1=0,i2=0,j=0,j1=0,k=0,np,Anyarvl=0,tq=0;//Time quantumfloat aw=0,at=0;void main(){clrscr();printf("Enter the no.of process:");scanf("%d",&np);for(i=0;i<np;i++){p[i].pid=i;printf("Enter Arr Time,CPUBurstTime for process %d:",p[i].pid);scanf("%d",&p[i].arrt);scanf("%d",&p[i].cbt);}//Sorting based on Arrival Timefor(i=0;i<np;i++){for(j=i;j<np;j++){if(p[i].arrt>p[j].arrt){t=p[i];p[i]=p[j];p[j]=t;}}}//Sorting based on process having eq arr timefor(i=0;i<np;i++){

Page 10: Os Programs

if(p[i].arrt==p[i+1].arrt&&p[i].cbt>p[i+1].cbt){t=p[i];p[i]=p[i+1];p[i+1]=t;}}//Display-After Sorting//printf("\n\t\t\t*After Sorting*\n");printf("\nProcess\t\tArrTime\tCPUBTime\n");for(i=0;i<np;i++)printf("%d\t\t%d\t\t%d\n",p[i].pid,p[i].arrt,p[i].cbt);getch();clrscr();j=0;for(i=0;i<np;i++){k=0;if(i==0)//For First Process{o[j]=p[i];o[j].wt=0;o[j].tat=o[j].wt+o[j].cbt;aw=aw+o[j].wt;at=at+o[j].tat;tq=tq+o[j].tat;p[i].pcompleted=1;j++;}else if(i!=0)//For other process-Q in Ready Q{rrr:for(i2=0;i2<np;i2++){if(p[i2].arrt<=tq&&p[i2].pcompleted==0){rq[k]=p[i2];k++;Anyarvl=1;}}//If noarvl within Time Qtm-advance tqif(Anyarvl==0){tq=tq+1;goto rrr;

Page 11: Os Programs

}sortrq();o[j]=rq[0];o[j].wt=o[j-1].tat;o[j].tat=o[j].wt+o[j].cbt;aw=aw+o[j].wt;at=at+o[j].tat;tq=tq+o[j].tat;for(i2=0;i2<np;i2++){if(p[i2].pid==o[j].pid);p[i2].pcompleted=1;}j++;}}//printf("\n\t\t\Output-SJF\n\n\n");printf("\nProcessID\tArrTime\tBurstTime\tWaitingTime\tTurnAtime\n\n");for(i=0;i<j;i++)printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n\n",o[i].pid,o[i].arrt,o[i].cbt,o[i].wt,o[i].tat);getch();}//End of main//Ready Q sortingsortrq(){for(i1=0;i1<k;i1++){for(j1=i1;j1<k;j1++){if(rq[i].cbt>rq[j1].cbt){t=rq[i1];rq[i1]=rq[j1];rq[j1]=t;}}}}

Page 12: Os Programs

RESULT

Enter the no.of Process:3

Enter Arr.Time,CPUBurstTime for process 0: 0 2

Enter ArrTime,CPUBurstTime for process 1: 2 4

Enter ArrTime,CPUBurstTime fro process 2: 1 3

Process Arr Time CPUBTime0 0 22 1 31 2 4

ProcessID ArrTime BurstTime WaitingTime TurnAtime

0 0 2 0 2

2 1 3 2 5

2 1 3 5 8

Page 13: Os Programs

IMPLEMENTATION OF DEKKER’S ALGORITHM

Ex. No. :

Date :

AIM:

To implement Mutual Exclusion Using Dekker’s Algorithm..

ALGORITHM:

1. Start the program.

2. Declare and initialise variable.

3. Define process 1 and process 2 as below.

i) Check Pi wants to enter as 1.

ii) Check if Pj wants to enter, if so then check whether favored process is

j or i.

If it is j, Pj wants to enter as 0 and wait until favored process equals i.

Else process I enters critical region.

iii) On exit from critical region, set favored process as j and Pi wants to

enter as 0.

4. If only one process wants to enter then that particular function is invoked.

5. If both process want to enter critical section then both process invoked. It allows

favored process to run first.

6. Stop the program.

Page 14: Os Programs

/*DEKKERS ALGORITHM*/#include<stdio.h>#include<conio.h>int c=1;int favour=1;int p1,p2;void process1(){p1=1;while(p2)if(favour==2){p1=0;while(favour==2)p1=1;}printf("\nprocess 1 is in critical section");printf("\n modified shared value c=%d",c++);favour=2;p1=0;}void process2(){p2=1;while(p1)if(favour==1){p2=0;while(favour==1);p2=1;}printf("\n process 2 is in critical section");printf("modified shared value c=%d",c++);favour=1;p2=0;}main(){int choice;p1=0;p2=0;while(1){printf("\n1.both process\n\n 2.process one\n\n 3.process two\n");printf("\nEnter your choice\n");scanf("%d",&choice);

Page 15: Os Programs

switch(choice){case 1:if(favour==1){p1=1;p2=0;process1();process2();}else if(favour!=1){p1=0; p2=1;process2();process1();}break;case 2:favour=1;p1=1; p2=0;process1();break;case 3:favour=2;p2=1; p1=0;process2();break;}}}

Result :

1.both process2.process one3.process two

Enter your choice : 2Process 1 is in critical sectionModified shared value c=1

1.both process2.process one3.process two

Page 16: Os Programs

Enter your choice : 1process 2 is in critical sectionModified shared value c=2process 2 is in critical sectionmodified shared value c=3

1.both process2.process one3.process two

Enter your choice : 3process 2 is in critical sectionModified shared value c=4

1.both process2.process one3.process two

Enter your choice : 4

Page 17: Os Programs

INTERPROCESS COMMUNICATION PROBLEM

(PRODUCER – CONSUMER PROBLEM)

Ex. No. :

Date :

AIM:

To implement producer – consumer problem using semaphore.

ALGORITHM:

1. Start the program.

2. Declare three semaphore variables.

Mutex initialised to 0 which allows only one process to execute at any

time.

Two variables to indicate the limit of buffer.

3. Wait and signal are two functions to implement the semaphore.

Wait-waits until semaphore variable reach 1 and then decrements it.

Signal – increments the semaphore variable by 1.

4. The reader process, checks if any process is writing. If so it waits else it reads the

content of shared variable and then signals.

5. The Writer process checks if any other process is accessing the shared variable. If

not it changes the value of shared variable and then signals.

6. End he program.

Page 18: Os Programs

/* PRODUCER AND CONSUMER PROBLEM*/#include<conio.h>#include<stdio.h>#include<stdlib.h>static int full,empty,mutex;int buffer[5],in=0,out=0;void wait(int *a);void signal(int *b);void producer(){int nextp;printf("producer\n");wait(&empty);wait(&mutex);nextp=rand()%10+1;buffer[in]=nextp;printf("produced item is %d\n",nextp);in=(in+1)%5;signal(&mutex);signal(&full);printf("full=%d\t empty=%d\n",full,empty);}void consumer(){int nextc;printf("consumer\n");wait(&full);wait(&mutex);nextc=buffer[out];printf("consumerd item is %d\n",nextc);out=(out+1)%5;signal(&mutex);signal(&empty);printf("full=%d\t empty=%d\n",full,empty);}void wait(int *a){while(*a<=0);*a=*a-1;}void signal(int *b){*b=*b+1;}main(){

Page 19: Os Programs

int c;mutex=1;empty=5;full=0;clrscr();while(1){printf("1.producer\t 2.consumer\t 3.both\t 4.Exit\n");printf("choice\n");scanf("%d",&c);switch(c){case 1:

if(empty==0)printf("producer has to wait\n");else{producer();}break;

case 2:if(full==0)printf("consumer has to wait");else{consumer();}break;

case 3:if(!empty){printf("producer has to wait\n");consumer();}else if(!full){printf("consumer has to wait\n");producer();}else{consumer();producer();}break;case 4:

Page 20: Os Programs

exit(0);break;}}getch();return 0;

}

RESULT

1.producer 2.consumer 3.both 4.Exitchoice1producerproduced item is 7full=1 empty=41.producer 2.consumer 3.both 4.Exitchoice2consumerconsumerd item is 7full=0 empty=51.producer 2.consumer 3.both 4.Exitchoice3consumer has to waitproducerproduced item is 1full=1 empty=4

1.producer 2.consumer 3.both 4.Exitchoice

Page 21: Os Programs

READER WRITER PROBLEM USING SEMAPHORE

Ex. No. :

Date :

AIM:

To implement reader – writer problem using semaphore.

ALGORITHM:

1. Start the program.

2. Declare three semaphore variable mutex and a shared variable which is used by

reader and writer processes.

3. Wait and signal are two functions to implement the semaphore.

Wait-waits until semaphore variable reach 1 and then decrements it.

Signal – increments the semaphore variable by 1.

4. The reader process, checks if any process is writing. If so it waits else it reads the

content of shared variable and then signals.

5. The Writer process checks if any other process is accessing the shared variable. If

not it changes the value of shared variable and then signals.

6. Pthreads is used to execute two processes simultaneously.

7. End he program.

Page 22: Os Programs

READERS WRITER PROBLEM USING SEMAPHORE

#include<iostream.h>#include<stdlib.h>#include<stdio.h>#include<conio.h>static int mutex;int wrt,readcount,ch;char buffer[50]="hello";void wait(int*);void signal(int*);void print();void reader(){wait(&mutex);readcount++;if(readcount==1)wait(&wrt);signal(&mutex);cout<<"\nREADER READS:"<<buffer;wait(&mutex);readcount--;if(readcount==0)signal(&wrt);signal(&mutex);}void writer(){wait(&wrt);cout<<"\nEnter the string to write:";gets(buffer);cout<<"\nWriter writes:"<<buffer;signal(&wrt);}void wait(int *x){while(*x<=0);*x=*x-1;}void signal(int *y){*y=*y+1;}main(){

Page 23: Os Programs

mutex=1;wrt=1;readcount=0;do{cout<<"\n\nREADER-WRITER PROBLEM\n\n1.READER\n2.WRITER\n3.READ AND WRITE\n4.WRITE AND READS\n5.EXIT\n\nENTER THE CHOCIE:";cin>>ch;{case 1:reader();break;case 2:writer();case 3:reader();writer();break;case 4:writer();reader();break;case 5:exit(0);}}while(ch<6);getch();}

Page 24: Os Programs

OUTPUT

READER – WRITER PROBLEM

1. READER2. WRITER3. READ AND WRITE4. WRITE AND READ5. EXIT

ENTER THE CHOICE: 1READER READS : helloREADER – WRITER PROBLEM

1. READER2. WRITER3. READ AND WRITE4. WRITE AND READ5. EXIT

ENTER THE CHOICE: 2Welcome

ENTER THE STRING TO WRITE:WRITER WRITERS: Welcome

READER – WRITER PROBLEM

1. READER2. WRITER3. READ AND WRITE4. WRITE AND READ5. EXIT

ENTER THE CHOICE: 3Hai

READER READS : welcome

ENTER THE STRING TO WRITE :

WRITER WRITES : hai

Page 25: Os Programs

READER – WRITER PROBLEM

1. READER 2. WRITER3. READ AND WRITE 4. WRITE AND READ 5. EXIT

ENTER THE CHOICE : 4bye

ENTER THE STRING TO WRITEWRITER WRITES : byeREADER READS :bye

READER – WRITER PROBLEM

1. READER2. WRITER3. READ AND WRITE4. WRITE AND READ5. EXIT

ENTER THE CHOICE : 5

Page 26: Os Programs

BEST FIT, FIRST FIT FOR MEMORY MANAGEMENT

Ex. No. :

Date :

AIM:

To implement Best fit, First Fit Algorithm for Memory Management.

ALGORITHM:

Step 1: Start the program.

Step 2: Create a menu for First fit and Best fit.

Step 3: Get the number of holes and size of holes.

Step 4: Enter the number of processes and their sizes for process creation.

Step 5: Compare the process and size then the process is successfully to allocate

given hole.

Step 6: In first fit memory management scheme the first biggest hole is allocated first.

Step 7: In best-fit memory management scheme allocates the smallest hole that is big

enough.

Page 27: Os Programs

BEST FIT , FIRST FOR MEMORY MANAGEMENT

#include<conio.h>#include<stdio.h>#include<process.h>typedef struct{int size,pro,alloc;}node;void main(){node s[10];int i,j,n,size,p,cc,t=0,q;clrscr();menu:printf("\t\t\t\tmenu\n");printf("\t\t\t1.First Fit.\n");printf("\t\t\t2.Best Fit.\n");printf("\t\t\t3.Exit\n");printf("\n\n Enter your choice\n");scanf("%d",&q);switch(q){case 1:{printf("\t\tFirst Fit\n");printf("\n\tEnter the number of holes:\n");scanf("%d",&n);printf("\n\t\tenter the size of the holes\n");for(i=1;i<=n;i++){s[i].pro=-1;scanf("%d",&s[i].size);}a:printf("\n\t\t1.Process insertion\n");printf("\n\t\t2.Exit\n");printf("\n\t\tEnter your choice:\n");scanf("%d",&cc);if(cc==1){printf("\n\t\tenter the process and size\n");scanf("%d%d",&p,&size);for(i=1;i<=n;i++){

Page 28: Os Programs

if(s[i].pro==-1&&(s[i].size>=size));{ s[i].alloc=size; s[i].pro=p; printf("\n\t\t\theprocess%dsucessfully allocated to hole size:%d\n",s[i].pro,s[i].size); goto a; } if(i>=n) { printf("\nMemeory not available\n"); goto a; } } } if(cc==2) { goto menu; } getch(); } break; case 2: { printf("\t\tBest Fit\n"); printf("\n\tEnter the number of holes:\n"); scanf("%d",&n); printf("\n\n\tenter the size of the holes\n"); for(i=1;i<=n;i++) { s[i].pro=-1; scanf("%d",&s[i].size); } b: printf("\n\t\t1.process insertion\n"); printf("\n\t\t2.Exit\n"); printf("\n\t\tenter your choice:\n"); scanf("%d",&&cc); if(cc==1) { j=-1; printf("\n\t\tenter the process and size\n"); scanf("%d%d,"&p,&size); for(i=1;i<=n;i++) { if(s[i].pro==-1&&(s[i].size>=size)) {

Page 29: Os Programs

if(j==-1)||(s[j].size>s[i].size)) { j=i; } } } if(j!=-1) { s[j].pro=p; s[j].alloc=size; printf("\n\t\t the process%d is allocated to %d \n",s[j].pro,s[j].size); } else { printf("\n\t\t\t\tNo Space\n"); } goto b; } if(cc==2) { goto menu; }

getch(); } break; case 3: exit(0); default: printf("\n wrong choice\n"); goto menu; } getch(); }

Page 30: Os Programs

OUTPUT

Menu

1.First Fit2.Best Fit3.Exitenter your choice 1First FitEnter the number of holes : 5Enter the size of the holes8004003005002001.process insertion2.exit

enter your choice:1enter the process and size 1 450the process 1 successfully allocated to hole size :8001.process insertion2.exitenter your choice : 1enter the process and size 1 400the process 1 successfully allocated to hole size :400

1.process insertion2.exit

Menu1.First Fit2.Best Fit3.Exit

enter your choice :2enter the number of holes: 5enter the size of holes800400300500200

Page 31: Os Programs

1.process insertion2.exitenter your choice : 1enter the process and size 1 150the process 1 successfully allocated to hole size :200

1.process insertion2.exit

enter your choice : 1enter the process and size 1 150the process 1 successfully allocated to hole size :2001.process insertion2.exitenter your choice : 1enter the process and size 1 250the process 1 successfully allocated to hole size :300

Page 32: Os Programs

MEMORY ALLOCATION WITH PAGES

Ex. No. :

Date :

AIM:

To implement Memory Allocation With Pages.

ALGORITHM:

1. Start the program.

2. Create a page map table with fields page no., frame no. and a bit which

indicates validity, i-invalid and v-valid.

3. Create physical memory with fields frame no., contents and validity.

4. Initialize the valid bits in memory as 0 and in page map table all valid bits are

set as “i” and frame no. as 0.

5. Get the process name and the number of pages.

6. Find an empty space in memory and place the contents of the page in that

position.

7. Set the valid bit and information about block number is entered in page map

table.

8. Display the contents of main memory and page map table.

9. Stop the program.

Page 33: Os Programs

MEMORY ALLOCATION WITH PAGES

#include<stdio.h>#include<string.h>struct page{int pageno;int frameno;char valid;}pmt[10];struct pmemory{int frameno;char con[6][10];int valid;}m[5];struct lmemory{char con[6][10];}l[10];int main(){int i,j,k,nop,x,addr;char name[10];for(i=0;i<5;i++)m[i].valid=0;for(i=0;i<5;i++)for(j=0;j<5;j++)strcpy(m[i].con[j]," ");m[1].valid=1;m[3].valid=1;for(i=0;i<3;i++){strcpy(m[1].con[i],"Welcome");strcpy(m[3].con[i],"hello");}for(i=0;i<3;i++){pmt[i].valid='i';pmt[i].frameno=0;}printf("Enter the process Name]n");scanf("%s",name);

Page 34: Os Programs

printf("Enter no.of pages of %s",name);scanf("%d",&nop);k=1;for(i=0;i<nop;i++){pmt[i].pageno=i;pmt[i].valid='v';printf("Contents of page%d",pmt[i].pageno);while(m[k].valid==1)k++;x=0;for(j=0;j<3;j++){pmt[i].frameno=k;m[k].valid=1;scanf("%s",l[i].con[j]);strcpy(m[k].con[x],l[i].con[j]);x++;}}printf("\n\tMAINMEMORY");printf("\n FRAME NO\t ADDRESS \t CONTENTS");addr=0;for(i=0;i<5;i++){printf("\n%d\t",i);for(j=0;j<3;j++){printf("\t%d\t%s\n\t",addr,m[i].con[j]);addr++;}printf("-----------\n");}printf("\n\t\t PAGE MAP TABLE");printf("\n PAGE NO\t FRAME NO\t VALIDITY");for(i=0;i<5;i++)printf("\n%d\t\t%d\t%c",pmt[i].pageno,pmt[i].frameno,pmt[i].valid);return(0);}

Page 35: Os Programs

Enter the process NameP1Enter no.of pages of P12Contents of page0abc def ghiContents of page1pqr stu vwx

MAINMEMORY FRAME NO ADDRESS CONTENTS0 0 1 2 -----------

1 3 Welcome 4 Welcome 5 Welcome -----------

2 6 abc 7 def 8 ghi -----------

3 9 hello 10 hello 11 hello -----------

4 12 pqr 13 stu 14 vwx -----------

PAGE MAP TABLE PAGE NO FRAME NO VALIDITY0 2 v1 4 v0 0 i0 0 i0 0 i

Page 36: Os Programs

#include<stdio.h>#include<string.h>struct page{int pageno;int frameno;char valid;}pmt[10];struct pmemory{int frameno;char con[6][10];int valid;}m[5];struct lmemory{char con[6][10];}l[10];int main(){int i,j,k,nop,x,addr;char name[10];for(i=0;i<5;i++)m[i].valid=0;for(i=0;i<5;i++)for(j=0;j<5;j++)strcpy(m[i].con[j]," ");m[1].valid=1;m[3].valid=1;for(i=0;i<3;i++){strcpy(m[1].con[i],"Welcome");strcpy(m[3].con[i],"hello");}for(i=0;i<3;i++){pmt[i].valid='i';pmt[i].frameno=0;}printf("Enter the process Name]n");scanf("%s",name);

Page 37: Os Programs

printf("Enter no.of pages of %s",name);scanf("%d",&nop);k=1;for(i=0;i<nop;i++){pmt[i].pageno=i;pmt[i].valid='v';printf("Contents of page%d",pmt[i].pageno);while(m[k].valid==1)k++;x=0;for(j=0;j<3;j++){pmt[i].frameno=k;m[k].valid=1;scanf("%s",l[i].con[j]);strcpy(m[k].con[x],l[i].con[j]);x++;}}printf("\n\tMAINMEMORY");printf("\n FRAME NO\t ADDRESS \t CONTENTS");addr=0;for(i=0;i<5;i++){printf("\n%d\t",i);for(j=0;j<3;j++){printf("\t%d\t%s\n\t",addr,m[i].con[j]);addr++;}printf("-----------\n");}printf("\n\t\t PAGE MAP TABLE");printf("\n PAGE NO\t FRAME NO\t VALIDITY");for(i=0;i<5;i++)printf("\n%d\t\t%d\t%c",pmt[i].pageno,pmt[i].frameno,pmt[i].valid);return(0);}

Page 38: Os Programs

FIFO PAGE REPLACEMENT ALGORITHM

Ex. No. :

Date :

AIM:

To implement first in first out (FIF) page replacement algorighm.

ALGORITHM:

1. Start the program.

2. Get the number of pages in the reference string and the number of block in

memory.

3. A queue is used to represent the block in memory.

4. For each page in the reference string if that page is not present in the queue,

a. Place the page in the appropriate position on FIFO basis and

b. Increment the page fault counter to 1.

If that page is present in the queue display “No page fault”.

5. Display the total number of page faults.

6. End the program.

Page 39: Os Programs

FIFO PAGE REPLACEMENT ALGORITHM

#include<iostream.h>#include<conio.h>main(){int ptr,i,j,b,n,fault=0,a[20],p[5],have;cout<<"\nENTER THE NO OF REFERNCE STRING";cin>>n;cout<<"\nENTER THE STRINGS:";for(i=0;i<n;i++)cin>>a[i];cout<<"\nENTER THE NO OF BOOKS:";cin>>b;for(j=0;j<b;j++)p[j]=-1;ptr=-1;cout<<"\n ref.string\t pages\n\\n";for(i=0;i<n;i++){have=0;for(j=0;j<b;j++)if(p[j]==a[i]){have=1;break;}if(have==0){ptr=ptr+1;p[ptr%b]=a[i];fault++;}cout<<a[i]<<"\t";for(j=0;j<b;j++)if(p[j]==-1)cout<<" ";elsecout<<p[j]<<"\t";cout<<"\n";}cout<<"NUMBER OF PAGE FAULTS ARE:"<<fault;getch(); }

Page 40: Os Programs

OUTPUT

ENTER THE NO OF REFERENCE STRING 10

ENTER THE STRINGS

4 9 2 4 0 9 1 2 1 9

ENTER THE NO OF BLOCKS : 3

Ref .string pages

B1 B2 B3

4 49 4 92 4 9 24 4 9 20 0 9 29 0 9 21 0 1 22 0 1 21 0 1 29 0 1 9

NUMBER OF PAGE FAULTS IS : 6

Page 41: Os Programs

LRU PAGE REPLACEMENT ALGORITHM

#include<iostream.h>#include<conio.h>main(){int min,k,ptr=0,ctr,i,j,b,n,fault=0,a[20],have;clrscr();struct page{int value,counter;}p[5];cout<<"\nEnter the no of reference string";cin>>n;cout<<"\nEnter the strings:";for(i=0;i<n;i++)cin>>a[i];cout<<"\nEnter the no of blocks:";cin>>b;for(j=0;j<b;j++)p[j].value=-1;ptr=0;cout<<"\n ref.string\t pages\n\n";for(i=0;i<n;i++){have=0;for(j=0;j<b;j++){if(p[j].value==a[i]){have=1;p[j].counter=i;break;}}if(have==0){if(ptr<b){

Page 42: Os Programs

p[ptr].value=a[i];p[ptr].counter=i;fault++;ptr=ptr+1;}else{min=0;for(k=1;k<b;k++)if(p[min].counter>p[k].counter)min=k;p[min].value=a[i];p[min].counter=i;fault++;}}cout<<a[i]<<"\t";for(j=0;j<b;j++)if(p[j].value==-1)cout<<" ";else cout<<p[j].value<<"\t";cout<<"\n";}cout<<"Number of Page faults are:"<<fault;getch();}

Page 43: Os Programs

OUTPUT

ENTER THE NO OF REFERENCE STRING 10

ENTER THE STRINGS

4 9 2 4 0 9 1 2 1 9

ENTER THE NO OF BLOCKS : 3

Ref .string pages

B1 B2 B3

4 49 4 92 4 9 24 4 9 20 4 0 29 4 0 91 1 0 92 1 2 91 1 2 99 1 2 9

NUMBER OF PAGE FAULTS ARE : 7

Page 44: Os Programs

#include<errno.h>#include<fcntl.h>#include<unistd.h>int main(int argc,char *argv[]){struct flock f1={F_WRLCK,SEEK_SET,0,0,0};int fd;f1.l_pid=getpaid();if(argc>1)f1.l_pid=getpaid();if(argc>1)f1.l_type=F_RDLCK;if(fd=open("test",O_RDWR))==-1){perror("open");exit(1);}printf("Press<RETURN>to try to get lock");getchar();printf("Trying to get lock----");if(fcntl(fd,F_SETLKW,&f1)==-1){perror("fcntl");exit(1);}printf("Unlocked.\n");close(fd);}

Page 45: Os Programs

#include<stdio.h>struct process{int max[10],alloc[10],need[10];}p[6];int avail[10],n,r,safe[10];void getdata();void banker();void getdata(){int i,j;printf("Enter the number of process=>\n");scanf("%d",&n);printf("Enter the number of resources=>\n");scanf("%d",&r);printf("Enter Allocated Resources=>\n");for(i=0;i<n;i++){printf("Process p%d\n",i);for(j=0;j<r;j++)scanf("%d",&p[i].alloc[j]);}printf("Enter Max of Each Process\n");for(i=0;i<n;i++){printf("Process p%d\n",i);for(j=0;j<r;j++)scanf("%d",&p[i].max[j]);}printf("Enter the Available Resources \n");for(i=0;i<r;i++)scanf("%d",&avail[i]);printf("Need Matrix\n");for(i=0;i<n;i++){for(j=0;j<r;j++){p[i].need[j]=p[i].max[j]-p[i].alloc[j];printf("%d\t",p[i].need[j]);}printf("\n");}

Page 46: Os Programs

}void banker(){int flag,c=0,finish[10],i,j,f,s;for(i=0;i<n;i++)finish[i]=0;do{f=0;for(i=0;i<n;i++){for(j=0;j<r;j++){flag=0;if(p[i].need[j]>avail[j]){flag=1;break;}}if(flag==0&&finish[i]==0){f=1;printf("After process %d Available Resources=",i);for(j=0;j<r;j++){p[i].need[j]=0;avail[j]=p[i].alloc[j]+avail[j];printf("%d",avail[j]);}printf("\n");safe[c++]=i;finish[i]=1;}//if}//forif(f==0)break;}while(f==1);s=1;for(i=0;i<n;i++)if(finish[i]==0){printf("Cannot Allocate Requested Resources for Process %d",i);s=0;break;

Page 47: Os Programs

}if(s!=0){printf("Safe sequence \n");for(i=0;i<c;i++)printf("%d\t",safe[i]);printf("\n");}elseprintf("Unsafe state\n");}//bankermain(){getdata();banker();}

Page 48: Os Programs

CREATION OF SHARED MEMORY SEGMENT

Ex. No. :

Date :

AIM:

To implement the creation of shared memory segment.

ALGORITHM:

1. Start the program.

2. Create a page map table with fields page number, block number, contents.

3. Create a structure of memory with block number, block size and contents.

4. Get the total number of blocks in memory along with its contents.

5. Get the total number of processes and their names.

6. For each process, get the total number of pages and the blocks, which they

want to share in memory.

7. Display the contents of page map table.

8. Display the contents in memory.

9. Display the contents, which the process shares from memory.

10. End the program.

Page 49: Os Programs

CREATION OF SHARED MEMORY SEGMENT

#include<stdio.h>#include<string.h>#include<stdlib.h>void main(){struct{int pageno[5],p,memblock[5];char file[10];}pmt[10];struct{int bsize,block;char cont[5][10];}rec[5];int i,j,m,n,p,k,l;printf("\n Enter the number of blocks in the memory");scanf("%d",&n);printf("\n Enter the block content");for(i=1;i<=n;i++){rec[i].block=i;printf("\nEnter the block size");scanf("%d",&rec[i].bsize);printf("\nEnter the contents of %d Block",i);for(j=1;j<=rec[i].bsize;j++)scanf("%s",rec[i].cont[j]);}printf("\n Enter the number of process");scanf("%d",&m);for(i=1;i<=m;i++){printf("\nEnter the process name");scanf("%s",pmt[i].file);printf("\nEnter the PMT for propcess:%d",i);printf("\nEnter the numbe of pages");scanf("%d",&pmt[i].p);for(j=1;j<=pmt[i].p;j++){printf("\nEnter the page number:");scanf("%d",&pmt[i].pageno[j]);printf("\nEnter the block number:");

Page 50: Os Programs

scanf("%d,&pmt[i].memblock[j]);}}printf("\nPAGE MAP TABLE");printf("\nPageno\t\tBlock no\t\tFileName");for(i=1;i<=m;i++){for(j=1;j<=pmt[i].p;j++)printf("\n%d\t\t%d\t\t%s",pmt[i].pageno[j],pmt[i].memblock[j],pmt[i].file);}printf("\nContent of memory");for(i=1;i<=n;i++){printf("\n-------------);for(j=1;j<=rec[i].bsize;j++){if(j==2)printf("\nBlock%d%s",rec[i].block,rec[i].cont[j]);elseprintf("\n\t%s",rec[i].cont[j]);}}for(i=1;<=m;i++){printf("\nContents of process%d",i);for(j=1;j<=pmt[i].p;j++){for(k=1;k<=n;k++){if(rec[k].block==pmt.memblock[j]){printf("\n");for(l=1;l<=rec[k].bsize;l++)printf("%s",rec[k].cont[l]);}}}}printf("\n");}

Page 51: Os Programs

OUTPUT

Enter the number of blocks in the memory 3Enter the block contentEnter the block size

Enter the contents of 1 Blockaaaaabbbcccc

Enter the block size

Enter the contents of 2 Block

ddddeeeeffff

Enter the block size 3

Enter the contents of 3 Blockgggghhhhiiii

Enter the number of process 2

Enter the process name p1Enter the PMT fopqr process :1Enter the number of pages 2

Enter the page number :0Enter the block number:1Enter the page number : 1Enter the block number :2Enter the process name P2Enter the PMT for process :2Enter the number of pages 2

Enter the page number :3Enter the block number:2Enter the page number:4Enter the block number :3

PAGE MAP TABLE

Page 52: Os Programs

Pageno BlockNo. FileName0 1 P11 2 P13 2 P24 3 P2

Contents of memory

----------------------------aaaa

Block1 bbbbcccc

-------------------------------dddd

Block2 eeeeffff

------------------------------- ggggBlock2 hhhh

iiiicontents of process 1

aaaa bbbb ccccdddd eeee ffff

contents of process 2dddd eeee ffffgggg hhhh iiii

Page 53: Os Programs

IMPLEMENTATION OF FILE LOCKING

Ex. No. :

Date :

AIM:

To implement the file locking using semaphore.

ALGORITHM:

1. Start the program.

2. Declare a semaphore variable.

3. Wait and Signal are two function to implement semaphore.

Wait – waits until semaphore variable reaches 1 and then decrements it.

Signal – increments the semaphore variable by 1.

4. Two processes P1 and P2 are defined.

5. Each process checks the semaphore variable before accessing the file. After

that it locks the file before reading. After finishing accessing the file, it

unlocks the file and signal.

6. The processes are run concurrently.

7. Stop the program.

Page 54: Os Programs

IMPLEMENTATION OF FILE LOCKING

#include<errno.h>#include<fcntl.h>#include<unistd.h>int main(int argc,char*argv[]){Struct flock f1={F_WRLCK,SEEK_SET,0,0,0};Int fd;F1.1_pid=getpid();If(argc>1)F1.1_type=F_RDLCK;If((fd=open(“test”,O_RDWR))==-1){perror(“open”);exit(1);}printf(“Press<RETURN>to try to get lock”);getchar():printf(“Trying to get lock ……”);if(fcntl(fd,F_SETLKW,&f1)==-1){perror(“fcntl”);exit(1);}printf(“Got Lock\n”);printf(“Press < RETURN>to release Lock:”);getchar();f1.1_type=F_UNLCK;if(fcnt1(fd,F_SETLK,&f1)==-1){perror(“fcntl”);exit(1)}printf(“Unlocked.\n”);close(fd)

}

OUTPUT

Red Hat Enterprise Linux ES Release 4 ( Nahant )Kernel 2.6.9-5.EL on an i686Login:01Last login : Tue Dec 12

Page 55: Os Programs

BANKER’S ALGORITHM

Ex. No. :

Date :

AIM:

To implement Banker’s algorithm.

ALGORITHM:

1. Start the program.

2. Get the number of processes, number of resources, maximum number of

resources, allocated resources and available resources.

3. Initialize the finish (i) variable for all processes to 0.

4. Calculate the need matrix as

Needed resources = max. No. of resources – allocated no. of resources

5. Find a process with

a. Needed resources <= available resources & finish [ i] = 0

b. If such a process is found

Available resources = allocated resources of process i

+ available resources & set finish [ i ] = 1.

6. If finish [i] of all processes = 1 then the system is in safe state. Otherwise they

system is in unsafe state.

7. End the program.

Page 56: Os Programs

BANKERS ALGORITHM

#include<stdio.h>struct process{int max[10],alloc[10],need[10];}p[6];int avail[10],n,r,safe[10];void getdata();void banker();void getdata(){int i,j;printf("Enter the number of process=>\n");scanf("%d",&n);printf("Enter the number of resources=>\n");scanf("%d",&r);printf("Enter Allocated Resources=>\n");for(i=0;i<n;i++){printf("Process p%d\n",i);for(j=0;j<r;j++)scanf("%d",&p[i].alloc[j]);}printf("Enter Max of Each Process\n");for(i=0;i<n;i++){printf("Process p%d\n",i);for(j=0;j<r;j++)scanf("%d",&p[i].max[j]);}printf("Enter the Available Resources \n");for(i=0;i<r;i++)scanf("%d",&avail[i]);printf("Need Matrix\n");for(i=0;i<n;i++){for(j=0;j<r;j++){p[i].need[j]=p[i].max[j]-p[i].alloc[j];printf("%d\t",p[i].need[j]);}printf("\n");

Page 57: Os Programs

}}void banker(){int flag,c=0,finish[10],i,j,f,s;for(i=0;i<n;i++)finish[i]=0;do{f=0;for(i=0;i<n;i++){for(j=0;j<r;j++){flag=0;if(p[i].need[j]>avail[j]){flag=1;break;}}if(flag==0&&finish[i]==0){f=1;printf("After process %d Available Resources=",i);for(j=0;j<r;j++){p[i].need[j]=0;avail[j]=p[i].alloc[j]+avail[j];printf("%d",avail[j]);}printf("\n");safe[c++]=i;finish[i]=1;}//if}//forif(f==0)break;}while(f==1);s=1;for(i=0;i<n;i++)if(finish[i]==0){printf("Cannot Allocate Requested Resources for Process %d",i);s=0;

Page 58: Os Programs

break;}if(s!=0){printf("Safe sequence \n");for(i=0;i<c;i++)printf("%d\t",safe[i]);printf("\n");}elseprintf("Unsafe state\n");}//bankermain(){getdata();banker();}

OUTPUT :Enter the number of processes=> 5Enter the Number of Resources=> 3Enter Allocated Resources=>Process p00 1 0Process p12 0 0Process p23 0 2Process p32 1 1Process p40 0 2Enter Max ofEach ProcessProcess p07 5 3Process p13 2 2Process p29 0 2Process p32 2 2Process p44 3 3Enter the Available Resources3 3 2

Page 59: Os Programs

Need matrix7 4 31 2 26 0 00 1 14 3 1After process 1 Available Resources=5 3 2After process 3 Available Resources=7 4 3After process 4 Available Resources=7 4 5After process 0 Available Resources=7 5 5After process 2 Available Resources=10 5 7

Safe Sequence1 3 4 0 2

/* FOR UNSAFE OF PROCESS*/Enter the Number of Processes=>4Enter the Number of Resources=>3Enter Allocated ResourcesProcess P00 1 2Process p13 1 0Process p20 2 2Process p32 1 1Enter Max of Each ProcessProcess p06 3 2Process p14 3 2Process p38 1 8Enter the Available Resources2 3 2Need Matrix6 2 01 2 28 1 16 0 7After process 1 Available Resources 5 4 2Cannot Allocate Requested Resources For Process 0Unsafe state