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;

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

500 .000 6 -16072.1 ProcessID 1 2 9 6 TurnAroundTime ExecutionTime WaitingTime 6 3 Average: -23239 3 0 -11618.

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

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

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

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

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 .Time.CPUBurstTime for process 1: 2 4 Enter ArrTime.RESULT Enter the no.of Process:3 Enter Arr.CPUBurstTime for process 0: 0 2 Enter ArrTime.

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

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

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

process one 3.both process 2.process one 3.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 .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.

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

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

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

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

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

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

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

5. READER WRITER READ AND WRITE WRITE AND READ EXIT ENTER THE CHOICE: 3 Hai READER READS : welcome ENTER THE STRING TO WRITE : WRITER WRITES : hai . 5. READER WRITER READ AND WRITE WRITE AND READ EXIT ENTER THE CHOICE: 2 Welcome ENTER THE STRING TO WRITE: WRITER WRITERS: Welcome READER – WRITER PROBLEM 1. 4. 4. 2. READER WRITER READ AND WRITE WRITE AND READ EXIT ENTER THE CHOICE: 1 READER READS : hello READER – WRITER PROBLEM 1.OUTPUT READER – WRITER PROBLEM 1. 5. 3. 4. 3. 3. 2. 2.

5. 2. 4.READER – WRITER PROBLEM 1. 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. READER WRITER READ AND WRITE WRITE AND READ EXIT ENTER THE CHOICE : 5 . 4. 3. 2. 3.

First Fit Algorithm for Memory Management.BEST FIT. ALGORITHM: Step 1: Start the program. FIRST FIT FOR MEMORY MANAGEMENT Ex. : . Step 3: Get the number of holes and size of holes. Step 5: Compare the process and size then the process is successfully to allocate given hole. 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. Step 2: Create a menu for First fit and Best fit. : Date AIM: To implement Best fit. Step 6: In first fit memory management scheme the first biggest hole is allocated first. No.

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

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

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

process insertion 2.First Fit 2.exit Menu 1.exit enter your choice:1 enter the process and size 1 450 the process 1 successfully allocated to hole size :800 1.Best Fit 3.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.First Fit 2.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.exit enter your choice : 1 enter the process and size 1 400 the process 1 successfully allocated to hole size :400 1.process insertion 2.OUTPUT Menu 1.

1.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 250 the process 1 successfully allocated to hole size :300 .exit enter your choice : 1 enter the process and size 1 150 the process 1 successfully allocated to hole size :200 1.

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

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

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

Enter the process NameP1 Enter no.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 .

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

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

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

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

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 .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 .

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

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

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 .

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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 .

Sign up to vote on this title
UsefulNot useful