Sie sind auf Seite 1von 60

ARUNAI ENGINEERING COLLEGE

(Affiliated to Anna University) Velu Nagar, Tiruvannamalai 606 603 Phone: 04175-237419/236799/237739 www.arunai.org
ISO 9001- 2000

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


2011 - 2012

FOURTH SEMESTER

CS 2257 - OPERATING SYSTEM LAB

LAB

MANUAL

ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI 606 603

DEPARTMENT OF

COMPUTER SCIENCE AND ENGINEERING


CERTIFICATE
Certified that this is a bonafide record of work done by Name University Reg.No Semester Branch Year Staff-in-Charge : : : IV : COMPUTER SCIENCE & ENGG : 2011 - 2012 Head of the Department

Submitted for the ___________________________________________ Practical Examination held on ______________

Internal Examiner

External Examiner

CONTENTS
S No 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11 12 13 14 Date Name of the Experiment Page No. Signature

IMPLEMENTATION OF SYSTEM CALLS

a) FORK SYSTEM CALL PROGRAM #include<stdio.h> #include<sys/types.h> #include<unistd.h> main() { int pid; pid=fork(); if(pid==0) { printf("I am the child,my process id is%d\n",getpid()); printf("The child parent process id is%d\n",getpid()); } else { printf("I am the parent,my process id is%d\n",getpid()); printf("The parent id is%d\n",getpid()); } }

OUTPUT I am the child, my process id is29414 The child parent process id is29414 I am the parent, my process id is29413 The parent id is29413

b) EXECUTE SYSTEM CALL

PROGRAM: #include<stdio.h> #include<string.h> #define EVER;; int main() { int process; char line[20]; for(EVER) { fprintf(stderr,"cmd"); if(gets(line)==(char*)NULL) exit(0); process=fork(); if(process>0) wait((int*)0); else if(process==0) { execlp(line,line,(char*)NULL); fprintf(stderr,"cannot execute %s \n"); exit(1); } else if(process==-1) { fprintf(stderr,"cannot fork \n"); exit(0); } } }

OUTPUT:

Cmddate Sat Feb 20 15:23:48 IST 2010 Cmdls a. out dec dns.c filename lashmanraj ms.c shankar as.c din e.c fit.c man nir.c sjfnp.c thulasi dir fag.c gate.c memory.c open.c tamil tr.c data dnc.c fcfs.c guru mm.c pra.c tcp.c vijay data.c dns15.c fghgh imp.c mms.c preevn tcpser.c Cmdcal February 2010 Su Mo Tu We Th 1 2 3 4 7 8 9 10 11 14 15 16 17 18 21 22 23 24 25 28

Fr Sa 5 6 12 13 19 20 26 27

c) GETPID SYSTEM CALL PROGRAM: #include<stdio.h> #include<unistd.h> #include<sys/types.h> main() { int pid1,pid2,pid3,pid4; pid1=fork(); if(pid1==0) { printf("\nfirst child id is %d\n",getpid()); pid2=fork(); if(pid2==0) { printf("\n2nd child is %d\n",getpid()); printf("\n2nd parent is %d\n",getppid()); pid3=fork(); if(pid3==0) { printf("\n3rd child id is %d\n",getpid()); printf("\n3rd parent id is %d\n",getppid()); pid4=fork(); if(pid4==0) { printf("\n4th child id is %d\n",getpid()); printf("\n4th parent id is %d\n",getppid()); } } } } else { printf("\nchild id is %d\n",getpid()); printf("\nparent id is %d\n",getppid()); } }

OUTPUT: 1st child id is 10172 1st parent id is 10170 2nd child id is 10173 2nd parent id is 10172 3rd child id is 10174 3rd parent id is 10173 4th child id is 10175 4th parent id is 10174 child id is 10171 parent id is 9859

