0% found this document useful (0 votes)
6 views

Cse Os Manual

Uploaded by

Angelin Shiny
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Cse Os Manual

Uploaded by

Angelin Shiny
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Ex.

No 1
13.02.2021 Basic Linux commands

Aim:
To execute basic commands in Linux Commands:
1. Creating a file:
Syntax: $ cat> filename
The cat command reads the standard input (the keyboard) and the > redirects the Output,
which normally goes to the screen, into a file
E.g. $ cat > myfile

Output
[mtec11@telnet ~]$ cat > display_file
Mtec11
Good Girl
Study well
Eat well
Sleep well

[1]+ Stopped cat >display_file

2. Display the contents of a file


Syntax: $ cat filename
To display the contents of the file
E.g., $ cat myfile

Output
[mtec11@telnet ~]$ cat display_file
Mtec11
Good Girl
Study well
Eat well
Sleep well

3. Append contents into a file


Syntax: $ cat>> filename
The form >> appends standard Output to an existing file.

E.g., $ cat>> filename

Output
[mtec11@telnet ~]$ cat >> display_file
Thus mtec11 can be crowned the best

[2]+ Stopped cat >>display_file

1
[mtec11@telnet ~]$ cat display_file
Mtec11
Good Girl
Study well
Eat well
Sleep well
Thus mtec11 can be crowned the best

4. Concatenation of files (cat)


Syntax: $ cat file1 file2… >> file
It’s used to concatenate file1, file2… to file
E.g., $ cat myfile1 myfile2>>myfile3

Output

[mtec11@telnet ~]$ cat > new_file


This a new file created to demonstrate concatenation of files

[3]+ Stopped cat >new_file

[mtec11@telnet ~]$ cat display_file new_file >> concatenated_file


[mtec11@telnet ~]$ cat concatenated_file
Mtec11
Good Girl
Study well
Eat well
Sleep well
Thus mtec11 can be crowned the best
This a new file created to demonstrate concatenation of files

5. Copying Files (cp)

The cp command makes a copy of file1 in the current working directory and
calls it file2 or if the file2 already exists then the contents in file2 is overwritten by contents in
file1 without any warning.
Syntax: $ cp file1 file2
E.g., $ cp myfile1 myfile2

Output
[mtec11@telnet ~]$ cp concatenated_file copied_file
[mtec11@telnet ~]$ cat copied_file
Mtec11
Good Girl
Study well
Eat well

2
Sleep well
Thus mtec11 can be crowned the best
This a new file created to demonstrate concatenation of files

6. Moving files (mv)


Syntax: $ mv file1 file2
To move a file from one place to another, the mv command is used. This has the effect of
moving rather than copying the file. It can also be used to rename a file, by moving the file to the
same directory, but giving it a different name. The source won’t exist after renaming or moving.
e.g., $ mv myfile1 myfile2

Output

[mtec11@telnet ~]$ mv copied_file moved_file

[mtec11@telnet ~]$ cat moved_file


Mtec11
Good Girl
Study well
Eat well
Sleep well
Thus mtec11 can be crowned the best
This a new file created to demonstrate concatenation of files

[mtec11@telnet ~]$ cat copied_file


cat: copied_file: No such file or directory
7. Removing a file (rm)
Syntax: $ rm filename
It’s used to delete (remove) a file.
E.g., $ rm myfile1

Output

[mtec11@telnet ~]$ rm moved_file

[mtec11@telnet ~]$ cat moved_file


cat: moved_file: No such file or directory

8. Creating a directory (mkdir)


Syntax: mkdir directoryname
It’s used to create an empty directory
E.g,: mkdir mydir

Output
[mtec11@telnet ~]$ mkdir exjayan

3
[mtec11@telnet ~]$ cd exjayan
[mtec11@telnet exjayan]$

9. Removing a directory (rmdir)


Syntax: $ rm filename
It’s used to delete (remove) a directory.
E.g.,: $ rm myfilename

