Вы находитесь на странице: 1из 59

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;

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 1 2 TimeOver 3 6 6 0 RemainingTime

1 ProcessID 1 2

6 TurnAroundTime

ExecutionTime WaitingTime 6 3 Average: -23239 3 0 -11618.000 6

-16072.500

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

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 quantum float 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 Time for(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 time for(i=0;i<np;i++) {

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 tq if(Anyarvl==0) { tq=tq+1; goto rrr;

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

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

IMPLEMENTATION OF DEKKERS ALGORITHM Ex. No. : Date AIM: To implement Mutual Exclusion Using Dekkers Algorithm.. ALGORITHM: 1. Start the program. 2. Declare and initialise variable. 3. Define process 1 and process 2 as below. i) ii) Check Pi wants to enter as 1. 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. :

/*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);

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 process 2.process one 3.process two Enter your choice : 2 Process 1 is in critical section Modified shared value c=1 1.both process 2.process one 3.process two

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.both process 2.process one 3.process two Enter your choice : 3 process 2 is in critical section Modified shared value c=4 1.both process 2.process one 3.process two Enter your choice : 4

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

/* 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() {

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:

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

RESULT 1.producer 2.consumer choice 1 producer produced item is 7 full=1 empty=4 1.producer 2.consumer choice 2 consumer consumerd item is 7 full=0 empty=5 1.producer 2.consumer choice 3 consumer has to wait producer produced item is 1 full=1 empty=4 1.producer choice 2.consumer 3.both 4.Exit

3.both 4.Exit

3.both 4.Exit

3.both 4.Exit

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

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() {

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

OUTPUT READER WRITER PROBLEM 1. 2. 3. 4. 5. READER WRITER READ AND WRITE WRITE AND READ EXIT

ENTER THE CHOICE: 1 READER READS : hello READER WRITER PROBLEM 1. 2. 3. 4. 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. 2. 3. 4. 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

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

ENTER THE CHOICE : 5

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

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++) {

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)) {

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

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

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

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

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

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

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

PAGE MAP TABLE PAGE NO FRAME NO 0 2 v 1 4 v 0 0 i 0 0 i 0 0 i

#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);

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

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

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<<" "; else cout<<p[j]<<"\t"; cout<<"\n"; } cout<<"NUMBER OF PAGE FAULTS ARE:"<<fault; getch(); }

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

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) {

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

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

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

#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"); }

} 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 }//for if(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;

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

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

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:");

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]); else printf("\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"); }

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

Pageno 0 1 3 4

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

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

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 i686 Login:01 Last login : Tue Dec 12

BANKERS ALGORITHM Ex. No. : Date AIM: To implement Bankers 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. :

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");

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

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

Вам также может понравиться