Sei sulla pagina 1di 80

10IT44

EX.NO: 01 DATE: 13.12.2011

GREATEST OF THREE NUMBERS

AIM: To write a shell program to find the greatest of three numbers.

ALGORITHM:

Step 1: Start the program. Step 2: Read the values of a, b and c. Step 3: Use if conditions to check whether a greater than b greater c. Step 4: Print the values of a. Step 5: Else check if b greater than c. Step 6: If true, print the value of b else print the value of c. Step 7: Stop the program.

10IT44

SOURCE CODE: echo enter the three number read a read b read c if [ $a gt $b] && [ $a gt $c] then echo greater is: $a elif [ $b gt $c] then echo greater is: $b else echo greater is: $c fi

10IT44

OUTPUT:

Enter the three numbers 8 5 9 Greatest is: 9

10IT44

RESULT: Thus the shell program to find the greatest of three numbers has successfully executed and output is verified.

10IT44

EX.NO: 02 DATE: 13.12.2011

FIBONACCI SERIES

AIM: To write a C program to find the Fibonacci series of a given number.

ALGORITHM:

Step 1: Start the program. Step 2: To initialize the value of variable. Step 3: Declare the function fibo(n). Step 4: Initialize if a=0,b=0 and c variables. Step 5: Check for loop to find the Fibonacci series. Step 6: Stop the program.

10IT44

SOURCE CODE: #include<stdio.h> void fibo(int); main() { int n; clrscr(); printf(\n enter the no:); scanf(%d,&n); fibo(n); getch(); } void fibo(int n) { int i,a=0,b=1,c; printf(fibonacci series is \n); printf(%d \n ,a); printf(%d \n,b); for(i=2;i<=n;i++) { c=a+b; a=b; b=c; printf(%d \n,c);}}
6

10IT44

OUTPUT:

enter the no: 5 Fibonacci series is 0 1 1 2 3 5

10IT44

RESULT: Thus the program to find the Fibonacci series has executed and output is verified.

10IT44

EX.NO: 03 DATE: 20.12.2011

I/O SYSTEM CALL

AIM: To write a C program to implement I/O system call.

ALGORITHM:

Step 1: Start the program. Step 2: Include the required header files. Step 3: Declare the variables fd and k as integers and s as character. Step 4: Using if conditions check the value not equal to 2. Step 5: Print the file name of the value not equal to 2. Step 6: Using while loop read the file and close. Step 7: Stop the program.

10IT44

SOURCE CODE: #include<unistd.h> #include<stdio.h> #include<fcntl.h> Int main(int argc,char *argv[]) { Int fd,k; Char s[200]; If(argc!=2) Printf(usage:a.out<filename>\n); Else { Fd=open(argv[1],O_CREAT|O_APPEND|O_WRONLY,077); While(k-read(0,s,200)>0) { While(fd,s,200) Close(fd); } } }

10

10IT44

OUTPUT:

[cselab@serverlinux ~]$ cc o append io.c [cselab@serverlinux ~]$cat>doc1 Hi

[1]+ stopped cat>doc1 [cselab@serverlinux ~]$./append doc1 Its me OS

[2]+ stopped ./append doc1 [cselab@serverlinux ~]$ cat doc1 Hi Its me OS.

11

10IT44

RESULT: Thus the program for I/O system calls has executed and the result is verified.

12

10IT44

EXNO: 04 DATE: 20.12.2011

FORK SYSTEM CALL

AIM: To write a C program to implement fork system call.

ALGORITHM:

Step 1: Start the program. Step 2: Include the header files. Step 3: Declare the variables i,j. Step 4: Using if condition create a new process using fork system call. Step 5: In parent get pid, ppid using get() function. Step 6: Use for loop to get id, ppid for child process unit it terminated. Step 7: Stop the program.

13

10IT44

SOURCE CODE: #include<sys/types.h> #include<stdio.h> main() { int i,j; if(fork()) { printf(\t\t in parent \n); printf(\t\t paid=%d and ppid=%d \n\n,get pid(),get pid()); for(i=0;i<50;i=i+5) { for(j=0;j<1000;j++) printf(\t\t in parent %d \n,i); } wait(0); printf(in parent: now the child has terminated \n); } else { printf(\t in child \n); printf(\t\t pid=%d and ppid=%d \n\n,get pid(),get pid()); for(i=0;i<50;i=i+10) { for(j=0;j<1000;j++) printf(\t\t in child %d \n,i);}}}
14

