0% found this document useful (0 votes)
26 views113 pages

Amjathfinal

The document describes various basic UNIX commands used to create directory structures, files, copy/move files, search files based on patterns, and manipulate file permissions. It provides examples of commands like mkdir, ls, cat, mv, cp, rm, grep, head, tail, sort, chmod and their usage along with sample outputs. The document contains 15 exercises demonstrating different use cases of these commands like creating nested directories; creating, renaming and deleting files; copying files to other directories; searching files based on patterns; displaying contents of files from specific lines; redirecting command outputs to files; and changing file permissions.

Uploaded by

steev spidey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views113 pages

Amjathfinal

The document describes various basic UNIX commands used to create directory structures, files, copy/move files, search files based on patterns, and manipulate file permissions. It provides examples of commands like mkdir, ls, cat, mv, cp, rm, grep, head, tail, sort, chmod and their usage along with sample outputs. The document contains 15 exercises demonstrating different use cases of these commands like creating nested directories; creating, renaming and deleting files; copying files to other directories; searching files based on patterns; displaying contents of files from specific lines; redirecting command outputs to files; and changing file permissions.

Uploaded by

steev spidey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 113

21IT1302-OPERATING SYSTEM 211422205021

EX.NO:1
DATE:

BASIC UNIX COMMANDS

AIM:

To write a code using Basic UNIX Commands and perform the following execerise

EXERCISE COMMAND:

1. Create the following directory structure in UNIX ,CSE under PG and UG

Description:

i) mkdir Command: It is used to create new directories or folders within the


file system

ii) Nested Directories: You can use mkdir to create nested directories,
meaning directories within directories. This allows you to create a hierarchical
directory structure.

iii) Parent Directory: When creating nested directories, it's essential to ensure that
the parent directory (in this case, "CSE") exists before creating its child directories
("UG" and "PG").

Output:

1
21IT1302-OPERATING SYSTEM 211422205021

2. Create files under cse with extensions like *.txt, *.c etc..

Description:
Creating and Writing Files with cat Command: You can use the cat
command to both create and write content to files. To create files with
specific extensions, you can use redirection to send content into new
files.

Output:

3. Display all files with extension ”.txt”.

Description:
l s c o m m a n d : When combined with the * . t x t pattern, the ls command
will list all files with the ".txt" extension in the specified directory.

Output:

2
21IT1302-OPERATING SYSTEM 211422205021

4. Rename all files with extension “.txt” to “.dat”.


Description:
mv Command: The mv command is used for moving or renaming files and directories
in UNIX. When used for renaming, it effectively changes the filename while keeping
the file's content intact.

Output:

5. Copy the files with extension C to UG directory.

Description:
cp Command: The cp command is used to copy files and directories in UNIX.
It allows you to duplicate files while preserving their content.

Output:

3
21IT1302-OPERATING SYSTEM 211422205021

6. Delete all the files with extension C from cse directory.

Description:

rm Command: The rm command is used to remove or delete files and directories in


UNIX. It is a powerful command, so use it with caution as deleted files are typically not
recoverable.

Output:

7. Display all the files with 2 characters filename and extension “.dat”.

Description:
l s c o m m a n d : When combined with the * . t x t pattern, the ls command
will list all files with the ".txt" extension in the specified directory.

Output:

4
21IT1302-OPERATING SYSTEM 211422205021

8. Create a file name with list of subject names and Display the first three
and last three subjects stored in the file.
Description:
i) head Command: The head command is used to display the beginning (head) of a
text file. By default, it displays the first ten lines of a file, but you can specify a different
number of lines to display.
ii) tail Command: The tail command is used to display the end (tail) of a text file.
Like head, it also defaults to displaying the last ten lines of a file, but you can specify a
different number of lines.

Output:

5
21IT1302-OPERATING SYSTEM 211422205021

9. List the contents of the file from the fourth subject from the end of the
file and also list the contents of the file from the seventh subject from the
beginning of the file.
Description:
i) head Command: The head command is used to display the beginning (head) of a
text file. By default, it displays the first ten lines of a file, but you can specify a
different number of lines to display.
ii) Tail command: The tail command is used to display the end (tail) of a text file.
Like head, it also defaults to displaying the last ten lines of a file, but you can specify a
different number of lines.

Output:

10. Create the file named INDIA which contains the following
data India is my country
All Indians are my brothers and
SisterI love my country

a) Use the filter command to search for the pattern “my”

Description:
Filter Commands: Filter commands like grep, awk, and sed are used to
process and manipulate text data. They are particularly useful for searching, extracting,
or transforming text based on specified patterns or conditions.
Output:
a) Display all the lines that do not contain the above pattern

Description:
Filter Commands: Filter commands like grep, awk, and sed are used to
process and manipulate text data. They are particularly useful for searching,
extracting, or transforming text based on specified patterns or conditions.
Output:

c) Count the total number of linescontaining“my”

6
21IT1302-OPERATING SYSTEM 211422205021

Description:

i) Using grep to Find Lines with "my": You can use the grep command to search for
lines that contain the pattern "my" in a text file. The -c option of grep can be used to
count the number of lines that match the pattern.

ii) Piping to wc for Counting: To count the lines, you can pipe the output of grep into
the wc command using the pipe operator |. Specifically, you will use wc -l to count the
number of lines in the filtered output.

d) Display all the lines which contais the pattern “country”


Description:
Filter Commands: Filter commands like grep, awk, and sed are used to process and
manipulate text data. They are particularly useful for searching, extracting, or
transforming text based on specified patterns or conditions.

Output:

7
21IT1302-OPERATING SYSTEM 211422205021

11) Create a file with 15 lines of data.Using filter commands select top 10
lines,last 8 lines and display them in two different files.Add line numbers to
the files.
Description:

i) head Command: The head command is used to display the beginning (head) of a
text file. By default, it displays the first ten lines of a file, but you can specify a
different number of lines to display.
ii) Tail command: The tail command is used to display the end (tail) of a text file.
Like head, it also defaults to displaying the last ten lines of a file, but you can specify a
different number of lines.

Output:

8
21IT1302-OPERATING SYSTEM 211422205021

12) Create a file containing 10 lines of information.Count the number


oflines,number of words and number of character.
Description:

Wc for Counting: To count the lines, you can pipe the output of grep into the wc
command using the pipe operator |. Specifically, you will use wc -l to count the number
of lines in the filtered output.

Output:

13) Redirect the contents of ls -p to a file calles LISTING.Give the


commandsto redirect the output a long listing of files and append it to the
file LISTING.

Description:

i ) O u t p u t R e d i r e c t i o n : Output redirection in UNIX allows you to direct the


output of a command to a file instead of displaying it on the terminal. The > operator is
used for regular output redirection, while >> is used to append output to an existing
file.

i i ) A p p e n d i n g t o a F i l e : By using the >> operator, you can append the


