# 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. 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. 6. Average turnaround time and waiting time is :

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(); }

RESULT Enter the No.of Process:2 Process1:4 Process2:2 processID CPUBurstTime Waitingtime TrunAroundTime

1 2

4 2 Average:

0 4

4 6 2.000000 4.000000

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. i. Total waiting time Average waiting time = ------------------------ii. Number of process 1. Total turnaround time Average turnaround time= ------------------------2. Number of process 10. Average turnaround time and waiting time is :

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;

remain>0) { tq+=ts.fc[i]. sw+=fc[i].remain==ts.c[0][i]. } printf("\n\n\t\t\tAverage:\t%5. } RESULT Enter the no.of process. stot+=fc[i]. c[2][k]=fc[i].i<n.fc[i].turn.fc[i].3f\t\t%5.fc[i].i<k. } } } printf("\nProcessID\tTimeOver\tRemainingTime\n").c[2][i]). c[1][k]=tq.c[1][i].k++. k++. for(i=0.3f".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 1 2 TimeOver 3 6 6 0 RemainingTime . getch().remain.i++) { printf("\n\t%d\t\t%d\t\t%d".i++) { printf("\n\n\t%d\t\t%d\t\t%d\t\t%d".wtime.pid.wtime. } printf("\nProcessID\tExecutionTime\tWaitingTime\tTurnAroundTime\n").turn). } else if(fc[i].(double)sw/n. c[0][k]=fc[i]. fc[i].pid.etime. printf("\n"). for(i=0.(double)stot/n).

1 ProcessID 1 2 9 6 TurnAroundTime ExecutionTime WaitingTime 6 3 Average: -23239 3 0 -11618.000 6 -16072.500 .

2. Total turnaround time = ∑ turnaround time of all processes. No. Sort the process in the ascending order of their burst time. Calculate total turn around time and total waiting time as Total waiting time = ∑ waiting time of all processes. Get the number of process. : Date AIM: To implement Shortest Job First (SJF) CPU scheduling algorithm with same arrival time. 5. 6.SHORTEST JOB FIRST SCHEDULING Ex. 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. : . 3. 4. 7. Initialize the turn around time of first process as its burst time and waiting time as 0. burst time of each process. process number. process name. End the program. Start the program. Calculate the turn around time and waiting time if remaining process using Turn around time = Turnaround time of previous process + Burst time. ALGORITHM: 1.

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

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

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

CPUBurstTime for process 1: 2 4 Enter ArrTime.RESULT Enter the no.CPUBurstTime for process 0: 0 2 Enter ArrTime.of Process:3 Enter Arr.Time.CPUBurstTime fro process 2: 1 3 Process 0 2 1 ProcessID 0 2 2 0 1 1 Arr Time 0 1 2 CPUBTime 2 3 4 WaitingTime 2 5 8 TurnAtime ArrTime BurstTime 2 3 3 0 2 5 .

i) ii) Check Pi wants to enter as 1. set favored process as j and Pi wants to enter as 0. Else process I enters critical region.IMPLEMENTATION OF DEKKER’S ALGORITHM Ex. : Date AIM: To implement Mutual Exclusion Using Dekker’s Algorithm. 5. if so then check whether favored process is j or i. 6.. 3. Start the program. Stop the program. If it is j. If only one process wants to enter then that particular function is invoked. ALGORITHM: 1. If both process want to enter critical section then both process invoked. 4. 2. Declare and initialise variable. Check if Pj wants to enter. Pj wants to enter as 0 and wait until favored process equals i. No. It allows favored process to run first. : . iii) On exit from critical region. Define process 1 and process 2 as below.

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

