You are on page 1of 44

SYSTEM CALL

Ex.No:1 Date:

PROBLEM STATEMENT:
To write a C program to illustrate the use of system calls of UNIX operating system such as fork, exec, getpid, exit, wait, close, stat, opendir, readdir.

PROBLEM DESCRIPTION:
A system call is the mechanism used by an application program to request service from the operating system. A child process is created by forking the shell, and the child process is overlaid, once again by exec, with the code associated with the program to be executed. In most systems, a parent process can create an independently executing child process. The parent process may then issue a wait system call, which suspends the execution of the parent process while the child executes. When the child process terminates, it returns an exit status to the operating system, which is then returned to the waiting parent process. The parent process then resumes execution fcntl.h - defines file control option. sys/types.h - defines data type used in system source code or it define primitive system datatype sys/stat.h - defines data return by the stat function. pid - process identification number fork() - creation of new process or child process. exit(-1) - denotes the failure of the program. execlp - replaces the current process image with the new process image created from the executable file specified in file name. wait(0) - suspends the execution of the current process. exit(0) - denotes the success of the program. stat - get file status. mode - permission given to the user to access file. user ID - identify user within the kernel by an unsigned integer. group ID - numeric value used to represent specific group. getpid() - to retrive the process ID. getppid() - to retrive parent process ID. sleep(1) - suspend execution for an interval 1sec. dirent.h - format of directory entries.

Opendir(.) to open a current directory Readdir - to read a current directory Closedir to close a current directory Direntp->d_name to list all file names of the current directory.

1. CHILD PROCESS CREATION: ALGORITHM:


1: Create the process using fork () system call. 2: Check the process id. If process id=-1 then display an error. 3: If process id =0 then display as child process. 4: Display its process id and parent process id. 5: Else display as parent process 6: Display its process id and child process id

PROGRAM:
#include<fcntl.h> #include<sys/types.h> int main(int argc,char *argv[]) { int pid; pid=fork(); printf("Process ID is%d",pid); if(pid<0) { printf("Fork failed"); exit(-1); } else if(pid==0) { execlp("/bin/ls","ls",0); } else { wait(0); printf("Childcomplete"); exit(0); } }

2. DISPLAY THE FILE STATUS: ALGORITHM:


1:Create a file 2:Using the members of the stat system call display the status of the file such as inode,size,mode,id etc.

PROGRAM:
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> void main() { struct stat s; char fname[20]; printf("Enter the file name"); scanf("%s",fname); stat(fname,&s); printf("File Inode\t%d\n",s.st_ino); printf("File Size\t%d\n",s.st_size); printf("File Mode\t%d\n",s.st_mode); printf("User ID\t%d\n",s.st_uid); printf("Group ID\t%d\n",s.st_gid); }

3. DISPLAY THE PROCESS IDENTIFIERS OF PARENT AND CHILD PROCESSES: ALGORITHM:


1:Create a process using fork() system call 2:In the child process, display the process id, child id and its parent id. 3:In the parent process, display the process id, child id and its parent id.

PROGRAM:
#include<stdio.h> #include<sys/types.h> #include<unistd.h> int main(void) { pid_t childpid; int x; childpid = fork(); if ( childpid == 0) {

printf("In the child process\n"); printf("------------------------\n"); printf("This is process %d \n", getpid()); printf("The parent of this process has id %d \n", getppid()); printf("childpid = %d \n", childpid); } else { printf("In the parent process\n"); printf("--------------------------\n"); printf("This is process %d \n", getpid()); printf("The parent of this process has id %d \n", getppid()); printf("childpid = %d \n", childpid); sleep(1); } return 0; }

4. PRINT ALL FILES IN THE CURRENT DIRECTORY: ALGORITHM:


1.Declare the directory pointer using dirp 2.Open a current directory 3.Read the current directory and display the names 4.Close the directory.

PROGRAM:
#include <stdio.h> #include <dirent.h> main() { DIR *dirp; struct dirent *direntp; dirp = opendir( "." ); while ( (direntp = readdir( dirp )) != NULL ) (void)printf( "%s\n", direntp->d_name ); (void)closedir( dirp ); return (0); }

SAMPLE INPUT AND OUTPUT: 1.CHILD PROCESS:


cc sc.c ./a.out a.out case cseb dis.c f2 f4 g3 pattern salary sreethivya swap bill check cube f1 f3 fact great prime sc.c sum Process ID is14786 Childcomplete

2.DISPLAY THE FILE STATUS:


cc dis.c ./a.out Enter the file name bill File Inode 2819450 File Size 232 File Mode 33188 User ID 1053 Group ID 504

3.DISPLAY THE PROCESS IDENTIFIERS OF PARENT AND CHILD PROCESSES:


cc pc.c ./a.out In the child process -----------------------This is process 5415 The parent of this process has id 5414 Childpid=0 In the parent process -----------------------This is process 5650 The parent of this process has id 4830 Childpid=5851

4.PRINT ALL FILES IN THE CURRENT DIRECTORY


cc cd.c ./a.out dispf.c .vininfo child.c pc.c cd.c bash_history cdir.c

DEPARTMENT OF IT Preparation 30 Performance 30 Record 40 Total 100

CONCLUSION:
The use of system calls of UNIX operating system such as fork,exec,getpid,exit,wait,close,stat,opendir,readdir was successfully executed.

I/O SYSTEM CALL


Ex.No:2 Date:

PROBLEM STATEMENT:
To write a C program using the I/O system calls of UNIX operating system such as open, read, write.

PROBLEM DESCRIPTION:
A program that needs access to a file stored in a filesystem uses the open system call. This system call allocates resources associated to the file (the file descriptor), and returns a handle that the process will use to refer to that file from then on.After using the file, the process should use the close system call. creat create new file. S_IWRITE|S_IREAD permitted to read and write. open to open a file. O_CREAT if file exist it opens or else it will create a new file. Strlen() count the number of bytes. write write from buffer to file. read reads from file to buffer. malloc allocates memory close to close a file

1.CREATE THE FILE: ALGORITHM:


1: Create a file by specifying its name. 2: If the return value is non-negative,the file is created. 3: If the return value is negative,the file is not created

PROGRAM:
#include<stdio.h> #include<sys/stat.h> #include<fcntl.h> void main() { if(creat("sree.c",S_IWRITE|S_IREAD)!=-1)

printf("File has been created"); else printf("File is not created"); }

2.OPEN A FILE: ALGORITHM:


1: Open a file by specifying its name,access and mode. 2: If the return value is non-negative,the file is opened. 3: If the return value is negative,the file is not opened

PROGRAM:
#include<stdio.h> #include<sys/stat.h> #include<fcntl.h> void main() { if(open("sree.c",O_CREAT)==-1) printf("file is not opened"); else printf("File is opened"); }

3.WRITE A FILE: ALGORITHM:


1: Get the data from the user. 2: Open a file. 3: Write the data from the file. 4: Get the data and update the file.

PROGRAM:
#include<stdio.h> #include<sys/stat.h> #include<string.h> void main() { int n,i; char s[20]; n=creat("sree.c",S_IWRITE|S_IREAD); printf("Enter the string where '.' denotes end of file\n"); for(i=0;s[i]!='.';i++) { scanf("%s",s); if(s[i]=='.') break; else write(n,s,strlen(s)); }

close(n); }

4.READ THE FILE: ALGORITHM:


1: Get the data from the user. 2: Open a file. 3: Read from the file. 4: Close the file.

PROGRAM:
#include<stdio.h> #include<fcntl.h> #include<malloc.h> void main() { int n; void *buf; buf=malloc(20); n=open("sree.c",O_CREAT); read(n,buf,20); printf("Contents of file is:\n%s",buf); close(n); }

SAMPLE INPUT AND OUTPUT: 1.CREATE THE FILE:


cc crea.c ./a.out File has been created

2.OPEN A FILE:
cc op.c ./a.out File is opened

3.WRITE A FILE:
cc wri.c ./a.out Enter the string where '.' denotes end of file os system.

4.READ THE FILE:


cc re.c ./a.out Contents of file is: ossystem.

DEPARMENT OF IT Preparation 30 Performance 30 Record 40 Total 100

CONCLUSION:
I/O system calls of UNIX operating system such as open, read, write using C was successfully executed.

SIMULATION OF UNIX COMMANDS


Ex no: 3 Date:

PROBLEM STATEMENT:
To write a C program to simulate UNIX commands like ls and grep.

PROBLEM DESCRIPTION:
This program simulates the ls command and grep command to list all the files in the current directory and finds for the specified pattern within a given file. ls to list all the files grep it allows us to search for a pattern in a file argc count of command line arguments argv[] list of command line arguments scandir list files and directories inside the specified path alphasort used as the comparison function for the scandir() function to sort the directory entries into alphabetical order strstr - locates the first occurrence in the string pointed by the sequence of bytes fgets reads in atmost one less than size characters from stream and stores them into the buffer

1. SIMULATION OF ls: ALGORITHM:


1.Define the main() function. 2.Get the pathname of current working directory. 3.If there is no directory error will be printed. 4.Otherwise display all files in the current directory.

PROGRAM:
#include<stdio.h> #include<dirent.h> int main() { struct dirent **namelist; int n,i;