d) EXIT AND CLOSE SYSTEM CALL PROGRAM: #include<unistd.h> #inlcude<sys/types.h> #include<unistd.h> #include<sys/types.h> int main() { int g; g=create(datafile.dat,S_IREAD|S_WRITE); if(g==-1) { printf(error in opening datafile.dat); } else { Printf (datafile.dat opened for read/write access); Printf(datafile.dat is currently empty); } close(g); exit(0); }

OUTPUT: Datefile.dat opened for read or write access Datafile.dat is currently empty

e) WAIT SYSTEM CALL PROGRAM #include<stdio.h> #include<sys/types.h> #include<unistd.h> main() { int i=0,p; printf("read to fork\n"); p=fork(); if(p==0) { printf("\n child starts\n"); for(i=0;i<15;i++) printf("%d\t",i); } else { wait(0); printf("\n parent starts\n"); for(i=0;i<10;i++) printf("%d\t",i); printf("\n parent end"); } }

OUTPUT Read to fork child starts 0 1 2 3 4 5 10 11 12 13 14 parent starts 0 1 2 3 4 5

6 6

7 7

8 8

9 9

f) STAT SYSTEM CALL PROGRAM: #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> int main(int argc,char *argv[]) { if(argc!=2) return1; struct stat filestat; if(stat(argv[1],&filestat)<0) return1; printf(\n information for %s\n,argv[1]); printf(\n); printf(filesize:\t\t %d bytes \n,filestat st_size); printf(no of links:\t %d,filestat st_nlink); printf(file inode:\t\t %d\n,filestat st_ino); }

OUTPUT: File size: 645bytes No of links:1 File inode:27296724

g) IMPLEMENTATION OF DIRECTORY PROGRAM: #include<stdio.h> #include<dirent.h> struct dirent *dptr; int main(int argc,char *argv[]) { char buff[256]; DIR *dirp; printf("\n\n enter the directory name"); scanf("%s",buff); if((dirp=opendir(buff))==NULL) { printf ("error"); exit (1); } while (dptr=readdir(dirp)) { printf("%s\n",dptr->d_name); } closedir(dirp); }

OUTPUT: enter the directory name/home/it24 priority.c server.c round.c macs.c directory.c client.c fork.c wait.c Io.c Read.c

IMPLEMNETATION OF I/O SYSTEM CALL PROGRAM: #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> static char message[]="hello world"; main() { int fd; char buffer [80]; fd=open ("datafile.dat",O_RDWR|O_CREAT|O_EXCL,S_IREAD|S_IWRITE ); if (fd!=-1) { printf("datafile.dat opened for read/write access\n"); write(fd,message,sizeof(message)); lseek(fd,0l,0); if(read(fd,buffer,sizeof(message))= =sizeof(message)) printf("\"%s\" it was written in file",buffer); else printf("eror in reading \n"); close(fd); } else printf("file already exist\n"); exit(0); }

OUTPUT: datafile.dat opened for read/write access "hello world" it was written in file

a) SIMULATION OF LS COMMAND PROGRAM: #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> #include<dirent.h> int main() { DIR *dp; struct dirent *dirp; if ((dp=opendir("/home/cse7"))==NULL) printf ("ERROR"); while ((dirp=readdir (dp))!=NULL) printf("%s\n", dirp->d_name); closedir (dp); }

OUTPUT: Fact.c p.sh . a.out arithmatic.c LSCOM.c wait.c fork.c exec.c. wait.c viminfi.tmp fibo sum student.c comparison elakkiya.c fcfs.c

b) IMPLEMENTATION OF GREP COMMAND

PROGRAM: #include<stdio.h> main() { char c; int m,n; printf("1.list of lines\n2.copy\n3.remove\n"); printf("enter the choice\n"); scanf("%d",&n); switch(n) { case 1: system("grep print exe.c"); break; case 2: system("cp exe.c pgm.c"); printf("the file is copied"); break; case 3: system("rm pgm.c"); printf("the file is deleted"); break; } }

