Os Lab Manual Final

71
SYLLABUS 1. Write programs using the following system calls of UNIX operating system: fork, exec, getpid, exit, wait, close, stat, opendir, readdir 2. Write programs using the I/O system calls of UNIX operating system (open, read, write, etc) 3. Write C programs to simulate UNIX commands like ls, grep, etc. 4. 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 5. 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 6. Developing Application using Inter Process communication (Using Shared memory, pipes or message queues). 7. Implement the Producer – Consumer problem using semaphores. 8. 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

Transcript of Os Lab Manual Final

Page 1: Os Lab Manual Final

SYLLABUS

1. Write programs using the following system calls of UNIX operating system: fork, exec, getpid, exit, wait, close, stat, opendir, readdir

2. Write programs using the I/O system calls of UNIX operating system (open, read, write, etc)

3. Write C programs to simulate UNIX commands like ls, grep, etc.

4. 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

5. 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

6. Developing Application using Inter Process communication (Using Shared memory, pipes or message queues).

7. Implement the Producer – Consumer problem using semaphores.

8. Implement some memory management schemes – I for egFree 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.

9. Implement some memory management schemes – II for egWhen 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.

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

OPERATING SYSTEM LAB

Page 2: Os Lab Manual Final

CONTENTS

Ex. No TITLE OF EXERCISE Page No

1 BASIC LINUX COMMANDS

2 SHELL PROGRAMMING-I

3 SHELL PROGRAMMING-II

4 UNIX SYSTEM CALLS

5 UNIX I/O SYSTEM CALLS

6 SIMULATION OF UNIX COMMANDS

7 CPU SCHEDULING ALGORITHMS -FCFS

8CPU SCHEDULING ALGORITHMS –SJF

9 CPU SCHEDULING ALGORITHMS –Priority Scheduling.

10 CPU SCHEDULING ALGORITHMS - Round Robin Scheduling

11 INTER PROCESS COMMUNICATION

12 PRODUCER – CONSUMER PROBLEM USING SEMAPHORES

13 MEMORY MANAGEMENT SCHEMES - IImplemention Of Memory Allocation Using Paged Memory

14MEMORY MANAGEMENT SCHEMES - IIImplementation Of Best And First Fit

15 IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHMS-FIFO & LRU

16 FILE ALLOCATION TECHNIQUE

EX. NO. : 01

Page 3: Os Lab Manual Final

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.

Page 4: Os Lab Manual Final

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 S1 2

3 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 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 system on the screen.

Page 5: Os Lab Manual Final

OUTPUT : 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 : To delete a directory. COMMAND : remove directory. SYNTAX : rmdir directory name EXPLANATION : This command is used to delete the specified directory. EXAMPLE : rmdir student. OUTPUT : 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 : To view a file.

Page 6: Os Lab Manual Final

COMMAND : cat. SYNTAX : cat file name. EXPLANATION : This command displays the contents of the specified file. EXAMPLE : cat wind. OUTPUT : Contents of the file called “wind” will be displayed on the screen.

20. TASK : To copy a file. COMMAND : copy. SYNTAX : cp sourcefile destinationfile. EXPLANATION : This command produces a copy of the source file and is stored in the specified destination file by overwriting its previous contents. EXAMPLE : cp sun moon. OUTPUT : The contents of “sun” file will be copied to the “moon” file.

21. TASK : To move a file. COMMAND : move. SYNTAX : mv sourcefile destinationfile. EXPLANATION : After moving the contents of the source file into destination file, the source file is deleted. EXAMPLE : mv sun moon. OUTPUT : After copying contents from the “sun” file to “moon” file, the “sun” file is deleted. 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 : To delete a file. COMMAND : remove. SYNTAX : rm file name. EXPLANATION : This command deletes the specified file from the directory. EXAMPLE : rm sun. OUTPUT : The file called “sun” will be deleted.

24. TASK : To retrieve a part of a file. COMMAND : head. SYNTAX : head -no. of rows file name. EXPLANATION : This command displays the specified no. of rows form the top of the specified file. EXAMPLE : head -1 sun.

Page 7: Os Lab Manual Final

OUTPUT : The first row of the file called “sun” is displayed.

25. TASK : To retrieve a file. COMMAND : tail. SYNTAX : tail -no. of rows file name. EXPLANATION : This command displays the specified no. of rows form the bottom of the specified file. EXAMPLE : tail -1 moon. OUTPUT : The last row of the file called “moon” is displayed.

