Os Lab Manual Final

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 57

SYLLABUS 1.

Write programs using the following system calls of UNIX operating system: fork, exec, getpid, exit, wait, close, stat, opendir, readdir Write programs using the I/O system calls of UNIX operating system (open, read, write, etc) Write C programs to simulate UNIX commands like ls, grep, etc. Given the list of processes, their CPU burst times and arrival times, display/print the Gantt chart for FCFS and SJF. For each of the scheduling policies, compute and print the average waiting time and average turnaround time Given the list of processes, their CPU burst times and arrival times, display/print the Gantt chart for Priority and Round robin. For each of the scheduling policies, compute and print the average waiting time and average turnaround time Developing Application using Inter Process communication (Using Shared memory, pipes or message queues). Implement the Producer Consumer problem using semaphores. Implement some memory management schemes I for eg Free space is maintained as a linked list of nodes with each node having the starting byte address and the ending byte address of a free block. Each memory request consists of the process-id and the amount of storage space required in bytes. Allocated memory space is again maintained as a linked list of nodes with each node having the process-id, starting byte address and the ending byte address of the allocated space. Implement some memory management schemes II for eg When a process finishes (taken as input) the appropriate node from the allocated list should be deleted and this free disk space should be added to the free space list. [Care should be taken to merge contiguous free blocks into one single block. This results in deleting more than one node from the free space list and changing the start and end address in the appropriate node]. For allocation use first fit, worst fit and best fit.

2.

3. 4.

5.

6.

7. 8.

9.

10.

Implement any file allocation technique (Linked, Indexed or Contiguous)

OPERATING SYSTEM LAB CONTENTS Ex. No 1 2 3 4 5 6 7 TITLE OF EXERCISE BASIC LINUX COMMANDS SHELL PROGRAMMING-I Page No

SHELL PROGRAMMING-II UNIX SYSTEM CALLS UNIX I/O SYSTEM CALLS SIMULATION OF UNIX COMMANDS CPU SCHEDULING ALGORITHMS -FCFS CPU SCHEDULING ALGORITHMS SJF CPU SCHEDULING ALGORITHMS Priority Scheduling.

8 9 10 11 12 13 14 15 16

CPU SCHEDULING ALGORITHMS - Round Robin Scheduling INTER PROCESS COMMUNICATION PRODUCER CONSUMER PROBLEM USING SEMAPHORES MEMORY MANAGEMENT SCHEMES - I Implemention Of Memory Allocation Using Paged Memory MEMORY MANAGEMENT SCHEMES - II Implementation Of Best And First Fit IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHMSFIFO & LRU FILE ALLOCATION TECHNIQUE

EX. NO. : 01 DATE :

BASIC COMMANDS IN Linux

AIM: To study the basic commands in Linux.

COMMANDS:
1. TASK : To display the system date and time. COMMAND : date. SYNTAX : date. EXPLANATION: This command displays the current system date and time on the screen. OUTPUT : Tue Jun 19 11:37:17 GMT 2007. 2. TASK : To display the current month. COMMAND : date. SYNTAX : date +%m. EXPLANATION: This command displays the current month on the screen. OUTPUT : 06. 3. TASK : To display the name of the current month. COMMAND : date. SYNTAX : date +%h. EXPLANATION: This command displays the name of the current month on the screen. OUTPUT : Jun. 4. TASK : To display the current system date. COMMAND : date. SYNTAX : date +%d. EXPLANATION: This command displays the current system date on the screen. OUTPUT : 19. 5. TASK : To display the current system date (year). COMMAND : date. SYNTAX : date +%y. EXPLANATION: This command displays the current year on the screen. OUTPUT : 07. 6. TASK : To display the current system time. COMMAND : date. SYNTAX : date +%H. EXPLANATION: This command displays the current system time (in hours) on the screen. OUTPUT : 11.

7. TASK : To display the current system time. COMMAND : date. SYNTAX : date +%M. EXPLANATION: This command displays the current system time (in minutes) on the screen. OUTPUT : 43. 8. TASK : To display the current system time. COMMAND : date. SYNTAX : date +%S. EXPLANATION: This command displays the current system time (in seconds) on the screen. OUTPUT : 15. 9. TASK : To display the calendar of the current month. COMMAND : calendar. SYNTAX : cal. EXPLANATION: This command displays the calendar of the current month on the screen. OUTPUT : Jun 07 S M T W T F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 10. TASK : To display user-defined message. COMMAND : echo. SYNTAX : echo message. EXPLANATION: This command displays on the screen the argument of the echo command. OUTPUT : echo OS. OS 11. TASK : To display the details of all users. COMMAND : who. SYNTAX : who. EXPLANATION : This command lists the information about all the users who have logged on to that system. OUTPUT 12. TASK : To display the user detail. COMMAND : who. SYNTAX : who am i. EXPLANATION : This command displays information about the current user of the