OUTPUT: 1.list of lines 2.copy 3.remove Enter the choice 1 Call.c exec.c fork.c forks.c grep.c open.c 1.list of lines 2.copy 3.remove Enter the choice 2 The file is copied 1.list of lines 2.copy 3.remove Enter the choice 3 The file is deleted

CPU SCHEDULING-I

a) FIRST COME FIRST SERVE SCHEDULING ALGORITHM b) PROGRAM: #include<stdio.h> void main() { int bt[3],wt[3],at[3],ft[3],tat[3]; int i,n,sum=0; float awt; char p[3][9]; printf("\nEnter the process and bursttime:"); for(i=0;i<3;i++) scanf("%s%d",p[i],&bt[i]); printf("\nEnter the arrival time:"); for(i=0;i<3;i++) scanf("%d",&at[i]); wt[0]=0; for(i=1;i<3;i++) wt[i]=wt[i-1]+bt[i-1]; ft[0]=bt[0]; for(i=1;i<3;i++) wt[i]=wt[i-1]+bt[i-1]; ft[0]=bt[0]; for(i=1;i<3;i++) ft[i]=ft[i-1]+bt[i]; for(i=0;i<3;i++) tat[i]=ft[i]-at[i]; for(i=0;i<3;i++) sum=sum+wt[i]; awt=sum/3.0; atat=tat/3.0; printf("\nFIRST COME,FIRST SERVE\n"); printf("\nprocess burst time arrival time waiting time finish time turnaround ti me\n"); for(i=0;i<3;i++) printf("\n\n%d%s\t%d\t%d\t%d\t%d\t%d",i+1,p[i],bt[i],at[i],wt[i],ft[i],tat[i]); printf("\n\naverage waiting time:%f",awt); printf(\n\n average turnaround time:%f,tat); }

OUTPUT: Enter the process and burst time: 1 7 2 5 3 3 Enter the arrival time: 4 5 6 FIRST COME,FIRST SERVE
process burst time arrival time waiting time finish time turnaround time

1 2 3

7 5 3

4 5 6

0 7 12

7 12 15

3 7 9

average waiting time:6.333333 average turnaround time:6.333333

b)SHORTEST JOB FIRST SCHEDULING ALGORITHM i) SHORTEST JOB FIRST NON-PREEMPTION PROGRAM: #include<stdio.h> void main() { int n,i,j,p[10],bt[10],wt[10],ta[10],twt=0,tat=0,temp; float awt=0.0,atat=0.0; printf("\n\nshortest job first non.preemption"); printf("\n\n Enter the number of process:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter the burst time for p%d:",i); scanf("%d",&bt[i]); p[i]=i; } wt[0]=0; for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(bt[i]<bt[j]) { temp=bt[i]; bt[i]=bt[j]; bt[j]=temp; temp=p[i]; p[i]=p[j]; p[j]=temp; } } } for(i=0;i<n;i++) { wt[i+1]=wt[i]+bt[i]; ta[i]=wt[i]+bt[i]; } for(i=0;i<n;i++) {

twt=twt+wt[i]; tat=tat+ta[i]; } awt=(float)twt/n; atat=(float)tat/n; printf("\n"); printf("Process_Number,Burst Time, Waiting Time, Turn Around Time\n\n"); for(i=0;i<n;i++) printf("p%d %d %d %d\n",p[i],bt[i],wt[i],ta[i]); printf("Average Waiting Time is %3.2f\n\n Average Turn Around Time is %3.2f",awt ,atat); }

OUTPUT: shortest job first non.preemption Enter the number of process: 3 Enter the burst time for P0: 3 Enter the burst time for P1: 4 Enter the burst time for P2: 5 Process number P0 P1 P2 burst time 3 4 5 waiting time turn around time 0 3 7 3 7 12

Total waiting time is 10 Average waiting time is 3.33 Total turn around time is 22 Average turn around time is 7.33