26. TASK : To sort the contents of a file. COMMAND : sort. SYNTAX : sort file name. EXPLANATION : This command helps in sorting the contents of a file in ascending order. EXAMPLE : sort win. OUTPUT : The contents of the file “win” are displayed on the screen in a sorted order.

27. TASK : To display the no. of characters in a file. COMMAND : word count. SYNTAX : wc file name. EXPLANATION : This command displays on the screen the no. of rows, words, and the sum of no. of characters and words. EXAMPLE : wc ball. OUTPUT : The no. of rows, words, and no. of characters present in the file

“ball” are displayed.

28. TASK : To display the calendar of a year. COMMAND : cal. SYNTAX : cal year. EXPLANATION : This command displays on the screen the calendar of the specified

year. EXAMPLE : cal 2007. OUTPUT : The calendar of the year 2007 will be displayed.

Result:

EX. NO. : 02

Page 8: Os Lab Manual Final

DATE : SHELL PROGRAMMING

AIM:

PROGRAM:echo “Enter the number:”read numberi= 1fact= 1while [$i –le $number]dofact= `expr $fact \* $i `i= `expr $i +1`doneecho “The factorial of $number is $fact.”

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

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

SAMPLE OUTPUT:

Enter two numbers:36The sum is 9.

PROGRAM:

Page 9: Os Lab Manual Final

echo “Enter the number:”read nx= $nsum= 0while [ $n –gt 0]doy= `expr $n % 10`z= `expr $y \* $y \* $y`sum= `expr $sum + $z`n= `expr $n / 10`doneif [ $x –eq $sum]thenecho “ $x is an Armstrong number.”elseecho “$x is not an Armstrong number.”fi

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

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

PROGRAM:echo “Enter the first number:”read aecho “Enter the second number:”read becho “Enter the third number:”read cif [$a –gt $b –a $a –gt $c]then echo “$a is greater.”elif [$b –gt $c]thenecho “$b is greater.”elseecho “$c is greater.”fi

SAMPLE OUTPUT:

Page 10: Os Lab Manual Final

Enter the first number:3Enter the second number:6Enter the third number:99 is greater.

PROGRAM:

echo “Enter any two numbers:”read aread becho “The numbers are $a and $b.”c= $aa= $bb= $cecho “The swapped numbers are $a and $b.”

SAMPLE OUTPUT:

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

PROGRAM:a= -1b= 1c= 0echo “Enter the limit:”read necho “FIBONACCI SERIES”for (i =1; i <=n; i++) doc= `expr $a + $b`echo $ca=$bb=$cdone

SAMPLE OUTPUT:

Page 11: Os Lab Manual Final

Enter the limit:5FIBONACCI SERIES01123

PROGRAM:echo “enter the day of the week (1-7)read ncase $n in1)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)1Sunday

SAMPLE OUTPUT 2:

Enter the day of the week(1-7)9Invalid option

PROGRAM:echo “ enter string 1”

Page 12: Os Lab Manual Final

read s1echo “enter string2”read s2if [ $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 IMPLEMENTATION OF UNIX SYSTEM CALLS DATE:

Page 13: Os Lab Manual Final

Aim:

To write a program using the fork, exec, getpid, exit, wait, close, stat, opendir, readdir system calls of UNIX Operating System.

Algorithm:

1. Start the program.2. Initialize the process id number .3. Create a process using fork() system call.4. 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:

Page 14: Os Lab Manual Final

#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());elseprintf("\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:

Page 15: Os Lab Manual Final

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

Page 16: Os Lab Manual Final

#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 IMPLEMENTATION OF I/O SYSTEM CALLS DATE:

Page 17: Os Lab Manual Final

Aim :

To write a program using the I/O System calls of UNIX operating system. (open, read, write, etc)

Algorithm :

1. Start the program.2. Create a structure variable and assign a pointer to a variable.3. If arguments for the directory is zero then the directory open.4. If the arguments for directory not equal to 0 then display the files in directory5. Choose the directory.6. 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>

Page 18: Os Lab Manual Final

#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:

Page 19: Os Lab Manual Final

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");elseprintf("File cannot be removed\n");}

OUTPUT:

RESULT:

EX NO: 05 IMPLEMENTATION OF UNIX COMMANDSDATE: LIKE LS, GREP

Aim

Page 20: Os Lab Manual Final

To write a program to implement unix commands like ls, grep.

AlgorithmSimulation 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 memory5. Close the directory.6. Stop the process.

Simulation Of GREP Command1. Start the process.2. Create a file pointer and pointer to a string.3. Open a file in read mode and the pointer points the position in the buffer.4. If string is #NULL then printf statement will get execute.5. Particular pattern of string will be displayed without using grep command.6. Stop the process.

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);}