OUTPUT

system on the screen. : root.

13. TASK : To create a directory. COMMAND : make directory. SYNTAX : mkdir. EXPLANATION : This command is used to create a new directory with the specified name. EXAMPLE : mkdir student. OUTPUT : The directory student is created. 14. TASK : To change directory. COMMAND : change directory. SYNTAX : cd directory name. EXPLANATION : This command is used to switch from one directory to another. EXAMPLE : cd staff. OUTPUT : The directory staff is switched onto. 15. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT : To delete a directory. : remove directory. : rmdir directory name : This command is used to delete the specified directory. : rmdir student. : The student directory is deleted.

16. TASK : To come out of a sub-directory. COMMAND : change directory. SYNTAX : cd .. EXPLANATION : This command helps in switching to the main directory. OUTPUT : 17. TASK : To list all the files and directories. COMMAND : list. SYNTAX : ls. EXPLANATION : This command displays all the files and directories of the system. OUTPUT : 18. TASK : To create a file. COMMAND : cat. SYNTAX : cat> file name. EXPLANATION : This command leads to the creation of a new file with the specified file name and contents. EXAMPLE : cat> wind. OUTPUT : A null file called wind is created.

19. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT 20. TASK COMMAND SYNTAX EXPLANATION

: To view a file. : cat. : cat file name. : This command displays the contents of the specified file. : cat wind. : Contents of the file called wind will be displayed on the screen. : To copy a file. : copy. : cp sourcefile destinationfile. : This command produces a copy of the source file and is stored in the specified destination file by overwriting its previous contents. : cp sun moon. : The contents of sun file will be copied to the moon file. : To move a file. : move. : mv sourcefile destinationfile. : After moving the contents of the source file into destination file, the source file is deleted. : mv sun moon. : After copying contents from the sun file to moon file, the sun file is deleted.

EXAMPLE OUTPUT 21. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT

22. TASK : To display / cut a column from a file. COMMAND : cut. SYNTAX : cut c no. file name. EXPLANATION : This command displays the characters of a particular column in the specified file. EXAMPLE : cut c3 moon. OUTPUT : Those characters occurring in the 3rd column of the file called moon are displayed. 23. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT 24. TASK COMMAND SYNTAX EXPLANATION : To delete a file. : remove. : rm file name. : This command deletes the specified file from the directory. : rm sun. : The file called sun will be deleted. : To retrieve a part of a file. : head. : head no. of rows file name. : This command displays the specified no. of rows form the top of the specified file.

EXAMPLE OUTPUT 25. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT 26. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT

: head -1 sun. : The first row of the file called sun is displayed. : To retrieve a file. : tail. : tail no. of rows file name. : This command displays the specified no. of rows form the bottom of the specified file. : tail -1 moon. : The last row of the file called moon is displayed. : To sort the contents of a file. : sort. : sort file name. : This command helps in sorting the contents of a file in ascending order. : sort win. : The contents of the file win are displayed on the screen in a sorted order. : To display the no. of characters in a file. : word count. : wc file name. : This command displays on the screen the no. of rows, words, and the sum of no. of characters and words. : wc ball. : The no. of rows, words, and no. of characters present in the file ball are displayed. : To display the calendar of a year. : cal. : cal year. : This command displays on the screen the calendar of the specified year. : cal 2007. : The calendar of the year 2007 will be displayed.

27. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT

28. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT

Result:

EX. NO. : 02 DATE :

SHELL PROGRAMMING

AIM:

PROGRAM:
echo Enter the number: read number i= 1 fact= 1 while [$i le $number] do fact= `expr $fact \* $i ` i= `expr $i +1` done echo The factorial of $number is $fact.

SAMPLE OUTPUT:
Enter the number: 3 The factorial of 3 is 6.

PROGRAM:
echo Enter two numbers: read a read b c=`expr $a + $b ` echo The sum is $c.

SAMPLE OUTPUT:
Enter two numbers: 3 6 The sum is 9.

PROGRAM:
echo Enter the number: read n x= $n sum= 0 while [ $n gt 0] do y= `expr $n % 10` z= `expr $y \* $y \* $y` sum= `expr $sum + $z` n= `expr $n / 10` done if [ $x eq $sum] then echo $x is an Armstrong number. else echo $x is not an Armstrong number. fi

SAMPLE OUTPUT 1:
Enter a number: 153 153 is an Armstrong number.

SAMPLE OUTPUT 2:
Enter a number: 33 33 is not an Armstrong number.

PROGRAM:
echo Enter the first number: read a echo Enter the second number: read b echo Enter the third number: read c if [$a gt $b a $a gt $c] then echo $a is greater. elif [$b gt $c] then echo $b is greater. else echo $c is greater. fi

SAMPLE OUTPUT:
Enter the first number: 3 Enter the second number: 6 Enter the third number: 9 9 is greater.