output of a command to an existing file. This is useful when you want to add
more content to a file without overwriting its existing contents.
Output:

14) Change the modes of those files which began with the letter “s” in such a
way that the owner has read and execute permission, the group has read and
9
21IT1302-OPERATING SYSTEM 211422205021

write permissions and others only read permission(use Octaldecimal


representation)
Description:

chmod Command: The chmod command is used to change file permissions


in UNIX-like operating systems.
Output:

10
21IT1302-OPERATING SYSTEM 211422205021

15) Create a file called MARK which contains the sample data as follows:
S001 RAJA dbms 78
S002 Ushaos 96

a) Display the contents of the file stored according to the mark in


thedescending order.
Description:
i) sort Command: The sort command is used to sort lines of
text files in various ways, including in ascending or descending
order.
ii) Sorting by Key: You can specify a key for sorting using the -
k option
Output:

b) Display the names of the students in the alphabetic order ignoring the
cases

11
21IT1302-OPERATING SYSTEM 211422205021

Description:

sort Command: The sort command is used to sort lines of text files in various ways,
including alphabetically.
Output:

c) Display the list of the students who have scored marks between 50
and 80

Description:
awk Command: The awk command is a versatile text processing tool that
allows you to perform various operations on text files, including filtering and
manipulating data.
Output:

d) Display the list of students and their registration number

Description:

awk Command: The awk command is a versatile text processing tool that allows you
to extract and manipulate data from text files.

Output:

e) Sort the files ccording to the third field & dump it to the file called
SCODE

Description:

i) sort Command: The sort command is used to sort lines of text files in
various ways, including sorting based on specific fields.

ii) Sorting by Field: You can use the -k option with sort to specify the field
number that you want to use as the sorting key. In this case, you want to
sort based on the third field.

12
21IT1302-OPERATING SYSTEM 211422205021

Output:

13
21IT1302-OPERATING SYSTEM 211422205021

16) Create the file called EMP as


given
below:E001:MALAR:MKTG:5000
E002:BALAN:ACCT:7000

a) Sort the file on the employee's departement and display the name of
theemployee and the departement

Description:

i) sort Command: The sort command is used to sort lines of text files based
on specified fields.
ii) awk Command: The awk command is used for text processing
and manipulation, including field extraction and formatting.
Output:

14
21IT1302-OPERATING SYSTEM 211422205021

b) List the employees who earn between 4000 and 6000

Description:

i) awk Command: The awk command is used for text processing


and manipulation, including field extraction and formatting.

ii) Filtering with Conditional Statements: You can use conditional


statements in awk to filter lines based on specific criteria, in this case, the
salary range between 4000 and 6000.
Output:

c) Sort the file on the employee name in the reverse order and extract their
codes and their names.

Description:

i) sort Command: The sort command is used to sort lines of text files based
on specified fields.

ii) awk Command: The awk command is used for text processing
and manipulation, including field extraction and formatting.

Output:

d) Display the contents of the file without redundancy in sorted

order Description:

sort Command: The sort command is used to sort lines of text files based on
specified fields.

Output:

15
21IT1302-OPERATING SYSTEM 211422205021

17) Display the permission of the group for the files whose names begin
with”p”
Description:
i) l s c o m m a n d : When combined with the * . t x t pattern, the ls command
will list all files with the ".txt" extension in the specified directory.
ii) grep Command: The grep command is used to search for patterns in text.
In this case, we use it to filter filenames that begin with "p."

Output:

18) Give the command to extract logins of the people whose login namestarts with
“panimalar”
Description:
grep Command: The grep command is used for searching text using regular expressions. It can
filter lines that match specific patterns in a text file.

16
21IT1302-OPERATING SYSTEM 211422205021

Output:

19)Display the entire text of the file in uppercase.


Description
i) tr Command: The tr command is used to translate,
squeeze, and/or delete characters in a text stream. In
this case, we'll use it to convert text to uppercase.
ii)awk Command: The awk command can be used to process text, and in
this command, it will be used to print the entire content of the file

Output:

20) Give the command to extract the links,the file owner and the file
nameonly in the current directory.

17
21IT1302-OPERATING SYSTEM 211422205021

Description:

ls Command: The ls command is used to list files and directories in a


directory. The -l option is used to display the information in long
format, including file owner, permissions, links, and more.

Output

RESULT:
Thus the Basic UNIX Commands are successfully

18
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:02
DATE:

SYSTEM CALLS

AIM:

To write a code using System Calls Commands

ALGORITHM:
Step 1: Import the necessary header files for using these functions in your C program.
Step 2: Define variables to store the parent PID (parent_pid) and child PID (child_pid).
Step 3: Inside the main function, get the parent's PID and display it. \
Step 4: Create a child process using fork().
Step 5: Check if the fork() call was successful (non-negative PID means it's the parent; 0 means
it's the child).
Step 6: Inside the child process block, get the child's PID and the parent's PID (PPID), display
them, and execute a different program using exec() (e.g., "ls -l").

PROGRAM:

//C program using fork()


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork(); printf("Hello world! \n");
return 0;
}

19
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

PROGRAM:
//C program using exec()
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
printf("I am EXEC.c called by execvp()");
printf("\n");
return 0;
}

OUTPUT:

20
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
//C program using getppid()

#include <stdio.h>
#include <conio.h>
int main()
{
int pid;
pid = fork();
if (pid == 0)
{
printf("\nParent Process id: %d", getpid()); printf("\nChild Process with parent id:%d",
getppid());
}
return 0;
}

OUTPUT:

PROGRAM:
//C program using wait()

#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
pid_t cpid;

21
21IT1302-OPERATING SYSTEM 211422205021

exit(0); /* terminate child */


if (fork()==0)
exit(0);
else
cpid =wait(NULL); printf("Parent pid = %d\n", getpid()); printf("Child pid = %d\n", cpid);
return 0;
}

OUTPUT:

PROGRAM:
//C program using exit()
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("START");
exit(0);
printf("End of program");
}

OUTPUT:

22
21IT1302-OPERATING SYSTEM 211422205021

RESULT:
Thus the System call functions are using and executed successfully.

23
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:03
DATE:

IMPLEMENTATION OF CPU SCHEDULING ALGORITHMS

3) a)First Come First

Serve AIM:

To implement FCFS Scheduling algorithm using C.

ALGORITHM:

Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process name and the burst time

Step 4: Set the waiting of the first process as '0' and its burst time as its turnaround time

Step 5: for each process in the Ready Q calculate

a) Waiting times(n) waiting time (n-1)+ Burst time (n-1)


b) Turnaround time (n)= waiting time(n)+Burst time(n)

Step 6: Calculate

(a) Average waiting time Total waiting Time/Number of process

(b) Average Turnaround time = Total Turnaround Time / Number of

process Step 7: Stop the process