process1(). } else if(favour!=1) { p1=0. p2=1. } } } Result : 1. case 3: favour=2.process one 3. process2().both process 2. break. process2(). process1(). p2=0.process one 3.switch(choice) { case 1: if(favour==1) { p1=1.process two Enter your choice : 2 Process 1 is in critical section Modified shared value c=1 1.process two .both process 2. } break. p2=1. process2(). case 2: favour=1. p1=0. process1(). p2=0. break. p1=1.

both process 2.process two Enter your choice : 3 process 2 is in critical section Modified shared value c=4 1.process two Enter your choice : 4 .both process 2.process one 3.Enter your choice : 1 process 2 is in critical section Modified shared value c=2 process 2 is in critical section modified shared value c=3 1.process one 3.

ALGORITHM: 1.INTERPROCESS COMMUNICATION PROBLEM (PRODUCER – CONSUMER PROBLEM) Ex. 3. checks if any process is writing. If not it changes the value of shared variable and then signals. 5. If so it waits else it reads the content of shared variable and then signals. : Date AIM: To implement producer – consumer problem using semaphore. : . The reader process. • • Mutex initialised to 0 which allows only one process to execute at any time. End he program. Start the program. 6. No. The Writer process checks if any other process is accessing the shared variable. 2. Wait and signal are two functions to implement the semaphore. Declare three semaphore variables. 4. Signal – increments the semaphore variable by 1. Two variables to indicate the limit of buffer. Wait-waits until semaphore variable reach 1 and then decrements it.

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

else { consumer(). case 4: . } break. } else if(!full) { printf("consumer has to wait\n"). full=0.both\t 4. } break. else { producer(). switch(c) { case 1: if(empty==0) printf("producer has to wait\n"). consumer(). } else { consumer(). case 3: if(!empty) { printf("producer has to wait\n"). clrscr(). } break.int c.Exit\n"). mutex=1. while(1) { printf("1. scanf("%d".producer\t 2. producer().consumer\t 3. printf("choice\n"). producer(). case 2: if(full==0) printf("consumer has to wait"). empty=5.&c).

producer choice 2.Exit 3.consumer choice 1 producer produced item is 7 full=1 empty=4 1.Exit 3.both 4.producer 2.both 4. } RESULT 1. } } getch(). return 0. break.Exit .both 4.consumer choice 2 consumer consumerd item is 7 full=0 empty=5 1.producer 2.Exit 3.consumer 3.both 4.producer 2.consumer choice 3 consumer has to wait producer produced item is 1 full=1 empty=4 1.exit(0).

2. : . End he program. checks if any process is writing. Start the program. The reader process. 4. 6. : Date AIM: To implement reader – writer problem using semaphore. Signal – increments the semaphore variable by 1. If so it waits else it reads the content of shared variable and then signals. Wait-waits until semaphore variable reach 1 and then decrements it. 7.READER WRITER PROBLEM USING SEMAPHORE Ex. Pthreads is used to execute two processes simultaneously. No. 5. Wait and signal are two functions to implement the semaphore. ALGORITHM: 1. Declare three semaphore variable mutex and a shared variable which is used by reader and writer processes. 3. 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.

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

READER – WRITER PROBLEM 1. 4. 3. 4. 2. 2. READER WRITER READ AND WRITE WRITE AND READ EXIT ENTER THE CHOICE : 5 . 3. 5. READER WRITER READ AND WRITE WRITE AND READ EXIT ENTER THE CHOICE : 4 bye ENTER THE STRING TO WRITE WRITER WRITES : bye READER READS :bye READER – WRITER PROBLEM 1. 5.