Output
[mtec11@telnet exjayan]$ cd
[mtec11@telnet ~]$ rmdir exjayan
[mtec11@telnet ~]$ cd exjayan
-bash: cd: exjayan: No such file or directory

10. Printing Current Working directory (pwd)


Syntax: $ pwd
It’s used to print the current working directory.
E.g.,: $ pwd

Output

[mtec11@telnet ~]$ pwd


/home/mtec11

11. Changing to a different directory


Syntax: $ cd dirname
It’s used change the current working directory to 'another directory'.
E.g.,: $ cd mydirname

Output
[mtec11@telnet ~]$ cd ex
[mtec11@telnet ex]$

12. Returning to home directory


Syntax: $ cd..
The cd.. Command is used to return to home directory.
E.g.,: $ cd..

Output
[mtec11@telnet ~]$ cd ex
[mtec11@telnet ex]$

13. Exiting current directory


Syntax: $ cd

4
The cd command with no argument is used to exit from current directory.
E.g.,$ cd

Output
[mtec11@telnet ex]$ cd
[mtec11@telnet ~]$

14. Listing files and directory


Syntax: $ ls
The ls command lists the contents (files and directories) of current working directory in
alphabetical order.
E.g.,: $ ls
Output
[mtec11@telnet ~]$ ls
alpha_numbers ignisha Read_File
a.out Input_File Reverse_file.c
Arithmetic_Manipulation Largest_Three Reverse_File.c
Arithmetic_Operations lynx_bookmarks.html salary
Biggest_Two matrix Salary
concatenated_file newcountries Sort
Count_Characters.c new_file sortcountries
Count_Characters_File.c newsortcountries String_Comparison
countries nish Student
countries_sort nisha Student1
Display_20Lines.c numbers Sum_Even_Numbers
display_file Output_File Sum_N_Natural_Numbers
Display_File_Contents.c Palindrome1.c Transpose_Matrix.c
greetings Palindrome.c unix_file
hello Palindrome_Checking.c ex

15. Listing Hidden files:


Syntax: $ ls -a
It’s used to list all files in home directory including those whose names begin with a dot.
E.g.,: $ ls -a

Output
[mtec11@telnet ~]$ ls -a
. matrix
.. newcountries
alpha_numbers new_file
a.out newsortcountries
Arithmetic_Manipulation nish
Arithmetic_Operations nisha
.bash_history numbers
.bash_logout Output_File
.bash_profile Palindrome1.c

5
.bashrc Palindrome.c
Biggest_Two Palindrome_Checking.c
.canna .Palindrome_Checking.c.swp
concatenated_file Read_File
Count_Characters.c Reverse_file.c
Count_Characters_File.c Reverse_File.c
countries salary
countries_sort Salary
Display_20Lines.c Sort
display_file sortcountries
Display_File_Contents.c String_Comparison
.Display_File_Contents.swo Student
.Display_File_Contents.swp Student1
.emacs Sum_Even_Numbers
greetings Sum_N_Natural_Numbers
.gtkrc Transpose_Matrix.c
hello unix_file
ignisha .viminfo
Input_File .viminfo.tmp
.kde ex
Largest_Three .xemacs
lynx_bookmarks.html .zshrc

16. Listing files and directories based on modification time


Syntax: $ls -t
It’s used to list the files and directories in the current working directory by sorting based on
modification time.
E.g., $ls -t

Output

[mtec11@telnet ~]$ ls -t
ex Count_Characters_File.c nisha
concatenated_file Display_File_Contents.c alpha_numbers
new_file Input_File numbers
display_file Reverse_File.c countries_sort
Arithmetic_Operations Display_20Lines.c newsortcountries
Output_File Sum_N_Natural_Numbers sortcountries
Reverse_file.c Biggest_Two newcountries
matrix greetings countries
a.out Sum_Even_Numbers lynx_bookmarks.html
Transpose_Matrix.c Arithmetic_Manipulation Student1
Palindrome_Checking.c String_Comparison Student
Palindrome.c Largest_Three salary
Palindrome1.c hello Sort
Count_Characters.c nish Salary