char pathname[100]; getcwd(pathname); n=scandir(pathname,&namelist,0,alphasort); if(n<0) printf("Error"); else { for(i=0;i<n;i++) { printf("%s\n",namelist[i]->d_name); } } }

2. SIMULATION OF grep: ALGORITHM:


1.Count and occurrences are initialized to zero. 2.A file is created with some pattern 3.The pattern to be identified is specified in the command line. 4.Total count and line of occurrence of the pattern is found and displayed similar to the grep command.

PROGRAM:
#include<stdio.h> #include<string.h> #define max 1024 void usage() { printf("usage:\t./grep filename pattern \n"); printf("example: ./grep grep.c int\n"); } int main(int argc,char *argv[]) { FILE *fp; char fline[max]; char *newline; int count=0; int occurences=0; if(argc!=3) { usage(); exit(1); }

if(!(fp=fopen(argv[1],"r"))) { printf("grep : Couldnot open file : %s\n",argv[1]); exit(1); } while(fgets(fline,max,fp)!=NULL) { count++; if(newline=strchr(fline,'\n')) *newline='\0'; if(strstr(fline,argv[2])!=NULL) { printf("%s: %d %s\n",argv[1],count,fline); occurences++; } } printf(total number of occurrences %d,occurrences); }

SAMPLE INPUT AND OUTPUT: 1.SIMULATION OF ls:


cc sls.c ./a.out .ccache .viminfo a.out ls.c

2.SIMULATION OF GREP:
Cc sgrep.c ./a.out gv.c a gv .c : 4a gv..c : 6a total number of occurrences : 2

DEPARMENT OF IT Preparation 30 Performance 30 Record 40 Total 100

CONCLUSION:
Thus the simulation of UNIX commands is executed successfully.

CPU SCHEDULING - I
Ex.No: 4 Date:

PROBLEM STATEMENT:
To implement FCFS and SJF scheduling algorithms using C.

PROBLEM DESCRIPTION: FIRST COME FIRST SERVE SCHEDULING:


With this scheme, the process that requests the CPU first is allocated the CPU first. The implementation of the FCFS policy is easily managed with a FIFO queue. The FCFS scheduling algorithm is nonpreemptive. Once the CPU has been allocated to a process, that process keeps the CPU until it releases the CPU either by terminating or by requesting I/O. While considering the performance of the FCFS scheduling algorithm, the higher burst time process makes the lower burst time process to wait for a long time. This effect is known as CONVOY effect.

SHORTEST JOB FIRST SCHEDULING:


This algorithm associates with each process the length of the latters next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU burst. If two processes have the same length next CPU burst, FCFS scheduling is used to break the tie. The SJF algorithm may be either preemptive or nonpreemptive. The choice arises when a new process arrives at the ready queue while a previous process is executing. The SJF scheduling algorithm is provably optimal, in that it gives the minimum average waiting time for a given set of processes.

1.FCFS: ALGORITHM:
1: Get the number of processes and burst time. 2: The process is executed in the order given by the user. 3: Calculate the waiting time and turn around time. 4: Display the gantt chart,avg waiting time and turn around time.

PROGRAM:
#include<stdio.h> void main(int argc,char *argv[]) { int i,j=0,n,burst[10],wait[10],turn[10]; float w=0,t=0; printf("Enter the no. of processes"); scanf("%d",&n); burst[0]=0;

printf("Enter the burst time"); for(i=1;i<=n;i++) { scanf("%d",&burst[i]); } printf("\n\nGantt chart\n"); printf("\n________________________________________________________\n"); for(i=1;i<=n;i++) printf("\tP%d\t|",i); printf("\n________________________________________________________\n"); for(i=0;i<=n;i++) { j=j+burst[i]; wait[i+1]=j; turn[i]=j; printf("%d\t\t",j); } for(i=1;i<=n;i++) w=w+wait[i]; for(i=0;i<=n;i++) t=t+turn[i]; w=w/n; t=t/n; printf("\nAverage waiting time %0.2f",w); printf("\nAverage turnaroundtime %0.2f",t); }

2.SJF: ALGORITHM:
1: Get the number of processes and burst time. 2: Sort the process based on the burst time in ascending order. 3: Calculate the waiting time and turn around time. 4: Display the gantt chart,avg waiting time and turn around time.

PROGRAM:
#include<stdio.h> void main(int argc,char *argv[]) { int b[10],temp,i,j,n,wait[10],burst[10],turn[10]; float w=0,t=0; printf("Enter the no. of processes"); scanf("%d",&n); burst[0]=0; b[0]=0; printf("Enter the burst time"); for(i=1;i<=n;i++)