24
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int n,pid[10],at[10],bt[10],ft[10],wt[10],ta[10],i,j,t,stt=0,totta=0,totwt=0;
float avgta,avgwt;
printf("ENTER THE NO.OF PROCESSES:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pid[i]=i;
printf("\n ENTER THE ARRIVAL TIME:");
scanf("%d",&at[i]);
printf("\n ENTER THE BURST TIME:");
scanf("%d",&bt[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(at[i]>at[j])
{
t=pid[i];
pid[i]=pid[j];
pid[j]=t;
t=at[i];
at[i]=at[j];
at[j]=t;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
stt=at[1];
}
}
printf("\nTHE VALUES OF THE ARRIVAL TIME IS %d",stt);
for(i=1;i<=n;i++)
{
25
21IT1302-OPERATING SYSTEM 211422205021

ft[i]=stt+bt[i];
wt[i]=stt-at[i];
ta[i]=ft[i]-at[i];
totta=totta+ta[i];
totwt=totwt+wt[i];
stt=ft[i];
}
avgta=(float)totta/n;
avgwt=(float)totwt/n;
printf("\nPNO\tARRIVAL TIME\tBURST TIME\tCOMPLETION TIME\t\tWAIT TIME\
tTAT\n");
for(i=1;i<=n;i++) printf("%d\t\t%d\t\t%d\t\t%d\t\t\t%d\t\t%d\
n",pid[i],at[i],bt[i],ft[i],wt[i],ta[i]
);
printf("\nAVERAGE TURN AROUND TIME=%f",avgta); printf("\
nAVERAGE WAITING TIME=%f",avgwt);
}

OUTPUT:

26
21IT1302-OPERATING SYSTEM 211422205021

3).b(i)Shortest Job First (Non pre-emptive Scheduling)

AIM:

To implement SJF Scheduling algorithm using C.

ALGORITHM:
Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time

Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time.

Step 5: Set the waiting time of the first process as '0' and its turnaround time as its burst time.

Step 6: Sort the processes names based on their Burst time

Step 7: For each process in the ready queue, calculate

a) Waiting times(n)= waiting time (n-1)+ Burst time (n-1)

b) Turnaround time (n) waiting time(n) + Burst time(n)

Step 8: Calculate

(a) Average waiting time = Total waiting Time/Number of process

(b) Average Turnaround time Total Turnaround Time/Number of

process Step 9: Stop the process

27
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:

#include<stdio.h>
void main()
{
int i,j,t,n,stt=0,pid[10],at[10],bt[10],ft[10],att,wt[10],ta[10],totwt=0,totta=0;
float avgwt,avgta;
printf("ENTER THE NUMBER OF PROCESSES:");
scanf("%d",&n);
printf("\nENTER THE ARRIVAL TIME:");
scanf("%d",&att);
for(i=1;i<=n;i++)
{
pid[i]=i;
at[i]=att;
printf("\nENTER THE BURST TIME:");
scanf("%d",&bt[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(bt[i]>bt[j])
{
t=pid[i];
pid[i]=pid[j];
pid[j]=t;

t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
stt=att;
}
}
for(i=1;i<=n;i++)
{
ft[i]=stt+bt[i];
wt[i]=stt-at[i];
ta[i]=ft[i]-at[i];
28
21IT1302-OPERATING SYSTEM 211422205021

totta=totta+ta[i]; totwt=totwt+wt[i]; stt=ft[i];


}
avgwt=(float)totwt/n;
avgta=(float)totta/n;
printf("\nPNO\tARRIVAL TIME\tBURST TIME\tCOMPLETION TIME\tWAIT TIME\
tTAT");
for(i=1;i<=n;i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",pid[i],at[i],bt[i],ft[i],wt[i],ta[i
]);
}
printf("\nAVERAGE TURN AROUND TIME=%f",avgta); printf("\
nAVERAGE WAITING TIME=%f",avgwt);
}

OUTPUT:

29
21IT1302-OPERATING SYSTEM 211422205021

3).b(ii)Shortest Job First (pre-emptive CPU Scheduling)

AIM:
To write a program to stimulate the CPU scheduling algorithm Shortest job first
(Pre- emption)

ALGORITHM:
Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU and
process arrival time. highest burst time

Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time

Step 5: Set the waiting time of the first process as '0' and its turnaround time as its burst time.

Step 6: Sort the processes names based on their Burt time and Arrival time

Step 7: For each process in the ready queue, calculate

a) Waiting time(n) waiting time (n-1)+ Burst time (n-1)-arrival time(n)

b) Turnaround time (n) waiting time(n)+Burst time(n)

Step 8: Calculate and print the results

a) Average waiting time Total waiting Time/Number of process


b) Average Turnaround time - Total Turnaround Time / Number of process

(c) Step 9: Stop the process

PROGRAM:
30
21IT1302-OPERATING SYSTEM 211422205021

#include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10],
temp[10]; int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("Enter the Total Number of Processes:");
scanf("%d", &limit);
printf("Enter Details of %d Processes",
limit); for(i = 0; i < limit; i++)
{
printf("nEnter Arrival Time:");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end – arrival_time[smallest];
}
}
31
21IT1302-OPERATING SYSTEM 211422205021

average_waiting_time = wait_time / limit;


average_turnaround_time = turnaround_time / limit;
printf("nnAverage Waiting Time:t%lf", average_waiting_time);
printf("Average Turnaround Time:t%lf",
average_turnaround_time); return 0;
}
OUTPUT:

32
21IT1302-OPERATING SYSTEM 211422205021

3) c)Round Robin Scheduling

AIM:

To implement Round Robin Scheduling algorithm using C.

ALGORITHM:
Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time

Step 4: Calculate the no. of time slices for each process where No. of time slice for process (n)
burst time process(n)/time slice

Step 5: If the burst time is less than the time slice then the no. of time slices =1.

Step 6: Consider the ready queue is a circular Q, calculate

(a) Waiting time for process(n) - waiting time of process(n-1)+ burst time of process(n-
1)+the time difference in getting the CPU from process(n-1)

(b) Turnaround time for process(n) = waiting time of process(n) + burst time of process(n)
+ the time difference in getting CPU from process(n)

Step 7: Calculate

(a) Average waiting time = Total waiting Time / Number of process

(b) Average Turnaround time = Total Turnaround Time / Number of

process Step 8: Stop the process