6
Read_File ignisha unix_file

17. Word count (wc)


Syntax: $wc filename
The wc command is used to count number of lines/words/characters in a file.
E.g., $wc myfilename

Output

[mtec11@telnet ~]$ wc new_file


1 10 62 new_file

18. Counting no of lines in a file:


Syntax: $ wc -l filename
It’s used to count the number of lines in a file.
Eg., $ wc -l myfilename

Output

[mtec11@telnet ~]$ wc -l new_file


1 new_file

19. Counting no of words in a file:


Syntax: $ wc -w filename
It’s used to count the number of words in a file.
E.g., $ wc -w myfilename

Output
[mtec11@telnet ~]$ wc -w new_file
10 new_file

20. Counting no of characters in a file:


Syntax: $ wc -c filename
It’s used to count the number of characters in a file.
E.g., $ wc -c myfilename

Output
[mtec11@telnet ~]$ wc -c new_file
62 new_file

21. Displaying the type of a file (file)


Syntax: $file filename
The file command is used to display the type of the file.
E.g., $file myfilename

Output

7
[mtec11@telnet ~]$ file new_file
new_file: ASCII text

22. Directing Output to a file:


Syntax: $ ls > file1
The Output of the command ls is directed to or stored in file1.
E.g., $ ls > file1
Output
[mtec11@telnet ~]$ ls > new_file
[mtec11@telnet ~]$ cat new_file
alpha_numbers
a.out
Arithmetic_Manipulation
Arithmetic_Operations
Biggest_Two
concatenated_file
greetings
hello
ignisha
Input_File
Largest_Three
lynx_bookmarks.html
matrix
newcountries
new_file
newsortcountries
nish
nisha
ex
23. PATH:
Syntax: $echo $PATH
It’s used to display the current path of the operating system
E.g., $echo $PATH
Output
[mtec11@telnet ~]$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/mtec11/bin
24. Clearing the screen:
Syntax: $ tput clear / clear
It’s used to clear the terminal screen.
E.g., $ tput clear / clear
Output

The display screen was cleared


Result:
Thus the basic linux commands are executed and the output is verified successfully.

8
Ex No 2a
08.03.2021 FCFS scheduling algorithm
Aim:
To write a C program to implement FCFS scheduling algorithm
Algorithm:
 Get the number of process
 Get the burst time for the process
 Arrange the process in the first come, first serve and complete the process
 Calculate the waiting time and turn-around time and find their averages
Program:
#include<stdio.h>
int main()
{
int i,j,n,ch,ts,count,ptm,flag;
float tot_wt,tot_tn;
struct process
{
int pno;
int btime;
int wtime;
int ttime;
int rrtime;
int pri;
}p[10],temp;
printf("FIFO scheduling\n");
tot_wt=tot_tn=0;
printf("Enter the no of process\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i].pno=i+1;

9
printf("Enter the burst time of process %d\n",i+1);
scanf("%d",&p[i].btime);
}
p[0].wtime=0;
p[0].ttime=p[0].btime;
tot_tn=p[0].btime;
for(i=1;i<n;i++)
{
p[i].wtime=p[i-1].ttime;
p[i].ttime=p[i].wtime+p[i].btime;
tot_wt+=p[i].wtime;
tot_tn+=p[i].ttime;
}
printf("Process no \tBurst time \t Waiting time \tTurnaroundtime\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\t\t%d\n",p[i].pno,p[i].btime,p[i].wtime,p[i].ttime);
printf("Avg waiting time %f\n",tot_wt/n);
printf("Avg turnaround time %f\n",tot_tn/n);
}
Output:
[firstmca1@fxtelnetserver ~]$ gcc fcfs.c
[firstmca1@fxtelnetserver ~]$ ./a.out
FIFO scheduling
Enter the no of process
4
Enter the burst time of process 1
2
Enter the burst time of process 2
12
Enter the burst time of process 3
3