{ scanf("%d",&burst[i]); } for(i=1;i<=n;i++) b[i]=burst[i]; for(i=1;i<n;i++) for(j=i+1;j<=n;j++) if(b[i]>b[j]) { temp=b[i]; b[i]=b[j]; b[j]=temp; } printf("\nGantt chart"); printf("\n________________________________________________________\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(b[i]==b[j]) printf("P%d|\t",j); printf("\n_________________________________________________________\n"); j=0; for(i=0;i<=n;i++) { j=j+b[i]; wait[i+1]=j; turn[i]=j; printf("%d\t",j); } for(i=1;i<=n;i++) w=w+wait[i]; for(i=0;i<=n;i++) t=t+turn[i]; w=w/n; t=t/n; printf("\nAverage waiting time is %0.2f",w); printf("\nAverage turnaroundtime is %0.2f",t); }

SAMPLE INPUT AND OUTPUT: 1.FCFS:


cc fcfs.c ./a.out Enter the no. of processes 3 Enter the burst time 3 6 8 Gantt chart ________________________________________________________ P1 | P2 | P3 | ________________________________________________________ 0 3 9 17 Average waiting time 4.00 Average turn around time 9.67

2.SJF:
cc sjf.c ./a.out Enter the no. of processes 3 Enter the burst time2 1 3 Gantt chart ________________________________________________________ P1| P2| P3| ________________________________________________________ 0 1 3 6 Average waiting time is 1.33 Average turn around time is 3.33

DEPARMENT OF IT Preparation 30 Performance 30 Record 40 Total 100

CONCLUSION:
FCFS and SJF scheduling algorithms is implemented using C and executed.

CPU SCHEDULING - II
Ex.No:5 Date:

PROBLEM STATEMENT:
To implement Round Robin and Priority scheduling algorithms using C.

ROUND ROBIN SCHEDULING: PROBLEM DESCRIPTION:


The round-robin scheduling algorithm is designed especially for time-sharing systems. It is similar to FCFS scheduling, but preemption is added to switch between processes. A small unit of time, called a time quantum is defined. A time quantum is generally from 10 to 100 milliseconds. The ready queue is treated as circular queue.

ALGORITHM:
1: Initialize all the structure elements 2: Receive inputs from the user to fill process id,burst time and arrival time. 3: Calculate the waiting time for all the process id. i) The waiting time for first instance of a process is calculated as: a[i].waittime=count + a[i].arrivt ii) The waiting time for the rest of the instances of the process is calculated as: a) If the time quantum is greater than the remaining burst time then waiting time is calculated as: a[i].waittime=count + tq b) Else if the time quantum is greater than the remaining burst time then waiting time is calculated as: a[i].waittime=count - remaining burst time 4: Calculate the average waiting time and average turnaround time 5: Print the results of the step 4.

PROGRAM:
#include<stdio.h> void main() { int b[10],i,j=1,n,temp,burst[10],wait[10],turn[10],p[10],a=1,q,tat[10],t1=0; float t=0,w=0; printf("Enter the no of process & Q"); scanf("%d%d",&n,&q); burst[0]=0;

b[0]=0; tat[0]=0; p[0]=0; printf("Enter burst time"); for(i=1;i<=n;i++) scanf("%d",&burst[i]); for(i=1;i<=n;i++) b[i]=burst[i]; printf("\n\n\t\t Gantt chart\n"); printf("-------------------------------------------------------\n"); for(i=1;i<=n;i++) { if(b[i]>0) { a=1; printf("P%d\t|",i); if(b[i]>=q) { t1=t1+q; p[j]=t1; j++; } else if(b[i]<q) { t1=t1+b[i]; p[j]=t1; j++; } b[i]=b[i]-q; if(b[i]<=0) tat[i]=t1; } else a++; if(a==n+1) break; if(i==n) i=0; } printf("\n---------------------------------------------------------\n"); for(i=0;i<j;i++) printf("%d\t",p[i]); for(i=1;i<=n;i++) { t=t+tat[i]; w=w+tat[i]-burst[i];

} w=w/n; t=t/n; printf("\nThe average waiting time is %0.2f",w); printf("\nThe average turn around time is %0.2f",t); }

PRIORITY SCHEDULING: PROBLEM DESCRIPTION:


A priority is associated with each process and the CPU is allocated to the process with the highest priority. Equal priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where the priority(p) is the inverse of the next CPU burst. The larger the CPU burst, the lower the priority and vice versa.

ALGORITHM:
1: Get the number of processes, priority and burst time. 2: Sort the process based on the priority in ascending order 3: Calculate the waiting time and turn around time. 4: Display the gantt chart,avg waiting time and turn around time.

