SYSTEM CALLS LISTING THE FILES IN A DIRECTORY Aim: To write and execute a C Program to list the files in a directory

with the use of exec () system calls. Algorithm: 1. Using function fork (), create a new process and its return value is stored in a variable called pid. 2. If pid is equal to 0, then it is the child process and using execp activities command. 3. else, it is parent process, do the following. 4. Wait until the child process stop its execution. 5. Print that the parent process completed its execution. 6. Stop the program. Program: #include<stdio.h> #include<sys/types.h> int main() { int pid; if(pid==0)//CHILD PROCESS { else//PARENT PROCESS { printf(“\nParent process”); wait(pid); printf(“completed\n”); exit(0); }}

1

Output: [user13@ws002~]$ cc sys3.c [user13@ws002~]$. /a.out a.out sys.c add.c Parent process completed. [user13@ws002~]$ fork.c file.c loop.c lexp1.sh loop3.sh open.c

Result: Thus the C program in LINUX to illustrate the use of exec() system call was executed successfully.

2

I/O SYSTEM CALLS COPYING ONE CONTENT FROM ONE FILE TO ANOTHER Aim: To write and execute a C Program in LINUX, using the I/O system calls namely open (), read (), write () and close (). Algorithm: 1. Open a file, sample is read-only using open (). 2. Open a file, output is write-only (or) truncated mode using open(). 3. Read the Content of the file using read() 4. Write input file content the output file using write (). 5. Display the numbers of bytes read and write successfully. Program: #include<stdio.h> #include<functl.h> int main() { int fp,fd,sz,I; char *c; c=(char *)calloc(50,sizeof(char)); fp=open(“output”,O-WRONLY|O_TRUNC); fd=open(“Sample”,O_RDONLY); sz=read(fd,c,20); i=write(fp,c,strlen©); printf(“\n number of bytes read=%d\n”,sz); printf(“\n number of byted written=%d\n”,i); close(fd); close(fp); }

3

OUTPUT [user13@ws002~]$ cc io.c [user13@ws002~]$ ./a.out Number of bytes read=-1 Number of bytes written=0

Result: Thus the C Program in LINUX, Using the I/O system calls namely open (), read (), write (), close () was executed successfully.

4

SIMULATION OF UNIX COMMAND STIMULATION OF LS COMMAND Aim: To write and execute a c program in LINUX, to stimulate the LS the command. Algorithm: 1. Get the current working directory and store in the variable, path name. 2. scan the directory in the path specified in the path name and sort the directory content alphabetically 3. Display the directory content. 4. Stop Program: #include<dirent.h> main () { struct dirent **namelist; int n,I; char *pathname; getcwd(pathname); n=scandir(pathname,&namelist,o,alphasort); if(n<0) perror(“scandir”); else { for(i=0;i<n;i++) { printf(“%s\n”,namlist[i]->d_name); free(namelist[i]); } free(namelist); }}

5

sh command dir.sh b big.c pat.sh pattern.out arith.sh p Result: thus the c program in linux.out Desktop a.txt io.c [user46@ws002~]$ .c lex.1 list.c for. 6 .OUTPUT: [user46@ws002~]$cc sim1.sh input./a. to stimulate a LS command was executed successfully.c input.sh c cmd.

the initialize newline as NULL string. to stimulate the grep command. then print the error message and exit. Read a max of 1024 characters from the o/p line and store in error line until end of error. of command line argument is not equal to 3. Open the i/p in read mode. 11. 7 . 4. 5. if end of file (or) error message 6. Stop the program. 10. 2. Scan the string is forward direction looking for a line character and return the pointer value to newline. goto step 4. 8. If the no.SIMULATION OF GRED COMMAND Aim: To write and execute a C Program in LINUX. 7. Find the first occurrence of the substring into the string fline and print the substring and increment occurrence by 1. If line not equal to NULL. Initialize count and occurrence as 0. 3. Increment the count by one. 9. Algorithm: 1.

char *newline.max.fline).Program: #include<stdio.h> #define max 1024 int main(int argc. occurrences++. if(newline=strchr(fline.argv[2])!=NULL) { printf(“%s:%d%s\n”.h> #include<string.count. if(!(fp=fopen(argv[1]. if(strstr(fline.”r”))) { printf(“grep:could not open file:%s\n”. char fline[max].’\n’) *newline=’\0’. } while(fgets(fline. int occurrences =0. } 8 .argv[1].argv[1]). exit(1).fp)!=NULL) { count++.char *argv[]) { File *fp. int count=0.

