Date post: | 25-Aug-2014 |
Category: |
Documents |
Upload: | chandrasekar-raguraman |
View: | 110 times |
Download: | 3 times |
CS 2257 - OPERATING SYSTEM LAB
ARUNAI ENGINEERING COLLEGE(Affiliated to Anna University)
Velu Nagar, Tiruvannamalai – 606 603Phone: 04175-237419/236799/237739
www.arunai.org
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
2011 - 2012
FOURTH SEMESTER
LAB MANUAL
ISO 9001- 2000
ARUNAI ENGINEERING COLLEGETIRUVANNAMALAI – 606 603
DEPARTMENT OFCOMPUTER SCIENCE AND ENGINEERING
CERTIFICATE
Certified that this is a bonafide record of work done by
Name :
University Reg.No :
Semester : IV
Branch : COMPUTER SCIENCE & ENGG
Year : 2011 - 2012
Staff-in-Charge Head of the Department
Submitted for the ___________________________________________Practical Examination held on ______________
Internal Examiner External Examiner
CONTENTS
S No Date Name of the Experiment Page No. Signature
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11
12
13
14
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 is29414The child parent process id is29414I am the parent, my process id is29413The 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:
CmddateSat Feb 20 15:23:48 IST 2010
Cmdlsa. 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.cdata dnc.c fcfs.c guru mm.c pra.c tcp.c vijaydata.c dns15.c fghgh imp.c mms.c preevn tcpser.c
Cmdcal February 2010Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728
c) GETPID SYSTEM CALLPROGRAM:
#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 accessDatafile.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 forkchild starts 0 1 2 3 4 5 6 7 8 910 11 12 13 14parent starts0 1 2 3 4 5 6 7 8 9
f) STAT SYSTEM CALLPROGRAM:
#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: 645bytesNo of links:1File 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.cserver.cround.cmacs.cdirectory.cclient.cfork.cwait.cIo.cRead.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);elseprintf("eror in reading \n");close(fd);}elseprintf("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.cp.sh .a.outarithmatic.cLSCOM.cwait.cfork.cexec.c.wait.cviminfi.tmpfibosumstudent.ccomparisonelakkiya.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 lines2.copy3.removeEnter the choice1Call.c exec.c fork.c forks.c grep.c open.c
1.list of lines2.copy3.removeEnter the choice2The file is copied
1.list of lines2.copy3.removeEnter the choice3The file is deleted
CPU SCHEDULING-I
a) FIRST COME FIRST SERVE SCHEDULING ALGORITHMb)
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 time\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 72 53 3
Enter the arrival time:
456
FIRST COME,FIRST SERVE
process burst time arrival time waiting time finish time turnaround time
1 7 4 0 7 3
2 5 5 7 12 7
3 3 6 12 15 9
average waiting time:6.333333average 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 burst time waiting time turn around time
P0 3 0 3 P1 4 3 7 P2 5 7 12
Total waiting time is 10Average waiting time is 3.33Total turn around time is 22Average 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 w[0]=1 pn[1]=0 w[1]=2 pn[2]=0 w[2]=4 pn[3]=1 w[3]=10 pn[4]=2 w[4]=18
Process no burst time arrival time waiting time turn around time
p0 4 0 0 4 p1 6 2 2 8 p2 8 1 9 17
Total waiting time is 11Average waiting time is 3.67Total Turn around time is 29Average 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:123
Enter the priority:453
Process burst time Priority waiting time turnaround timeP(3) 3 3 0 3P(1) 1 4 3 4P(2) 2 5 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:3Enter the arrival time for P0:0Enter the priority for P0:2
Enter the burst time for P1:6Enter the arrival time for P1:1Enter the priority for P1:1
Enter the burst time for P2:9Enter the arrival time for P2:1Enter 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 burst time arrival time priority waiting time turn around time
P1 3 0 2 6 9 P0 6 1 1 0 6 P2 9 1 3 8 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 20p2 30p3 5
Enter the arrival time:123
Enter the time quantum:5
ROUND ROBIN SCHEDULING
Process bursttime arrival time finish time turnaroundtimep1 5 1 40 39p2 5 2 55 53p3 5 3 15 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.outUsage:read from the pipeClient:./a.out haiFull duplex server :read from the pipe:haiFull 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();elseprintf("\n Buffer is full");break;case 2:if((mutex==1)&&(full!=0))consumer();elseprintf("\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. Producer2. Consumer3. ExitEnter your choice1
Producer produces the item 1Enter your choice1
Producer produces the item 2Enter your choice1
Producer produces the item 3Enter your choice1
Buffer is fullEnter your choice2
Consumer consumes the item 3Enter your choice2
Consumer consumes the item 2Enter your choice2
Consumer consumes the item 1Enter your choice2
Buffer is EmptyEnter your choice3Exit
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 1Enter the partition size100
Enter the partition no 2Enter the partition size50
Enter the partition no 3Enter the partition size200
Enter the partition no 4Enter the partition size150
Before memory allocationPartition Starting address Size of partition Status 1 0 100 F 2 100 50 F 3 150 200 F 4 350 150 F
Enter your choice1. Allocate2. Release3. Display4. Exit1Best fit allocationEnter the program size120
Enter your choice1. Allocate2. Release
3. Display4. Exit
3
After memory allocationPartition table Partition no Starting address Sizeofpartition Status Free space 1 0 100 F 100 2 100 50 F 50 3 150 200 F 200 4 350 150 A 30
Enter your choice1. Allocate2. Release3. Display4. Exit2Enter the partition no to be released4
Enter your choice1. Allocate2. Release3. Display4. Exit3After memory allocationPartition table Partition no Starting address Size of partition Status Free space 1 0 100 F 100 2 100 50 F 50 3 150 200 F 200 4 350 150 F 150
Enter your choice1. Allocate2. Release3. Display4. Exit
4
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 162 223 184 10Enter the process_size: 20Stored in partition:20Partition size:22External fragmentation:2
BEST-FITPROGRAM:
#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:16181211
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-FITPROGRAM:
#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:14162018
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