PROGRAM:
#include<stdio.h> void main(int argc,char *argv[]) { int temp,i,n,j,p[10],burst[10],b[10],wait[10],turn[10],b1[10],p1[10]; float w=0,t=0; printf("Enter the number of processes"); scanf("%d",&n); burst[0]=0; for(i=1;i<=n;i++) { printf("Enter the Burst Time and Priority"); scanf("%d%d",&b1[i],&p[i]); } for(i=1;i<=n;i++) p1[i]=p[i]; for(j=1;j<=n;j++) p1[j]=p[j]; for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) if(p[i]>p[j]) { temp=p[i]; p[i]=p[j];

p[j]=temp; temp=b[i]; b[i]=b[j]; b[j]=temp; } for(i=1;i<=n;i++) printf("%d",p[i]); printf("\n\t\t\tGanttChart\n"); printf("\t_______________________\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(p[i]==p1[j]) { b[i]=b1[j]; printf("\t\tp%d",j); } printf("\t\n\t____________________\n\t"); j=0; for(i=0;i<=n;i++) { j=j+b[i]; wait[i+1]=j; turn[i]=j; printf("%d\t\t",j); } for(i=1;i<=n;i++) { t=t+turn[i]; w=w+wait[i]; } w=w/n; t=t/n; printf("\nThe average Waiting Time %f",w); printf("\nThe average Turnaround Time %f",t); }

SAMPLE INPUT AND OUTPUT: ROUND ROBIN SCHEDULING:


Enter the number of processes and Quantum 3 5 Enter the Burst Time 10 Enter the Burst Time 5 Enter the Burst Time 3 GanttChart ____________________________________ p1 p2 p3 p1 ____________________________________ 0 5 10 13 18 The average Waiting Time 9.3333 The average Turnaround Time 13.67

PRIORITY SCHEDULING:
Enter the number of processes 3 Enter the Burst Time and Priority 2 3 Enter the Burst Time and Priority 2 1 Enter the Burst Time and Priority 4 2 123 GanttChart _______________________ p2 p3 p1 ____________________ 0 2 6 8 The average Waiting Time 2.666667 The average Turnaround Time 5.333333

DEPARTMENT OF IT Preparation 30 Performance 30 Record 40 Total 100

CONCLUSION:
Round Robin and priority scheduling algorithms is implemented using C and executed.

PRODUCER-CONSUMER PROBLEM
Ex.No:6 Date:

PROBLEM STATEMENT:
To write a C program to implement the Producer & consumer Problem (Semaphore).

PROBLEM DESCRIPTION:
The producer-consumer problem (also known as the bounded-buffer problem) is a classical example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer. The producer's job is to generate a piece of data, put it into the buffer and start again. At the same time the consumer is consuming the data (i.e. removing it from the buffer) one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer. The solution for the producer is to go to sleep if the buffer is full. The next time the consumer removes an item from the buffer, it wakes up the producer who starts to fill the buffer again. In the same way the consumer goes to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached by means of inter-process communication, typically using semaphores. An inadequate solution could result in a deadlock where both processes are waiting to be awakened.

ALGORITHM:
1: The Semaphore mutex, full & empty are initialized. 2: In the case of producer process a)Produce an item in to temporary variable. b)If there is empty space in the buffer check the mutex value for enter into the critical section. c)If the mutex value is 0, allow the producer to add value in the temporary variable to the buffer. 3: In the case of consumer process a)It should wait if the buffer is empty b)If there is any item in the buffer check for mutex value, if the mutex==0,remove item from buffer c)Signal the mutex value and reduce the empty value by 1.Consume the item. 4: Print the result

PROGRAM :
#include<stdio.h> char buf[20],p[20],cos[20]; int mutex,i,k,c,sz,n; mutex=0; void prosig() { mutex=mutex+1; } void consig() { mutex=mutex-1; } int buffer(int mutex) { if(mutex==0) return 1; else return 0; } void producer(int sz) { int c; c=buffer(mutex); if(c==1) { printf("\nProducer can produce the item and give $ for exit\n"); i=0; while(i<sz&&(p[i]=getchar())!='$') { buf[i]=p[i]; i++; } k=i; prosig(); printf("\nProduction done successfully\n"); } else if(k<sz) { printf("Producer can also produce items"); while((p[k]=getchar())!='$') { buf[k]=p[k]; k++; } prosig();

printf("\nProduction done successfully\n"); } else if(k>=sz) { printf("\nBuffer is full,can't produce\n"); } } void consumer() { int c1; c1=buffer(mutex); if(c1==0) { printf("\nConsumer can consume item\n"); for(i=0;i<k;i++) cos[i]=buf[i]; printf("\nConsumed item is:\n"); for(i=0;i<k;i++) printf("\n%c",cos[i]); consig(); printf("\nSuccessfully done\n"); } else { printf("\nBuffer is empty,can't consume\n"); } } int main() { int op,sz; printf("Enter the buffer size"); scanf("%d",&sz); do { printf("\n1.Producer\t2.Consumer\t3.Exit\n"); printf("\nEnter your choice\n"); scanf("%d",&op); switch(op) { case 1: producer(sz); break; case 2: consumer(); break; case 3:

exit(0); } } while(op<=2); return 0; }