PROGRAM:
echo Enter any two numbers: read a read b echo The numbers are $a and $b. c= $a a= $b b= $c echo The swapped numbers are $a and $b.

SAMPLE OUTPUT:
Enter any two numbers: 3 9 The numbers are 3 and 9. The swapped numbers are 9 and 3.

PROGRAM:
a= -1 b= 1 c= 0 echo Enter the limit: read n echo FIBONACCI SERIES for (i =1; i <=n; i++) do c= `expr $a + $b` echo $c a=$b b=$c done

SAMPLE OUTPUT:
Enter the limit: 5 FIBONACCI SERIES 0 1 1 2 3

PROGRAM:
echo enter the day of the week (1-7) read n case $n in 1)echo 1 is Sunday;; 2)echo 2 is Monday;; 3)echo 3.is Tuesday;; 4)echo 2 is Wednesday;; 5)echo 3.is thursday;; 6)echo 2 is Friday;; 7)echo 3.is Saturday;; *)echo Invalid option;;

SAMPLE OUTPUT 1:
Enter the day of the week(1-7) 1 Sunday

SAMPLE OUTPUT 2:
Enter the day of the week(1-7) 9 Invalid option

PROGRAM:
echo enter string 1 read s1 echo enter string2 read s2 if [ $s1 == $s2] then echo the given string are equal else echo The given strings are not equal fi

SAMPLE OUTPUT 1:
enter string 1 ram enter string 2 sam The given strings are not equal

SAMPLE OUTPUT 2
enter string 1 sam enter string 2 sam The given strings are qual

RESULT:

EX NO: 03 DATE:
Aim:

IMPLEMENTATION OF UNIX SYSTEM CALLS

To write a program using the fork, exec, getpid, exit, wait, close, stat, opendir, readdir system calls of UNIX Operating System. Algorithm: 1. 2. 3. 4. Start the program. Initialize the process id number . Create a process using fork() system call. if process id<0 print error. If process id==0 means then print child process id number and Parent process id number 5. Else wait until the process gets completed.

PROGRAM: Fork() System Call: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { printf("Hello \n"); fork(); printf("bye\n"); return 0; } OUTPUT:

Fork() & Getpid() System Call: #include <stdio.h> #include <string.h> #include <sys/types.h> Void main() { Pid_t pid; fork(); pid=getpid(); if(pid == -1) printf(\n Error in creating process ); else if(pid == 0) printf("\nExecuting in child process, pid=%d and its parent pid = %d ", getpid(),getppid()); else printf("\nExecuting in parent process,pid=%d \n",getppid()); }

OUTPUT:

Exec() System Call: #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { execvp(argv[1], &argv[1]); perror("exec failure"); exit(1); }

OUTPUT:

Wait() and Exit() system call: #include <sys/types.h> #include <sys/wait.h> void main() { int status; pid_t pid; pid = fork(); if(pid == -1) printf(\nERROR child not created ); else if (pid == 0) /* child process */ { printf("\n I'm the child!"); exit(0); } else /* parent process */ { wait(&status); printf("\n I'm the parent!") printf("\n Child returned: %d\n", status) } } OUTPUT:

exit system call #include<stdio.h> main() { int pid; pid=fork(); printf("%d\n",pid); if(pid<0) { perror("Child can't be executed\n"); exit(-1); } else { printf("Child created\n"); exit(0); } }
OUTPUT

RESULT:

EX NO: 04 DATE:
Aim :

IMPLEMENTATION OF I/O SYSTEM CALLS

To write a program using the I/O System calls of UNIX operating system. (open, read, write, etc) Algorithm : 1. 2. 3. 4. 5. 6. Start the program. Create a structure variable and assign a pointer to a variable. If arguments for the directory is zero then the directory open. If the arguments for directory not equal to 0 then display the files in directory Choose the directory. Exit.

PROGRAM: 1. Write a program to open, read and write files using system calls. #include<stdio.h> #include<fcntl.h> main() { char buff[100],fn[10]; int fd,n; printf("Enter file name\n"); scanf("%s",fn); fd=open(fn,O_RDONLY); n=read(fd,buff,100); n=write(1,buff,n); close(fd); } OUTPUT:

2. Using system calls write line of texts in a file #include<stdio.h> #include<fcntl.h> #include<string.h> main() { char *buff,fn[10]; int fd,n,i; printf("\nEnter file name "); scanf("%s",fn); printf("\nEnter text "); scanf("%s",buff); //fgets(buff,100,stdin); fd=open(fn,O_CREAT|O_WRONLY); n=write(fd,buff,strlen(buff)); close(fd); } OUTPUT:

3. Write a program to open, read and write files and perform file copy operation. #include<stdio.h> #include<fcntl.h> main() { char buff[1000],fn1[10],fn2[10]; int fd1,fd2,n; printf("Enter source filename\n"); scanf("%s",fn1); printf("Enter destination file name\n"); scanf("%s",fn2); fd1=open(fn1,O_RDONLY); n=read(fd1,buff,1000); fd2=open(fn2,O_CREAT|O_WRONLY); n=write(fd2,buff,n); close(fd1);

close(fd2); }

OUTPUT:

4. Write a program to remove a directory. #include<stdio.h> #include<fcntl.h> main() { char fn[10]; printf("Enter source filename\n"); scanf("%s",fn); if(remove(fn)==0) printf("File removed\n"); else printf("File cannot be removed\n"); }

OUTPUT:

RESULT:

EX NO: 05 DATE:
Aim

IMPLEMENTATION OF UNIX COMMANDS LIKE LS, GREP

To write a program to implement unix commands like ls, grep. Algorithm Simulation Of LS Command 1. Start the process. 2. Create a structure variable and assign a pointer to the variable. 3. If argument for the directory is 0 then the directory open. 4. If argument for directory not equal to 0 then display the files in the memory 5. Close the directory. 6. Stop the process. Simulation Of GREP Command Start the process. Create a file pointer and pointer to a string. Open a file in read mode and the pointer points the position in the buffer. If string is #NULL then printf statement will get execute. Particular pattern of string will be displayed without using grep command. Stop the process.

1. 2. 3. 4. 5. 6.

PROGRAM: Simulation of Ls Command:


a program for the simulation of ls command. #include<stdio.h> #include<dirent.h> main() { char dirname[10]; DIR*p; struct dirent *d; printf("Enter directory name\n"); scanf("%s",dirname); p=opendir(dirname); if(p==NULL) { perror("Cannot find directory"); exit(-1); }

while(d=readdir(p)) printf("%s\n",d->d_name); }

OUTPUT:

Simulation of grep command:


#include<stdio.h> #include<string.h> main() { char fn[10],pat[10],temp[200]; FILE *fp; printf("Enter file name\n"); scanf("%s",fn); printf("Enter pattern to be searched\n"); scanf("%s",pat); fp=fopen(fn,"r"); while(!feof(fp)) { fgets(temp,1000,fp); if(strstr(temp,pat)) printf("%s",temp); } fclose(fp); } OUTPUT:

RESULT:

EX NO: 06 Date:
AIM:

IMPLEMENTATION OF FIRST COME FIRST SERVE SCHEDULING ALGORITHM

To write a C-program to implement the FCFS scheduling algorithm ALGORITHM: 1. 2. 3. 4. 5. 6. 7. 8. Start the process Declare the array size Get the number of processes to be inserted Get the value Start with the first process from its initial position let other process to be in queue Calculate the total number of burst time Display the values Stop the process

PROGRAM : #include<stdio.h> main() { int n,a[10],b[10],t[10],w[10],g[10],i,m; float att=0,awt=0; for(i=0;i<10;i++) { a[i]=0; b[i]=0; w[i]=0; g[i]=0; } printf("enter the number of process"); scanf("%d",&n); printf("enter the burst times"); for(i=0;i<n;i++) scanf("%d",&b[i]); printf("\nenter the arrival times");

for(i=0;i<n;i++) scanf("%d",&a[i]); g[0]=0; for(i=0;i<10;i++) g[i+1]=g[i]+b[i]; for(i=0;i<n;i++) { w[i]=g[i]-a[i]; t[i]=g[i+1]-a[i]; awt=awt+w[i]; att=att+t[i]; } awt =awt/n; att=att/n; printf("\n\tprocess\twaiting time\tturn arround time\n"); for(i=0;i<n;i++) { printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]); } printf("the average waiting time is %f\n",awt); printf("the average turn around time is %f\n",att); }

OUTPUT :

Enter the number of process 4 Enter the burst times 4 9 8 3 Enter the arrival times 0 2 4 3 process p0 p1 p2 p3 waiting time 0 2 9 18 turn arround time 4 11 17 21

the average waiting time is 7.250000 the average turnaround time is 13.250000

RESULT :

EX NO: 07 DATE: IMPLEMENTATION OF SHORTEST JOB FIRST SCHEDULING ALGORITHM


AIM : To write a C-Program to implement the shortest job first scheduling algorithm ALGORITHM : 1. 2. 3. 4. 5. 6. 7. Start the process Declare the array size Get the number of elements to be inserted Select the process which have shortest burst will execute first If two process have same burst length then FCFS scheduling algorithm used Make the average waiting the length of next process Start with the first process from its selection as above and let other process to be in queue 8. Calculate the total number of burst time 9. Display the values 10. Stop the process PROGRAM : #include<stdio.h> int main() { int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i; float att=0,awt=0; for(i=0;i<10;i++) { b[i]=0;w[i]=0; } printf("enter the number of process"); scanf("%d",&n); printf("enter the burst times"); for(i=0;i<n;i++) { scanf("%d",&b[i]); p[i]=i; }