c [user13@ws002~]$.out sankar1 operating Sankar: 1an operating system is layer of software takes care of technical aspects of a computer’s operating. Sankar: 4UNIX is a operating system designed for multiprocessing. Result: Thus the c Program in LINUX to stimulate grep command is executed successfully. multitasking and multi-user. 9 . /a.OUTPUT: [user13@ws002~]$ cc six1.

4. call ganttchart() ganttchart() 1. swap the process according to the arrival time 2. call fcfs() to perform the first come first served scheduling algorithm getdata() 1. for each process do the following / calculate the wait time of each process / calculate total wait time / calculate total turn around time 3. burst time and arrival time from the user fcfs() 1. Display the output 10 . initialize the waiting time and turn around time and time of first process as 0 2.SCHEDULING ALGORITHMS FIRST COME FIRST SERVED SCHEDULING Aim: To write and excute a c program to implement the first come first served scheduling algorithm Algorithm: 1. burst time and arrival time from the user 2. call calculate() 3. get the number of processes from the user 2. Display the waiting time of each process calculate() 1. Display the process names 2. for each process get the process name. calculate the average wait time and average turn around time. call getdata() to get process name.

Program: #include<conio. printf(“enter the process name”).pname[i]). void ganttchart(). scanf(“%d”. void calculate() void fcfs().&Bu[i]).i<=n. scanf(“%s”. } } 11 .c[20][20]. char pname[20][20]. for(i=1. scanf(“%d”.h> #include<string.Wt[10]. scanf(“%d”.A[10].Ttt.Att. printf(“Enter the burst time process %s”. void getdata() { int I.Twt.h> int n. printf(“Enter the arrival time process %s”. void getdata().i++) { fflush(stdin).&n).w.h> #include<conio. float Awt.A[i}).Bu[20].pname[i]).&pname[i]). printf(“Enter the number of process”).