ii)SHORTEST JOB FIRST PREEMPTION PROGRAM: #include<stdio.h> void main() { printf("shortest job first with preemption"); printf("\n\n enter the no of process"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n enter burst time for p%d:",i); scanf("%d",&bt[i]); printf("\n enter arrival time for p%d:",i); scanf("%d",&ar[i]); p[i]=i; bt1[i]=bt[i]; ar1[i]=ar[i]; } int j,temp,m=0; wt[-1]=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(ar[i]<ar[j]) { temp=ar[i]; ar[i]=ar[j]; ar[j]=temp; temp=p[i]; p[i]=p[j]; p[j]=temp; temp=bt[i]; bt[i]=bt[j]; bt[j]=temp; } } } for(i=0;i<n-1;i++) { pn[i]=p[m]; w[i]=ar[i+1];

bt[m]=bt[m]-(ar[i+1]-ar[i]); if(bt[m]>bt[i+1]) m=i+1; } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(bt[i]<bt[j]) { temp=ar[i]; ar[i]=ar[j]; ar[j]=temp; temp=p[i]; p[i]=p[j]; p[j]=temp; temp=bt[i]; bt[i]=bt[j]; bt[j]=temp; } } } for(i=0;i<n;i++) { pn[i+n-1]=p[i]; for(i=0;i<(2*n)-1;i++) { for(j=2*(n-1);j>=0;j--) { if(i==pn[j]) { ta[i]=w[j]-ar1[i]; break; } } } for(i=0;i<n;i++) { wt[i]=ta[i]-bt1[i]; twt=twt+wt[i]; tat=tat+ta[i]; }

awt=(float)twt/n; atat=(float)tat/n; printf("\n"); printf("process_no burst time arrival time wait time turn around time \n\n" ); for(i=0;i<n;i++) printf("p%d %d %d %d %d\n",p[i],bt1[i],ar1[i],wt[i],ta[i ]); printf("avg wait time is %3.2f \n\n avg turn around time is %3.2f",awt,atat); }

OUTPUT:

Shortest job first with preemption Enter the no of process3 Enter burst time for P0: 4 Enter arrival time for P0:0 Enter burst time for P1: 6 Enter arrival time for P1:2 Enter burst time for P2:8 Enter arrival time for P2:1 pn[0]=0 pn[1]=0 pn[2]=0 pn[3]=1 pn[4]=2 Process no p0 p1 p2 w[0]=1 w[1]=2 w[2]=4 w[3]=10 w[4]=18 burst time 4 6 8 arrival time 0 2 1 waiting time 0 2 9 turn around time 4 8 17

Total waiting time is 11 Average waiting time is 3.67 Total Turn around time is 29 Average turn around time is 9.67

CPU SCHEDULING-II a) i) PRIORITY SCHEDULING ALGORITHM-NON-PREEMPTION PROGRAM: #include<stdio.h> #include<unistd.h> main() { int b[10],n,i,j,pr[10],p[10],t,w=0,wt=0,tt=0; float x,y; printf("enter the number of process:\n"); scanf("%d",&n); printf("enter the burst time:\n"); for(i=0;i<n;i++) { scanf("%d",&b[i]); pr[i]=i+1; } printf("enter the priority\n"); for(i=0;i<n;i++) { scanf("%d",&p[i]); } for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(p[i]<p[j]) { t=p[i]; p[i]=p[j]; p[j]=t; t=pr[i]; pr[i]=pr[j]; pr[j]=t; t=b[i]; b[i]=b[j]; b[j]=t; }}} printf("process\tburst time\tpriority\twaitingtime\tturnarounttime\n"); for(i=0;i<n;i++) { printf("p(%d)\t\t%d\t\t%d\t\t%d\t\t",pr[i],b[i],p[i],w); wt=wt+w;

w=w+b[i]; printf("%d\n",w); tt=tt+w; } printf("\n totalwaiting time=%d",wt); printf("\n total turn arounttime=%d",tt); printf("\n average waiting time=%3.2f",(float)wt/n); printf("\n average turn around time=%3.2f",(float)tt/n); }