for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(b[i]>b[j]) { temp=b[i]; temp1=p[i]; b[i]=b[j]; p[i]=p[j]; b[j]=temp; p[j]=temp1; } } } w[0]=0; for(i=0;i<n;i++) w[i+1]=w[i]+b[i]; for(i=0;i<n;i++) { t[i]=w[i]+b[i]; awt=awt+w[i]; att=att+t[i]; } awt=awt/n; att=att/n; printf("\n\t process \t waiting time \t turn around time \n"); for(i=0;i<n;i++) printf("\t p[%d] \t %d \t\t %d \n",p[i],w[i],t[i]); printf("the average waitingtimeis %f\n",awt); printf("the average turn around time is %f\n",att);

return 1; }

OUTPUT: Enter the number of process 5 Enter the burst times 2 4 5 6 8

process p[0] p[1] p[2] p[3] p[4]

waiting time 0 2 6 11 17

turn around time 2 6 11 17 25

the average waitingtime is 7.200000 the average turn around time is 12.200000

RESULT :

EX NO: 08 DATE:

IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM

Aim To write a C-program to implement the Priority scheduling algorithm Algorithm 1. 2. 3. 4. 5. 6. 7. Start the process Declare the array size Get the number of elements to be inserted Select the process which have smallest priority will execute first If two process have same priority then FCFS scheduling algorithm used Make the average waiting the length of next process Start with the shortest priority first from its selection as above and let other process to be in queue 8. Calculate the total number of burst time 9. Display the values 10. Stop the process

PROGRAM: #include<stdio.h> main() { float avgwt,avgtt; char pname[10][10],c[10][10]; int wt[10],tt[10],bt[10],pt[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=10; printf("\n\n Enter the number of processes : "); scanf("%d",&n); printf("\n\n Enter the NAME and BURSTTIME "); for(i=0;i<n;i++) { printf("\n\n NAME : "); scanf("%s",&pname[i]); printf("\n\n BURSTTIME : "); scanf("%d",&bt[i]); }

printf("\n\n Enter the priorities of the processes "); for(i=0;i<n;i++) { printf("\n\n Priority of process%d : ",i+1); scanf("%d",&pt[i]); } for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(pt[i]>pt[j]) { t=pt[i]; pt[i]=pt[j]; pt[j]=t; q=bt[i]; bt[i]=bt[j]; bt[j]=q; strcpy(c[i],pname[i]); strcpy(pname[i],pname[j]); strcpy(pname[j],c[i]); } } wt[0]=0; for(i=0;i<n;i++) { wt[i+1]=wt[i]+bt[i]; sum=sum+wt[i]; sbt=sbt+wt[i+1]; tt[i]=wt[i]+bt[i]; ss=ss+bt[i]; } printf("\n\n GANTT CHART"); printf("\n-----------------------------------------------------------------\n"); for(i=0;i<n;i++) { printf("|\t%s\t",pname[i]); } printf("\n-----------------------------------------------------------------\n"); for(i=0;i<n;i++) {

printf("%d\t\t",wt[i]); } printf("%d\n",ss); printf("\n-----------------------------------------------------------------\n"); printf("\n\n Total WAITING TIME of the process = %d",sum); printf("\n\n Total TURNAROUND TIME of the process = %d",sbt); avgwt=(float)sum/n; avgtt=(float)sbt/n; printf("\n\n Average WAITING TIME of the process = %f",avgwt); printf("\n\n Average TURNAROUND TIME of the process = %f",avgtt); }

OUTPUT:

RESULT

EX NO: 09 DATE:
Aim

IMPLEMENTATION OF ROUND ROBIN SCHEDULING ALGORITHM

To write a C-program to implement the round robin scheduling algorithm Algorithm 1. 2. 3. 4. 5. 6. 7. 8. 9. Start the process Declare the array size Get the number of elements to be inserted & value Set the time sharing system with preemption Define quantum is defined from 10 to 100ms Declare the queue as a circular Make the CPU scheduler goes around the ready queue allocating CPU to each process for the time interval specified Make the CPU scheduler picks the first process and sets time to interrupt after quantum expired dispatches the process 10. If the process have burst less than the time quantum than the process release the CPU 11. If the process have bust greater then time quantum then time will go off and cause interrupt to OS and the process put into the tail of ready queue and the schedule select next process 12. Display the result & Stop the process

PROGRAM: #include<stdio.h> int ttime,i,j,temp; main() { int pname[10],btime[10],pname2[10],btime2[10]; int n,x,z; printf("Enter the no. of process:"); scanf("%d",&n); printf("Enter the process name and burst time for the process\n"); for(i=0;i<n;i++) { printf("Enter the process name:"); scanf("%d",&pname2[i]); printf("Enter burst time for the process %d:",pname2[i]);