Twt=0.2f ms”. for(i=1.temp1.Att). Ttt=0.Awt). printf(“%d”. temp. for(i=1. Ttt=Ttt+((Wt[i]+Bu[i])-A[i]).i<=n.Wt[i}). } void calculate() { int I. Awt=(float)Twt/n.void gantt_chart() { int I.Wt[n]+Bu[n]). printf(“Average Turn Around Time=%3. for(i=1. for(i=2.i++) printf(“%d”. printf(“Average waiting time=3.pname[i]). Wt[1]=0. Att=(float)Ttt/n. printf(“Gauntchart”). } } void fcfs() { int I.i<=n.i<=n.i++) { Twt=Twt+(Wt[i]-A[i]).2f ms”.i++) printf(“%s”.i<=n.j++) { 12 .

temp1=A[i].c[i]). fcfs(). ganttchart().j<=n. clrscr() printf(FIRST COME FIRST SERVED ALGORITHM). } Output: 13 . A[j]=temp1. Bu[j]=temp. getdata(). getch().j++) { if(A[i]>.pname[j]). strcpy(pname[j]. } } } calculate().j++) { if(A[i]>A[j]) { temp=Bu[i]. Bu[i]=Bu[j]. strcpy(c[i]. } main() { int ch.j<=n. A[i]=A[j].for(j=i+1. strcpy(pname[i].pname[j]).

Enter the number of processes: 3 Enter the process name: a Enter the burst time for process a: 12 Enter the arrival time for process a: 0 Enter the process name: b Enter the burst time for process b: 24 Enter the arrival time for process b: 2 Enter the process name: c Enter the burst time for process a: 2 Enter the arrival time for process a: 1 Average Turn around time= 20. 14 .67 ms GANTT CHART A 0 12 B 14 C 38 Result Thus the c program for FFS algorithm was executed successfully.33 ms Average waiting time=7.

2. Initialize wait time and turn around time of first process as 0.SHORTEST JOB FIRST SCHEDULING Aim: To write and execute a C Program to implement the Shortest job first scheduling algorithm. 2. Swap the process according to their burst time and calculate it as sum of it and burst time. call sjf() to perform the sjf scheduling algorithm getdata () 1. Call getdata () to get process name. sjf () 1. Get the number of process from the user. Algorithm: 1. Initialize s[i] for each process as ‘7’ 4. Store the burst time for each process in a temporary arry. 5. Set w as WTB[1] and s[1] as F (i)While w<Tt. burst time and arrival time for the user. 2.do the following Set I as 2 While i<=n do the following 15 . 3. burst time and arrival time from the user. For each process get the process name.

12. 13. 11. 3. wait time and burst time and the arrival time of the current process 10. cal Gantt-chart() Ganttchart () 1. 6. Display the waiting time of each process. Calculate total waiting time. Ttt as the difference between sum of Ttt. Set w as w+B[i] and t as w. do the following 1. Calculate the average turn around time. calculate the total sum around time. 9. Set waiting time of I ith process as w and s[i] as ‘F’ 2. 7. Display the all process times 2. Display the average waiting and turn around time. do the following steps. Set I as 2. twt. 16 . 8. For each process. as the sum of bwt and the difference between the wait time and arrival time of current process. Else increment I by 1.(ii) if s[i] =’T’ and A[i] <=t. Calculate the average wait time.

pname[i]). 17 . printf(“\n \n\t\t\t GANTT CHART\n”). void getdata() { int I. scanf(“%d”.i<=n. scanf(“%d”. void sjf().bu[20].h> #include<conio. printf(“\n-------------------------------\n”).&a[i]).pname[i]). char pname[20][20].ttt. void getdata(). printf(“\n Enter arrival time for process %s”.b[20].&n). printf(“\n Enter the number of procesors:”). printf(“\n Enter burst time for process %s”. void gantt_chart(). scanf(“%d”.h> int n.Program: #include<stdio. scanf(“%s”.&bu[i]).&pname[i]). for(i=1.a[20].att. } } void gantt_chart() { int I. printf(“\n\n Enter the process name:”).twt. float awt.i++) { fflush(stdin).

i<=n.wt[n]+b[n]). printf(“\n----------------------------------\n”). w=0. printf(“\n”). } for(i=1.i++) { for(j=3.j<=n. for(i=1.temp.i++) printf(“\t%s\t”.i++) printf(“%d\t\t”.pname[i]). printf(“\n”). } void sjf() { int w.tt=0.i++) { b[i]=bu[i]. printf(“\t\n”).for(i=1.c[20][20]. tt=tt+b[i]. int temp1.j++) { if(b[j-1]>b[j]) { temp=b[j-1]. for(i=1.i<=n. char s[10]. printf(“\n--------------------------------\n”).j. twt=ttt=0.t. printf(“%d”.wt[i]). 18 .i<=n.I. s[i]=’T’.i<=n.

w=w+b[i]. s[1]=’F’. s[i]=’F’. strcpy(c[j-1]. b[j]=temp. strcpy(pname[j]. strcpy(pname[j-1]. } } } wt[1]=0. while(i<=n) { if(s[i]==’T’&&a[i]<=t) { wt[i]=w.pname[j]). i=2. } else i++. t=w. t=w.temp1=a[j-1]. b[j-1]=b[j]. w=w+b[1]. a[j]=temp1. a[j-1]=a[j].pname[j-1]).c[j-1]). while(w<tt) { i=2. } 19 .

} for(i=1. gantt_chart(). ttt=ttt+(wt[i]+bu[i]-a[i]). awt=(float)twt/n. sjf(). } att=(float)ttt/n. } 20 .2f ms”. printf(“\n\nAvg Turnaround time :%3. } void main() { printf(“\n\n shortest job first \n\n”).2f ms”awt). printf(“\n\n Avg waiting time :%3.i<=n.att). getdata().i++) { twt=twt+([i]-a[i]).

00 ms Avg waiting time: 60.Output: Shortest job first Algorithm Enter number of processors:3 Enter the process name:aa Enter burst Time for process aa: 56 Enter arrival time for process aa: 1 Enter the process name:bb Enter burst time for process bb: 77 Enter arrival time for process bb: 3 Enter the process name:cc Enter burst Time for process cc: 89 Enter arrival time for process cc: 5 Avg Turn around time: 134. 21 .00 ms GANTT CHART ------------------------------| aa | bb | cc 0 56 133 | 222 --------------------------------------------------------------- Result: Thus the C program to implement the shortest Job First algorithm was executed successfully.

2. Call getdata () to get process name. 3. burst time and arrival time from the user. getdata () 1. Start the burst time of each process in temp array. burst time and arrival time from the user. 4. do the following i) Set i as 2. Swap the processes according to their priorities and calculate it has sum of Tt and burst time of the current process. Initialize s[i] for each process as ‘T’. prior () 1. burst time and arrival time for the user. Algorithm: 1. 2.PRIORITY SCHEDULING ALGORITHM Aim: To write and execute a c program to implement the priority scheduling algorithm. ii)) until i<=n do the following 1)if s[i]=’T’ and T[i] <=t. set w as w+B[i] and S[i] as F While w<Tt. 3. Get the number of process from the user. 22 . 2 Call prior () to perform the priority scheduling algorithm.do the following a) Set waiting time of ith process as w and s[i] as f b) Set w as w+B[i] and t as w c) Set I as 2 ii) Else increment I by 1. For each process get the process name. For each process get the process name. Initialize the wait time and turn around time of first process as 0.