Step 7: In best-fit memory management scheme allocates the smallest hole that is big enough. Step 4: Enter the number of processes and their sizes for process creation. ALGORITHM: Step 1: Start the program. : Date AIM: To implement Best fit. First Fit Algorithm for Memory Management. FIRST FIT FOR MEMORY MANAGEMENT Ex. Step 5: Compare the process and size then the process is successfully to allocate given hole.BEST FIT. Step 3: Get the number of holes and size of holes. Step 2: Create a menu for First fit and Best fit. Step 6: In first fit memory management scheme the first biggest hole is allocated first. : . No.

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

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

} else { printf("\n\t\t\t\tNo Space\n").if(j==-1)||(s[j]. } getch(). } .pro. } if(cc==2) { goto menu. } goto b. default: printf("\n wrong choice\n").s[j]. } } } if(j!=-1) { s[j].pro=p.s[j].size>s[i]. s[j].size)) { j=i. } break. case 3: exit(0).alloc=size.size). goto menu. } getch(). printf("\n\t\t the process%d is allocated to %d \n".

Best Fit 3.Exit enter your choice :2 enter the number of holes: 5 enter the size of holes 800 400 300 500 200 .process insertion 2.OUTPUT Menu 1.exit enter your choice:1 enter the process and size 1 450 the process 1 successfully allocated to hole size :800 1.First Fit 2.Exit enter your choice 1 First Fit Enter the number of holes : 5 Enter the size of the holes 800 400 300 500 200 1.process insertion 2.exit enter your choice : 1 enter the process and size 1 400 the process 1 successfully allocated to hole size :400 1.First Fit 2.Best Fit 3.exit Menu 1.process insertion 2.

exit enter your choice : 1 enter the process and size 1 250 the process 1 successfully allocated to hole size :300 .process insertion 2.process insertion 2.process insertion 2.exit enter your choice : 1 enter the process and size 1 150 the process 1 successfully allocated to hole size :200 1.exit enter your choice : 1 enter the process and size 1 150 the process 1 successfully allocated to hole size :200 1.1.

. 2. 9. Find an empty space in memory and place the contents of the page in that position. ALGORITHM: 1. Initialize the valid bits in memory as 0 and in page map table all valid bits are set as “i” and frame no. 6.. frame no. : . 7. Set the valid bit and information about block number is entered in page map table. Create a page map table with fields page no.MEMORY ALLOCATION WITH PAGES Ex. Stop the program. No. as 0. i-invalid and v-valid. Create physical memory with fields frame no. 4. and a bit which indicates validity. Display the contents of main memory and page map table. 3. 5. : Date AIM: To implement Memory Allocation With Pages. 8. contents and validity. Start the program. Get the process name and the number of pages.

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

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

of pages of P12 Contents of page0 abc def ghi Contents of page1 pqr stu vwx MAINMEMORY FRAME NO ADDRESS 0 0 1 2 ----------1 3 Welcome 4 Welcome 5 Welcome ----------6 abc 7 def 8 ghi ----------9 hello 10 hello 11 hello ----------12 pqr 13 stu 14 vwx ----------VALIDITY CONTENTS 2 3 4 PAGE MAP TABLE PAGE NO FRAME NO 0 2 v 1 4 v 0 0 i 0 0 i 0 0 i .Enter the process NameP1 Enter no.

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

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

3. A queue is used to represent the block in memory. Place the page in the appropriate position on FIFO basis and b. Increment the page fault counter to 1. Start the program.FIFO PAGE REPLACEMENT ALGORITHM Ex. No. Display the total number of page faults. a. Get the number of pages in the reference string and the number of block in memory. ALGORITHM: 1. 4. For each page in the reference string if that page is not present in the queue. 5. If that page is present in the queue display “No page fault”. : Date AIM: To implement first in first out (FIF) page replacement algorighm. End the program. 6. : . 2.

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

string B1 4 9 2 4 0 9 1 2 1 9 4 4 4 4 0 0 0 0 0 0 pages B2 9 9 9 9 9 1 1 1 1 B3 2 2 2 2 2 2 2 9 NUMBER OF PAGE FAULTS IS : 6 .OUTPUT ENTER THE NO OF REFERENCE STRING ENTER THE STRINGS 4 9 2 4 0 9 1 2 1 9 10 ENTER THE NO OF BLOCKS : 3 Ref .

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

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

string B1 4 9 2 4 0 9 1 2 1 9 4 4 4 4 4 4 1 1 1 1 pages B2 9 9 9 0 0 0 2 2 2 B3 2 2 2 9 9 9 9 9 NUMBER OF PAGE FAULTS ARE : 7 .OUTPUT ENTER THE NO OF REFERENCE STRING ENTER THE STRINGS 4 9 2 4 0 9 1 2 1 9 10 ENTER THE NO OF BLOCKS : 3 Ref .

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

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

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

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

: . Display the contents in memory. block number. 6. 10. No. Get the total number of processes and their names. Create a page map table with fields page number. Start the program. Display the contents. Display the contents of page map table. 4. Create a structure of memory with block number. 3. which they want to share in memory. contents. ALGORITHM: 1. : Date AIM: To implement the creation of shared memory segment.CREATION OF SHARED MEMORY SEGMENT Ex. block size and contents. 9. Get the total number of blocks in memory along with its contents. get the total number of pages and the blocks. End the program. which the process shares from memory. 7. 2. 5. 8. For each process.

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

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

OUTPUT Enter the number of blocks in the memory 3 Enter the block content Enter the block size Enter the contents of 1 Block aaaa abbb cccc Enter the block size Enter the contents of 2 Block dddd eeee ffff Enter the block size 3 Enter the contents of 3 Block gggg hhhh iiii Enter the number of process 2 Enter the process name p1 Enter the PMT fopqr process :1 Enter the number of pages 2 Enter the page number :0 Enter the block number:1 Enter the page number : 1 Enter the block number :2 Enter the process name P2 Enter the PMT for process :2 Enter the number of pages 2 Enter the page number :3 Enter the block number:2 Enter the page number:4 Enter the block number :3 PAGE MAP TABLE .

1 2 2 3 FileName P1 P1 P2 P2 Contents of memory ---------------------------aaaa Block1 bbbb cccc ------------------------------dddd Block2 eeee ffff ------------------------------gggg Block2 hhhh iiii contents of process 1 aaaa bbbb cccc dddd eeee ffff contents of process 2 dddd eeee ffff gggg hhhh iiii .Pageno 0 1 3 4 BlockNo.

Wait – waits until semaphore variable reaches 1 and then decrements it. Stop the program. No. it unlocks the file and signal. : . 5. After finishing accessing the file. ALGORITHM: 1. 2. Start the program. : Date AIM: To implement the file locking using semaphore. The processes are run concurrently. 7.IMPLEMENTATION OF FILE LOCKING Ex. Two processes P1 and P2 are defined. Signal – increments the semaphore variable by 1. Declare a semaphore variable. 4. 3. After that it locks the file before reading. Each process checks the semaphore variable before accessing the file. Wait and Signal are two function to implement semaphore. 6.

Int fd.0}. getchar(): printf(“Trying to get lock ……”).IMPLEMENTATION OF FILE LOCKING #include<errno.F_SETLK. close(fd) } OUTPUT Red Hat Enterprise Linux ES Release 4 ( Nahant ) Kernel 2. If(argc>1) F1. exit(1).1_type=F_RDLCK.h> int main(int argc.char*argv[]) { Struct flock f1={F_WRLCK.&f1)==-1) { perror(“fcntl”).9-5. exit(1) } printf(“Unlocked. f1. If((fd=open(“test”.1_type=F_UNLCK.F_SETLKW.h> #include<unistd.\n”).h> #include<fcntl. } printf(“Press<RETURN>to try to get lock”).SEEK_SET. exit(1). } printf(“Got Lock\n”).&f1)==-1) { perror(“fcntl”).6.O_RDWR))==-1) { perror(“open”). if(fcntl(fd.EL on an i686 Login:01 Last login : Tue Dec 12 . getchar(). printf(“Press < RETURN>to release Lock:”).0.0. if(fcnt1(fd.1_pid=getpid(). F1.