10IT44

OUTPUT:

In child pid=11850 and ppid=11849 in child 0 in child 10 in child 20 in child 30 in child 40

In parent pid=11849 and ppid=17629 in parent 0 in parent 5 in parent 10 in parent 15 in parent 20 in parent 25 in parent 30 in parent 35 in parent 40 in parent 45 in parent: now the child has terminated

15

10IT44

RESULT: Thus the program for fork system call was successfully executed and output is verified.

16

10IT44

EXNO: 05

IMPLEMENTATION OF EXEC() SYSTEM CALL

DATE: 27.12.2011

AIM: To write a C program to implement exec() system call.

ALGORITHM:

Step 1: Start the program. Step 2: Include the header files. Step 3: Declare the variable at argc, argv[]. Step 4: Print the value of get pid. Step 5: Stop the program..

17

10IT44

SOURCE CODE: #include<stdio.h> #include<unistd.h> main(argc,argv) int argc; char*argv[]; { printf(before exec my id is %d \n,get pid()); printf(my parent process id is %d \n,get ppid()); printf(exec starts \n); execl(argv[1],argv[2],argv[3],argv[4],(char*)0); printf(this will not print \n); } .... #include<stdio.h> #include<unistd.h> main(argc,argv) int argc; char*argv[]; { printf(after the exec %d \n,get pid()); printf(child %d \n,get pid()); printf(exec ends \n);}
18

10IT44

OUTPUT:

Before exec my id is 7715 My parent process id is 5523 Exec starts This will not print After the exec 7935 Child 7935 Exec ends

19

10IT44

RESULT: Thus the program for exec() system call was executed successfully and output is verified.

20

10IT44

EX.NO: 06 DATE: 27.12.2011

INTER PROCESS COMMUNICATION USING SHARED MEMORY

AIM: To write a C program to implement inter process communication using shared memory.

ALGORITHM:

Step 1: Start the program. Step 2: Create the child process using fork(). Step 3: Create the shared memory per parent process using shmget(). Step 4: Now attach the same shared memory to the child process. Step 5: The data in the shared memory is load by the child process using shmget pointer. Step 6: Now detach and release the shared memory program. Step 7: Stop the program.

21

10IT44

SOURCE CODE: #include<stdio.h> #include<sys/shm.h> #include<sys/ipc.h> int main() { int child,shrmid,i; char*shmptr; child=fork(); if(!child) { shmid=shmget(2041,32,0666/IPC-CREATE); shmptr=shmat(shmid ,0,0); printf(\n parent writing \n); for(i=0;i<10;i++0) { shmptr[i]=a+i; put char(shmptr[i]); } printf(\n\n %s,shmptr); wait(NULL); } else {
22

10IT44

shmid=shmget(2041,32,0666); shmptr=shmat(shmid,0,0); printf(\n child is reading:); for(i=0;i<10;i++) put char(shmptr[i]); shmdt(NULL); shmctrl(shmid,IPC-RMID,NULL); } return 0; }

23

10IT44

OUTPUT:

PARENT WRITING abcdefghij abcdefghij CHILD IS READING: a b c d e f g h i j

24

10IT44

RESULT: Thus the program to implement inter process communication using shared memory has successfully executed and output is verified.

25

10IT44

EX.NO: 07

INTER PROCESS COMMUNICATION USING PIPES

DATE: 03.01.2012

AIM: To write a C program to implement the inter process communication using pipes.

ALGORITHM:

Step 1: Start the program. Step 2: Create the child process using fork(). Step 3: Now change the read of the place. Step 4: Process using close(). Step 5: Create the pipe structure using pipes. Step 6: Write the data in the pipe using write(). Step 7: Display the string. Step 8: Stop the program.

26

10IT44

SOURCE CODE: #include<stdio.h> int main() { int fd[2],child; char a[10]; printf(\n Enter the string to enter into pipe:); scanf(%s,a); pipe(fd); child=fork(); if(!child) { close(fd[0]); write(fd[1],a,5); wait(0); } else { close(fd[1]); read(fd[0],a,5); printf(\n The string received from the pipe is %s\n,a); } return 0; }
27

10IT44

OUTPUT:

Enter the string to enter into the pipe: friends The string received from the pipe is friends

28

10IT44

RESULT: Thus the program to implement inter process communication using pipes has successfully executed and output is verified.