Page 21: Os Lab Manual Final

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 IMPLEMENTATION OF FIRST COME FIRST SERVE Date: SCHEDULING ALGORITHM

AIM:

Page 22: Os Lab Manual Final

To write a C-program to implement the FCFS scheduling algorithm

ALGORITHM:

1. Start the process2. Declare the array size3. Get the number of processes to be inserted4. Get the value5. Start with the first process from it’s initial position let other process to be in queue6. Calculate the total number of burst time 7. Display the values8. 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;

Page 23: Os Lab Manual Final

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

Page 24: Os Lab Manual Final

Enter the burst times

4 9 8 3

Enter the arrival times

0 2 4 3

process waiting time turn arround time

p0 0 4

p1 2 11

p2 9 17

p3 18 21

the average waiting time is 7.250000

the average turnaround time is 13.250000

RESULT :

EX NO: 07DATE:

IMPLEMENTATION OF SHORTEST JOB FIRST SCHEDULING ALGORITHM

Page 25: Os Lab Manual Final

AIM :

To write a C-Program to implement the shortest job first scheduling algorithm

ALGORITHM :

1. Start the process2. Declare the array size3. Get the number of elements to be inserted4. Select the process which have shortest burst will execute first5. If two process have same burst length then FCFS scheduling algorithm used6. Make the average waiting the length of next process7. Start with the first process from it’s selection as above and let other process to be in

queue8. Calculate the total number of burst time 9. Display the values10. 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++)

Page 26: Os Lab Manual Final

{

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;

}

Page 27: Os Lab Manual Final

OUTPUT:

Enter the number of process 5

Enter the burst times

2 4 5 6 8

process waiting time turn around time

p[0] 0 2

p[1] 2 6

p[2] 6 11

p[3] 11 17

p[4] 17 25

the average waitingtime is 7.200000

the average turn around time is 12.200000

RESULT :

EX NO: 08DATE: IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM

Page 28: Os Lab Manual Final

Aim

To write a C-program to implement the Priority scheduling algorithm

Algorithm

1. Start the process2. Declare the array size3. Get the number of elements to be inserted4. Select the process which have smallest priority will execute first5. If two process have same priority then FCFS scheduling algorithm used6. Make the average waiting the length of next process7. Start with the shortest priority first from its selection as above and let other process

to be in queue8. Calculate the total number of burst time 9. Display the values10. 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);

Page 29: Os Lab Manual Final

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");

Page 30: Os Lab Manual Final

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: 09DATE: IMPLEMENTATION OF ROUND ROBIN SCHEDULING ALGORITHM

Aim

Page 31: Os Lab Manual Final

To write a C-program to implement the round robin scheduling algorithm

Algorithm

1. Start the process2. Declare the array size3. Get the number of elements to be inserted & value4. Set the time sharing system with preemption5. Define quantum is defined from 10 to 100ms6. Declare the queue as a circular7. Make the CPU scheduler goes around the ready queue allocating CPU to each process 8. for the time interval specified9. Make the CPU scheduler picks the first process and sets time to interrupt after

quantum expired dispatches the process10. If the process have burst less than the time quantum than the process release the CPU11. 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]);