SAMPLE INPUT AND OUTPUT:


cc pcp.c ./a.out Enter the buffer size5 1.Producer 2.Consumer 3.Exit Enter your choice 1 Producer can produce the item and give $ for exit ho$ Production done successfully 1.Producer 2.Consumer 3.Exit Enter your choice 1 Producer can also produce items ney$ Production done successfully 1.Producer 2.Consumer 3.Exit Enter your choice 1 Buffer is full,can't produce 1.Producer 2.Consumer 3.Exit Enter your choice 2 Consumer can consume item Consumed item is: h o n e y Successfully done 1.Producer 2.Consumer 3.Exit Enter your choice 3

PREPARATION PERFORMANCE RECORD TOTAL

30 30 40 100

CONCLUSION:
The Producer & consumer Problem (Semaphore) is implemented using C and executed.

MEMORY MANAGEMENT BESTFIT, WORSTFIT, FIRSTFIT


Ex no:7 Date:

PROBLEM STATEMENT:
To implement first fit, best fit and worst fit using C.

ALGORITHM:
1: Get the number of free space available. 2: Get the starting address of each free space and how much space available. 3:.While getting the address and space we have to check whether there is duplication, if there is duplication then give the error message for duplication & get the address and space once again. 4: Get the space for the process. 5: If choice is first fit, then search the first memory location from the available list its size should be greater than or equal to the process size an or equal to available space in fifo order. 6: .If choice is best fit, and then sort the available list in ascending order based on the space. and search the first memory location from the available list its size should be greater than or equal to the process size an or equal to available space in FIFO order. 7: .If choice is worst fit, then sort the available list in descending order based on the space. and search the first memory location from the available list its size should be greater than or equal to the process size an or equal to available space in FIFO order. 8: If there is no sufficient space output the error message. 9: Display the process and its corresponding allocated memory space

PROGRAM:
#include<stdio.h> #include<conio.h> int ps[20],ms[20],bs[20],ws[20]; int op,p,m,j,i,k,temp,mm,mm1=0,b[20],w[20]; void firstfit(int *ps,int *ms,int p,int m) { printf("\nPROCESS\t MEMORYBLOCK\tREMAINING MEMORY\n"); for(i=0;i<p;i++) {

for(j=0;j<m;j++) { if(ps[i]<=ms[j]) { printf("\n%d(%dk)\t\t%d(%dk)\t%d",i+1,ps[i],j+1,ms[j],ms[j]-ps[i]); ms[j]=0; break; } } } } void bestfit(int *ps,int *bs,int *b,int p,int m) { printf("\nPROCESS\t MEMORYBLOCK\tREMAINING MEMORY\n"); for(i=0;i<p;i++) { for(j=0;j<m;j++) { if(ps[i]<=bs[j]) { printf("\n%d(%dk)\t\t%d(%dk)\t%d",i+1,ps[i],b[j]+1,bs[j],bs[j]-ps[i]); bs[j]=0; break; } } } } void worstfit(int *ps,int *ws,int *w,int p,int m) { printf("\nPROCESS\t MEMORYBLOCK\tREMAINING MEMORY\n"); for(i=0;i<p;i++) { for(j=0;j<m;j++) { if(ps[i]<=ws[j]) { printf("\n%d(%dk)\t\t%d(%dk)\t%d",i+1,ps[i],w[j]+1,ws[j],ws[j]-ps[i]); ws[j]=0; break; } } } } void main() { int choice,b[20],w[20];

clrscr(); for(i=0;i<20;i++) { bs[i]=0; ws[i]=0; } printf("\nENTER THE SIZE OF MEMORY\t"); scanf("%d",&mm); printf("\nENTER THE NO.OF MEMORY BLOCKS:\t"); scanf("%d",&m); for(i=0;i<m;i++) { printf("\nMEMORY BLOCK %d:\t",i+1); scanf("%d",&ms[i]); mm1=ms[i]+mm1; if(mm<mm1) { printf("\nINSUFFICIENT MEMORY"); printf("\n\nNO. OF BLOCKS :%d",i++); m=i; break; } bs[i]=ms[i]; ws[i]=bs[i]; } printf("\n\nENTER THE NO. OF PROCESS:\t"); scanf("%d",&p); printf("\nENTER THE PROCESS SIZE\n"); for(i=0;i<p;i++) { printf("\nPROCESS %d:\t",i+1); scanf("%d",&ps[i]); } for(i=0;i<m;i++) { for(j=i+1;j<m;j++) { if(bs[i]>bs[j]) { temp=bs[i]; bs[i]=bs[j]; bs[j]=temp; } } } for(i=0;i<m;i++)

{ for(j=0;j<m;j++) { if(bs[i]==ms[j]) b[i]=j; } } for(i=0;i<m;i++) { for(j=i+1;j<m;j++) { if(ws[i]<ws[j]) { temp=ws[i]; ws[i]=ws[j]; ws[j]=temp; } } } for(i=0;i<m;i++) { for(j=0;j<m;j++) { if(ws[i]==ms[j]) w[i]=j; } } do { printf("\n\nENTER UR CHOICE\t"); scanf("%d",&choice); switch(choice) { case 1: firstfit(ps,ms,p,m); break; case 2: bestfit(ps,bs,b,p,m); break; case 3: worstfit(ps,ws,w,p,m); break; case 4: exit(0); break; }

} while(choice<=3); getch(); }