OUTPUT: Enter the number of process: 3 Enter the burst time: 1 2 3 Enter the priority: 4 5 3 Process P(3) P(1) P(2) burst time 3 1 2 Priority 3 4 5 waiting time 0 3 4 turnaround time 3 4 6

Total waiting time is 7 Total turn around time is 13 Average waiting time is 2.33 Average turn around time is 4.33

ii) PRIORITY SCHEDULING ALGORITHM-PREEMPTION PROGRAM: #include<stdio.h> void main() { Int I,n,p[10],bt[10],wt[10],ta[10],ar[10],pn[10],w[10],bt1[10],ar1[10],pr[10],pr1[10],twt=0,tat =0; float awt=0.0,atat=0.0; printf(" priority with preemption"); printf("\n\nEnter the number of process:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the burst time for p%d:",i); scanf("%d",&bt[i]); printf("\nEnter the arrival time for p%d:",i); scanf("%d",&ar[i]); printf("\nEnter the priority for p%d:",i); scanf("%d",&pr[i]); p[i]=i; bt1[i]=bt[i]; ar1[i]=ar[i]; pr1[i]=pr[i]; } int j,temp,m=0; wt[-1]=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(ar[i]<ar[j]) { temp=ar[i]; ar[i]=ar[j]; ar[j]=temp; temp=p[i]; p[i]=p[j]; p[j]=temp; temp=bt[i]; bt[i]=bt[j]; bt[j]=temp; temp=pr[i]; pr[i]=pr[j];

pr[j]=temp; } } } for(i=0;i<n-1;i++) { pn[i]=p[m]; w[i]=ar[i+1]; bt[m]=bt[m]-(ar[i+1]-ar[i]); if(pr[m]>pr[i+1]) m=i+1; } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(pr[i]<pr[j]) { temp=ar[i]; ar[i]=ar[j]; ar[j]=temp; temp=p[i]; p[i]=p[j]; p[j]=temp; temp=bt[i]; bt[i]=bt[j]; bt[j]=temp; temp=pr[i]; pr[i]=pr[j]; pr[j]=temp; } } } for(i=0;i<n;i++) { pn[i+n-1]=p[i]; w[i+n-1]=w[i+n-2]+bt[i]; } for(i=0;i<(2*n)-1;i++) printf("\npn[%d]=%d\tw[%d]=%d\n",i,pn[i],i,w[i]); for(i=0;i<n;i++) { for(j=2*(n-1);j>=0;j--) { if(i==pn[j]) {

ta[i]=w[j]-ar1[i]; break; } } } for(i=0;i<n;i++) { wt[i]=ta[i]-bt1[i]; twt=twt+wt[i]; tat=tat+ta[i]; } awt=(float)twt/n; atat=(float)tat/n; printf("\n"); printf("process_no burst time arrival time priority wait time TAT\n\n"); for(i=0;i<n;i++) printf("p%d\t%d\t%d\t%d\t%d\t%d\n",p[i],bt1[i],ar1[i],pr1[i],wt[i],ta[i]); printf("avg wait time is %3.2f\n",awt); printf("\navg turn around time is %3.2f",atat); }

OUTPUT: Priority with preemption Enter the number of process: 3 Enter the burst time for P0:3 Enter the arrival time for P0:0 Enter the priority for P0:2 Enter the burst time for P1:6 Enter the arrival time for P1:1 Enter the priority for P1:1 Enter the burst time for P2:9 Enter the arrival time for P2:1 Enter the priority for P2:3 Pn[0]=0 w[0]=1 Pn[1]=1 w[1]=1 Pn[2]=1 w[2]=7 Pn[3]=0 w[3]=9 Pn[4]=2 w[4]=18 Process no P1 P0 P2 burst time 3 6 9 arrival time 0 1 1 priority 2 1 3 waiting time 6 0 8 turn around time 9 6 17