scanf("%d",&btime2[i]); } printf("PROCESS NAME \t\t BURST TIME\n"); for(i=0;i<n;i++) printf("%d\t\t\t %d\n",pname2[i],btime2[i]); z=1; while(z==1) { ttime=0; for(i=0;i<n;i++) { pname[i]=pname2[i]; btime[i]=btime2[i]; } printf ("PRESS 1.ROUND ROBIN 2.EXIT\n"); scanf("%d",&x); switch(x) { case 1: rrobin(pname,btime,n); break; case 2: exit(0); break; default: printf("Invalid option"); break; } printf("\n\n If you want to continue press 1:"); scanf("%d",&z); } } rrobin(int pname[],int btime[],int n) { int tslice; j=0; printf("\n\t ROUND ROBIN SCHEDULING \n\n"); printf("Enter the time slice:\n"); scanf("%d",&tslice); printf("PROCESS NAME \t REMAINING TIME\t TOTAL TIME");

while(j<n) { for(i=0;i<n;i++) { if(btime[i]>0) { if(btime[i]>=tslice) { ttime+=tslice; btime[i]=btime[i]-tslice; printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime); if(btime[i]==0) j++; } else { ttime+=btime[i]; btime[i]=0; printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime); } } } }

OUTPUT :

Enter the no. of process: 4 Enter the process name and burst time for the process Enteer the process name: 1 Enter burst time for the process 1: 8 Enteer the process name: 2 Enter burst time for the process 2: 3 Enteer the process name: 3 Enter burst time for the process 3: 6 Enteer the process name: 4 Enter burst time for the process 4: 1

PROCESS NAME 1 2 3 4

BURST TIME 8 3 6 1

PRESS 1.ROUND ROBIN 2.EXIT 1 ROUND ROBIN SCHEDULING Enter the time slice: 2

PROCESS NAME 1 2 3 4 1 2 3 1 3

REMAINING TIME TOTAL TIME 6 1 4 0 4 0 2 2 0 2 4 6 7 9 10 12 14 16

RESULT

EX NO: 10 DATE: AIM:

PRODUCER CONSUMER PROBLEM

To write a c-program to implement the producer consumer problem using semaphores.

Algorithm:

1. Start the program. 2. Declare the required variables. 3. Initialize the buffer size and get maximum item you want to produce. 4. Get the option, which you want to do either producer, consumer or exit from the operation. 5. If you select the producer, check the buffer size if it is full the producer should not produce the item or otherwise produce the item and increase the value buffer size. 6. If you select the consumer, check the buffer size if it is empty the consumer should not consume the item or otherwise consume the item and decrease the value of buffer size. 7. If you select exit come out of the program. 8. Stop the program.

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("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n"); while(1) { printf("\nENTER YOUR CHOICE\n"); scanf("%d",&n); switch(n) {

case 1: if((mutex==1)&&(empty!=0)) producer(); else printf("BUFFER IS FULL"); break; case 2: if((mutex==1)&&(full!=0)) consumer(); else printf("BUFFER 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 item%d",x); x--; mutex=signal(mutex); }

OUTPUT:1.PRODUCER 2.CONSUMER 3.EXIT ENTER YOUR CHOICE: 1 producer produces the item1 ENTER YOUR CHOICE :1 producer produces the item2 ENTER YOUR CHOICE:1 producer produces the item3 ENTER YOUR CHOICE : 1 BUFFER IS FULL ENTER YOUR CHOICE : 2 consumer consumes item3 ENTER YOUR CHOICE : 2 consumer consumes item2 ENTER YOUR CHOICE : 2 consumer consumes item1 ENTER YOUR CHOICE: 2 BUFFER IS EMPTY ENTER YOUR CHOICE : 3

RESULT:

EX NO: 11 DATE: DEVELOPING APPLICATION USING INTER PROCESS COMMUNICATION

PROGRAM: #include<stdio.h> #include<unistd.h> #include<sys/ipc.h> #include<sys/uio.h> #include<sys/types.h> main() { int pid,pfd[2],n,a,b,c; if(pipe(pfd)==-1) { printf("\nError in pipe connection\n"); exit(1); } pid=fork(); if(pid>0) { printf("\nParent Process");\ printf("\n\n\tFibonacci Series"); printf("\nEnter the limit for the series:"); scanf("%d",&n); close(pfd[0]); write(pfd[1],&n,sizeof(n)); close(pfd[1]); exit(0); } else {

close(pfd[1]); read(pfd[0],&n,sizeof(n)); printf("\nChild Process"); a=0; b=1; close(pfd[0]); printf("\nFibonacci Series is:"); printf("\n\n%d\n%d",a,b); while(n>2) { c=a+b; printf("\n%d",c); a=b; b=c; n--; } } }

RESULT:

EX NO: 12 DATE: IMPLEMENTION OF MEMORY ALLOCATION USING PAGED MEMORY