SAMPLE INPUT & OUTPUT


Enter the size of memory 1200 Enter the no of memory blocks:3 Memory block 1:400 Memory block 2:300 Memory block 3:500 Enter the no of process:3 Enter the process size Process 1:200 Process 2:300 Process 3:500 Enter your choice 1 Process 1(200k) 2(300k) 3(400k) Enter your choice 2 Process 1(200k) 2(300k) 3(400k) Enter your choice 3 Process 1(200k) 2(300k) Memory block 1(400k) 2(300k) 3(500k) Remaining time 200 0 100

Memory block 2(300k) 1(400k) 3(500k) Memory block 3(500k) 1(400k)

Remaining time 100 100 100 Remaining time 300 100

CONCLUSION:

DEPARTMENT OF IT Preparation 30 Performance 30 Record 40 Total 100

Thus first fit, best fit and worst fit using C is implemented and executed.

MEMORY MANAGEMENT - PAGING


Ex No:8 Date:

PROBLEM STATEMENT:
To implement the concept of paging.

PROBLEM DESCRIPTION:
In computer operating systems that have their main memory divided into pages, paging (sometimes called swapping) is a transfer of pages between main memory and an auxiliary store, such as hard disk drive.Paging is an important part of virtual memory implemention in most contemporary general-purpose operating systems, allowing to easily use disk storage for data that do not fit into physical RAM. Paging is usually implemented as a task built into the kernel of the operating system. The Main functions of paging are performed when a program tries to access pages that do not currently reside in RAM, a situation causing page fault.

ALGORITHM:
1: Read the base address, page size, number of pages and memory unit. 2: If the memory limit is less than the base address display the memory limit is less than limit. 3: Create the page table with the number of pages and page address. 4: Read the page number and displacement value. 5: If the page number and displacement value is valid, add the displacement value with the address corresponding to the page number and display the result. 6: Display the page is not found or displacement should be less than page size.

PROGRAM:
#include<stdio.h> int i,j,k,ps,np,l,op,np1,np2,fn,f=0; char p1[50][50],p2[50][50]; int pgtb(int r,int fn,int np) { for(i=0;i<np;i++) { printf("\n\t%d\t\t%d",i,r++); if(i>np) printf("\n\t%d\t\tPage Fault",i); }return(r); } void frame(int np1,int np2,int fn,int ps,char p1[50][50],char p2[50][50]) {

for(i=0;i<np1;i++) { if(i<fn) { printf("\n--------------------\n"); printf("\nFrame No:%d\n",i); for(j=0;j<ps;j++) printf("\t%c",p1[i][j]); }} k=np1; for(i=0;i<np2;i++) { if(k<fn) { printf("\n--------------------\n"); printf("\nFrame No:%d\n",k); k++; for(j=0;j<ps;j++) printf("\t%c",p2[i][j]); }}} int main() { printf("\nENTER THE PAGE SIZE:"); scanf("%d",&ps); printf("\nENTER NO OF FRAMES:"); scanf("%d",&fn); printf("\nENTER NO OF PAGES FOR PROCESS1:"); scanf("%d",&np1); printf("\nENTER NO OF PAGES FOR PROCESS2:"); scanf("%d",&np2); if(np1+np2>fn) printf("\nPage Fault will occur\n"); printf("\nPROCESS1"); printf("\n--------------------\n"); p1[np1][ps]; p2[np2][ps]; for(i=0;i<np1;i++) { printf("Enter CHAR for PAGE%d:",i); scanf("%s",&p1[i]); } printf("\nPROCESS2"); printf("\n--------------------\n"); for(i=0;i<np2;i++) { printf("Enter CHAR for PAGE%d:",i);

scanf("%s",&p2[i]); } while(1) { printf("\n1.Page Table for PROCESS1\n2.Page Table for PROCESS2\n3.Frame allotment\n4.Free Frame List\n5.Exit\n"); printf("ENTER YOUR CHOICE:"); scanf("%d",&op); switch(op) { case 1: printf("Page Table for PROCESS1"); printf("\nPAGE INDEX\tFRAME INDEX\n"); f=pgtb(f,fn,np1); break; case 2: printf("Page Table for PROCESS2"); printf("\nPAGE INDEX\tFRAME INDEX\n"); f=pgtb(f,fn,np2); break; case 3: frame(np1,np2,fn,ps,p1,p2); break; case 4: if(np1+np2>fn) printf("Page Fault has occurred"); else if(np1+np2==fn) printf("\nNo Free Frames"); else { printf("Free Frame List"); printf("\n----------------\n"); for(i=np1+np2;i<fn;i++) printf("%dth frame",i); } break; case 5: return(0); break; } } }