Total waiting time is 14 Total turn around time is 32 Average waiting time is 4.67 Average turn around time is 10.67

b) ROUND ROBIN SCHEDULING ALGORITHM

PROGRAM: #include<stdio.h> #include<unistd.h> main() { int bt[3],at[3],tq,b; int ft[3],tat[3]; int ec[]={0,0,0},i,bt1[3]; char p[3][9]; printf("\nEnter the process and bursttime:"); for(i=0;i<3;i++) { scanf("%s%d",p[i],&bt[i]); bt1[i]=bt[i]; } printf("\nEnter the arrival time:"); for(i=0;i<3;i++) scanf("%d",&at[i]); printf("\nEnter the time quantum:"); scanf("%d",&tq); b=0; while(ec[0]!=1||ec[1]!=1||ec[2]!=1) for(i=0;i<3;i++) if(ec[i]==0) { if(bt[i]<=tq) { ft[i]=b+bt[i]; b=ft[i]; ec[i]=1; } else { b=b+tq; bt[i]=bt[i]-tq; }} for(i=0;i<3;i++) tat[i]=ft[i]-at[i]; printf("\nROUND ROBIN SCHEDULING\n"); printf("process bursttime arrival time finish time turnaroundtime"); for(i=0;i<3;i++) printf("\n%d%s\t\t%d\t\t%d\t\t%d\t\t%d",i+1,p[i],bt[i],at[i],ft[i],tat[i]); }

OUTPUT: Enter the process and bursttime: p1 20 p2 30 p3 5 Enter the arrival time: 1 2 3 Enter the time quantum: 5 ROUND ROBIN SCHEDULING Process bursttime arrival time p1 5 1 p2 5 2 p3 5 3 finish time 40 55 15 turnaroundtime 39 53 12

INTERPROCESS COMMNICATION USING PIPES

PROGRAM: CLIENT: #define NP1 "|tmp|np1" #define NP2 "|tmp|np2" #define NP1 "|tmp|np1" #define NP2 "|tmp|np2" #define MAX_BUF_SIZE 255 #include<stdio.h> #include<errno.h> #include<ctype.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> int main(int argc,char*argv[]) { int rdfd,wrfd,numread; char rdbuf[MAX_BUF_SIZE]; if(argc!=2) { printf("usage:%sn",argv[0]); exit(1); } wrfd=open(NP1,O_WRONLY); rdfd=open(NP2,O_RDONLY); write(wrfd,argv[1],strlen(argv[1])); numread=read(rdfd,rdbuf,MAX_BUF_SIZE); rdbuf[numread]='0'; printf("full duplex client:read from the pipe:%sn",rdbuf); }

SERVER: #define NP1/tmp/np1 #define NP2/tmp/np2 #define MAX_BUF_SIZE 255 #include<stdio.h> #include<errno.h> #include<ctype.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> int main(int argc,char*argv[]) { Int rdfd,wrfd,ret_val,count,numread; Char buf[MAX_BUF_SIZE]; ret_val=mkfifo(NP1,0666); if((ret_val= = -1) && (errno!=EEXIST)) { Perror(Error creating the named pipe); Exit(1); } rdfd= open(NP1,O_RDONLY); wrfd= open(NP2,O_WRONLY); numread=read(rdfd,buf,MAX_BUF_SIZE); buf[numread]=0; printf(Full duplex Server:Read from the pipe:%s,buf); count=0; while(count<numread) { buf[count]=toupper(buf[count]); count++; } write(wrfd,buf,strlen(buf)); }

OUTPUT: Server :./a.out Usage:read from the pipe Client:./a.out hai Full duplex server :read from the pipe:hai Full duplex client:read from the pipe :HAI