29

10IT44

EX.NO: 08 DATE: 24.01.2012

PRODUCER CONSUMER PROBLEM USING SEMAPHORE

AIM: To write a C program to implement producer consumer problem using semaphores.

ALGORITHM:

Step 1: Start the program. Step 2: Define a seek and implement producer consumer function which simulates signal and unit. Step 3: In producer, buffer full condition is checked and if it is full then it is displayed. Step 4: The both producer and consumer program produces randomly from main(). Step 5: Stop the program.

30

10IT44

SOURCE CODE: #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #define SIZE 5 int stack[SIZE],top=-1,in=0; void produce(); void consume(); main() { char ch; do { scanf(%s,&ch); switch(tolower(ch)) { case p: produce(); break; csae c: consume(); break; case q: exit(0);
31

10IT44

} }while(1); } } void produce() { int value; if(top<(SIZE-1)) { value = top +2; stack[++top]=value; printf(produce: %d,value); } else { printf(cannot produce); } } void consume() { if(top ==-1) printf(cannot consume); else printf(consume: %d,stack[top--]); }
32

10IT44

OUTPUT:

P Produce: 1 P Produce: 2 P Produce: 3 P Produce: 4 P Produce: 5 P Cannot produce C Consume: 5 C Consume: 4 C Consume: 3 C Consume: 2 C Consume: 1 C Cannot consume
33

10IT44

RESULT: Thus the program for producer consumer problem using semaphores has executed successfully and output is verified.

34

10IT44

EX.NO: 09 DATE: 31.01.2012

IMPLEMENTATION OF FCFS ALGORITHM

AIM: To write a C program to implement the FCFS algorithm for process scheduling.

ALGORITHM:

Step1: Create the child process using fork(). Step2: Create the pipes structure using pipe(). Step3: Now close the read end of parent process using close. Step4: Write data in pipe using write(). Step5: Read the data in pipe using read(). Step6: Display the string.

35

10IT44

SOURCE CODE: #include<stdio.h> #include<stdlib.h> main() { int i,j,pro[20],p,t1,t2,n,at[20],bt[20],tat[20],wt[20],ft[20]; printf(\n enter the no of process); scanf(%d,&p); for(i=1;i<=p;i++) { printf(enter the process no:); scanf(%d,&pno[i]); printf(enter the arrival time); scanf(%d,&at[i]); printf(enter the burst time); scanf(%d,&bt[i]); } for(i=1;i<p;i++) { for(j=i+1;j<=p;j++) { if(at[i]>at[j]) { t1=at[i];
36

10IT44

at[i]=at[j]; at[j]=t1; t2=bt[i]; bt[i]=bt[j]; bt[j]=t2; n=pno[i]; pno[i]=pno[j]; pno[j]=n;}}} ft[0]=at[1]; ft[1]=at[1]+bt[1]; tat[1]=ft[1]-at[1]; for(i=1;i<=p;i++) {ft[i+1]=ft[i]+bt[i+1]; tat[i+1]=ft[i+1]-a1[i+1]; wt[i]=ft[i+1]-at[i]; }pno[0]=0; printf(pno\t ft\t\n); for(i=1;i<=p;i++) { printf(%d\t,pno[i]); printf(%d\n,ft[i]); } printf(pno\t at\t bt\t tat\t wt\t \n); for(i=1;i<=p;i++) { Printf(%d\t %d\t %d\t %d\t %d\t %d\t \n,pno[i],at[i],bt[i],ft[i],tat[i],wt[i]);}}
37

10IT44

OUTPUT:

Enter the process number: 3 Enter the process no: 1 Enter the arrival time: 0 Enter the burst time: 6 Enter the process no: 2 Enter the arrival time: 0 Enter the burst time: 4 Enter the process no: 3 Enter the arrival time 0 Enter the burst time: 5

Pno 1 2 3

ft 6 4 9

Pno 1 2 3

at 0 0 0

bt 6 4 5

ft 0 4 9

wt 0 4 9

tat 0 0 4

38

10IT44

RESULT: Thus the program to implement FCFS algorithm has successfully executed and output is verified.

39

10IT44

EX.NO: 10 DATE: 07.02.2012

IMPLEMENTATION OF SJF ALGORITHM

AIM: To write a C program to implement SJF algorithm for process scheduling.

ALGORITHM:

Step 1: Start the program. Step 2: Declare the variables. Step 3: Get the number of process and burst time for each process. Step 4: Calculate turnaround time. Step 5: Display the calculated value and their average. Step 6: Stop the process.

40

10IT44

SOURCE CODE: #include<stdio.h> int main() { int burst[10],n,wt=0,tat=0,temp,temp1,p[10]; float twt=0,ttat=0,awt,atat; printf(Enter the number of process:); scanf(%d,&n); for(i=0,j=0;j<n;i++,j++) { printf(\n The burst time for the process number %d :,i+1); p[i]=i+1; } scanf(%d,burst[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(burst[i]>burst[j]) { temp=burst[i]; burst[i]=burst[j]; burst[j]=temp;
41

10IT44

temp1=p[i];; p[i]=p[j]; p[j]=temp; } } } for(i=0;i<n;i++) { twt=twt+wt; printf(\n The waiting time for process number %d is %d\n,p[i],wt); wt=wt+burst[i]; } awt=twt/n; printf(\n Average waiting time is %f\nawt); wt=0; for(i=0;i<n;i++) { tat=wt+burst[i]; printf(\n The turnaround time for process number %d is %d\np[i],tat); ttat=ttat+tat; } atat=ttat/n; printf(\n\n The average turnaround time is %f \n,atat); }
42

10IT44

OUTPUT:

Enter the number of process:3

The burst time for the process number1:5 The burst time for the process number2:6 The burst time for the process number3:7

The waiting time for process number1 is 0 The waiting time for process number2 is 6 The waiting time for process number3 is 11

The average waiting time is 5.3333

The turnaround time for process1 is 5 The turnaround time for process2 is 11 The turnaround time for process3 is 18

The average turnaround time is 11.3333

43

10IT44

RESULT: Thus the program to implement SJF scheduling algorithm has executed successfully and output is verified.

44

10IT44

EX.NO: 11

IMPLEMENTATION OF ROUND ROBIN ALGORITHM

DATE: 14.02.2012

AIM: To write a C program to implement Round Robin algorithm for process scheduling.

ALGORITHM:

Step 1: Start the program. Step 2: Initialize two variables. Step 3: Get the number of process and burst number.Step 4:calculate turnaround time. Step 4: Display the calculated value and their average. Step 5: Stop the program.

45

10IT44

SOURCE CODE: #include<stdio.h> #include<unistd.h> main() {int a[10],b[10],p[10],I,j,m,n,t1=0 ,w=0,t2=[10]; float avg.tot=0; printf(enter the number of process:); scanf(%d,&n); j=n;k=0; for(i=0;i<n;i++) { printf(process no); scanf(%d,&p[i]); printf(burst time); scanf(%d,&b[i]);} printf(enter the quantum no); scanf(%d,&t); m=n; for(i=0;i<n;i++) {If(b[i]>t) {printf(processing %d process \n,p[i]); printf(remaining time is:,b[i]-t); p[i]=p[j]; b[j]=b[i]-t; t1+=t; j++;
46

10IT44

sleep[2]; }else {Printf(process removed is %d\n,p[i]); k=p[i]-1; ti+=b[i]; w=t1-b[k]; t2[k]=w; printf(the remaining time is %d\n,t1); sleep(2);} else {Printf(\n process removed is %d\n,p[i]); k=p[i]-1; t1+=b[i]; w=t1-b[i]; t2[k]=w; printf(the remaining time is %d\n,t1); printf(\n waiting time is %d\n,w); sleep(2);} { n=j;} For(k=0;k<m;k++) tot=tot+t2[k]; printf(waiting time for process %d is %d \n ,k+1,t2[k],3);} Avg=tot/m; Printf(total waiting time is %f \n,tot); printf(average waiting is %f \n,avg);}
47

10IT44

OUTPUT:

Enter the no of process:2

Process no:1 Burst no:2 Prcess no:2 Burst time:5

Enter the quantum no:5

Process removed is 1 The remaining time is 2 The waiting time is 0

The process removed is2 The remaining time is 7 The remaining time for process 1 is 0

The waiting time for process 2 is 2

Tot waiting time is 2.00000 Avg waiting time is 1.00000

48

10IT44