Page 32: Os Lab Manual Final

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){

Page 33: Os Lab Manual Final

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 BURST TIME

1 8

2 3

Page 34: Os Lab Manual Final

3 6

4 1

PRESS 1.ROUND ROBIN 2.EXIT

1

ROUND ROBIN SCHEDULING

Enter the time slice: 2

PROCESS NAME REMAINING TIME TOTAL TIME

1 6 2

2 1 4

3 4 6

4 0 7

1 4 9

2 0 10

3 2 12

1 2 14

3 0 16

RESULT

EX NO: 10DATE: PRODUCER CONSUMER PROBLEM

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

Page 35: Os Lab Manual Final

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();elseprintf("BUFFER IS FULL");break;

Page 36: Os Lab Manual Final

case 2:if((mutex==1)&&(full!=0))consumer();elseprintf("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.PRODUCER2.CONSUMER3.EXITENTER YOUR CHOICE: 1

Page 37: Os Lab Manual Final

producer produces the item1ENTER YOUR CHOICE :1producer produces the item2ENTER YOUR CHOICE:1producer produces the item3ENTER YOUR CHOICE : 1BUFFER IS FULLENTER YOUR CHOICE : 2 consumer consumes item3ENTER YOUR CHOICE : 2 consumer consumes item2ENTER YOUR CHOICE : 2 consumer consumes item1ENTER YOUR CHOICE: 2BUFFER IS EMPTYENTER YOUR CHOICE : 3

RESULT:

EX NO: 11

DATE: DEVELOPING APPLICATION USING INTER PROCESS COMMUNICATION

Page 38: Os Lab Manual Final

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;

Page 39: Os Lab Manual Final

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

Page 40: Os Lab Manual Final

AIM:

To write a C-Program to implement memory allocation using paged memory management scheme.

ALGORITHMS:

1. Start the Program.2. Get the Total memory size and Page size.3. Memory size is divided by the Page size,the memory is divided into pages.4. The condition is checked to know the end of page.5. 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. 

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);

Page 41: Os Lab Manual Final

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);

Page 42: Os Lab Manual Final

}

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");

}

Page 43: Os Lab Manual Final

OUTPUT:-

MEMORY IFNORMATION

*************************

Enter the number of pages: 2

Enter the page size: 2

Enter the starting addresss for page[1]: 100

page[100] Data:blue

page[101] Data:green

Enter the starting addresss for page[2]: 200

page[200] Data:red

page[201] Data:yellow

MEMORY

**********

page[100] Data:blue

page[101] Data:green

page[200] Data:red

page[201] 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 100

2 200

Page 44: Os Lab Manual Final

Enter the logical addressL: 0101

pageno1 disp1

physical address101

Datagreen

RESULT:

EX NO: 13

DATE: IMPLEMENTATION OF FIFO PAGE REPLACEMENT

ALGORITHM

Page 45: Os Lab Manual Final

Aim

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

Algorithm

1. Start the process2. Declare the size with respect to page length3. Check the need of replacement from the page to memory4. Check the need of replacement from old page to new page in memory5. Forma queue to hold all pages6. Insert the page require memory into the queue7. Check for bad replacement and page fault8. Get the number of processes to be inserted9. Display the values10. 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;

Page 46: Os Lab Manual Final

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 : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

Page 47: Os Lab Manual Final

ENTER THE NUMBER OF FRAMES :3

ref string page frames

7 7 -1 -1

0 7 0 -1

1 7 0 1

2 2 0 1

0

3 2 3 1

0 2 3 0

4 4 3 0

2 4 2 0

3 4 2 3

0 0 2 3

3

2

1 0 1 3

2 0 1 2

0

1

7 7 1 2

0 7 0 2

1 7 0 1

Page Fault Is 15

RESULT:EX NO: 14

DATE: IMPLEMENTATION OF LRU PAGE REPLACEMENT

ALGORITHM

Page 48: Os Lab Manual Final

Aim

To write a C-program a program to implement LRU page replacement algorithm

Algorithm

1. Start the process2. Declare the size3. Get the number of pages to be inserted4. Get the value5. Declare counter and stack6. Select the least recently used page by counter value7. Stack them according the selection.8. Display the values9. 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];

Page 49: Os Lab Manual Final

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]++;elsebreak;}}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 :

Page 50: Os Lab Manual Final

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 5

7 5 9

4 5 9

4 3 9

4 3 7

9 3 7

9 6 7

9 6 2

1 6 2

The no of page faults is 10

RESULT :

EX NO: 15DATE: IMPLEMENTATION OF BEST FIT ALGORITHM

Aim

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

Page 51: Os Lab Manual Final

Algorithm

1. Start the process2. Declare the size3. Get the number of processes to be inserted4. Allocate the best hole that is small enough searching5. 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 values10. 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];

Page 52: Os Lab Manual Final

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

Page 53: Os Lab Manual Final

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

Page 54: Os Lab Manual Final

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

Algorithm

1. Start the process2. Declare the size3. Get the number of processes to be inserted4. Allocate the first hole that is big enough searching5. 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 values10. 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;

Page 55: Os Lab Manual Final

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

Page 56: Os Lab Manual Final

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 :