10
Enter the burst time of process 4
10
Process no Burst time Waiting time Turnaroundtime
1 2 0 2
2 12 2 14
3 3 14 17
4 10 17 27
Avg waiting time 8.250000
Avg turnaround time 15.000000
Result:
Thus the C program for FCFS scheduling is done and the output is verified successfully.

11
Ex No 2b
08.03.2021 SJF scheduling algorithm
Aim:
To write a C program to implement SJF scheduling algorithm
Algorithm:
 Get the number of process as input
 Get the burst time for each process
 Arrange the process with minimum burst time to execute at first
 Complete the processes and calculate their waiting and turn-around time and their
averages.
Program:
#include<stdio.h>
int main()
{
int i,j,n,ch,ts,count,ptm,flag;
float tot_wt,tot_tn;
struct process
{
int pno;
int btime;
int wtime;
int ttime;
int rrtime;
int pri;
}p[10],temp;
printf("SJF scheduling\n");
tot_wt=tot_tn=0;
printf("Enter the no of process\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{

12
p[i].pno=i+1;
printf("Enter the burst time of process %d\n",i+1);
scanf("%d",&p[i].btime);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].btime>p[j].btime)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
p[0].wtime=0;
p[0].ttime=p[0].btime;
tot_tn=p[0].btime;
for(i=1;i<n;i++)
{
p[i].wtime=p[i-1].ttime;
p[i].ttime=p[i].wtime+p[i].btime;
tot_wt+=p[i].wtime;
tot_tn+=p[i].ttime;
}
printf("Process no \tBurst time \t Waiting time \tTurnaroundtime\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\t\t%d\n",p[i].pno,p[i].btime,p[i].wtime,p[i].ttime);
printf("Avg waiting time %f\n",tot_wt/n);
printf("Avg turnaround time %f\n",tot_tn/n);

13
}
Output:
[firstmca1@fxtelnetserver ~]$ ./a.out
SJF scheduling
Enter the no of process
4
Enter the burst time of process 1
2
Enter the burst time of process 2
12
Enter the burst time of process 3
3
Enter the burst time of process 4
10

Process no Burst time Waiting time Turnaroundtime


1 2 0 2
3 3 2 5
4 10 5 15
2 12 15 27
Avg waiting time 5.500000
Avg turnaround time 12.250000
Result:
Thus a C program for SJF scheduling is done and the output is verified successfully

14
Ex No 2c
08.03.2021 Priority scheduling algorithm
Aim:
To write a C program to implement priority scheduling
Algorithm:
 Get the number of process as input
 Get the priority and the burst time for each process
 Arrange the process and execute based on their priority such that, lower the number,
higher the priority.
 Calculate the waiting and turn-around time and their averages.
Program:
#include<stdio.h>
void main()
{
int i,j,n,ch,ts,count,ptm,flag;
float tot_wt,tot_tn;
struct process
{
int pno;
int btime;
int wtime;
int ttime;
int rrtime;
int pri;
}p[10],temp;
printf("Priority Scheduling\n");
tot_wt=tot_tn=0;
printf("Enter the no of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{

15
p[i].pno=i+1;
printf("Enter the Burst time of process %d\n",i+1);
scanf("%d",&p[i].btime);
printf("Enter the Priority of process %d\n",i+1);
scanf("%d",&p[i].pri);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].pri>p[j].pri)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}}}
p[0].wtime=0;
p[0].ttime=p[0].btime;
tot_tn=p[0].btime;
for(i=1;i<n;i++)
{
p[i].wtime=p[i-1].ttime;
p[i].ttime=p[i].wtime+p[i].btime;
tot_wt+=p[i].wtime;
tot_tn+=p[i].ttime;
}
printf("Process no \tPriority\tBurst time \tWaiting time\tTurnaroundtime\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n",p[i].pno,p[i].pri,p[i].btime,p[i].wtime,p[
i].ttime);
printf("Avg waiting time %f\n",tot_wt/n);

16
printf("Avg turn around time %f\n",tot_tn/n);
}