PRODUCER CONSUMER PROBLEM USING SEMAPHORES PROGRAM: #include<stdio.h> int mutex=1,full=0,empty=3,x=0; main() { int n; void producer(); void consumer(); int wait(int); int signal(int); printf("\n 1.Producer\n2.Consumer\n3.Exit"); while(1) { printf("\nEnter your choice"); scanf("%d",&n); switch(n) { case 1: if((mutex==1)&&(empty!=0)) producer(); else printf("\n Buffer is full"); break; case 2: if((mutex==1)&&(full!=0)) consumer(); else printf("\nBuffer is Empty"); break; case 3: exit(0); break; } } } int wait(int s) { return(--s); } int signal(int s) { return(++s);

} void producer() { mutex=wait(mutex); full=signal(full); empty=wait(empty); x++; printf("\nProducer produces the item %d",x); mutex=signal(mutex); } void consumer() { mutex=wait(mutex); full=wait(full); empty=signal(empty); printf("\n Consumer consumes the item %d",x); x--; mutex=signal(mutex); }

OUTPUT: 1. Producer 2. Consumer 3. Exit Enter your choice1 Producer produces the item 1 Enter your choice1 Producer produces the item 2 Enter your choice1 Producer produces the item 3 Enter your choice1 Buffer is full Enter your choice2 Consumer consumes the item 3 Enter your choice2 Consumer consumes the item 2 Enter your choice2 Consumer consumes the item 1 Enter your choice2 Buffer is Empty Enter your choice3 Exit

IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME-I PROGRAM: #include<stdio.h> #include<stdlib.h> struct list { int start; int sizeofpar; char status; int freespace; int pgmsize; int parno; }list[5]; struct list temp; allocate(int n) { int pgmsize,i,j; printf("best fit allocation\n"); printf("enter the program size\n"); scanf("%d",&pgmsize); for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(list[i].sizeofpar>list[j].sizeofpar) { temp=list[i]; list[i]=list[j]; list[j]=temp; } } } for(i=0;i<n;i++) { if(pgmsize<+list[i].sizeofpar&&list[i].status=='F') { list[i].status='A'; list[i].freespace=list[i].sizeofpar-pgmsize; return(1); printf("process cant be allocated\n"); return(0); } release(int n)

{ int r,i; printf("enter the partition not to be released\n"); scanf("%d",&r); for(i=0;i<n;i++) { if(list[i].parno==r&& list[i].status=='A') { list[i].status='F'; list[i].freespace=list[i].sizeofpar; } display(int n) { int i,j; printf("after memory allocation\n"); printf("partition table\n"); for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(list[i].parno>list[j].parno) { temp=list[i]; list[i]=list[j]; list[j]=temp; } } } for(i=0;i<n;i++) { } } main() printf("\ntotal memory size"); scanf("%d",&t); printf("\n number of partition"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nenter the partition no"); scanf("%d",&list[i].parno); s=s+list[i].sizeofpar; } if(s<=t) { list[0].start=0;

for(i=0;i<n-1;i++) { list[i+1].start=list[i].start+list[i].sizeofpar; } } else { exit(0); } printf("\nbefore memory allocation\n"); printf("partition\tstartingaddress\tsizeofpartition\t\tstatus\n"); for(i=0;i<n;i++) { list[i].status='F'; list[i].freespace=list[i].sizeofpar; } while(1) { printf("\n enter ur choice\n1.allocate\n2.release\n3.display\n4.exit\n"); scanf("%d",&ch); switch(ch) { case 1: allocate(n); break; case 2: release(n); break; case 3: display(n); break; case 4: exit(0); default: printf("invalid number"); exit(1); } } }

OUTPUT: Total memory size500 Number of partition4 Enter the partition no 1 Enter the partition size100 Enter the partition no 2 Enter the partition size50 Enter the partition no 3 Enter the partition size200 Enter the partition no 4 Enter the partition size150 Before memory allocation Partition Starting address 1 0 2 100 3 150 4 350 Enter your choice 1. Allocate 2. Release 3. Display 4. Exit 1 Best fit allocation Enter the program size 120 Enter your choice 1. Allocate 2. Release 3. Display 4. Exit 3 After memory allocation Partition table Partition no Starting address Size of partition 100 50 200 150 Status F F F F