33
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
#include<stdio.h>
int main()
{
int i,f[10],bt[10],p[10],cbt[10],wt[10],tat[10];
int j,k,n,t,q,flag;
float atat,awt;
printf("\nRound Robin Scheduling");
printf("\nEnter the number of process:");
scanf("%d",&n);
printf("\nEnter the process burst time:");
for(i=1;i<=n;i++)
{
scanf("%d",&bt[i]);
cbt[i]=bt[i];
p[i]=i;
}
printf("\nEnter the time quantum:");
scanf("%d",&q);
for(i=1;i<=n;i++)
{
wt[i]=0;
tat[i]=0;
f[i]=0;
}
do
{
for(i=1;i<=n;i++)
{
if(f[i]==0)
{
if(bt[i]<=q)
{ f[i]=
1;
t=bt[i];
}
else
t=q;
bt[i]=bt[i]-t;
for(j=1;j<=n;j++)
34
21IT1302-OPERATING SYSTEM 211422205021

{
if(j==i||f[j]==1)
continue;
wt[j]=wt[j]+t;
}
}
}
flag=0;
for(i=1;i<=n;i++)
if(f[i]==0)
flag=1;
}while(flag==1);
atat=awt=0;
for(i=1;i<=n;i++)
{
tat[i]=wt[i]+cbt[i];
atat+=tat[i];
awt+=wt[i];
}
printf("\n\tProcess id\tBurst time\twaiting time\tturnaround time");
for(i=1;i<=n;i++) printf("\n\tP%d\t\t%d\t\t%d\t\t%d\
t",p[i],cbt[i],wt[i],tat[i]); printf("\navg.turnaround
time:%f",atat/n);
printf("\navg.waiting time:%f\n",awt/n);
return 0;
}

35
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

36
21IT1302-OPERATING SYSTEM 211422205021

3) d)Priority Scheduling

AIM:

To write a c program to simulate the CPU Scheduling priority algorithm.

ALGORITHM:
Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst

time Step 4: Sort the ready queue according to the priority number.

Step 5: Set the waiting of the first process as '0' and its burst time as its turnaround time Step 6:
Arrange the processes based on process priority

Step 7:For each process in the Ready Q calculate

Step 8: for each process in the Ready Q calculate

a) Waiting time(n) = waiting time (n-1)+ Burst time (n-1)

b) Turnaround time (n) waiting time(n)+ Burst time(n)

Step 9: Calculate

(a) Average waiting time Total waiting Time/Number of process

(b) Average Turnaround time = Total Turnaround Time / Number of process Print the results
in an order.

Step 10: Stop the process

37
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,t,n,stt=0,pid[10],pr[10],at[10],bt[10],ft[10],att;
int wt[10],ta[10],totwt=0,totta=0;
float avgwt,avgta;
printf("ENTER THE NO.OF.PROCESS:");
scanf("%d",&n);
printf("\nENTER THE ARRIVAL TIME:");
scanf("%d",&att);
for(i=1;i<=n;i++)
{
pid[i]=i;
at[i]=att;
printf("\nENTER THE BURST TIME:");
scanf("%d",&bt[i]);
printf("\nENTER THE PRIORITY OF THE PROCESS:");
scanf("%d",&pr[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i]>pr[j])
{
t=pid[i];
pid[i]=pid[j];
pid[j]=t;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
t=pr[i];
pr[i]=pr[j];
pr[j]=t;
}
}
}
stt=att;

38
21IT1302-OPERATING SYSTEM 211422205021

for(i=1;i<=n;i++)
{
ft[i]=stt+bt[i];
wt[i]=stt-at[i];
ta[i]=ft[i]-at[i];
if(wt[i]<0)
wt[i]=0;
totwt=totwt+wt[i];
totta=totta+ta[i];
stt=ft[i];}
avgwt=(float)totwt/n;
avgta=(float)totta/n;
printf("PNO\tARR TIME\tBURST TIME\tFINISH TIME\tWAIT TIME\tTURN
TIME\n");
for(i=1;i<=n;i++)
{
printf("\n%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d",pid[i],at[i],bt[i],ft[i],wt[i],ta[i]);
}
printf("\n THE AVERAGE WAITING TIME IS:%f\n",avgwt);
printf("\n THE AVERAGE TURN TIME IS:%f\n",avgta);
}

OUTPUT:

39
21IT1302-OPERATING SYSTEM 211422205021

RESULT:

Thus the CPU Scheduling processes was executed successfully

40
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:04
DATE:

IMPLEMENTATION OF SEMAPHORES

AIM:

To implement the producer and consumr problem using semaphores

ALGORITHM:
Step 1: Start

Step 2: Initialize the semaphore variable

S Step 3: In the producer function,

While s==1 do nothing

Produce the value

Assign s=1

Return

Step 4: In the Consumer function

While s==0 do nothing

Display the consumed value

Assign s=0

Return

Step 5: Create threads for producer and consumer function to make it run concurrently

Step 6: Stop

41
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define num_loops 5
union semun
{
int val;
struct semid_ds *buf;
short *array;
};
int main(int argc,char *argv[])
{
int semset_id;
union semun sem_val;
int child_pid;
int i;
struct sembuf sem_op;
int rc;
struct timespec delay;
semset_id=semget(IPC_PRIVATE,1,0600);
if(semset_id==-1)
{
perror("semget");
exit(1);
}
printf("SEMAPHORE SET CREATED SEMAPHORE SET ID%d\n",semset_id);
sem_val.val=0;
rc=semctl(semset_id,0,SETVAL,sem_val);
child_pid=fork();
switch(child_pid)
{
case 1:perror("fork");exit(1);
case 0:for(i=0;i<num_loops;i++)
{

42
21IT1302-OPERATING SYSTEM 211422205021

sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(semset_id,&sem_op,1);
printf("Consumer consumed item %d\n",i);
fflush(stdout);
}
break;
default:for(i=0;i<num_loops;i++)
{
printf("Producer produced item %d\n",i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(semset_id,&sem_op,1);
if(rand()>3*(RAND_MAX/4))
{
delay.tv_sec=0;
delay.tv_sec=10;
nanosleep(&delay,NULL);
}
}
break;
}
return 0;
}

43
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

RESULT:
Thus the producer and consumer problem using semaphores was executed
successfully.

44
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:05
DATE:

INTER PROCESS COMMUNICATION

AIM:

To develop a client-server application program, this uses shared memory using IPC

ALGORITHM:
Server

Step1: Initialize size of shared memory shmsize to 27

Step2: Initialize key to 2013 (some random value)

Step3: Create a shared memory segment using shmget with key & IPC CREAT as
parameter. Step4: If shared memory identifier shmid is -1, then stop.

Step5: Display shmid.

Step6: Attach server process to the shared memory using shimmat with shmid as parameter.

Step7: If pointer to the shared memory is not obtained, then stop.

Step8:Clear contents of the shared region using memset function.

Step9. Write a-z onto the shared memory. Step10 Wait till client reads the shared
memory contents

Step11: Detatch process from the shared memory using shimdt system call

Step12: Remove shared memory from the system using shmeti with IPC_RMID argument

Step13: Stop

Client

Step1: Initialize size of shared memory shmsize to 27.

45
21IT1302-OPERATING SYSTEM 211422205021

Step2:Initialize key to 2013 (same value as in server)

Step3: Obtain access to the same shared memory segment using same
key, If obtained then display the shmid else print "Server not started"

Step4: Attach client process to the shared memory using shimmat with shmid as parameter. If
pointer to the shared memory is not obtained, then stop.

Step5: Read contents of shared memory and print it.

Step6: After reading, modify the first character of shared memory to

Step7: Stop

PROGRAM:
SENDER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<string.h>
#define msgsz 50
typedef struct msgbuffer
{
long mtype;
char mtext[msgsz];
}mbuffer;
main()
{
int msgflg=IPC_CREAT|0666;
key_t key;
int msqid,buffer;
mbuffer mb;
key=1234;
if((msqid=msgget(key,msgflg))<0)
{
perror("msgget");
return 1;
}
else
46
21IT1302-OPERATING SYSTEM 211422205021

mb.mtype=1;
strcpy(mb.mtext," HAI TO ALL ");
buffer=strlen(mb.mtext);
if(msgsnd(msqid,&mb,buffer,msgflg)<0)
{
printf("%d%ld%s%d",msqid,mb.mtype,mb.mtext,buffer);
perror("msgsnd");
return 1;
}
else
printf(" MESSAGE : %s IS SENT ",mb.mtext);
return 0;
}

OUTPUT:

RECEIVER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<stdio.h>
#define msgsz 70
typedef struct msgbuffer{
long mtype;
char mtext[msgsz];
}
mbuffer;
main()
{
key_t key;
int msqid,buffer;
mbuffer mb;
key=1234;if((ms
qid=msgget(key,0
47
21IT1302-OPERATING SYSTEM 211422205021

666))<0)
{
perror("msgget");
return 0;
}
if(msgrcv(msqid,&mb,msgsz,1,0)<0)
{
perror("msgrev");
return 1;
}
printf(" MESSAGE : %s IS RECEIVED\n",mb.mtext);
return 0;
}

OUTPUT:

48
21IT1302-OPERATING SYSTEM 211422205021

RESULT:
Thus the inter process communication using IPC was executed successfully.

49
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:06)i)
DATE:

Implementation of Bankers Algorithm for Deadlock Avoidance

AIM:

Simulate bankers algorithm for Deadlock Avoidance.

ALGORITHM:
Step1: Start the program.

Step2: Get the values of resources and processes.

Step3: Get the avail value.

Step4: After allocation find the need value.

Step5: Check whether it is possible to allocate.

Stepó: If it is possible then the system is in safe state.

Step7: Else system is not in safety state.

Step8: If the new request comes then check that the system is in

safety. Step9: or not if we allow the request.

Step10: stop the program.

Step11: end

50
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Banker's Algo ************\n");
input();
show();
cal();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
51
21IT1302-OPERATING SYSTEM 211422205021

}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}

printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
52
21IT1302-OPERATING SYSTEM 211422205021

finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\n");
while(flag)
{
flag=0; for(i=0;i<n;i+
+)
{
int c=0; for(j=0;j<r;j+
+)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{ c+
+;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
for(i=0;i<n;i++)
53
21IT1302-OPERATING SYSTEM 211422205021

{
if(finish[i]==1)
{ c1+
+;
}
else
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}

54
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

55
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:06)ii)
DATE:

Implementation of Deadlock Detection

AIM:

Simulate bankers algorithm for Deadlock Detection.

ALGORITHM 1:
Simply detects the existence of a Cycle:

Step1: Start at any vertex finds all its immediate neighbors.


Step2: From each of these find all immediate neighbors, etc.

Step3: Until a vertex repeats (there is a cycle) or one cannot continue (there is no cycle)

Step4: Stop.

ALGORITHM 2:
On a copy of the graph:

Step1: See if any Processes NEEDS can all be satisfied.

Step2: If so satisfy the needs with holds and remove that Process and all the Resources it
holds from the graph.

Step3: If any Process are left Repeat step a

Step4: If all Processes are finally removed by this procedure there is no Deadlock in the original
graph, if not there is.

Step5: Stop.

56
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algorithm ************\n");
input();
show();
cal();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++) scanf("%d",&max[i]
[j]);
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++) scanf("%d",&alloc[i]
[j]);
}

57
21IT1302-OPERATING SYSTEM 211422205021

printf("Enter the available Resources\n");


for(j=0;j<r;j++)
scanf("%d",&avail[j]);
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
printf("%d ",alloc[i][j]);
printf("\t"); for(j=0;j<r;j+
+) printf("%d ",max[i]
[j]); printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
finish[i]=0;
for(i=0;i<n;i++)
{
for(j=0;j<r;j++) need[i]
[j]=max[i][j]-alloc[i][j];
}
while(flag)

58
21IT1302-OPERATING SYSTEM 211422205021

{
flag=0; for(i=0;i<n;i+
+)
{
int c=0; for(j=0;j<r;j+
+)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{ c+
+;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("p%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
j=0;
flag=0; for(i=0;i<n;i+
+)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}

59
21IT1302-OPERATING SYSTEM 211422205021

}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
printf("P%d\t",dead[i]);
}
else
{
printf("\nNo Deadlock Occur");
}
}

60
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

61
21IT1302-OPERATING SYSTEM 211422205021

RESULT:
Thus the Deadlock Detection and Avoidance using Bankes Algorithm was
executed successfully.

62
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:07
DATE:

Implementation of Memory Allocation Strategies

AIM:

To Simulate a memory management strategies.

ALGORITHM:
1. Start the program.

2. Declare the size.

3. Get the number of processes to be inserted.

4. Start at the beginning of the set of holes.

5. If not, start at the hole that is sharing the previous best fit search end.

6. Allocate the best hole that is small enough searching.

7. If small enough, then stop searching in the procedure.

8. Display the values.

9. Stop the program.

PROGRAM:

63
21IT1302-OPERATING SYSTEM 211422205021

#include <stdio.h>