Output:
[firstmca1@fxtelnetserver ~]$ ./a.out
Priority Scheduling
Enter the no of processes
3
Enter the Burst time of process 1
2
Enter the Priority of process 1
5
Enter the Burst time of process 2
3
Enter the Priority of process 2
2
Enter the Burst time of process 3
6
Enter the Priority of process 3
1
Process no Priority Burst time Waiting time Turnaroundtime
3 1 6 0 6
2 2 3 6 9
1 5 2 9 11
Avg waiting time 5.000000
Avg turn around time 8.666667
Result:
Thus the C program for priority scheduling is done and the output is verified successfully.

17
Ex No 2d
08.03.2021 Round Robin scheduling algorithm
Aim:
To write a C program for round robin scheduling
Algorithm:
 Get the number of process as input
 Get the time slice and the burst time for each process
 Complete the process based on their time slice and when the time slice expires, complete
the next process for the given time slice
 Calculate the waiting and turn-around time and their averages
Program:
#include<stdio.h>
void main()
{
int i,j,n,ch,ts,count,ptm,flag;
float tot_wt,tot_tn;
struct process
{
int pno;
int btime;
int wtime;
int ttime;
int rrtime;
int pri;
}p[10],temp;
ptm=0;
printf("RR Scheduling\n");
tot_wt=tot_tn=0;
printf("Enter the no of processes\n");
scanf("%d",&n);
printf("Enter the Time slice\n");

18
scanf("%d",&ts);
for(i=0;i<n;i++)
{
p[i].pno=i+1;
p[i].wtime=p[i].ttime=0;
printf("Enter the Burst time of process %d\n",i+1);
scanf("%d",&p[i].btime);
p[i].rrtime=p[i].btime;
}
do
{
flag=0;
for(i=0;i<n;i++)
{
count=p[i].btime;
if(count>0)
{
flag=1;
count=(count>=ts)?ts:count;
printf("Process %d from %d to %d\n",p[i].pno,ptm,ptm+count);
p[i].wtime+=ptm-p[i].ttime;
ptm=ptm+count;
p[i].ttime=ptm;
p[i].btime-=count;
}
}
}while(flag);
for(i=0;i<n;i++)
{
tot_wt+=p[i].wtime;
tot_tn+=p[i].ttime;

19
}
printf("Process no \tBurst time \tWaiting time \tTurnaroundtime\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\t\t%d\n",p[i].pno,p[i].rrtime,p[i].wtime,p[i].ttime);
printf("Avg waiting time %f\n",tot_wt/n);
printf("Avg turn around time %f\n",tot_tn/n);
}
Output:
[firstmca1@fxtelnetserver ~]$ ./a.out
RR Scheduling
Enter the no of processes
4
Enter the Time slice
2
Enter the Burst time of process 1
6
Enter the Burst time of process 2
5
Enter the Burst time of process 3
4
Enter the Burst time of process 4
8
Process 1 from 0 to 2
Process 2 from 2 to 4
Process 3 from 4 to 6
Process 4 from 6 to 8
Process 1 from 8 to 10
Process 2 from 10 to 12
Process 3 from 12 to 14
Process 4 from 14 to 16
Process 1 from 16 to 18

20
Process 2 from 18 to 19
Process 4 from 19 to 21
Process 4 from 21 to 23
Process no Burst time Waiting time Turnaroundtime
1 6 12 18
2 5 14 19
3 4 10 14
4 8 15 23
Avg waiting time 12.750000
Avg turn around time 18.500000

Result:

Thus a C program for round robin scheduling algorithm is done and the output is verified
successfully.

21
Ex No 3
08.03.2021 Bankers algorithm for Deadlock Avoidance
Aim:
To write a C program to implement Bankers algorithm for deadlock avoidance

Safety Algorithm:
 LetWork and Finish be vectors of length m and n, respectively. Initialize
Work = Available and Finish[i] = false for i = 0, 1, ...,n− 1.
 Find an index i such that both
a. Finish[i] == false
b. Needi≤Work
If no such i exists, go to step 4.
 Work =Work + Allocation