SAMPLE INPUT AND OUTPUT Enter the page size:2 Enter the number of frames :9 Enter the no of pages for process1: 2 Enter the no of pages for process2: 2 Process 1 ----------Enter char for page 0:a Enter char for page1:b Process 2 ----------Enter char for page 0:e Enter char for page 1:d 1.pagetable for process 1 2.pagetable for process2 3.frame allotment 4.free framelist 5.exit Enter your choice :1 Page table for process 1 Page index 0 1 frame index 0 1

Enter your choice :1 Page table for process 1 Page index 0 1 frame index 2 3

1.Page table for process 1 2.page table for process 2 3.frame allotment 4.free framelist 5.exit Enter your choice

--------------------Frame no :0 a --------------------Frame no: 1 b --------------------Frame no:2 c ---------------------Frame no:3 d ---------------------Free framelist 4th framelist 5th frame list 6th framelist 7th framelist 8th framelist

DEPARTMENT OF IT Preparation 30 Performance 30 Record 40 Total 100

CONCLUSION:
Thus concept of paging is implemented and executed.

INTER PROCESS COMMUNICATION USING PIPE


Ex.No: 9 Date: AIM: To implement the interprocess communication using pipe in C. ALGORITHM: 1.Define the pipe function by using the array pfds. 2.The child process is created. 3.Child process writes the data into the input of the pipe. 4.Parent process reads the data from the output of the pipe. PROGRAM: #include<sys/types.h> #include<unistd.h> int main(void) { int pfds[2]; char buf[30]; pipe(pfds); if(!fork()) { printf("CHILD:Writing to the pipe\n"); write(pfds[1],"test",5); printf("CHILD":Executing\n"); exit(0); } else { printf("PARENT:Reading from pipe\n"); read(pfds[0],buf,5); printf("PARENT:read\"%s\"\n",buf); wait(NULL}; } return 0; }

SAMPLE INPUT OUTPUT: Child:writing to the pipe Child:Executing Parent:Reading from pipe Parent:read test

Performance Preparation Record Total

30 30 40 100

CONCLUSION: Thus interprocess communication using pipe is implemented in C.

CONTIGUOUS FILE ALLOCATION


Ex.No:10 Date: AIM: To implement the contiguous file allocation technique using C. ALGORITHM: 1.Get the value for no of files, disk space available, filename and size of the file along with the starting address. 2.If the starting address is greater than diskspace then display it as invalid. 3.Else contiguous function is defined. 4.In contiguous function, the starting address and alloted address is displayed PROGRAM: #include<stdio.h> char fn; int n,m,k,ds; void main() { int i=0; printf("Enter the no of files:"); scanf("%d",&k); printf("Enter the diskspace:"); scanf("%d",&ds); while(i<k) { printf("Enter the file name:"); scanf("%s",&fn); printf("Enter the size of the file:"); scanf("%d",&n); pritnf("Enter the starting address:"); scanf("%d",&m); if(m>=ds) { printf("Invalid"); } else { con(fn,n,m); } i++; } }

void con(char fn,int n,int m) { int j; printf("Starting address:%d",m); pritnf("The address alloted are:"); for(j=0;j<n;j++) { printf("%d',m+j); } }

SAMPLE INPUT AND OUTPUT: Enter the no of files:2 Enter the diskspace:50 Enter the file name:t enter the size of the file:2 Enter the starting address:51 Invalid Enter the filename:y Enter the size of the file:2 Enter the starting address:49 Starting address:49 The address alloted are: 49 50

Performance Preparation Record Total

30 30 40 100

CONCLUSION: Thus contiguous file allocation technique is implemented using C.

You might also like