int main() {
int p[10], np, b[10], nb, ch, c[10], d[10], alloc[10], flag[10] = {0}, i, j;

printf("Enter the number of processes: ");


scanf("%d", &np);

printf("Enter the number of blocks: ");


scanf("%d", &nb);

printf("Enter the size of each process:\n"); for


(i = 0; i < np; i++) {
printf("Process %d: ", i);
scanf("%d", &p[i]);
}

printf("Enter the block sizes:\n");


for (j = 0; j < nb; j++) {
printf("Block %d: ", j);
scanf("%d", &b[j]);
c[j] = b[j];
d[j] = b[j];
}

if (np <= nb) {


printf("1. First fit\n2. Best fit\n3. Worst fit\n");

do {
printf("Enter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1:
printf("\nFirst Fit\n");
for (i = 0; i < np; i++) {
for (j = 0; j < nb; j++) {
if (p[i] <= b[j] && flag[j] == 0) {
alloc[j] = p[i];
printf("\nAlloc[%d]: %d", j, alloc[j]);
printf("Process %d of size %d is allocated in block %d of size %d\n", i,
p[i], j, b[j]);
flag[j] = 1;

64
21IT1302-OPERATING SYSTEM 211422205021

break;
}
}
if (flag[i] == 0) {
printf("Process %d of size %d is not allocated\n", i, p[i]);
}
}
break;

case 2:
printf("\nBest Fit\n");
for (i = 0; i < nb; i++) {
for (j = i + 1; j < nb; j++) {
if (c[i] > c[j]) {
int temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
printf("After sorting block sizes:\n");
for (i = 0; i < nb; i++) {
printf("Block %d: %d\n", i, c[i]);
}
for (i = 0; i < np; i++) {
for (j = 0; j < nb; j++) {
if (p[i] <= c[j] && flag[j] == 0)
{ alloc[j] = p[i];
printf("\nAlloc[%d]: %d", j, alloc[j]);
printf("Process %d of size %d is allocated in block %d of size %d\n", i,
p[i], j, c[j]);
flag[j] = 1;
break;
}
}
if (flag[i] == 0) {
printf("Process %d of size %d is not allocated\n", i, p[i]);
}
}
break;

case 3:

65
21IT1302-OPERATING SYSTEM 211422205021

printf("\nWorst Fit\n");
for (i = 0; i < nb; i++) {
for (j = i + 1; j < nb; j++) {
if (d[i] < d[j]) {
int temp = d[i];
d[i] = d[j];
d[j] = temp;
}
}
}
printf("After sorting block sizes:\n");
for (i = 0; i < nb; i++) {
printf("Block %d: %d\n", i, d[i]);
}
for (i = 0; i < np; i++) {
for (j = 0; j < nb; j++) {
if (p[i] <= d[j] && flag[j] == 0) {
alloc[j] = p[i];
printf("\nAlloc[%d]: %d", j, alloc[j]);
printf("Process %d of size %d is allocated in block %d of size %d\n", i,
p[i], j, d[j]);
flag[j] = 1;
break;
}
}
if (flag[i] == 0) {
printf("Process %d of size %d is not allocated\n", i, p[i]);
}
}
break;

default:
printf("Invalid Choice...!\n");
break;
}
} while (ch <= 3);
} else {
printf("Number of processes exceeds the number of blocks. Unable to allocate.\n");
}

return 0;
}

66
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

67
21IT1302-OPERATING SYSTEM 211422205021

RESULT:
Thus the implementation of Memory Allocation Strategies for First Fit,Best Fit,Worst Fit
programs was successfully executed

68
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:08
DATE:

Implementation of Paging Techniques

AIM:

To implementation memory allocation with pages.

ALGORITHM:
Step1: Read all the necessary input from the keyboard.

Step2: Pages Logical memory is broken into fixed-sized blocks.

Step3: Frames - Physical memory is broken into fixed-sized blocks.

Step4: Calculate the physical address using the following Physical address = ( Frame number *
Frame size) + offset

Step5: Display the physical address.

Step6: Stop the process.

69
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
#include<stdio.h>
int main()
{
int ms, ps, nop, np, rempages, i, j, x, y
, pa, offset;
int s[10], fno[10][20]; printf("\
nEnter the memory size --");
scanf("%d",&ms);
printf("\nEnter the page size --");
scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are --%d ",nop);
printf("\nEnter number of processes --");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
printf("\nEnter no. of pages required for p[%d]--",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full");
break;
}
rempages = rempages -s[i]; printf("\
nEnter pagetable for p[%d] ---",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("\nEnter Logical Address to find Physical Address "); printf("\
nEnter process no. and pagenumber and offset --"); scanf("%d %d
%d",&x,&y, &offset);
if(x>np || y>=s[i] || offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[x][y]*ps+offset;

70
21IT1302-OPERATING SYSTEM 211422205021

printf("\nThe Physical Address is --%d",pa);


}
return (0);
}

OUTPUT:

RESULT:
Thus the implementation of Paging Techniques programs was successfully executed.

71
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:09

DATE:

Page Replacement Algorithm

EX.NO:09)i)
FIFO page replacement algorithm

AIM:

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

ALGORITHM:
Step1:Start the process

Step2:Read number of pages n

Step3: Read number of pages no

Step4:Read page numbers into an array a[i]

Step5:Initialize avail[i]=0 .to check page hit

Step6: Replace the page with circular queue, while re-placeing check page availability in the
frame

Place avail[i]=1 if page is placed in the frame Count page faults.

Step7: Print the results.

Step8: Stop the process

72
21IT1302-OPERATING SYSTEM 211422205021

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:

73
21IT1302-OPERATING SYSTEM 211422205021

74
21IT1302-OPERATING SYSTEM 211422205021

75
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:09)ii)
LRU page replacement algorithm

AIM:

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

ALGORITHM:
Step1:Start the process

Step2: Declare the size

Step3: Get the number of pages to be inserted

Step4:Get the value

Step5: Declare counter and stack

Step6:Select the least recently used page by counter value

Step7:Stack them according the selection.

Step8:Display the values

step9. Stop the process

76
21IT1302-OPERATING SYSTEM 211422205021

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

77
21IT1302-OPERATING SYSTEM 211422205021

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

78
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

79
21IT1302-OPERATING SYSTEM 211422205021

80
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:09)iii)
LFU page replacement algorithm

AIM:

To write a C program to implement LFU page replacement algorithm..

ALGORITHM:
Step1:Start Program

Step2:Read Number Of Pages And Frames

Step3:Read Each Page Value

Step4:Search For Page In The Frames

Step5:If Not Available Allocate Free

Frame

Stepб:If No Frames Is Free Replace The Page With The Page that is least Used

Step7:Print Page Number Of Page Faults

Step8:Stop process.

PROGRAM:
#include<stdio.h>
int n;
main()
{
int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s,pf=0;
int count=1,p=0;
float pfr;
printf("enter max limit of the sequence:");
scanf("%d",&max);
printf("enter the sequence:");
for(i=0;i<max;i++)
scanf("%d",&seq[i]);
printf("enter the no of frames:");
81
21IT1302-OPERATING SYSTEM 211422205021

scanf("%d",&n);
fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j])
flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%d\t",fr[count] );
count++;
pf++;
} i+
+;
}
printf("\n");
for(i=p;i<max;i++)
{
flag=1; for(j=0;j<n;j+
+)
{
if(seq[i]==fr[j])
flag=0;
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)

82
21IT1302-OPERATING SYSTEM 211422205021

{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
pos[j]=-1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==-1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==-1)
{
s=k;
break;
}
}
}
pf++;
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\n theno of page faults are:%d",pf);
printf("\n page fault rate:%f",pfr);

83
21IT1302-OPERATING SYSTEM 211422205021

}
int findmax(int a[])
{
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
k=i;
}
}
return k;
}
OUTPUT:

84
21IT1302-OPERATING SYSTEM 211422205021

RESULT:
Thus the implementation of Paging Techniques programs was successfully executed.

85
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:10)i)
DATE:

implementation of File Organization Techniques

AIM:

To implement single level directory structure in C Step1:Start the process

ALGORITHM:
Step 1:Start

Step 2: Initialize values gd DETECT,gm,count,i,j,mid,cir_x; Initialize character


array ame[10][20];

Step 3: Initialize graph function as Initgraph(& gd, &gm," c:/tc/bgi"), Clear device():

Step 4:set back ground color with setbkcolor();

Step 5:read number of files in variable

count. Step 6:if check i<count

Step 7: for i=0 & i<count i increment;


Cleardevice(); setbkcolor(GREEN); read file name;
setfillstyle(1,MAGENTA);

Step 8: mid=640/count;

cir x-mid/3; bar3d(270,100,370,150,0,0); settextstyle(2,0,4); senextstylet 1.1k


outtextxy(320,125,"rootdirectory"); setcolor(BLUE);

Step 9:for j=0&&j<=1&&cir_x+-mid j increment; line(320,150,cir_x,250);

fillellipse(cir_x.250,30,30); outtextxy(cir_x,250, fname[i]);

Step 10: End

PROGRAM:
#include<stdio.h>

86
21IT1302-OPERATING SYSTEM 211422205021

#include<stdlib.h>
#include<string.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
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);

87
21IT1302-OPERATING SYSTEM 211422205021

else
dir.fcnt--;
break;
case 3: 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 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;
case 5: exit(0);
break;
default:
printf("\nterminate");
}
}
}

88
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

89
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:10)ii)
Two Level Directory Structure

AIM:

To implement two level directory structure in C.

ALGORITHM:
Step 1:Start

Step 2:Initialize structure elements

struct tree_element{ char name[20];

Initialize integer variables x, y, ftype, lx, rx, nc, level;

struct tree_element *link[5];} typedef structure tree_element node;

Step 3:start main function

Step 4: Step variables gd-DETECT.gm; node

root; root-NULL;

Step 5:create structure using create(&root,0,"null",0,639,320);

Step 6:initgraph(&gd, &gm,"c:\te\bgi"); display(root);


closegraph();

Step 7:end main function

Step 8:Initialize variables i.gap:

Step 9:if check root-NULL (root)-

(node*)malloc(sizeof(node)); enter name of ir file name in

dname; fflush(stdin):
gets((root)->name);

Step 10 if check lev=Olev 1 (root)->ftype=1; else("root)->ftype=2; (root)->level-lev; (root)->y


50+lev 5; (*root)->x=x;
90
21IT1302-OPERATING SYSTEM 211422205021

(root)->Ix-lx: (root)->rx-rx; Step 11:for i=0&&i<5 increment

i (root)->link[i] NULL; if check (root)->ftype=1

Step 12: if check (lev=0 lev-1) if check(root)->level=0 print "how many users else print "how
many files" print (root)->name

read (root)->nc

Step 13:Then (root)->nc-0; if check(root)->nc-0 gap-rx-lx;

else gap (rx-lx)/("root)->nc;

Step 14:for i=0&&i<(root)->nc increment i;

create(&((root)->link[i]),lev+1,(root)->name,

Ix+gap*i,lx+gapi gap,lx+gap*i+gap/2),

then

(root)->nc 0;

Step 15: Initialize e display function Initialize I set textstyle(2,0.4); set textjustify(1,1); set
fillstyle(1,BLUE); setcolor(14); step 13:if check root!=NULL

Step 16:for i=0&&i<root->nc increment i

line(root->x,root->y.root->link[i]->x,root-link[i]->y); Step 17: if check root->ftype=-1


bar3d(root-x-20,root->y-10,root-x+20,root->y+10,0,0); else fill ellipse(root->x,root-
>y,20,20); out textxy(root-x,root->y.root->name);

Step 18 for i=0&&i<root->ne increment i display(root->link[i]),

Step 19:End

PROGRAM:
#include<stdio.h>
struct
{

91
21IT1302-OPERATING SYSTEM 211422205021

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

92
21IT1302-OPERATING SYSTEM 211422205021

for(i=0;i<dcnt;i++)
{
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++)
{
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;
}
}

93
21IT1302-OPERATING SYSTEM 211422205021

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);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
getch();
}

OUTPUT:

94
21IT1302-OPERATING SYSTEM 211422205021

95
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:10)iii)

Hierarchical Directory Structure


AIM:

To implement Hierarchical Directory Structure in C.

ALGORITHM:
Step 1:Start

Step 2: define structure and declare structure variables

Step 3: start main and declare variables


Node root Root = NULL

Step 4: create root null Initgraph &gd,&gm Display root

Step 5:create a directory tree structure If check *root-NULL


Display dir/file mane

Step 6: gets root->name*root-> level-lev *root->y 50+lev 50 *root->x=x *root->Ix-Ix root->rx


= rx

Step7: for i=0 to i<5Root->link[i]=NULL Display sub dir/ files

Step8: if check *root->nc==0 Gap=rx-lx