Finish[i] = true
 Go to step 2.
 If Finish[i] == true for all i, then the system is in a safe state.
Program:
#include<stdio.h>
#include<conio.h>
void main() {
int
k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz
, count=0;
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for (i=0;i<rz;i++) {
avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");

22
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");
for (i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for (i=0;i <pno;i++) {
P[i]=i;
printf("P[%d] ",P[i]);
for (j=0;j<rz;j++) {
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}
printf("\nEnter the MAX matrix \n ");
for (i=0;i<rz;i++) {
printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for (i=0;i <pno;i++) {
printf("P[%d] ",i);
for (j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
}
printf("\n");
A: d=-1;
for (i=0;i <pno;i++) {
count=0;
t=P[i];
for (j=0;j<rz;j++) {
need[t][j] = MAX[t][j]-allocated[t][j];

23
if(need[t][j]<=avail[j])
count++;
}
if(count==rz) {
output[k++]=P[i];
for (j=0;j<rz;j++)
avail[j]+=allocated[t][j];
} else
P[++d]=P[i];
}
if(d!=-1) {
pno=d+1;
goto A;
}
printf("Safe Sequence\n");
printf("\t <");
for (i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
getch();
}
Output:
Enter the number of resources : 3

enter the max instances of each resources

a= 1

b= 5

c= 7

Enter the number of processes : 5

Enter the allocation matrix

24
abc

P[0] 0 1 0

P[1] 2 0 0

P[2] 3 0 2

P[3] 2 1 1

P[4] 0 0 2

Enter the MAX matrix

abc

P[0] 7 5 3

P[1] 3 2 2

P[2] 9 0 2

P[3] 2 2 2

P[4] 4 3 3

Safe Sequence < P[1] P[3] P[4] P[0] P[2] >

Result:
Thus a C program to implement Bankers algorithm for deadlock avoidance is done and
the output is verified successfully.

25
Ex.No:4a
15.03.2021 FIFO Page replacement algorithm
Aim:
To write a C program to implement FIFO page replacement
Algorithm:
 Get the total number of pages and their reference strings
 Get the total number of frames
 Assign the first page to the first free frame and when frames are full, replace the frame
for which the page was first assigned.
 Calculate the page fault when pages are being replaced and display it.
Program:
#include<stdio.h>
int main()
{
int reference_string[10], page_faults = 0, m, n, s, pages, frames;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter values of Reference String:\n");
for(m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &reference_string[m]);
}
printf("\nEnter Total Number of Frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;

26
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(reference_string[m] == temp[n])
{
s++;
page_faults--;
}
}
page_faults++;
if((page_faults <= frames) && (s == 0))
{
temp[m] = reference_string[m];
}
else if(s == 0)
{
temp[(page_faults - 1) % frames] = reference_string[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", page_faults);
return 0;
}

27
Output:
[firstmca1@fxtelnetserver ~]$ ./a.out
Enter Total Number of Pages: 5
Enter values of Reference String:
Value No. [1]: 4
Value No. [2]: 1
Value No. [3]: 2
Value No. [4]: 4
Value No. [5]: 5
Enter Total Number of Frames: 3
4 -1 -1
4 1 -1
4 1 2
4 1 2
5 1 2
Total Page Faults: 4
Result:
Thus a C program for FIFO page replacement is done and the output is verified successfully

28
Ex.No:4b
15.03.2021 LRU Page replacement algorithm
Aim:
To write a C program for LRU page replacement strategy
Algorithm:
 Get the total number of pages and their reference strings
 Get the total number of frames
 Assign the first page to the first free frame and when frames are full, replace the page that
was least recently used.
 Calculate the page fault when pages are being replaced and display it.
Program:
#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i){
if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10],
flag1, flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");

29
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;

30
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
Output:
[firstmca1@fxtelnetserver ~]$ gcc lr.c
[firstmca1@fxtelnetserver ~]$ ./a.out
Enter number of frames: 3
Enter number of pages: 6
Enter reference string: 5 7 5 6 7 3

5 -1 -1
5 7 -1
5 7 -1
5 7 6
5 7 6
3 7 6
Total Page Faults = 4
Result:
Thus a C program for LRU page replacement is done and the output is verified successfully

31
Ex.No:4c
15.03.2021 LFU Page replacement algorithm
Aim:

To Write a C program for LFU page replacement.

Algorithm:

 Get the number of pages and number of pages as input.


 Get the reference string as input.
 Replace the page with the smallest count.
 Display the results.

Program:

#include<stdio.h>

int main()

int total_frames, total_pages, hit = 0;

int pages[25], frame[10], arr[25], time[25];

int m, n, page, flag, k, minimum_time, temp;

printf("Enter Total Number of Pages:\t");

scanf("%d", &total_pages);

printf("Enter Total Number of Frames:\t");

scanf("%d", &total_frames);

for(m = 0; m < total_frames; m++)

frame[m] = -1;

for(m = 0; m < 25; m++)

32
arr[m] = 0;

printf("Enter Values of Reference String\n");

for(m = 0; m < total_pages; m++)

printf("Enter Value No.[%d]:\t", m + 1);

scanf("%d", &pages[m]);

printf("\n");

for(m = 0; m < total_pages; m++)

arr[pages[m]]++;

time[pages[m]] = m;

flag = 1;

k = frame[0];

for(n = 0; n < total_frames; n++)

if(frame[n] == -1 || frame[n] == pages[m])

if(frame[n] != -1)

hit++;

flag = 0;

frame[n] = pages[m];

33
break;

if(arr[k] > arr[frame[n]])

k = frame[n];

if(flag)

minimum_time = 25;

for(n = 0; n < total_frames; n++)

if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)

temp = n;

minimum_time = time[frame[n]];

arr[frame[temp]] = 0;

frame[temp] = pages[m];

for(n = 0; n < total_frames; n++)

printf("%d\t", frame[n]);

34
printf("\n");

printf("Page Hit:\t%d\n", hit);

return 0;

Output:

Enter Total Number of Pages: 6

Enter Total Number of Frames: 3

Enter Values of Reference String

Enter Value No.[1]: 5

Enter Value No.[2]: 7

Enter Value No.[3]: 5

Enter Value No.[4]: 6

Enter Value No.[5]: 7

Enter Value No.[6]: 3

5 -1 -1

5 7 -1

5 7 -1

5 7 6

5 7 6

5 7 3

Page Hit: 2

Result:

Thus a C program for LFU replacement algorithm is done and the output is verified successfully.

35
Ex No 5a

15.03.2021 Single Level Directory

Aim:

To write a C program to implement single level directory.

Algorithm:

 Declare the number, names and size of the directories and file names.
 Get the values for the declared variables.
 Display the files that are available in the directories.

Program:

#include<stdio.h>

struct

char dname[10],fname[10][10];

int fcnt;

}dir;

void main()

int i,ch;

char f[30];

clrscr();

dir.fcnt = 0;

printf("\nEnter name of directory -- ");

scanf("%s", dir.dname);

while(1)

36
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");

scanf("%d",&ch);

switch(ch){

case 1: printf("\n Enter the name of the file -- ");

scanf("%s",dir.fname[dir.fcnt]);

dir.fcnt++;

break;

case 2: printf("\n Enter the name of the file -- ");

scanf("%s",f);

for(i=0;i<dir.fcnt;i++){

if(strcmp(f, dir.fname[i])==0)

printf("File %s is deleted ",f);

strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);

break;}

if(i==dir.fcnt)

printf("File %s not found",f);

else

dir.fcnt--;

break;

case 3: printf("\n Enter the name of the file -- ");

scanf("%s",f);

for(i=0;i<dir.fcnt;i++)

37
{

if(strcmp(f, dir.fname[i])==0)

printf("File %s is found ", f);

break;

if(i==dir.fcnt)

printf("File %s not found",f);

break;

case 4: if(dir.fcnt==0)

printf("\n Directory Empty");

else

printf("\n The Files are -- ");

for(i=0;i<dir.fcnt;i++)

printf("\t%s",dir.fname[i]);

break;

default: exit(0);

}}

getch();

Output:

Enter name of directory -- CSE

38
1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- A

1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- B

1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- C

1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 4

The Files are -- A B C

1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 3

Enter the name of the file – ABC

File ABC not found

1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 2

Enter the name of the file – B

File B is deleted

1. Create File 2. Delete File 3. Search File

4. Display Files 5. Exit Enter your choice – 5

Result:

Thus a C program for single level directory is done and the output is verified successfully.

Ex No 5b Two Level Directory

39
Ex No 5b

15.03.2021 Two Level Directory

Aim:

To write a C program for two level directory.

Algorithm:

 Declare the number, names and size of the directories and subdirectories and file names.
 Get the values for the declared variables.
 Display the files that are available in the directories and subdirectories.

Program:

#include<stdio.h>

struct

char dname[10],fname[10][10];

int fcnt;

}dir[10];

void main()

int i,ch,dcnt,k;

char f[30], d[30];

clrscr();

dcnt=0;

while(1)

printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");

printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");

scanf("%d",&ch);

40
switch(ch)

case 1: printf("\n Enter name of directory -- ");

scanf("%s", dir[dcnt].dname);

dir[dcnt].fcnt=0;

dcnt++;

printf("Directory created");

break;

case 2: printf("\n Enter name of the directory -- ");

scanf("%s",d);

for(i=0;i<dcnt;i++)

if(strcmp(d,dir[i].dname)==0)

printf("Enter name of the file -- ");

scanf("%s",dir[i].fname[dir[i].fcnt]);

dir[i].fcnt++;

printf("File created");

break;

if(i==dcnt)

printf("Directory %s not found",d);

break;

case 3: printf("\nEnter name of the directory -- ");

scanf("%s",d);

for(i=0;i<dcnt;i++)

41
{

if(strcmp(d,dir[i].dname)==0)

printf("Enter name of the file -- ");

scanf("%s",f);

for(k=0;k<dir[i].fcnt;k++)

if(strcmp(f, dir[i].fname[k])==0)

printf("File %s is deleted ",f);

dir[i].fcnt--;

strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);

goto jmp;

printf("File %s not found",f);

goto jmp;

printf("Directory %s not found",d);

jmp : break;

case 4: printf("\nEnter name of the directory -- ");

scanf("%s",d);

for(i=0;i<dcnt;i++)

42
if(strcmp(d,dir[i].dname)==0)

printf("Enter the name of the file -- ");

scanf("%s",f);

for(k=0;k<dir[i].fcnt;k++)

if(strcmp(f, dir[i].fname[k])==0)

printf("File %s is found ",f);

goto jmp1;

printf("File %s not found",f);

goto jmp1;

printf("Directory %s not found",d);

jmp1: break;

case 5: if(dcnt==0)

printf("\nNo Directory's ");

else{

printf("\nDirectory\tFiles");

for(i=0;i<dcnt;i++){

printf("\n%s\t\t",dir[i].dname);

43
for(k=0;k<dir[i].fcnt;k++)

printf("\t%s",dir[i].fname[k]);}}

break;

default:exit(0);

getch();

Output:

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR1

Directory created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR2

Directory created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR1

Enter name of the file -- A1

File created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR1

44
Enter name of the file -- A2

File created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR2

Enter name of the file -- B1

File created

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice -- 5

Directory Files

DIR1 A1 A2

DIR2 B1

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice -- 4

Enter name of the directory – DIR

Directory not found

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice -- 3

Enter name of the directory – DIR1

Enter name of the file -- A2

File A2 is deleted

1. Create Directory 2. Create File 3. Delete File

4. Search File 5. Display 6. Exit Enter your choice – 6

Result:

Thus a C program for two level directory is done and the output is verified successfully.

45

You might also like