Sizeofpartition

Status

Free space

1 2 3 4

0 100 150 350

100 50 200 150

F F F A

100 50 200 30

Enter your choice 1. Allocate 2. Release 3. Display 4. Exit 2 Enter the partition no to be released 4 Enter your choice 1. Allocate 2. Release 3. Display 4. Exit 3 After memory allocation Partition table Partition no Starting address 1 0 2 100 3 150 4 350 Enter your choice 1. Allocate 2. Release 3. Display 4. Exit 4

Size of partition 100 50 200 150

Status F F F F

Free space 100 50 200 150

FIRST FIT PROGRAM: #include<stdio.h> void main() { char p[4][9]; int i,partitionsize[4],x=0,c=0; int process_size; printf("\n enter the partition and partition size:"); for(i=0;i<4;i++) scanf("%s%d",p[i],&partitionsize[i]); printf("enter process_size:"); scanf("%d",&process_size); for(i=0;i<4;i++) if(process_size<=partitionsize[i]) printf("\n stored in partition:%d",i+1); printf("\n partitionsize:%d",partitionsize[i]); x=partitionsize[i]-process_size; printf("\n external fragmnentation:%d",x); c++; }

OUTPUT: enter the partition and partition size: 1 16 2 22 3 18 4 10 Enter the process_size: 20 Stored in partition:20 Partition size:22 External fragmentation:2

BEST-FIT PROGRAM: #include<stdio.h> void main() { int prs,n,ps[10],i,l,j=10,k,min,d[10]; printf("\n enter the number of partitons:"); scanf("%d",&n); printf("\n enter the size of each partition:"); for(i=0;i<n;i++) scanf("%d",&ps[i]); printf("\n enter the process_size:"); scanf("%d",&prs); for(i=0;i<n;i++) d[i]=ps[i]-prs; printf("\n difference:"); for(i=0;i<n;i++) printf("\t %d",d[i]); for(i=0;i<n;i++) if(d[i]>=0) { min=d[i]; l=i; break; } for(i=l;i<n;i++) { if(d[i]<=min) { if(d[i]>=0) { min=d[i]; k=i; } } } printf("\n min:%d",min); if(min>=0) { printf("\n the index to which process is allocated:%d",k); printf("\n ther size of the partition to which the prtocess is allocated:%d",ps[ k]);

printf("\n external fragmentaton :%d",ps[k]-prs); j=1; if(j==0) printf("\n process size is greater than each fot he partition"); }

OUTPUT: enter the number of partitons:4 enter the size of each partition:16 18 12 11 enter the process_size:10 difference: 6 8 2 1 min:1 the index to which process is allocated:3 ther size of the partition to which the prtocess is allocated:11 external fragmentaton :1

NEXT-FIT PROGRAM: #include<stdio.h> void main() { int prs,n,ps[10],i,j=0,k; printf("\n enter the number of partitions:"); scanf("%d",&n); printf("\n enter the size of each partition:"); for(i=0;i<n;i++) scanf("%d",&ps[i]); printf("\n enter the siz eof the process:"); scanf("%d",&prs); printf("\n enter the index allocated matrix:"); scanf("%d",&k); printf("\n last of allocated block:%d",ps[k]); for(i=k+1;i<n;i++) { if(ps[i]>=prs) { printf("\n the index value to which the process is allocated:%d",i); printf("\n the size of the partiton to which the processis allocated:%d",ps[i]); printf("\n external fragmentation:%d",ps[i]-prs); j=1; break; } } if(j==0) printf("\n the process size is greater than each fo the partition"); }

OUTPUT: enter the number of partitions:4 enter the size of each partition:14 16 20 18 enter the siz eof the process:10 enter the index allocated matrix:2 last of allocated block:20 the index value to which the process is allocated:3 the size of the partiton to which the processis allocated:18 external fragmentation:8