RESULT: ` Thus the program to implement Round Robin algorithm has successfully executed and output is verified.

49

10IT44

EX.NO: 12 DATE: 21.02.2012

IMPLEMENTATION OF PRIORITY SCHEDULING

AIM: To write C program to implement priority scheduling algorithm for process scheduling.

ALGORITHM:

Step 1: Start the process. Step 2: Enter the number of process. Step 3: Get the process number,burst time and priority value. Step 4: Calculate turnaround time and waiting time. Step 5: Display the result. Step 6: Stop the process.

50

10IT44

SOURCE CODE: #include<stdio.h> struct {int p,pr,bt,tat,wt;} k[10],m; main() {int i,j,n; float twt=0,ttt=0; printf(\t\tPriority scheduling\n\n); printf(\nEnter the number of process :\n); scanf(%d,&n); for(i=1;i<=n;i++) {printf(\nEnter the process number:\n); scanf(%d,&k[i].p); printf(Enter the priority:); scanf(%d,&k[i].pr); printf(Enter the burst time:); scanf(%d,&k[i].bt);} for(i=1;i<=0;i++) {for(j=i+1;j<=n;j++) {if(k[i].pr<k[j].pr) {m=k[i]; k[i]=k[j]; k[j]=m;}
51

10IT44

if(k[i].pr=k[j].pr) {If(k[i].p>k[j].p) {m=k[i]; k[i]=k[j]; k[j]=m;}}}} k[i].wt=0; k[i].tat=k[i].bt; for(j=2;j<=n;j++) {k[j].wt=k[j-1].wt+k[j-1].bt; k[j].tat=k[j].wt+k[j].bt;} printf(\t Process \t Priority \t bt \t wt \t tat \n); for(i=1;i<=n;i++) { printf(\t %d \t \t %d \t \t %d \t \t %d \n,k[i].p,k[i].pr,k[i].bt,k[i].wt,k[i].tat); twt=twt+k[i].wt; ttt=ttt+k[i].tat; } printf(\nAverage waiting time:%f\n,(twt/n)); printf(\nAverage turnaround time:%f\n\n,(ttt/n)); }

52

10IT44

OUTPUT:

Priority scheduling

Enter the number of process:3 Enter the process number:1 Enter the priority:5 Enter the burst time:7 Enter the number of process:8 Enter the process number:3 Enter the priority:2 Enter the burst time:6

Process 1 2 3

Priority 5 7 2

bt 7 8 6

wt 0 7 15

tat 7 15 21

Average waiting time:7.33333 Average turnaround time :14.33333

53

10IT44

RESULT: Thus the program to implement priority scheduling has successfully executed and output is verified.
54

10IT44

EX.NO: 13 DATE: 28.02.2012

SIMULATION OF GREP COMMAND

AIM: To simulate the Unix command grep using C program.

ALGORITHM:

Step1: Start the program. Step2: Enter the grep<pattern><filename> Step3: Create a file which is mentioned in program. Step4: The pattern in specified file is read. Step5: Display all the pattern present in specified file. Step6: Stop the process.

55

10IT44

SOURCE CODE: #include<stdio.h> #include<string.h> int main(int argv,char*args[]) { FILE *F; char c; char str[100]; int i,flag,j,m,k; char ar[100]=hi; char temp[30]; if(arg v<3) { printf(usage:grep<pattern><filename>\n\n); return; } f=fopen(arg s[2],r); while(! Feop(f)) { i=0; while(1) scanf(%c,&c); { str[i--]=\0;
56

10IT44

break; } if(c==\n) { str[i++]=c; If(str ln(str)>=strln(arg s[1])) for(k=0;k<=strln(str)-strlnlarg s[1];k++) { temp[m]=\0; if(strcmp ctemp,arg s[1]==0) { printf(%s \n,str); break;}}} return 0; } .. .. #include<stdio.h> int main() {int a,b,c; printf(\n enter the two number to be added: ); scanf(%d%d,&a,&b); c=a+b; printf(\n the sum of the number is:%d,c);}
57

10IT44

OUTPUT:

Usage:

grep<pattern><filename>

Grep printf grep1.c Printf(enter the two numbers to be added:); Printf(the sum of the numbers are :%d,c);

58

10IT44

RESULT: Thus the program for the simulation of grep command has successfully executed and output is verified.

59

10IT44

EX.NO: 14 DATE: 06.03.2012

IMPLEMENTATION OF LS COMMAND

AIM: To implement the ls command using C program.

ALGORITHM:

Step 1: Start the program. Step 2: Include the header files. Step 3: Initialize the pid and assign pid=fork(). Step 4: Set the condition statement. Step 5: If statement is true print the result. Step 6: Stop the program.

60

10IT44

SOURCE CODE: #include<stdio.h> #include<unistd.h> int main() { int pid; pid=fork(); printf(pid value=%d\n,pid); if(pid<0) { Printf(\n fork failed\n); exit(1); } else if(pid==0) { execlp(/bin/ls,ls,-1,NULL); } else { wait(NULL); printf(\n child complete\n); exit(0); } }
61

10IT44

OUTPUT:

[cselab@serverlinux~]$./a.out

pid value=0 pid value=9408 child complete.

62

10IT44

RESULT: Thus the program to implement ls command has successfully executed and output is verified.

63

10IT44

EX.NO: 15

MEMORY MANAGEMENT-I

DATE: 13.03.2012 IMPLEMENTATION OF BEST FIT ALGORITHM

AIM: To write a C program to implement best fit algorithm for memory management.

ALGORITHM:

Step1: Start the process. Step2: Declare the size. Step3: Get the number of process to be inserted. Step4: Compare the hole. Step5: Display the values. Step6: Stop the process.

64

10IT44

SOURCE CODE: #include<stdio.h> int main() { int a[20],p[20],i,j,m,n, temp,b[20],temp1,temp2,c[20]; printf(Enter the number of blocks: ); scanf(%d,&n); for(i=0;i<n;i++) { printf(Enter the %d block size:,i); scanf(%d,&a[i]); b[j]=i; } printf(Enter the number of process:\n); scanf(%d,&m); for(i=0;i<m;i++) { printf(Enter size of %d process is:,i); scanf(%d,&p[i]); c[i]=i; } for(i=0;i<n;i++) { for(j=0;j<m;j++)
65

10IT44

{ if(a[i]<a[j]) { temp= a[i]; temp1= b[i]; a[i]=a[j]; b[i]=b[j]; a[j]=temp; b[j]=temp1; } if(p[i]<p[j]) {temp=p[i]; temp2=c[i]; p[i]=p[j]; c[i]=c[j]; p[j]=temp; c[j]=temp2;}}} for(i=0;i<n;i++) {for(j=0;j<m;j++) {if(p[j]<=a[i]) {printf(The process %d allocated to block %d \n,c[j],b[i]); p[j]=10000; break;}}} for(j=0;j<m;j++) {if(p[j]!=10000) {printf(The process %d is not allocated\n,j);}}}
66

10IT44

OUTPUT:

Enter the number of blocks: 3 Enter the 0 block size: 5 Enter the 1 block size: 7 Enter the 2 block size: 8 Enter the number of processes: 4 Enter the size of 0 process: 6 Enter the size of 1 process: 9 Enter the size of 2 process: 2 Enter the size of 3 process: 3 The process 2 is allocated to block 0 The process 0 is allocated to block 1 The process 1 is not allocated The process 2 is not allocated The process 3 is not allocated

67

10IT44

RESULT: Thus the program to implement best fit algorithm for memory management has successfully executed and output is verified.

68

10IT44

EX.NO: 16

MEMORY MANAGEMENT-I

DATE: 20.03.2012 IMPLEMENTATION OF FIRST FIT ALGORITHM

AIM: To write a C program to implement first fit algorithm for memory management.

ALGORITHM:

Step 1: Start the process. Step 2: Declare the size. Step 3: Get the no of process to be inserted. Step 4: Start the beginning of the set of holes. Step 5: Compare the values. Step 6: Display the values. Step 7: Stop the process.

69

10IT44

SOURCE CODE: #include<stdio.h> #include<process.h> {int a[20],p[20],i,j,m,n; printf("Enter no of blocks:"); scanf("%d",&m); for(i=0;i<m;i++) {printf("Enter the %d block size \n:",i); scanf("%d",&a[i]);} for(i=0;i<m;i++) {printf("enter the size of %d process:",i);

scanf("%d",&p[i]);} for(i=0;i<n;i++) {for(j=0;j<m;j++) {if(p[j]<=a[i]) {printf("The process %d allocated to the block %d \n",j,a[i]); p[j]=10000; break;}}} for(j=0;j<m;j++) {if(p[j]!=10000) printf("The process %d is not allocated \n",j);}}}

70

10IT44

OUTPUT:

Enter the number of blocks: 3 Enter the number of processes: 2 Enter the 0 block size: 4 Enter the 1 block size: 2 Enter the size of 0 process: 4 Enter the size of 1 process: 4 The process 0 allocated to the block 4 The process 0 allocated to block 2 The process 0 is not allocated The process 1 is not allocated

71

10IT44

RESULT: Thus the program to implement the first fit algorithm has successfully executed and output is verified.

72

10IT44

EX.NO: 17 DATE: 27.03.2012

MEMORY MANAGEMENT-II IMPLEMENTATION OF FIFO, LRU, PAGE REPLACEMENT ALGORITHM

AIM: To write a C program to perform memory management using FIFO, LRU, page management.

ALGORITHM:

Step 1: Start the program. Step 2: Include the header files. Step 3: Declare the datatype and variables. Step 4: Give values to variables. Step 5: Use condition statements for performing the operation. Step 6: Stop the process.

73

10IT44

SOURCE CODE: #include<stdio.h> #include<unistd.h> #include<stdlib.h> void FIFO_LRU(); void FIFO_LRU_OPT(); void checkhit(); int*refstring,*frame,*temp; int Rsize,Fsize,I,j,k,hit,fault=0,less,choice; int main() { printf(enter the size of replacing string); scanf(%d,&Rsize); refstring=(int*)malloc(Rsize *size of(int)); printf(enter the reference string); for(i=0;i<Rsize;i++) printf(%d,&refstring[i]); printf(\n enter the no of frames:); scanf(%d,&Fsize); frame=(int*)malloc(Fsize*sizeof(int)); temp=(int*)malloc(Fsize*sizeof(int)); while(1) { printf(\n 1.FIFO);
74

10IT44

printf(\n 2.LRU); printf(\n 3.OPTIMAL); printf(\n 4.EXIT); printf(\n enter ur choice:); scanf(%d,&choice); if(choice>3) exit(0); for(i=0;i<Fsize;i++) Frame[i]-temp[i]-0; switch(choice) { case 1:FIFO_LRU(); break; case 2:FIFO_LRU(); break; case 3:Fault=0; for(i=0;i<Rsize;i++) { checkhit(); if(hit!=1) { fault++; for(k=0;k<Fsize;k++) {for(j=j+1;j<Rsize;j++) if(frame[k]=refstring[i]) {temp[k]=j;
75

10IT44

break;} if(j==Rsize) temp[k]-k-Rsize-2; } FIFO_LRU_OPT(); } printf(%5d,refstring[1]); for(j=0;j<Fsize;j++) printf(%3d,frame[i]);} printf(no of pages faults,fault); break;}} return(0); } void FIFO_LRU() { fault=0; for(i=0;i<Rsize;i++) { checkhit(); if(hit!=1) { fault++; FIFO_LRU_OPT(); If(choice==1)
76

10IT44

temp[i]=i+1;} if(choice==2) temp[j]=j+1; printf(%5d,refstring[i]); for(j=0;j<Fsize;j++) printf(%d 3d,frame[i]); } printf(\n no of page fault %d,fault);} void FIFO_LRU_OPT() { less=temp[0]; for(j=0;j<Fsize;j++) if(less>temp[i]) less=temp[i]; for(j=0;j<Fsize;j++) if(less=temp[j]) break; frame[i]=refstring[i];} void checkhit() {hit=0; for(j=0;j<Fsize;j++) if(frame[j]=refstring[i]) {hit=1; break;}}

77

10IT44

OUTPUT:

Enter the size of replace string 3 Enter the reference string 3 2 5 Enter the no of frames:5

1.FIFO 2.LRU 3.OPTIMAL 4.EXIT

Enter your choice:1 3 3 3 3 3 3 2 2 2 2 2 2 1 1 1 1 1 1 No of page fault 3 1.FIFO 2.LRU 3.OPTIMAL 4.EXIT

Enter your choice: 2 3 3 3 3 3 3 2 2 2 2 2 2 1 1 1 1 1 1


78

10IT44

No of page fault 0

1.FIFO 2.LRU 3.OPTIMAL 4.EXIT

Enter your choice: 3 2 3 3 3 3 3 2 2 2 2 2 2 2 5 5 5 5 5 No of page fault 0

1.FIFO 2.LRU 3.OPTIMAL 4.EXIT Enter your choice: 4

79

10IT44

RESULT: Thus the program FIFO, LRU page replacement algorithm has successfully executed and output is verified.

80

Potrebbero piacerti anche