Then
Gap=rx-lx/*root->nc

Step9: for i=0 to i<*root->nc

Then rot->nc-0

Step 10: display the directory tree in graphical mood Display nood *root

If check rooy !=NULL Step 11: foe i=0 to i<root->ne


Line of root->x, root->y, root->link[i]->x, root->link[i]-
y

96
21IT1302-OPERATING SYSTEM 211422205021

Step12: if check root->ftype] Bard of root->x-20, root->y-10,root-x+20,root->y+10,0 Then


Display root-link[i]

PROGRAM:
#include<stdio.h>
#include<graphics.h>

struct tree_element

char name[20];

int x,y,ftype,lx,rx,nc,level;

struct tree_element*link[5];

};

typedef struct tree_element node;

void main()

int gd=DETECT,gm;

node*root;

root=NULL;

clrscr();

create(&root,0,"null",0,639,320);

clrscr(); initgraph(&gd,&gm,"..\\

bgi"); display(root);

getch();

closegraph();

create(node **root,int lev,char *dname,int lx,int rx,int x)

97
21IT1302-OPERATING SYSTEM 211422205021

int i,gap;

if(*root==NULL)

(*root)=(node*)malloc(sizeof(node));

printf("enter name of dir/file(under%s):",dname);fflush(stdin);

gets((*root)->name);

if(lev==0||lev==1)

(*root)->ftype=1;

else

(*root)->ftype=2;

(*root)->level=lev;

(*root)->y=50+lev*50;

(*root)->x=x;

(*root)->lx=lx;

(*root)->rx=rx;

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

(*root)->link[i]=NULL;

if((*root)->ftype==1)

if(lev==0||lev==1)

if((*root)->level==0)

printf("How many users");

else

printf("How many files");

98
21IT1302-OPERATING SYSTEM 211422205021

printf("(for%s):",(*root)->name);

scanf("%d",&(*root)->nc);

else(*root)->nc=0;

if((*root)->nc==0)gap=rx-lx;

else gap=(rx-lx)/(*root)->nc;

for(i=0;i<(*root)->nc;i++)

create(&((*root)->link[i]),lev+1,(*root)-

>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);

else(*root)->nc=0;

return;

display(node *root)

int i;

settextstyle(2,0,4);

settextjustify(1,1);

setfillstyle(1,BLUE);

setcolor(14); if(root!

=NULL)

for(i=0;i<root->nc;i++)

line(root->x,root->y,root->link[i]->x,root->link[i]->y);

99
21IT1302-OPERATING SYSTEM 211422205021

if(root->ftype==1)bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);

else

fillellipse(root->x,root->y,20,20);

outtextxy(root->x,root->y,root->name);

for(i=0;i<root->nc;i++)

display(root->link[i]);

return;

OUTPUT:

100
21IT1302-OPERATING SYSTEM 211422205021

RESULT:
Thus the C programs was successfully executed.

101
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:11
DATE:

Implementation of file allocation Strategies


EX.NO:11)i)

Sequential File Allocation

AIM:

To implement Sequential File Allocation in C.

ALGORITHM:
Step 1: Start the program. Step 2: Get the number of files. Step 3: Get the memory requirement
of each file.

Step 4: Allocate the required locations to each in sequential order a) Randomly select a location
from available location

s1= random(100); b) Check whether the required locations are free from the selected location.

if(b[sl] flag-0)

if((b[j].flag)==0) count ! 1; } if(count p[i]) break;

c) Allocate and set flag-1 to the allocated locations. for(s=s1;s<(s+p[i]);s++) 1

kills: j-j+1; b[s].bno-s; b[s].flag-1;

Step 5: Print the results fileno, length Blocks allocated.

Step 6: Stop the program

PROGRAM:

102
21IT1302-OPERATING SYSTEM 211422205021

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int st[20],b[20],b1[20],ch,i,j,n,blocks[20][20],sz[20];
char F[20][20],S[20];
clrscr();
printf("\n Enter no. of Files ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);

scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter Starting block of %d::",i+1);
scanf("%d",&st[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
b1[i]=(sz[i]*1024)/b[i];
for(i=0;i<n;i++)
{
for(j=0;j<b1[i];j++)
blocks[i][j]=st[i]+j;
}
do
{
printf("\nEnter the Filename ::");
scanf("%s",S);
for(i=0;i<n;i++)
{
if(strcmp(S,F[i])==0)

103
21IT1302-OPERATING SYSTEM 211422205021

{
printf("\nFname\tStart\tNblocks\tBlocks\n");
printf("\n-------------------------------------------\n");
printf("\n%s\t%d\t%d\t",F[i],st[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}

}
printf("\n----------------------------------------------\n");
printf("\nDo U want to continue ::(Y:n)");
scanf("%d",&ch);
if(ch!=1)
break;
}while(1);
}

OUTPUT:

104
21IT1302-OPERATING SYSTEM 211422205021

105
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:11)ii)
Index File Allocation

AIM:

To implement indexed file allocation technique in C.

ALGORITHM:
Step 1: Start the program.

Step 2: Get the number of files.

Step 3: Get the memory requirement of each file. Step 4: Allocate the required locations by
selecting a location randomly q= random(100),

a) Check whether the selected location is free.

b) If the location is free allocate and set flag-1 to the allocated

locations. q=random(100);

if(blal flag-0) b[q] flag-1; b[q] fno-j: [i][i]";

Step 5: Print the results fileno. length Blocks allocated.

Step 6: Stop the program

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n;
void main()
{
int b[20],b1[20],i,j,blocks[20][20],sz[20];
char F[20][20],S[20],ch;
clrscr();
printf("\n Enter no. of Files ::");

106
21IT1302-OPERATING SYSTEM 211422205021

scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::",i+1);
scanf("%s",&F[i]);
printf("\n Enter file%d size(in kb)::",i+1);
scanf("%d",&sz[i]);
printf("\n Enter blocksize of File%d(in bytes)::",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
b1[i]=(sz[i]*1024)/b[i];
printf("\n\nEnter blocks for file%d",i+1);
for(j=0;j<b1[i];j++)
{
printf("\n Enter the %dblock ::",j+1);
scanf("%d",&blocks[i][j]);
}}
do
{
printf("\nEnter the Filename ::");
scanf("%s",&S);
for(i=0;i<n;i++)
{
if(strcmp(F[i],S)==0)
{
printf("\nFname\tFsize\tBsize\tNblocks\tBlocks\n")
;
printf("\n----------------------------------------------\n");
printf("\n%s\t%d\t%d\t%d\t",F[i],sz[i],b[i],b1[i]);
for(j=0;j<b1[i];j++)
printf("%d->",blocks[i][j]);
}}
printf("\n----------------------------------------------\n");
printf("\nDo U want to continue ::(Y:n)");

107
21IT1302-OPERATING SYSTEM 211422205021

scanf("%d",&ch);
}while(ch!=0);
}

OUTPUT:

108
21IT1302-OPERATING SYSTEM 211422205021

109
21IT1302-OPERATING SYSTEM 211422205021

EX.NO:11)iii)
Linked File Allocation.

AIM:

To allocate the files in the secondary storage using Linked allocation techniques

ALGORITHM:
Step 1: Start the program.

Step 2: Get the number of files.

Step 3: Get the memory requirement of each file.

Step 4: Allocate the required locations by selecting a location randomly q random(100),

a) Check whether the selected location is free.

b) If the location is free allocate and set flag-1 to the allocated locations. While allocating
next location address to attach it to previous location

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

for(j=0;j<s[i];j++) { q=random(100); if(b[q].flag==0) b[q].flag=1; b[q].fno-j; r[i][j]=q; if(j>0)

{ p=r[i][j-1]; b[p].next=q; }

Step 5: Print the results fileno, length,Blocks allocated.

Step 6: Stop the program

110
21IT1302-OPERATING SYSTEM 211422205021

PROGRAM:
#include<stdio.h>
void main()
{
char a[10];
int i,sb,eb,fb1[10];
printf("\n enter the file name:");
scanf("%s",a);
printf("\n Enter the starting block:");
scanf("%d",&sb);
printf("Enter the ending Block:");
scanf("%d",&eb);
for(i=0;i<5;i++)
{
printf("Enter the free block %d",i+1);
scanf("%d",&fb1[i]);
}
printf("\n File name \t Starting block \t Ending block \n");
printf("%s \t\t %d\t\t %d",a,sb,eb);
printf("\n %s File Utilization of Linked type of following blocks:",a);
printf("\n %d->",sb);
for(i=0;i<5;i++)
{
printf("%d->",fb1[i]);
}
printf("%d\n",eb);
}

111
21IT1302-OPERATING SYSTEM 211422205021

OUTPUT:

RESULT:
Thus the implementation of Paging Techniques programs was successfully executed.

112
21IT1302-OPERATING SYSTEM 211422205021

113

You might also like