EX.
NO:1 DATE: AIM:
IMPLEMENTATION OF UNIX SYSTEM CALLS : (fork, getpid, exit, wait)
To write a C program using system calls fork(), getpid(), exit() and wait(). ALGORITHM: Step 1: Start the program. Step 2: Initialize the process id number. Step 3: Create a process using fork() system call. Step 4: If process id<0 print error. Step 5: If process id=0 means then print child process id number and parent process id number. Step 6: Else wait until the process gets completed
Register No: 100606314011
Pageno:1
PROGRAM:
#include<stdio.h> int main(void) { int pid; pid=fork(); if(pid<0) { printf("error"); exit(0); } else if(pid==0) { printf("\n child processid is %d",getpid()); printf("\n parent processid is %d",getppid());
} else { wait(pid); execl("/bin/date","date",0); } }
Register No: 100606314011
Pageno:2
OUTPUT:
[student@localhost ~]$ vi ex1.c [student@localhost ~]$ gcc ex1.c [student@localhost ~]$ ./a.out
child processid is 7984 parent processid is 7983Wed Mar 28 16:24:10 IST 2012
RESULT: Thus the above program using system calls fork(), getpid(), exit() and wait() has been executed successfully.
Register No: 100606314011
Pageno:3
EX.NO:1(b) DATE: AIM:
IMPLEMENTATION OF UNIX SYSTEM CALLS: (stat, opendir, readdir, close)
To write a C program using system calls stat(), opendir() and readdir() and close().
ALGORITHM: Step 1: Start the program. Step 2: Initialize the process id number. Step 3: Create a process using stat(), opendir(), readdir() and close(). Step 4: Start the process at first and close the process at last. Step 5: Open and read directory are used for opening the files and reading the files. Step 6: Stop the program.
Register No: 100606314011
Pageno:4
PROGRAM: #include<stdio.h> #include<sys/types.h> #include<dirent.h> int main(int argc,char * argv[]) { DIR * dp; struct dirent * dirp; if(argc!=2) { printf("A single argument (the directory name )is required\n"); exit(1); } if((dp=opendir(argv[1]))==0) { printf("can't open %s\n",argv[1]); exit(1); } while((dirp=readdir(dp))!=0) printf("%s\n",dirp>d_name); closedir(dp); exit(0); }
Register No: 100606314011
Pageno:5
OUTPUT: [student@localhost ~]$ vi ex1b.c [student@localhost ~]$ gcc ex1b.c [student@localhost ~]$ ./a.out surya One Two
RESULT: Thus the above program using system calls stat(), close(), opendir() and readdir() has been executed successfully.
Register No: 100606314011
Pageno:6
EX.NO:2 DATE: AIM:
IMPLEMENTATION OF I/O SYSTEM CALLS: (open, read, write)
To write a file operation using system calls. ALGORITHM: Step 1: Start the program. Step 2: Create a file in read mode. Step 3: Read the content of a file in buffer. Step 4: Check the condition if[->seek==-1]. Step 5: If it is true read the content of the buffer. Step 6: Else terminate the process. Step 7: Repeat step-4 until all the are read. Step 8: Stop the process.
Register No: 100606314011
Pageno:7
PROGRAM: #include<stdio.h> #include<fcntl.h> #include<sys/types.h> main() { int s,fd,n,l=0; char buff[80]; fd=open("ram.txt",O_RDWR,0); printf("the file in reverse order is:\n"); lseek(fd,-1,2); while (1) { n=read(fd,buff,1); write(1,buff,1); if((lseek(fd,-2,1))==1) break; } close(fd); }
Register No: 100606314011
Pageno:8
OUTPUT: [student@localhost ~]$ vi ex2.c [student@localhost ~]$ gcc ex2.c [student@localhost ~]$ ./a.out The file in reverse order is hanujnam
RESULT: Thus the above program using i/o system calls of unix operating system open(), write(), and read() has been executed successfully
Register No: 100606314011
Pageno:9
EX.NO:3(a) DATE: AIM:
IMPLEMENTATION OF GREP COMMANDS
To write a c program using the commands like grep.
ALGORITHM: Step1: Start the process. Step2: Create a file pointer and pointer to a string. Step3: Open a file in read mode and the pointer points the position in the buffer. Step4: If string is #NULL then printf statement will get execute. Step5: Particular pattern of string will be displayed without using grep command. Step6: Stop the process.
Register No: 100606314011
Pageno:10
PROGRAM: #include<stdio.h> #include<stdlib.h>
int substr(char *line,char *pat); int main(int argc,char *argv[]) {
FILE *fp; char line[128]; if(argc==2) { fp=stdin; } else if(argc==3) { fp=fopen(argv[2],"r"); if(fp==NULL) { printf("file %s cannot be opened \n",argv[1]); } } else { printf("usage:%s<pattern><file>\n",argv[0]); exit(1); } while(fgets(line,sizeof(line),fp)) if(substr(line,argv[1])) printf("%s",line); fclose(fp); return 0; } int substr(char *line,char *pat) {
int i,j,k; for(i=0;line[i]!='\0';i++) {
for(j=i,k=0;line[j]==pat[k] && pat[k] !='\0';j++,k++); if(pat[k]=='\0') return 1; } return 0; }
Register No: 100606314011
Pageno:11
OUTPUT: [student@localhost ~]$ vi ex3a.c [student@localhost ~]$ gcc ex3a.c [student@localhost ~]$ ./a.out substr grep.c\ int substr( char *line, char *pat); if ( substr(line, argv[1]) ) int substr( char *line, char *pat)
RESULT:
Thus the above program using grep commands has been executed successfully.
Register No: 100606314011
Pageno:12
EX.NO:3(b) DATE: AIM:
IMPLEMENTATION OF LS COMMANDS
To write a c program using the commands like ls. ALGORITHM: Step 1: Start the program. Step 2: Initialize the ls command. Step 3: Create a directory with help of the commands. Step 4: The directories like open and read are used in the program. Step 5: Run the program. Step 6: Display the result. Step 7: Stop the program.
Register No: 100606314011
Pageno:13
PROGRAM: #include<stdio.h> #include<sys/types.h> #include<dirent.h> int main(int argc,char*argv[]) { DIR*dp;
struct dirent*dirp; if(argc!=2) {
printf("a single argument(the directory name)is required\n"); exit(1); } if((dp=opendir(argv[1]))==0) { printf("can\'t open %s\n",argv[1]); exit(1); } while((dirp=readdir(dp))!=0) printf("%s\n",dirp>d_name); closedir(dp); exit(0); }
Register No: 100606314011
Pageno:14
OUTPUT: [student@localhost ~]$ vi ex3b.c [student@localhost ~]$ gcc ex3b.c [student@localhost ~]$./a.out muni calls3.c . a.out one .. two
RESULT:
Thus the above program using ls commands has been executed successfully.
Register No: 100606314011
Pageno:15
EX.NO:4(a) DATE:
IMPLEMENTATION OF FCFS SCHEDULING
AIM: To write a C program for the implementation of FCFS scheduling.
ALGORITHM: Step 1: Start the program. Step 2: Initialize the variable. Step 3: Read the number of process. Step 4: Get the runtime of each process using for loop. Step 5: Burst time, Turn around time and Waiting time is calculated using while loop. Step 6: Calculate average Turn around time, average waiting time and print it. Step 7: Exit the program.
Register No: 100606314011
Pageno:16
PROGRAM: #include<stdio.h> #include<string.h> #include<conio.h> Main() Char pn[10] [10],t[10];
Int array[10],bur[10],star[10],finish[10],tat[10],wt[10],I,j,n,temp; Int totwt=0,tottat=0; //clrscr();
Printf(enter the number of process:); Scanf(%d,&n); For(i=0;i<n;i++) { Printf(enter the process name,arrival time and brust time:); Sacnf(%s%d%d,&pn[i],&arr[i],&bur[i]); } For(i=0;i<n;i++) { For(j=0;j<0;j++) { If (arr[i]<arr[j]) { Temp=arr[i]; Arr[i]=arr[j]; Arr[j]=temp; Temp=bur[i]; Bur[i]=bur[j]; Bur[j]=temp; Strcpy(t,pn[i]); Strcpy(pn[i];pn[j]); Strcpy(pn[j],t); } } } For(i=0;i<n;i++) { If(i==0) Star[i]=arr[i]; Else star[i]=finish[i-1]; wt[i]=star[i]arr[i];
Register No: 100606314011
Pageno:17
finish[i]=atar[i]+bur[i]; tat[i]=finish[i]-arr[i]; } Printf(\npname arrtime burtime waittime start tat finish); For(i=0;i<n;i++) {
Printf(\n%s\t%3d\t%3d\t%3d\t%3d\t%6d\t%6d,pn[i]arr[i],bur[i],wt[i],star[i],tat[i],finish[i]); Totwt+=wt[i]; Tottat+=tat[i]; }
Printf(\naverage waiting time:%f,(float)totwt/n); Printf(\naverage turn around time:%f,(float)tottat/n); Getch(); Return 0; }
Register No: 100606314011
Pageno:18
OUTPUT:
RESULT:
Thus the above program FCFS scheduling has been executed successfully.
Register No: 100606314011
Pageno:19
EX.NO:4(b) DATE: AIM:
IMPLEMENTATION OF SJF SCHEDULING
To write a C program for the implementation of SJF scheduling.
ALGORITHM: Step 1: Start the program. Step 2: Initialize the variable. Step 3: Read the number of process. Step 4: Enter the runtime of each process using for loop. Step 5: Process having shortest burst time will be executed using swapping Step 6: Using while loop print the burst and waiting time. Step 7: Calculate and print the average Turnaround time. Step 8: Stop the program.
Register No: 100606314011
Pageno:20
PROGRAM: #include<stdio.h> struct sjf {
int burst,turn,process,no,waiting; char name; } a[10]; int main() { int i,j,k,l,temp,temp1; float avgwait,waiting1=0,burst1=0,avgburst; printf("Enter the no of process:"); scanf("%d",&k); printf("\nProcess\t\tBurst\n"); for(i=1;i<=k;i++) { printf("%d\t\t",i); scanf("%d",&a[i].process); a[i].no=i; } for(i=1;i<=k;i++) for(j=i+1;j<=k;j++) { if(a[i].process>a[j].process) { temp=a[i].process; a[i].process=a[j].process; a[j].process=temp; temp1=a[i].no; a[i].no=a[j].no; a[j].no=temp1; } }
for(i=1;i<=k;i++) a[i].waiting=a[i1].waiting+a[i].process; for(i=1;i<k;i++) { waiting1=waiting1+a[i].waiting; } printf("\nProcess\t\tWaiting\t\tBurst time\n"); for(i=0;i<k;i++)
Register No: 100606314011
Pageno:21
{ l=i+1; printf("\n%d\t\t%d\t\t%d",a[l].no,a[i].waiting,a[l].waiting); burst1=burst1+a[l].waiting; } avgwait=(waiting1/k); avgburst=(burst1/k); printf("\nAverage waiting time is:%f",avgwait); printf("\nAverage turn around time is:%f\n",avgburst); return 0; }
Register No: 100606314011
Pageno:22
OUTPUT:
[student@localhost ~]$ vi ex4b.c [student@localhost ~]$ gcc ex4b.c [student@localhost ~]$ ./a.out Enter the no of process:5 Process Burst 1 6 2 7 3 4 4 1 5 3 Process Waiting Burst time 4 0 1 5 1 4 3 4 8 1 8 14 2 14 21 Average waiting time is:5.400000 Average turn around time is:9.600000
RESULT: Thus the above program SJF scheduling has been executed successfully
Register No: 100606314011
Pageno:23
EX.NO:5(a) DATE:
IMPLEMENTATION OF PRIORITY SCHEDULING
AIM: To write a C program for the implementation of priority scheduling. ALGORITHM: Step1: Start the program. Step2: Get the arrival time and burst time for each process. Step3: Get priority for execution of each process. Step4: Allocate cpu for process pi depends on the priority assign to it. Step5: Process with highest priority is allocated to cpu first Step6: Repeat step4 &step5 until all the process have been completed Step7: Stop the program.
Register No: 100606314011
Pageno:24
PROGRAM: #include<stdio.h> struct ps {
int burst,turn,process,p,no,waiting; char name; }a[10]; int main() { int i,j,k,l,temp,temp1; float avgwait,waiting1=0,burst1=0,avgburst; printf("Enter the no of process:"); scanf("%d",&k); printf("\nProcess\t\tBurst\n"); for(i=1;i<=k;i++) { printf("%d\t\t",i); scanf("%d",&a[i].process); a[i].no=i; }
printf("\nEnter the priority"); printf("\n"); for(i=1;i<=k;i++) {
printf("For process%d:",i); scanf("%d",&a[i].p);
} for(i=1;i<=k;i++) for(j=i+1;j<=k;j++) { if(a[i].p>a[j].p) { temp=a[i].p; a[i].p=a[j].p; a[j].p=temp; temp=a[i].process; a[i].process=a[j].process; a[j].process=temp; temp1=a[i].no; a[i].no=a[j].no; a[j].no=temp1; }
Register No: 100606314011
Pageno:25
} for(i=1;i<=k;i++) a[i].waiting=a[i1].waiting+a[i].process; for(i=1;i<k;i++) { waiting1=waiting1+a[i].waiting; } printf("\nProcess\t\tWaiting\t\tTurn around\tPriority\n"); for(i=0;i<k;i++) { l=i+1; printf("\n%d\t\t%d\t\t%d\t\t%d",a[l].no,a[i].waiting,a[l].waiting,a[i].p+1); burst1=burst1+a[l].waiting; } avgwait=(waiting1/k); avgburst=(burst1/k); printf("\nAverage waiting time is:%f",avgwait); printf("\nAverage turn around time is:%f\n",avgburst); return 0; }
Register No: 100606314011
Pageno:26
OUTPUT:
[student@localhost ~]$ vi ex5a.c [student@localhost ~]$ gcc ex5a.c [student@localhost ~]$ ./a.out Enter the no of process:5 Process Burst 1 94 2 7 3 12 4 1 5 6 Enter the priority For process1:5 For process2:2 For process3:1 For process4:3 For process5:4 Process Waiting Turn around Priority 3 0 12 1 2 12 19 2 4 19 20 3 5 20 26 4 1 26 120 5 Average waiting time is:15.400000 Average turn around time is:39.400002
RESULT: Thus the above program for PRIORITY scheduling has been executed successfully.
Register No: 100606314011
Pageno:27
EX.NO:5(b) DATE: AIM:
IMPLEMENTATION OF ROUND ROBIN SCHEDULING
To write a C program for the implementation of ROUND ROBIN scheduling. ALGORITHM: Step 1: Start the program. Step 2: Declare the data types. Step 3: Initialize the ROUND ROBIN scheduling process. Step 4: Get input and output values. Step 5: Display the result. Step 6: Stop the program.
Register No: 100606314011
Pageno:28
PROGRAM: #include <stdio.h> #include <stdlib.h> typedef struct pcb { int pid, bt, bt_bal, tat, wt; } PCB; int main() {
PCB pr[10]; int i,j,k,n,tq; int sum_bt=0,sum_tat=0,sum_wt=0,tq_used=0; int gantt[2][50];
printf("Enter the no. of process : "); scanf("%d",&n); for(i=0;i<n; i++) { printf("Enter burst time of the process %d: ",i+1); pr[i].pid = i+1;
scanf("%d",&pr[i].bt); pr[i].bt_bal = pr[i].bt;
} printf("Enter the time quantum no. : "); scanf("%d",&tq); for( i=0;i<n;i++) sum_bt += pr[i].bt; printf("\nsum of bt = %d\n",sum_bt); k=0; do { for( i=0;i<n;i++) if( pr[i].bt_bal > 0 && pr[i].bt_bal <= tq ) {
tq_used += pr[i].bt_bal; pr[i].tat = tq_used;
pr[i].wt = pr[i].tat pr[i].bt; pr[i].bt_bal = 0; gantt[0][k] = pr[i].pid; gantt[1][k] = tq_used; k++; } else if( pr[i].bt_bal >0 )
Register No: 100606314011
Pageno:29
{ tq_used += tq; pr[i].bt_bal -= tq; gantt[0][k] = pr[i].pid; gantt[1][k] = tq_used; k++; } else if( pr[i].bt_bal < 0 ) { printf("\nError: bt -ve\n"); exit(1); } } while( tq_used != sum_bt); printf("\nROUND ROBBIN Scheduling GANTT Chart\n\n"); printf("PID: ");
for( i=0; i<k; i++) printf("%4d",gantt[0][i]); printf("\n\nTime: "); for( i=0; i<k; i++) printf("%4d",gantt[1][i]); for( i=0;i<n;i++) sum_wt += pr[i].wt; for( i=0;i<n;i++) sum_tat += pr[i].tat; printf("\n\nPID: "); for( i=0;i<n;i++) printf("%4d",i+1); printf("\nBtime:"); for( i=0;i<n;i++) printf("%4d",pr[i].bt); printf("\nTAT: "); for( i=0;i<n;i++) printf("%4d",pr[i].tat); printf("\nWtime:"); for( i=0;i<n;i++) printf("%4d",pr[i].wt);
printf("\n\nTotal waiting time = %d\n",sum_wt); printf("Average waiting time = %.2f\n",(float)sum_wt/n); printf("\nTotal turn around time = %d\n",sum_tat); printf("Average turn around time = %.2f\n\n",(float)sum_tat/n); return 0; }
Register No: 100606314011
Pageno:30
OUTPUT: [student@localhost ~]$ vi ex5b.c [student@localhost ~]$ gcc ex5b.c [student@localhost ~]$ ./a.out Enter the no. of process : 5 Enter burst time of the process 1: 4 Enter burst time of the process 2: 3 Enter burst time of the process 3: 7 Enter burst time of the process 4: 9 Enter burst time of the process 5: 5 Enter the time quantum no. : 2 sum of bt = 28 ROUND ROBBIN Scheduling GANTT Chart PID: 1 2 3 4 5 1 2 3 4 5 3 4 5 3 4 4 Time: 2 4 6 8 10 12 13 15 17 19 21 23 24 25 27 28 PID: 1 2 3 4 5 Btime: 4 3 7 9 5 TAT: 12 13 25 28 24 Wtime: 8 10 18 19 19 Total waiting time = 74 Average waiting time = 14.80 Total turn around time = 102 Average turn around time = 20.40
RESULT: Thus the above program ROUND ROBIN scheduling has been executed successfully
Register No: 100606314011
Pageno:31
EX.NO:6 DATE: AIM:
DEVELOPING APPLICATION USING INTER PROCESS COMMUNICATION
To write a C program using Inter Process Communication for developing an application.
ALGORITHM: Step 1: Create the child process using fork(). Step 2: Create the shared memory for parent process using shmget() system call. Step 3: Now allow the parent process to write in shared memory using shmpet pointer which is turn type a shmget(). Step 4: Now across and attach the same shared memory to the child process. Step 5: The data in the shared memory is read by the child process using the shmpet pointer. Step 6: Now, detach and rebase the shared memory.
Register No: 100606314011
Pageno:32
PROGRAM: #include<sys/types.h> #include<sys/ipc.h>
#include<sys/shm.h> int main(void) { int child,shmid; char *shmptr; int n; child=fork(); if (child) { sleep(5); shmid=shmget(2041,32,066); shmptr=shmat(shmid,0,0); for(n=0;n<9;n++) putchar(shmptr[n]); putchar('\n'); } else { shmid=shmget(2041,32,0666|IPC_CREAT); shmptr=shmat(shmid,0,0); for(n=0;n<9;n++)
shmptr[n]='1'+n; for(n=0;n<9;n++) putchar(shmptr[n]); putchar('\n'); shmdt(shmid); } exit(0); }
Register No: 100606314011
Pageno:33
OUTPUT: [student@localhost ~]$ vi ex6.c [student@localhost ~]$ gcc ex6.c [student@localhost ~]$ ./a.out 123456789 123456789
RESULT: Thus the above C program using Inter Process Communication for developing an application has been executed successfully.
Register No: 100606314011
Pageno:34
EX.NO: 7 DATE: AIM:
IMPLEMENTATION OF PRODUCER-CONSUMER PROBLEM USING SEMAPHORES
To write a C program using semaphores for the implementation of producer-consumer problem.
ALGORITHM: Step1: start the program. Step2: Initialize semaphore full=0, empty= buffer size. Step3: Assign two pointer in and act respectively for producer and consumer. Step4:Using call construct get the choice for producer consumer ,display & exit respectively. Step5: For the producer write the item in to the buffer if it is not empty. Step6: for the consumer read the item from the buffer it it is not full. Step7: Repeat the step 5 and 6 until the buffer is empty. Step8: Stop the process.
Register No: 100606314011
Pageno:35
PROGRAM: # define BUFFER_SIZE 3 #include<stdio.h> typedef int semaphore; semaphore full=0;
semaphore empty=BUFFER_SIZE; typedef struct { int year; int roll; }item; item buffer[BUFFER_SIZE]; int counter=0,i=0; int in=0; int out=0; main() { int choice; int year1,roll1; int I; while(1) { printf("\n1.Producer"); printf("\n2.Consumer"); printf("\n3.Display"); printf("\n4.Exit"); printf("\n\nEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: if(counter>=2) { printf("\n\n*****Buffer Full*****\n\n"); } if(((in+1)%BUFFER_SIZE)!=out) { printf("\n\nEnter the Year:"); scanf("%d",&year1); printf("\n\nEnter Roll no:"); scanf("%d",&roll1); wait(empty); buffer[in].year=year1;
Register No: 100606314011
Pageno:36
buffer[in].roll=roll1;
signal(full); in=(in+1)%BUFFER_SIZE; counter++;
}
break; case 2: if(in!=out) { wait(full);
printf("\n\nThe Year is %d",buffer[out].year); printf("\n\nThe Roll No. is %d\n\n",buffer[out].roll); signal(empty);
out=(out+1)%BUFFER_SIZE; counter--;
} if(in<=out) printf("\n\n*****Buffer is Empty!!!!*****\n\n"); break;
case 3: for(i=out;i<in;i++) { printf("\nThe Year is %d",buffer[i].year); printf("\nThe Roll No. is %d\n\n",buffer[i].roll);
} if(in<=out) printf("\n\n******** BUFFER IS EMPTY! **********\n\n "); break;
case 4: exit(0);
} } }
Register No: 100606314011
Pageno:37
OUTPUT: [student@localhost ~]$ vi ex7.c [student@localhost ~]$ gcc ex7.c [student@localhost ~]$ ./a.out 1.Producer 2.Consumer 3.Display 4.Exit
Enter your choice:1 Enter the Year:2011 Enter Roll no:11 1.Producer 2.Consumer 3.Display 4.Exit Enter your choice:1 Enter the Year:2012 Enter Roll no:12 1.Producer 2.Consumer 3.Display 4.Exit Enter your choice: 1 *****Buffer Full***** Enter your choice: 2 The Name is 1900 The Roll No. is 1 1. Producer 2. Consumer 3. Display 4. Exit Enter your choice:2 The Name is 2000 The Roll No. is 2 *****Buffer is Empty!!!!*****
RESULT: Thus the above C program using semaphores for the implementation of producer-consumer problem has been executed successfully.
Register No: 100606314011
Pageno:38
EX.NO:8 DATE: AIM:
IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME-I
To write a C program for the implementation of memory management scheme-I.
ALGORITHM: Step 1: Get the number of memory partitions and memory partitions one by one. Step 2: Get the number of processes and size of each process. Step 3: In first fit strategy allocate the first hole that is big enough. Searching can start either at the beginning of the set of holes or where the previous first-fit search ended. Stop searching as soon as find a free hole that is large enough. Step 4: In best fit strategy allocate the smallest hole that is big enough. Search the entire list, unless the list is kept ordered by size. This strategy produces the smallest leftover hole. . Step 5: In worst fit strategy allocate the largest hole. Again, search the entire list, unless it is sorted by size. This strategy produces the largest left over hole, which may be more useful than the smaller leftover hole from a best-fit approach.
Register No: 100606314011
Pageno:39
PROGRAM: #include<stdio.h>
int aflag=0,np,nmp,p[10],mp[10],mp1[10],i,j,ch; main( ) {
printf(\n\nMemory Management Scheme I); printf("\n\nEnter No. of memory partitions:"); scanf("%d",&nmp); printf("Enter the Memory Partitions:\n"); for(i=0;i<nmp;i++) scanf("%d",&mp[i]); memcpy(mp1,mp,10); printf("\nEnter no.of processes:"); scanf("%d",&np); printf("Enter the size of each process:\n"); for(i=0;i<np;i++) {
printf("\nP-%d :",i); scanf("%d",&p[i]); }
printf("\nFirstFit Strategy\n"); Firstfit(); memcpy(mp,mp1,10); printf("\n\nBestFit Strategy\n"); Bestfit(); memcpy(mp,mp1,10); printf("\n\nWorstFit Strategy\n"); Worstfit(); } Firstfit() { for(i=0;i<np;i++) { aflag=0; for(j=0;j<nmp;j++) { if(mp[j]>=p[i]) { printf("\nPrs %d(%dkb) alctd to Mmy Ptn %d(%dkb)",i,p[i],j,mp[j]); mp[j]=mp[j]-p[i]; aflag=1; break; }
Register No: 100606314011
Pageno:40
} if(aflag==0) printf("\nPrs %d(%dkb) can't be allocated! ",i,p[i]); } } Bestfit() {
int bfmp,fsize=10000; for(i=0;i<np;i++) { aflag=0; for(j=0;j<nmp;j++) { if(mp[j]>=p[i] && ((mp[j]-p[i])<fsize)) {
fsize=mp[j]p[i]; bfmp=j; aflag=1; } } if(aflag==0) printf("\nPrs %d(%dkb) can't be allocated! ",i,p[i]); else { printf("\nPrs %d(%dkb) alctd to Mmy Ptn%d(%dkb)",i,p[i],bfmp,mp[bfmp]); mp[bfmp]=mp[bfmp]-p[i]; fsize=10000; } } } Worstfit() {
int bfmp,fsize=0; for(i=0;i<np;i++) { aflag=0; for(j=0;j<nmp;j++) { if(mp[j]>=p[i] && ((mp[j]-p[i])>fsize)) {
fsize=mp[j]p[i]; bfmp=j; aflag=1;
Register No: 100606314011
Pageno:41
} } if(aflag==0) printf("\nPrs %d(%dkb) can't be allocated! ",i,p[i]); else { printf("\nPrs %d(%dkb) alctd to Mmy Ptn %d(%dkb)",i,p[i],bfmp,mp[bfmp]); mp[bfmp]=mp[bfmp]-p[i]; fsize=0; } } }
Register No: 100606314011
Pageno:42
OUTPUT: Enter no.of memory partitions:3 Enter the Memory Partitions: 125 100 250
Enter no.of processes:5 Enter the size of each process: P-0: 100 P-1: 75 P-2: 200 P-3: 50 P-4: 90 FirstFit Strategy
Prs 0(100kb) alctd to Mmy Ptn 0(125kb) Prs 1(75kb) alctd to Mmy Ptn 1(100kb) Prs 2(200kb) alctd to Mmy Ptn 2(250kb) Prs 3(50kb) alctd to Mmy Ptn 2(50kb) Prs 4(90kb) can't be allocated! BestFit Strategy Prs 0(100kb) alctd to Mmy Ptn 1(100kb) Prs 1(75kb) alctd to Mmy Ptn 0(125kb) Prs 2(200kb) alctd to Mmy Ptn 2(250kb) Prs 3(50kb) alctd to Mmy Ptn 0(50kb) Prs 4(90kb) can't be allocated! WorstFit Strategy Prs 0(100kb) alctd to Mmy Ptn 2(250kb) Prs 1(75kb) alctd to Mmy Ptn 2(150kb) Prs 2(200kb) can't be allocated! Prs 3(50kb) alctd to Mmy Ptn 0(125kb) Prs 4(90kb) alctd to Mmy Ptn 1(100kb)
RESULT: Thus the above program for the implementation of memory management scheme-I has been executed successfully.
Register No: 100606314011
Pageno:43
EX.NO:9 DATE: AIM:
IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME-II
To write a C program for the implementation of memory management scheme-II.
ALGORITHM: Step 1: Obtain the number of frames and sequence string from the user. Step 2: In the FIFO page replacement method if the page is not available in the frame increase page fault by one and then remove the page based on FIFO manner. Step 3: In the LRU page replacement method replace the page that has not been need for the longest period of time. Step 4: Display the number of page faults in each method.
Register No: 100606314011
Pageno:44
PROGRAM: #include<stdio.h> intnf,i,j=0,k,l,sref,refstr[30],frame[5]={-1,-1,-1,-1,1},count[5]={0,0,0,0,0}; int flag=0,pfault=0;
int mincnt=0,minfr,i1,rctcnt=0; void lru(); int main() { int ch;
printf(\n\n Memory Management Scheme II); printf("\n\nEnter the No. of Frames:"); scanf("%d",&nf); printf("\nEnter the Size of Reference String:"); scanf("%d",&sref); printf("\nEnter the Reference String:"); for(i=0;i<sref;i++) scanf("%d",&refstr[i]); printf("\n\n1.FIFO\t2.LRU\n"); printf("\n\nEnter the choice:"); scanf("%d",&ch); switch(ch) {
case 1: fifo(); break; case 2: lru(); break; default: return 0; } } fifo() { printf("\n\t********FIFO**********"); printf("\n\nReference Str\tFrame\n"); for(i=0;i<sref;i++) { printf("\n%d\t\t",refstr[i]); pfaultcheck(); if(flag!=1) {
Register No: 100606314011
Pageno:45
frame[j]=refstr[i]; printframe(); pfault++; flag=0; j++; if(j==nf) j=0; } } printf("\nNo. of Page Faults Occured : %d ",pfault); } pfaultcheck() { for(k=0;k<nf;k++) { if(frame[k]==refstr[i]) { flag=1; break; }
else flag=0; } } void lru() { printf("\n\t********LRU********\n"); printf("\nReference Str\tFrame\n"); for(i=0;i<sref;i++) { printf("\n%d\t\t",refstr[i]); pfaultcheck1(); if(flag!=1) { if(count[nf-1]==0)//intial frame filling-fifo { frame[j]=refstr[i]; count[j]=rctcnt++; j++; } else//rest---lru { findminframe();
Register No: 100606314011
Pageno:46
frame[minfr]=refstr[i]; count[minfr]=rctcnt++; } printframe(); pfault++; } } printf("\nNo. of page faults occured : %d ",pfault); } pfaultcheck1() { for(k=0;k<nf;k++) { if(frame[k]==refstr[i]) { count[k]=rctcnt++; flag=1; break; }
else flag=0; } } findminframe() { mincnt=count[0]; minfr=0; for(i1=0;i1<nf;i1++)//finding mincount-lru frame { if(mincnt>count[i1]) { mincnt=count[i1]; minfr=i1; } } } printframe() { for(l=0;l<nf;l++) {
if(frame[l]==1) printf("* "); else
Register No: 100606314011
Pageno:47
printf("%d ",frame[l]); } }
Register No: 100606314011
Pageno:48
OUTPUT: Enter the no. of frames:3 Enter the size of reference string:20 Enter the reference string:7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 1.FIFO 2.LRU Enter the choice:1 ********FIFO********** Reference Str Frame 7 7** 0 70* 1 701 2 201 0 3 0 4 2 3 0 3 2 1 2 0 1 7 712 0 702 1 701 No. of Page Faults Occured : 15 [080606314005@localhost ~]$
[student@localhost ~]$ ./a.out Enter the no. of frames:3 Enter the size of reference string:20 Enter the reference string:7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 1. FIFO 2.LRU Enter the choice:2 ********LRU******** Reference Str Frame 7 7** 0 70* 1 701 2 201
231 230 430 420 423 023
013 012
0 3 0 4 2 3 0 403 402 432 032
Pageno:49
203
Register No: 100606314011
3 2 1 132 2 0 102 1 7 107 0 1 No. of page faults occured : 12
RESULT: Thus the above program for the implementation of memory management scheme-II has been executed successfully .
Register No: 100606314011
Pageno:50
EX.NO:10 DATE: AIM:
IMPLEMENTATION OF CONTINUOUS FILE ALLOCATION TECHNIQUES
To write a C program for the implementation of continuous file allocation techniques.
ALGORITHM: Step 1: Get the file id , starting location and number of blocks, from the user. Step 2: Disk addresses define a linear ordering on the disk. . Step 3: If the file is n blocks /arg and starts at location b, then it occupies blocks b,b+1,b+2,b+n. Step 4: If two files process to the save address block an error message will be displayed.
Register No: 100606314011
Pageno:51
PROGRAM: #include<stdio.h>
int a[20],num=0,fid[10],length[10],start[10]; void fieldescriptor(); void fieldescriptor() { int i; printf("\nFile id\tStarting address\tLength\n"); for(i=0;i<num;i++) printf("%d\t\t%d\t\t%d\n",fid[i],start[i],length[i]); } void display() {
int i; for(i=0;i<16;i++) printf("%2d ",i); printf("\n"); for(i=0;i<16;i++) printf("%2d ",a[i]); } int main() {
int i,n,k,temp,st,id,l,flag=0,cho; for(i=0;i<16;i++) a[i]=0;
printf("\n\tFile Allocation Technique: CONTIGUOUS ALLOCATION METHOD\n"); printf("\n\nMemory Before Allocation:\n"); display(); while(1) { printf("\n\nEnter the FILE ID:"); scanf("%d",&id); printf("\nEnter the Number of Blocks the File Occupies:"); scanf("%d",&l); fid[num]=id; length[num]=l; printf("\nEnter the Starting Address:"); l:scanf("%d",&st); flag=0; if((st+l)>16) { printf("\nSorry the given Memory goes out of Space!!!"); printf("\nEnter the Starting Address:");
Register No: 100606314011
Pageno:52
goto l; } for(i=st;i<(st+l);i++) { if(a[i]!=0) { flag=1; break; } } if(flag==0) { start[num]=st; for(i=st;i<(st+l);i++) a[i]=id; } else {
printf("\n\nSorry the given blocks are already occupied"); printf("\n\nEnter the NEW Starting Address:"); goto l; } flag=0; num++; fieldescriptor();
printf("\n\nMemory After Allocation\n"); display(); printf("\n\nDO you want to Continue ?\n1.Yes\n2.No\n"); scanf("%d",&cho); if(cho==2) exit(0); } return 0; }
Register No: 100606314011
Pageno:53
OUTPUT: [student@localhost ~]$ vi ex10.c [student@localhost ~]$ gcc ex10.c [student@localhost ~]$ ./a.out File Allocation Technique: CONTIGUOUS ALLOCATION METHOD Memory Before Allocation: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Enter the FILE ID:2 Enter the Number of Blocks the File Occupies:5 Enter the Starting Address:2 File id Starting address Length 2 2 5 Memory After Allocation 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 2 2 2 2 2 0 0 0 0 0 0 0 0 0 DO you want to Continue ? 1.Yes 2.No 1 Enter the FILE ID:3 Enter the Number of Blocks the File Occupies:6 Enter the Starting Address:9 File id Starting address Length 2 2 5 3 9 6 Memory After Allocation 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 2 2 2 2 2 0 0 3 3 3 3 3 3 0 DO you want to Continue ? 1.Yes 2.No 2
RESULT: Thus the above program for the implementation of continuous file allocation techniques has been executed successfully.
Register No: 100606314011
Pageno:54
Register No: 100606314011
Pageno:55