AIM: To write a C-Program to management scheme. ALGORITHMS: 1. 2. 3. 4. 5. Start the Program. Get the Total memory size and Page size. Memory size is divided by the Page size,the memory is divided into pages. The condition is checked to know the end of page. If the condition is true then get the program size. 5a. If the program size exceeds the page size then display Memory is not Sufficient, else print the Pages allocated to the program. 6. Stop the program. implement memory allocation using paged memory

PROGRAM: #include<stdio.h> struct ptable { int pno, staddr; }p[50]; struct pcount { int pnum; char data[50]; }pg[50][50]; main() { int n,psize, i,j,k,st,pagenum,disp,phyaddr,laddr,found=0;

printf("\n MEMORY IFORMATION\n"); printf("***************\n"); printf("\n Enter the number of pages:\n"); scanf("%d",&n); printf("Enter the page size:\n"); scanf("%d", &psize); for(i=1;i<=n;i++) { printf("\n Enter the starting addresss for page[%d]:\n",i); scanf("%d",&st); for(j=1;j<=psize;j++) { printf("page[%d]\tData:",st); scanf("%s", pg[i][j].data); pg[i][j].pnum=st; st=st+1; } } printf("\n MEMORY \n"); printf("**********\n"); for(i=1;i<=n;i++) { for(j=1;j<=psize;j++) { printf("\n page[%d]\t Data:%s\n", pg[i][j].pnum, pg[i][j].data); } } printf("\n page table details\n"); printf("***********************"); printf("\n Enter the starting address for the pages:",i); for(k=1;k<=n;k++)

{ printf("page [%d] \t starting address:\n",k); p[k].pno=k; scanf("%d", &p[k].staddr); } printf("\n pageno\t starting address\n"); for(k=1;k<=n;k++) { printf("\n %d\t\t%d\n", p[k].pno, p[k].staddr); } printf("\nEnter the logical addressL:\n"); scanf("%d", &laddr); pagenum=laddr/100; disp=laddr%100; printf("\n pageno%d\tdisp%d\n", pagenum, disp); for(k=1;k<=n;k++) if(p[k].pno==pagenum) { phyaddr=p[k].staddr+disp; found=1; break; } if(found==1) { printf("\n physical address%d\n", phyaddr); for(j=1;j<=psize;j++) { if(pg[pagenum][j].pnum==phyaddr) printf("\nData%s\n",pg[pagenum][j].data); } }

else printf("\n not found\n"); }

OUTPUT:-

MEMORY IFNORMATION *************************

Enter the number of pages: 2 Enter the page size: 2

Enter the starting addresss for page[1]: 100 page[100] page[101] Data:blue Data:green

Enter the starting addresss for page[2]: 200 page[200] page[201] Data:red Data:yellow

MEMORY ********** page[100] page[101] page[200] page[201] Data:blue Data:green Data:red Data:yellow

page table details *********************** Enter the starting address for the pages:page [1] starting address: 100

page [2]

starting address: 200

pageno starting address 1 2 100 200

Enter the logical addressL: 0101 pageno1 disp1

physical address101 Datagreen

RESULT:

EX NO: 13 DATE:

IMPLEMENTATION OF FIFO PAGE REPLACEMENT ALGORITHM

Aim To write a C-program to implement FIFO page replacement algorithm

Algorithm 1. Start the process 2. Declare the size with respect to page length 3. Check the need of replacement from the page to memory 4. Check the need of replacement from old page to new page in memory 5. Forma queue to hold all pages 6. Insert the page require memory into the queue 7. Check for bad replacement and page fault 8. Get the number of processes to be inserted 9. Display the values 10. Stop the process PROGRAM: #include<stdio.h> int main() { int i,j,n,a[50],frame[10],no,k,avail,count=0; printf("\n ENTER THE NUMBER OF PAGES:\n"); scanf("%d",&n); printf("\n ENTER THE PAGE NUMBER :\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\n ENTER THE NUMBER OF FRAMES :"); scanf("%d",&no); for(i=0;i<no;i++) frame[i]= -1; j=0; printf("\tref string\t page frames\n"); for(i=1;i<=n;i++) { printf("%d\t\t",a[i]); avail=0; for(k=0;k<no;k++)

if(frame[k]==a[i]) avail=1; if (avail==0) { frame[j]=a[i]; j=(j+1)%no; count++; for(k=0;k<no;k++) printf("%d\t",frame[k]); } printf("\n"); } printf("Page Fault Is %d",count); return 0; }

OUTPUT :

ENTER THE NUMBER OF PAGES: 20 ENTER THE PAGE NUMBER : 70120304230321201701

ENTER THE NUMBER OF FRAMES :3 ref string 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 7 7 7 1 0 0 2 2 1 0 0 1 1 3 2 2 2 4 4 4 0 3 3 3 2 2 2 1 0 0 0 3 3 page frames 7 7 7 2 -1 0 0 0 -1 -1 1 1