void ganttchart(). printf(“\n enter the no of the processes”).pname[i]). char pname[20[20].i<n. printf(“\n\n enter the burst time for process %s”. float awt.att.h> int n. Display the average waiting turn around time 11. scanf(“%d”. For each process do the following steps a) Calculate total wait time twt as the sum of twt and the difference between the wait time and the arrival time of the current process. void getdata() { int I.h> #include<string. scanf(“%d”.a[20].&pname).h> #include<string. printf(“\n\n enter the process name”). scanf(“%d”.bu[20]. b) Calculate total turn around time ttt and the difference between sum of the Ttt wait time and burst time and arrival time of current process.ttt.i++) { fflush(stdin).twt.&bu[i]).b[20]. 23 .8. Call Gantt chart Program: #include<stdio. void prior(). for(i=1.pname[i]).wt[20]. void getdata().&n). 9. printf(“\n\n enter the arrival time for process %s”.p[20]. Calculate the average turn around time 10.

j char s[10]. printf(“\n\n Gant chart”).tt=0.wt[n]+b[n]).i++) printf(“/t %s \t”.i<n.i++) printf(“/t %d \t”wt[i]). printf(“\t\n”).pname[i]). printf(\n------------------------------\n”). tt=tt+b[i]. for(i=0.temp.scanf(“%d”.&a[i]).i<=n. printf(“\t\n”). twt =ttt=0. int temp1. for(i=1. printf(“%d”. for(i=0. printf(\n------------------------------\n”). s[i]=’T’.c[20][20]. printf(“/n”). printf(\n------------------------------\n”). }} void gantt_chart() { int I.i++) { b[i]=bu[i]. } 24 .i<n. } void prior() { int w. w=0.t.

pname[j]).j<=n.for(i=1. b[j-1]=b[j]. w=w+b[l]. t=w. p[j-1]=p[j]. a[j-1]=a[j].pname[j-1}).i<=n. while(i<=n) { if(s[i]==’T’ && a[i]<=t) 25 . s[l]=’F’.c[j-1]). b[j]=temp. strcpy(pname[j-1]. a[j]=temp1.i++) { for(j=3. temp1=a[j-1]. temp=p[j-1].j++) { if(b[j-1]>b[j]) { temp=b[j-1]. strcpy(pname[j]. p[j]=temp. } } } wt[1]=0. while(w<tt) { i=2. strcpy(c[j-1).

gantt_chart().2f ms”. } else i++. } att=(float)ttt/n. getdata().{ wt[i]=w.awt). i=2. printf(“\n \n avg waiting time :%3. printf(“\n \n priority scheduling algorithm”).att). w=w+b[i].i++) { twt=twt+(wt[i]-a[i]). pruior(). t=w. awt=(float)twt/n. ttt=ttt+((wt[i]+bu[i]-a[i]).2f ms”. s[i]=’F’. } void main() { clrscr(). printf(“\n\n Avg turn around time:%3. } 26 . } } for(i=1.i<=n.

00ms Avg waiting time=4. 27 .00ms Result: Thus the C program to implement the priority scheduling algorithm was executed successfully.Output: Priority scheduling algorithm: Enter the number of processors: 3 Enter the process name: a Enter burst time for process a: 12 Enter arrival time for process a: 0 Enter the priority for process a: 1 Enter the process name:a Enter burst time for process a: 12 Enter arrival time for process a: 0 Enter the priority for process a: 1 Enter the process name:a Enter burst time for process a: 12 Enter arrival time for process a: 0 Enter the priority for process a: 1 Avg Turn around time=9.

28 . Call rr() to perform the RRS algorithm. 3. rr () 1. Calculate the average wait time and average time around. 2. 2. Synchronize the process such that each process is allocated with cpu for a time slice. Display the result. Call getdata() to get process name. Similarly calculate the number of slice for each process. 2. Calculate () 1.ROUND ROBIN SCHEDULING ALGORITHM Aim: To write and execute a C Program to implement the Round Robin Scheduling. 3. 4. Get the number of processor from the user. Calculate the wait time and turnaround time. Display the average a wait time and average turn around. get data() 1. burst time and arrival time from the user. Algorithm: 1. Call calculates (). 2. 5. For each process get the process name and burst time from the user. 3. Get the time quantum from the user. If process time is less than time slice means assigns the number of slice as 1.

j.j++) { i=1.Awt. float Att.n. void getdata(). scanf(“%d”.Tu=0. void rr().j<n. } 29 . for(i=0. scanf(“%s”.i<n.pname[15][15]. printf(“\n\n Enter the process name”).&Bu[i][j]).&pname[i]).Program: #include<stdio. } } void cal. void calculate().h> #include<conio.Twt=0. scanf(“%d”. void getdata() { printf(“\n\n Enter the number of process”).h> int Bu[15].k=0. printf(“\n Enter the Burst Time for Process %s”.a[15][15].I. Ttt+a[i-1][j].cuate() { for(j=0.&n).q.pname[i]). while(a[i][j]!=0) i+=1.i++) { j=0.

j<15.2f ms”. A[2*j+1][i]=k. K+=Bu[i][j]. printf(“5d\t%s\t%d\n”.K+bu[i][j]).j<15. printf(“\n\n average Waiting Time=%3.K. } 30 . printf(“\n\n Average Turn around time=%3. for(i=0. } void rr() { printf(“\n\n”). K+=q. for(j=0.i++) for(j=1.Att).K.(k+q)).pname[i]. for(i=0. printf(“%d\t%s\t%d\n”.Awt).pname[i].j++) { A[2*j][i]=k.Att=(float)Ttt/n. if((Bu[i][j]<=q)&&(Bu[i][j]!=0) { Bu[i][j+1]=G.i<n. } else if([i][j]!=0 { Bu[i][j+1]=Bu[i][j]-q. A[2*j+1][i]=k.j++) if((a[j][i]!=0)&&(a[j+1][i]!=0)&&((j+1)%2==0)) Awt=(float)Twt/n.i++) Twt+=a[0][i].i<n.2f ms”.

A[2*j+1][i]=0. printf(“\n\n Round Robin Scheduling Algorithm\n\n”). getdate(). } } calculate(). rr(). } void main() { clrscr().else { A[2*j][i]=0. getch(). } 31 .

67ms Result: Thus the C program to implement the Round Robin Scheduling algorithm was executed. 32 .OUTPUT: ROUND ROBIN SCHEDULING ALGORITHM Enter the number of processes: 3 Enter the process name: A Enter the burst Time for Process: 24 Enter the Process name: B Enter the Burst Time for the Process: 3 Enter the process name: C Enter the burst Time for the Process: 3 Enter the Time Quantum: 4 0 4 7 10 14 18 22 26 A B C A A A A A 4 7 10 14 18 22 26 30 Average Turn Around time=15.67ms Average Waiting Time=5.

p=pipe(feildes).b[15]={0}. Program #include<stdio. then execute the placement process. Display the odd number. Algorithm 1.feildes[2]. if(k==0) { printf(“Parent process”).p. Check the value of k i) If the value of k is equal to zero . 33 .j=0. k=fock(). 5. then execute the child process.INTER PROCESS COMMUNICATION USING PIPES Aim To write a C program for implementation of pipes.h> main() { int k.n2. ii) Parent process gets the total number.h> #include<unistd. 2. i) If the value of k is !equal to -1. Call the fork() 3. Initialize the feildes vector. 4.n. then it is consider as odd number. if the remainder value is not equal to zero.a[15]={0}.I. ii) Get the number from parent process and divide the number by 2. iii) Repeat the step 2 for all the numbers in parent process.

&n).i++) { scanf(%d”. j++.c[i]).sizeof(n)). printf(“Enter the total number”).getpid()).sizeof(n)). write(feildes[1]. if((b[i]%2)!=0) { c[j]=b[i].printf(“the parent process ID is %d”. } 34 .i++) { read(feildes[0].i++) printf(“%d”.&a[i]. for(i=0. } } printf(“Odd numers are”) for(i=0. printf(“Enter the total number”). } } else if(k!=-1) { printf(“the child process ID is : %d”.&b[i].sizeof(n2)).i<j. write(feildes[1].i<n2. } else printf(“error infoking”). scanf(%d”.sizeof(n)). read(feildes[1].&n2.&a[i]). for(i=0.i<n.getpid()).&n.

35 .Output Parent process the parent process ID is 2755 the child process ID is 2754 Enter the total number 5 Enter the total numbers 1 3 4 6 7 Odd numers are 1 3 7 Result Thus the program for pipes are implemented and verified successfully.

f).kbhit.flag=1. Stop the program.h> int n. 4.until a key stroke is not available. Algorithm: 1.f). n=n+1. void producer() { n=n+1. the buffer is not empty the item is consumed from the buffer 5.f=0. Program: #include<stdio. printf(“\n Producer has produced %d\n”.PRODUCER CONSUMER PROBLEM Aim: To write and execute a c program to implement producer consumer relationship. f=f+1. Read the buffer length from the user. } 36 . Repeat the following steps. 3. 2. if the buffer is empty the items is producer and is store in the buffer. } void consumer() { printf(“\n consumer has consumed %d\n”.

void main() { char s. printf(“\n Enter the buffer state”).&s). while(!kbhit) { if(n==0) { producer(). } } } 37 . scanf(“%c”. else n=1. if(s==’e’) n=0. if(n==1 consumer().

Output: Enter the Buffer state:e Producer has produced 1 Consumer has consumed 1 Producer has produced 2 Consumer has consumed 2 Producer has produced 3 Consumer has consumed 3 Producer has produced 4 Consumer has consumed 4 Result: Thus the C Program to implement Producer consumer relationship was executed successfully. 38 .

scanf(“%d”. printf(“Enter the max size”). scanf(“%d”.i<m. for(i=0.size3=0.. for(i=0.&m). clrscr().h> void main () { int c. printf(“Simulation of paging”).i<m. Fill element in each page randomly to the wish of the user.i+1).j++) a[i][j]=0.m.&k). Algorithm 1 2 3 4 Consider a matrix and assume itself as it is maintain the no of pages from user.k=0. printf(“……………………. 39 . Program #include <stdio.”). Depending upon the element size place is first phase display it.i++) for(j=0.i++) { printf(“Enter the number of elements to be inserted in page %d”.a[10][10].I. Get the element to be placed in main memory.FIRST FIT ALGORITHM Aim: To write and execute a c program to stimulate placement technique of first fit algorithm.j<m.j.n[10].h> #include <conio.small.

for(i=0.a[i][j]).. } } for(i=0.j++) a[i][j]=n[i]. } break.i<m.i<m.i++) printf(“page %d”.i<m. for(i=0.i<m.i++) { for(j=0.i++) printf(“page %d”.i+1). scanf(“%d”. printf(“……………………………………….j++) printf(“%d”.n[i]=k.i<m. for(i=0.i++) for(j=0. printf(“APPEARANCE OF MAIN MEMORY”).j<(size3+n[i]).i++) { if(size3<=(m-n[i]) { for(j=n[i].i+1). for(i=0.&size3).j<m. } for(i=0.”).i<m.j<m.i++) 40 . } printf(“FIRST FIT ALGORITHM”). printf(“Enter the element size”).j++) { a[i][j]=i+1.

a[i][j]). } Output SIMULATION OF PAGING Enter the maximum size 4 Enter the number of elements to be inserted in page 1=1 Enter the number of elements to be inserted in page 2=2 Enter the number of elements to be inserted in page 3=3 Enter the number of elements to be inserted in page 4=4 41 .{ for(j=0. } getch().j<m.j++) printf(“%d”.

APPEARENCES OF MAIN MEMORY PAGE1 1 0 0 0 PAGE2 2 2 0 0 PAGE3 3 3 3 0 PAGE4 4 4 4 4 FIRST FIT ALGORITHM Enter the element size 2 PAGE1 1 1 1 0 PAGE2 2 2 0 0 PAGE3 3 3 3 0 PAGE4 4 4 4 4 Result Thus the c program in Linux to stimulate the placement technique is executed and output verified successfully. 42 .

for(i=0.j<m.k=0. printf(“……………………. clrscr(). Algorithm 1 2 3 4 5 Consider the matrix and assume the matrix itself as if maintain the pages from user.BEST FIT ALGORITHM Aim To write and execute a c program in Linux to stimulate the placement technique in best fit algorithm.size3=0. for(i=0. Fill element in each page randomly.m.z. printf(“Enter the max size”).i<m.p=i.i<m.j++) a[i][j]=0.I.i++) for(j=0.i++) 43 .a[10][10]. Program #include <stdio..j.h> #include <conio.h> void main () { int c. scanf(“%d”. Depending upon the element size. Cut the element to be placed in main memory.&m).n[10]. Display it. printf(“Simulation of paging”).”).

i++) printf(“page %d”. printf(“……………………………………….&size3). scanf(“%d”.i++) { for(j=0.{ printf(“Enter the number of elements to be inserted in page %d”. 44 . for(i=0.i<m.i+1).i++) for(j=0.j++) a[i][j]=n[i].”).i++) printf(“page %d”.j++) printf(“%d”.i<m. printf(“Enter the element size”). for(j=z-size3. for(i=0.a[i][j]). n[i]=k.&k).j++) a[i][j]=size3. scanf(“%d”.i<m. for(i=0.z--) { for(i=0.i<10.j<(size3+n[i]). } printf(“BEST FIT ALGORITHM”).i+1). for(z=m.i++) { if(size3<=(z-n[i]) { c=n[i]. } for(i=0.z>0..i<m.j<m. printf(“APPEARANCE OF MAIN MEMORY”).j<m.i+1).

i++) { for(j=0.for(i=0.j<m. } Output SIMULATION OF PAGING Enter the maximum size 4 Enter the number of elements to be inserted in page 1=1 Enter the number of elements to be inserted in page 2=2 Enter the number of elements to be inserted in page 3=3 Enter the number of elements to be inserted in page 4=4 45 . } getch().i<m.a[j][i]).j++) printf(“%d”.

46 .APPEARENCES OF MAIN MEMORY PAGE1 1 0 0 0 PAGE2 2 2 0 0 PAGE3 3 3 3 0 PAGE4 4 4 4 4 BEST FIT ALGORITHM Enter the element size 2 PAGE1 1 0 0 0 PAGE2 2 2 2 2 PAGE3 3 3 3 0 PAGE4 4 4 4 4 Result Thus the c program in Linux to stimulate the placement technique is executed successfully.

printf(“……………………. Cut the element to be placed in it.j.k=0.WORST FIT ALGORITHM Aim To write and execute a c program in Linux to stimulate the worst fit algorithm. clrscr().m.”).i++) for(j=0.&m). Depending upon the element size and place.I.h> #include <conio. for(i=0. scanf(“%d”.. Printf(“Enter the max size”).i++) { printf(“Enter the number of elements to be inserted in page %d”.j<m.z.i<m.h> void main () { int c.n[10]. Display it.j++) a[i][j]=0. Fill element in each page randomly.size3=0.a[10][10]. 47 . Algorithm 1 2 3 4 5 Consider a matrix and assume the matrix itself as it maintains the pages. printf(“Simulation of paging”).q=0.small. Program #include <stdio.i<m.i+1). for(i=0.

j<m.i++) printf(“page %d”. scanf(“%d”..j++) a[q][j]=q+1.&k).&size3).j++) a[i][j]=n[i]. printf(“Enter the element size”).i<m.i<m. } for(i=0.”).i++) { if(n[i]<small) { q=i.i+1). } printf(“WORST FIT ALGORITHM”). for(i=1.i<m. small=n[0]. printf(“……………………………………….i<m. for(i=0.a[i][j]).i<m. n[i]=k. for(i=0.i++) 48 . for(i=0.j<n[i]. } for(j=n[q].i++) for(j=0.j++) printf(“%d”.scanf(“%d”.j<(size3+n[q]). } for(i=0.i++) { for(j=0.i<m.i+1).i++) printf(“page %d”. printf(“APPEARANCE OF MAIN MEMORY”).

{ for(j=0.a[i][j]). } Output SIMULATION OF PAGING Enter the maximum size 4 Enter the number of elements to be inserted in page 1=1 Enter the number of elements to be inserted in page 2=2 Enter the number of elements to be inserted in page 3=3 Enter the number of elements to be inserted in page 4=4 49 .j++) printf(“%d”.j<m. } getch().

APPEARENCES OF MAIN MEMORY PAGE1 1 0 0 0 PAGE2 2 2 0 0 PAGE3 3 3 3 0 PAGE4 4 4 4 4 WORST FIT ALGORITHM Enter the element size 2 PAGE1 1 1 1 0 PAGE2 2 2 0 0 PAGE3 3 3 3 0 PAGE4 4 4 4 4 Result Thus the c program to stimulate the technique of worst fit algorithm is executed successfully. 50 .

3. for(i=0. 51 . clrscr(). npgfrm. rfstr[25]. check rfstr=pgfrm then 5. ++i) { cout << "\n Enter the reference String . stop Program #include<iostream. continue the loop 6. int pgfrm[5]. get the page frame and reference string from the user. cout << "\nEnter the Number of Reference String : ". pgflt. start 2. Display the pgfrm. cout << "\nEnter the Number of Page Frames : ". initialize rfstr[25]." << i << " : ".pgfrm[5] 4.h> void main() { int i. j. find the page faults 7. cin >> npgfrm. cout << "\n FIFO PAGE REPLACEMT TECHNIQUE". cin >> nrfstr. k. 8. nrfstr.h> #include<conio. i<nrfstr.FIFO PAGE REPLACEMENT ALGORITHM Aim: To write and execute a C++ Program of Page Replacement algorithm Algorithm: 1.

for(i=0. pgflt=0.getch(). } for(i=0. j<npgfrm. cout << "\n Press Any key to Continue. j < npgfrm && i < nrfstr. ++i) pgfrm[i] = -1. } Output 52 .\n\n".. for(j=0. i<nrfstr. if(pgfrm[j] != rfstr[i]) { pgfrm[j++] = rfstr[i]. ++k) if(pgfrm[k] == rfstr[i]) ++i. i<npgfrm.. ++i ) { for(k=0. ++j) cout << endl << pgfrm[j]. } } --i.cin >> rfstr[i]. getch(). } cout << "\nNumber of Page Faults = " << pgflt. k<npgfrm. ++pgflt. ++i) { for(j = 0 .

3 : 2 Enter the reference String .10 : 0 Enter the reference String .7 : 4 Enter the reference String .1 : 0 Enter the reference String .0 : 7 Enter the reference String .4 : 0 Enter the reference String .Enter the Number of page Frames : 3 Enter the Number of Reference String : 20 FIFO PAGE REPLACEMENT TECHNIQUE Enter the reference String .17 : 7 Enter the reference String .13 : 1 Enter the reference String .18 : 0 Enter the reference String .16 : 1 Enter the reference String .6 : 0 Enter the reference String .15 : 0 Enter the reference String .8 : 2 Enter the reference String .19 : 1 53 .9 : 3 Enter the reference String .2 : 1 Enter the reference String .12 : 2 Enter the reference String .5 : 3 Enter the reference String .14 : 2 Enter the reference String .11 : 3 Enter the reference String .

.. 0 1 2 Press Any Key To Continue.. 4 2 3 Press Any Key To Continue. 54 . 2 3 0 Press Any Key To Continue.. 7 0 1 Number of Page Faults = 15 Result: Thus the c++ program to page replacement algorithm is executed successfully..7 0 1 Press Any Key To Continue....

Sign up to vote on this title
UsefulNot useful