6. 7. No. of resources – allocated no. Get the number of processes. Calculate the need matrix as Needed resources = max. If finish [i] of all processes = 1 then the system is in safe state. 4. End the program. Find a process with a. maximum number of resources.BANKER’S ALGORITHM Ex. ALGORITHM: 1. 2. number of resources. : . allocated resources and available resources. Initialize the finish (i) variable for all processes to 0. : Date AIM: To implement Banker’s algorithm. No. of resources 5. 3. If such a process is found Available resources = allocated resources of process i + available resources & set finish [ i ] = 1. Start the program. Needed resources <= available resources & finish [ i] = 0 b. Otherwise they system is in unsafe state.

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

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

} if(s!=0) { printf("Safe sequence \n"). }//banker main() { getdata(). for(i=0. banker(). } OUTPUT : Enter the number of processes=> 5 Enter the Number of Resources=> 3 Enter Allocated Resources=> Process p0 0 1 0 Process p1 2 0 0 Process p2 3 0 2 Process p3 2 1 1 Process p4 0 0 2 Enter Max ofEach Process Process p0 7 5 3 Process p1 3 2 2 Process p2 9 0 2 Process p3 2 2 2 Process p4 4 3 3 Enter the Available Resources 3 3 2 .i++) printf("%d\t". printf("\n").break.i<c.safe[i]). } else printf("Unsafe state\n").

Need matrix 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 After process 1 Available Resources=5 After process 3 Available Resources=7 After process 4 Available Resources=7 After process 0 Available Resources=7 After process 2 Available Resources=10 Safe Sequence 1 3 4 0 2 3 4 4 5 5 2 3 5 5 7 /* FOR UNSAFE OF PROCESS*/ Enter the Number of Processes=> 4 Enter the Number of Resources=> 3 Enter Allocated Resources Process P0 0 1 2 Process p1 3 1 0 Process p2 0 2 2 Process p3 2 1 1 Enter Max of Each Process Process p0 6 3 2 Process p1 4 3 2 Process p3 8 1 8 Enter the Available Resources 2 3 2 Need Matrix 6 2 0 1 2 2 8 1 1 6 0 7 After process 1 Available Resources 5 4 2 Cannot Allocate Requested Resources For Process 0 Unsafe state .