Page Fault Is 15

RESULT:

EX NO: 14 DATE: IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM

Aim To write a C-program a program to implement LRU page replacement algorithm Algorithm 1. 2. 3. 4. 5. 6. 7. 8. 9. Start the process Declare the size Get the number of pages to be inserted Get the value Declare counter and stack Select the least recently used page by counter value Stack them according the selection. Display the values Stop the process

PROGRAM : #include<stdio.h> main() { int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20]; printf("Enter no of pages:"); scanf("%d",&n); printf("Enter the reference string:"); for(i=0;i<n;i++) scanf("%d",&p[i]); printf("Enter no of frames:"); scanf("%d",&f); q[k]=p[k]; printf("\n\t%d\n",q[k]); c++; k++; for(i=1;i<n;i++) { c1=0; for(j=0;j<f;j++) { if(p[i]!=q[j]) c1++; }

if(c1==f) { c++; if(k<f) { q[k]=p[i]; k++; for(j=0;j<k;j++) printf("\t%d",q[j]); printf("\n"); } else { for(r=0;r<f;r++) { c2[r]=0; for(j=i-1;j<n;j--) { if(q[r]!=p[j]) c2[r]++; else break; } } for(r=0;r<f;r++) b[r]=c2[r]; for(r=0;r<f;r++) { for(j=r;j<f;j++) { if(b[r]<b[j]) { t=b[r]; b[r]=b[j]; b[j]=t; } } } for(r=0;r<f;r++) { if(c2[r]==b[0]) q[r]=p[i]; printf("\t%d",q[r]); } printf("\n"); }

} } printf("\nThe no of page faults is %d",c); } OUTPUT : Enter no of pages:10 Enter the reference string:7 5 9 4 3 7 9 6 2 1 Enter no of frames:3 7 7 7 4 4 4 9 9 9 1 5 5 5 3 3 3 6 6 6 9 9 9 7 7 7 2 2

The no of page faults is 10

RESULT :

EX NO: 15 DATE:
Aim

IMPLEMENTATION OF BEST FIT ALGORITHM

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

Algorithm 1. Start the process 2. Declare the size 3. Get the number of processes to be inserted 4. Allocate the best hole that is small enough searching 5. Start at the best of the set of holes 6. If not start at the hole that is sharing the pervious best fit search end 7. Compare the hole 8. If small enough then stop searching in the procedure 9. Display the values 10. Stop the process PROGRAM : #include<stdio.h> #include<process.h> void main() { int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20]; printf("Enter no of Blocks.\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the %dst Block size:",i); scanf("%d",&a[i]); b[i]=i; } printf("Enter no of Process.\n"); scanf("%d",&m); for(i=0;i<m;i++) { printf("Enter the size of %dst Process:",i); scanf("%d",&p[i]); c[i]=i; } for(i=0;i<n;i++)

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

OUTPUT:

Enter no of Blocks. 3 Enter the 0st Block size:300 Enter the 1st Block size:200 Enter the 2st Block size:100 Enter no of Process. 3 Enter the size of 0st Process:100 Enter the size of 1st Process:300 Enter the size of 2st Process:200

The Process 0 allocated to Block 2 The Process 2 allocated to Block 1 The Process 1 allocated to Block 0

RESULT

EX NO: 16 DATE:

IMPLEMENTATION OF FIRST FIT ALGORITHM

Aim To write a C-program to implement best fit algorithm for memory management.

Algorithm 1. Start the process 2. Declare the size 3. Get the number of processes to be inserted 4. Allocate the first hole that is big enough searching 5. Start at the beginning of the set of holes 6. If not start at the hole that is sharing the pervious first fit search end 7. Compare the hole 8. if large enough then stop searching in the procedure 9. Display the values 10. Stop the process PROGRAM: #include<stdio.h> #include<process.h> void main() { int a[20],p[20],i,j,n,m; printf("Enter no of Blocks.\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the %dst Block size:",i); scanf("%d",&a[i]); } printf("Enter no of Process.\n"); scanf("%d",&m); for(i=0;i<m;i++) { printf("Enter the size of %dst Process:",i); scanf("%d",&p[i]); } for(i=0;i<n;i++)

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

OUTPUT : Enter no of Blocks : 5 Enter the 0st Block size:500 Enter the 1st Block size:400 Enter the 2st Block size:300 Enter the 3st Block size:200 Enter the 4st Block size:100

Enter no of Process.: 5 Enter the size of 0st Process:100 Enter the size of 1st Process:350 Enter the size of 2st Process:400 Enter the size of 3st Process:150 Enter the size of 4st Process:200

The Process 0 allocated to 500 The Process 1 allocated to 400 The Process 3 allocated to 200 The Process 2 is not allocated The Process 4 is not allocated

RESULT :

You might also like