0% found this document useful (0 votes)
13 views164 pages

Os Lab Record Final

The document provides an overview of the UNIX operating system, detailing its components such as the kernel, shell, and file management. It includes basic UNIX commands and examples of their usage, as well as explanations of processes and the directory structure. Additionally, it covers programming exercises involving fork system calls and the implementation of a Fibonacci series using child processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views164 pages

Os Lab Record Final

The document provides an overview of the UNIX operating system, detailing its components such as the kernel, shell, and file management. It includes basic UNIX commands and examples of their usage, as well as explanations of processes and the directory structure. Additionally, it covers programming exercises involving fork system calls and the implementation of a Fibonacci series using child processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 164

NAME: K.

MANIKAVALLI REGNO:411621104029

EX NO:1
BASIC UNIX COMMANDS
DATE:

INTRODUCTION TO UNIX

WHAT IS UNIX?

UNIX is an operating system which was first developed in the 1960s, and
has been under constant development ever since. By operating system, we
mean the suite of programs which make the computer work. It is a stable,
multi-user, multi-tasking system for servers, desktops and laptops.

UNIX systems also have a graphical user interface (GUI) similar to


Microsoft Windows which provides an easy to use environment. However,
knowledge of UNIX is required for operations which aren't covered by a
graphical program, or for when there is no windows interface available, for
example, in a telnet session.

TYPES OF UNIX

There are many different versions of UNIX, although they share common
similarities. The most popular varieties of UNIX are Sun Solaris,
GNU/Linux, and MacOS X.

Here in the School, we use Solaris on our servers and workstations, and
Fedora Linux on the servers and desktop PCs.

THE UNIX OPERATING SYSTEM

The UNIX operating system is made up of three parts; the kernel, the shell
and the programs.

THE KERNEL

The kernel of UNIX is the hub of the operating system: it allocates time and
memory to programs and handles the filestore and communications in
response to system calls.

As an illustration of the way that the shell and the kernel work together,
suppose a user types rm myfile (which has the effect of removing the

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

filemyfile). The shell searches the filestore for the file containing the
program rm, and then requests the kernel, through system calls, to execute
the program rm on myfile. When the process rm myfile has finished
running, the shell then returns the UNIX prompt % to the user, indicating
that it is waiting for further commands.

THE SHELL

The shell acts as an interface between the user and the kernel. When a user
logs in, the login program checks the username and password, and then
starts another program called the shell. The shell is a command line
interpreter (CLI). It interprets the commands the user types in and arranges
for them to be carried out. The commands are themselves programs: when
they terminate, the shell gives the user another prompt (% on our systems).

The adept user can customize his/her own shell, and users can use different
shells on the same machine. Staff and students in the school have the tcsh
shell by default.

The tcsh shell has certain features to help the user inputting commands.

Filename Completion - By typing part of the name of a command, filename


or directory and pressing the [Tab] key, the tcsh shell will complete the rest
of the name automatically. If the shell finds more than one name beginning
with those letters you have typed, it will beep, prompting you to type a few
more letters before pressing the tab key again.

History - The shell keeps a list of the commands you have typed in. If you
need to repeat a command, use the cursor keys to scroll up and down the list
or type history for a list of previous commands.

FILES AND PROCESSES

Everything in UNIX is either a file or a process.

A process is an executing program identified by a unique PID (process


identifier).

A file is a collection of data. They are created by users using text editors,
running compilers etc.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

Examples of files:

 a document (report, essay etc.)


 the text of a program written in some high-level programming language
 instructions comprehensible directly to the machine and incomprehensi-
ble to a casual user, for example, a collection of binary digits (an exe-
cutable or binary file);
 a directory, containing information about its contents, which may be a
mixture of other directories (subdirectories) and ordinary files.

UNIX ARCHITECTURE

The main concept that unites all versions of UNIX is the following four basics

 Kernel: The kernel is the heart of the operating system. It interacts with
hardware and most of the tasks like memory management, tash schedul-
ing and file management.

 Shell: The shell is the utility that processes your requests. When you
type in a command at your terminal, the shell interprets the command
and calls the program that you want. The shell uses standard syntax for

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

all commands. C Shell, Bourne Shell and Korn Shell are most famous
shells which are available with most of the Unix variants.

 Commands and Utilities: There are various command and utilities


which you would use in your day to day activities. cp, mv,
cat and grepetc. are few examples of commands and utilities. There are
over 250 standard commands plus numerous others provided through
3rd party software.

 Files and Directories: All data in UNIX is organized into files. All files
are organized into directories. These directories are organized into a tree-
like structure called the file system.

THE DIRECTORY STRUCTURE

All the files are grouped together in the directory structure. The file-system
is arranged in a hierarchical structure, like an inverted tree. The top of the
hierarchy is traditionally called root (written as a slash / )

In the diagram above, we see that the home directory of the undergraduate
student "ee51vn" contains two sub-directories (docs and pics) and a file
called report.doc.

The full path to the


file report.doc is "/home/turing/z123456/cs241/p1.cpp"

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

UNIX PROCESS

When a program is started on UNIX, it creates what is known as a “process”


on the system. Every process is assigned a unique serial number called its
process id or PID for short. Processes can be created by any user, but can only
be destroyed by someone with the permissions to do so – usually the user that
created the process or the system administrator. This ensures that the compute
jobs you start on the system will not be disturbed by any other user of the
system until they complete or you decide to stop them yourself.

Processes and process management becomes important on UNIX systems that


are shared between a number of users. The concept of users and PIDs is the
main tool by which the available system resources are shared fairly among
everybody who needs access to them. Processes can be suspended or given
lower priority in cases where one or more users shoot step out of the way for
someone else, but wish to do so without losing their work of up to that point.

One further consideration on this topic is the fact that a running UNIX process
can spawn “child” processes. For example, any program you run from inside a
UNIX Shell will be a child process of that shell. Conversely, the shell is the
parent process of this child. Child processes have associated with them both
their own process id (PID) as well as their parent’s process id (PPID).

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

Normally this concept of parent and child processes is not something you need
to be bothered with as a user. However, it can be useful to understand how
UNIX organizes processes if you are trying to keep track of certain system
resources (e.g. memory and CPU), if you are working with environment
variables, or if you need to track down a rogue program or script.

OUTPUT:
1.Date:
[oslab@localhost~]$date

Fri jan 9 12:20:33 IST 2020

2.Cal:
[oslab@localhost~]$cal

January 2020

Su Mo Tu We Th Fr Sa

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

3.Echo:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

[oslab@localhost~]$ echo GOOD MORNING

GOOD MORNING

4.Who:
[oslab@localhost~]$ who

cse014 pts/1 2020-01-09 10:52 (192.168.1.50)

oslab pts/2 2020-01-09 10:52 (192.168.1.168)

5.Who am i:
[oslab@localhost~]$ who am i

oslab pts/2 2020-01-09 10:52 (192.168.1.168)

6.TTY:
[oslab@localhost~]$tty

/dev/pts/2

7.Man:
[oslab@localhost~]$ man date

DATE(1) User Commands DATE(1)

NAME

date - print or set the system date and time

SYNOPSIS

date [OPTION]... [+FORMAT]

date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION

Display the current time in the given FORMAT, or set the system date.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

-d, --date=STRING

-f, --file=DATEFILE

-r, --reference=FILE

-R, --rfc-2822

--rfc-3339=TIMESPEC -s, --set=STRING

set time described by STRING

-u, --utc, --universal

print or set Coordinated Universal Time

--help display this help and exit

--version

...skipping...

display time described by STRING

-f, --file=DATEFILE

like --date once for each line of DATEFILE

-r, --reference=FILE

display the last modification time of FILE

-R, --rfc-2822

output date and time in RFC 2822 format

-- output date and time in RFC 3339 format.

TIMESPEC

sec-for date and time to the indicated precision.

-s, --set=STRING

set time described by STRING

-u, --utc, --universal

print or set Coordinated Universal Time

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

--help display this help and exit

--version

Directory related commands:


1.Mkdir:
[oslab@localhost~]$mkdir int

2.Cd:
[oslab@localhost~]$cd int

3.Pwd:
[oslab@localhost~]$pwd

/home/oslab/int

4.Rmdir:
[oslab@localhost~]$rmdir int

rmdir:int: No such file or directory

5.ls:
[oslab@localhost~]$ls

Fruits

File manipulation commands


1.cat:
[oslab@localhost~]$ cat fruits

Mango is a fruit which is delicious and juicy

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

2.cp:
[oslab@localhost~]$ cp fruits mango

[oslab@localhost~]$ cat mango

Mango is a fruit which is delicious and juicy

3.Move:
[oslab@localhost~]$ mv fruits fruit

[oslab@localhost~]$ cat fruit

Mango is a fruit which is delicious and juicy

[oslab@localhost~]$ cat fruits

cat: fruits: No such file or directory

4.Remove:
[oslab@localhost~]$ rm fruits

[oslab@localhost~]$ cat fruits

cat: fruits: No such file or directory

5.Word count:
[oslab@localhost~]$wc -c mango

46 mango

[oslab@localhost~]$wc mango

1 9 46 mango

6.Sort:
[oslab@localhost~]$ sort demo

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

File comparison commands:


1.Compare:
[oslab@localhost~]$cmp demo example

demo example differ : byte 1, line 1

2.Difffer:

[oslab@localhost~]$diff demo example

>Mango is a fruit which is delicious and juicy

3.Filter:
(a) Head:

[oslab@localhost~]$head -3 example

Mango is a fruit which is delicious and juicy

A
C
(b)Tail:

[oslab@localhost~]$tail -2 example

FB

4.Numbering:
[oslab@localhost~]$nl mango

1 Mango is a fruit which is delicious and juicy


Pattern searching command:
1.GREP:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

[oslab@localhost~]$grep mango example


Mango

RESULT:

Thus, the program to study the basic unix command is executed


successfully and the output is verified.

EX NO:2a
FORK SYSTEM CALLS
DATE:

AIM:

To write a c program in linear to implement fork () system call.

ALGORITHM:

STEP 1: Start .
STEP 2: Fork is used to create a child process.
STEP 3: Fork () - a child process is created.
STEP 4: Prints“ Hello World”.
STEP 5: Stop.

PROGRAM

#include<stdio.h>

#include<unistd.h>

main()

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

fork();

printf("Hello World\n");

OUTPUT

[it1@localhost ~]$ vi ex1a.c

[it1@localhost ~]$ cc ex1a.c

[it1@localhost ~]$ ./a.out

Hello World

Hello World

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

PROGRAM

#include<stdio.h>

#include<unistd.h>

main()

fork();

fork();

fork();

printf("Hello World\n");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT

[it1@localhost ~]$ cc ex1aa.c

[it1@localhost ~]$ ./a.out

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus, the program to write a c program in linear to implement


fork () system call is executed successfully and the output is veri-
fied.

EX NO:2b SIMULATION OF FORK, GETPID AND WAIT SYSTEM


CALLS
DATE:

AIM:
To write a c program in linear to implement fork (), exec (),
wait () a system.

ALGORITHM:
STEP 1: Get the number of terms for Fibonacci series.

STEP 2: Retrieve the id of the parent process using getpid () system


call.

STEP 3: Print the id of the parent process.

STEP 4: Create a child process using fork () system call.

PROGRAM

#include<stdio.h>

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

#include<unistd.h>

#include<stdlib.h>

#include<conio.h>

main()

int a=-1,b=1,i,c=a+b,n,pid,cpid;

printf("\nenter no. of terms ");

scanf("%d",&n);

pid=getpid();

printf("\nparent process id is %d",pid);

pid=fork();

cpid=getpid();

printf("\nchild process:%d",cpid);

if(pid==0)

printf("\nchild is producing fibonacci series ");

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

c=a+b;

printf("\n%d",c);

a=b;

b=c;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\nchild ends");

else

printf("\nparent is waiting for child to complete");

wait(NULL);

printf("\nparent ends");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT

[it27@mm4 ~]$ cc fork.c

[it27@mm4 ~]$a.out

enter no. of terms 5

parent process id is 8723

child process:8723

parent process id is 8723

child process:8732

child is producing fibonacci series

child endsparent is waiting for child to complete

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus, the program in linux to implement fork (), exec (), wait ()
a system is executed successfully and the output is verified.

EX NO:2c EXECUTION OF EXECLP SYSTEM CALL

DATE:

AIM:
To write a c program in linear linux to implement execlp system
call.

ALGORITHM:
STEP 1: Start.
STEP 2: Create a child process using fork () system call.
STEP 3: If pid is less than zero then print error message “fork
failed”.
STEP 4: Else if pid is equal to zero execute execlp ()
system call to list the file using ls command.
STEP 5: Child process completes execution.
STEP 6: Stop

PROGRAM

#include<stdio.h>

#include<unistd.h>

#include<sys/wait.h>main()

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

int pid;

pid=fork();

if(pid<0){

fprintf(stderr,"fork failed\n");

exit(-1);

else if(pid==0){

execlp("/bin/ls","ls",NULL);

else{

wait(NULL);

printf("Child Complete");

exit(0);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT

[it1@localhost ~]$ cc ex1c.c

[it1@localhost ~]$ ./a.out

a1 ara.carunv ex1aa.c facts itbgalskpmatrix.vpre.c rad2.sh rad.cskwooow

a2 arain.cbaabaaa ex1a.c fib.c k1 lee obupriya rad3.sh rad.sh ss xxx

a.carav.c cat ex1b.c fio k2 lyffactzos rad1.c rad4.c sat sum.c

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus, the c program in linear linux to implement execlp system call is executed successfully and the
output is verified.

EX NO:2d SIMULATION OF OPENDIR, READDIR SYSTEM CALLS

DATE:

AIM:
To write a unix c program to stimulate opendir and readdir sys-
tem calls.

ALGORITHM:

STEP 1: Get the directory to be listed through command line


arguments.
STEP 2: Open the directory using opendir () system call.
STEP 3: Create files under the directory using cat command.
STEP 4: Read the file from the directory using readdir () system
call.
STEP 5: Print the inode number and length of this record.
STEP 6: Stop

PROGRAM

#include<stdio.h>

#include<unistd.h>

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

#include<sys/types.h>

#include<dirent.h>

main(int argc,char *argv[])

DIR *dirname;

struct dirent *dir;

dirname=opendir(argv[1]);

dir=readdir(dirname);

while(dir!=NULL)

printf("Entry found :%s\n",dir->d_name);

printf("Inode number of entry:%d\n",dir->d_ino);

printf("Length of this record:%d\n",dir->d_reclen);

getchar();

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT

[it1@localhost ~]$ cc ex1f.c


[it1@localhost ~]$mkdir anu
[it1@localhost ~]$ cd
[it1@localhost ~]$ cd anu
[it1@localhost anu]$ cat>>new
os is an interface
[it1@localhost anu]$ cat>>new1
os is resident monitor
[it1@localhost anu]$ cd
[it1@localhost ~]$ ./a.out anu
Entry found :..
Inode number of entry:26542353
Length of this record:16

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus, the program to stimulate opendir and readdir sys-


tem calls is executed successfully and the output is verified.
EX:NO:3a SIMULATION OF LS SYSTEM CALLS
DATE:

AIM:
To write a UNIX C- program to stimulate LS system call.

ALGORITHM:
STEP 1: Get the directory to be listed through command line
arguments.
STEP 2: Open the directory using open dir () function.
STEP 3: While there are files to be processed then read the file from
the directory using readdir ().
STEP 4: Print the file name.
STEP 5: Stop.

PROGRAM

#include<stdlib.h>

#include<unistd.h>

#include<sys/types.h>

#include<stdio.h>

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

#include<string.h>

#include<dirent.h>

main(int argc,char *argv[])

DIR *dp;

struct dirent *link;

dp=opendir(argv[1]);

printf("contents of directory %s are \n",argv[1]);

while((link=readdir(dp))!=0)

printf("%s",link->d_name);

closedir(dp);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT

[it27@mm4 aaa]$ cat>>aa

os[it27@mm4 aaa]$ cat>>bb

system[it27@mm4 aaa]$ cd ..

[it27@mm4 ~]$ cc ls.c

[it27@mm4 ~]$ ./a.outaaa

contents of directory aaa are

.bb..aa

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to stimulate LS system call is executed


successfully and the output is verified.
EX NO:3b SIMULATION OF GREP SYSTEM CALLS

DATE:

AIM:
To write a c program for grep system calls in c program .

ALGORITHM:
STEP 1: Declare a file pointers.
STEP 2: Get the file name.
STEP 3: Get the pattern.
STEP 4: Open the file using fopen () system call.
STEP 5: Find the occurrence of the pattern using strstr () function
in the file.
STEP 6: Print the pattern containing statements.
STEP 7: Stop.

PROGRAM

#include<unistd.h>

#include<string.h>

main()

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

char fn[10],pat[10],temp[1000];

FILE *fp;

printf(" enter file name:");

scanf("%s",fn);

printf("\n enter the pattern::");

scanf("%s",pat);

fp=fopen(fn,"r");

while(!feof(fp)){

fgets(temp,1000,fp);

if(strstr(temp,pat)){

printf("%s",temp);

}}

fclose(fp);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT

[it27@mm4 ~]$a.out

enter file name:fork.c

enter the pattern::include

#include<stdio.h>

#include<unistd.h>

#include<stdlib.h>

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for grep system calls in c program is executed


successfully and the output is verified.

EX NO:3c SIMULATION OF I/O SYSTEM CALLSPROGRAM

DATE:

AIM:
To write a c program for system call for I/O system using linux.

ALGORITHM:
STEP 1: Get the file name
STEP 2: Open the file using open () system call in read only mode.
STEP 3: Read the content of the file using read() system call.
STEP 4: Read from the file descriptor in to the buffer using
read() system call.
STEP 5: Print the content in the buffer / file.
STEP 6: Stop

PROGRAM

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

#include<stdio.h>

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

int main()

int fd;

char buf1[100],fname[30];

printf("Enter the filename:");

scanf("%s",fname);

fd=open(fname,O_RDONLY);

read(fd,buf1,30);

printf("\n The content is %s:",buf1);

close(fd);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT

[it3@localhost ~]$ cc io.c

[it3@localhost ~]$ ./a.out

Enter the filename:f1

The content is : hi

Hello

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the c program for system call for I/O system using
Linux is executed successfully and the output is verified.

EX NO:4a SHELL PROGRAM -GREATEST OF THREE


NUMBERS
DATE:

AIM:
To write a program to find largest of three numbers.

ALGORITHM:
STEP1: Get the value of a , b and c.
STEP 2: Compare if a is greater than both b and c.
STEP 3: If true then print a is big.
STEP 4: Otherwise compare b is greater than c.
STEP 5: If condition is true then print b is big else print c is big.
STEP 6: Stop.

PROGRAM:
echo "Enter three numbers"

read a b c

if [ $a -gt $b -a $a -gt $c ]

then

echo "A is big"

elif [ $b -gt $c ]

then

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

echo "B is big"

else

echo "C is big"

fi

OUTPUT:
[oslab@localhost~]$sh large3n.sh

Enter three numbers

873

A is big

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus, the program to find greatest of three numbers is executed


successfully and the output is verified.

EX NO:4b SHELL PROGRAM TO ADD, SUB, MUL, DIV


DATE:

AIM:
To write a program to perform arithmetic operations.

ALGORITHM:
STEP1: Get the values of a and b.
STEP 2: Get the option.
STEP 3: If the option is add then perform addition operation.
STEP 4: If option is subtract then do a-b.
STEP 5: If option is multiplication then do a*b.
STEP 6: If option is division then do a /b.
STEP 7: Otherwise print invalid choice.
STEP 8: Stop.
PROGRAM:
echo Enter two numbers

read a b

echo 1.ADD 2.SUB 3.MUL 4.DIV 5.EXIT

read op

case $op in

1) echo addition=` expr $a + $b `;;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

2) echo subtraction=` expr $a - $b `;;

3) echo multiplication=` expr $a \* $b `;;

4) echo division=` expr $a / $b `;;

5) break;;

*) echo invalid choice

esac

OUTPUT:
[oslab@localhost~]$sh airthmetic.sh

Enter two numbers 10 20

1.ADD 2.SUB 3.MUL 4.DIV 5.EXIT

addition=30

[oslab@localhost~]$sh airthmetic.sh

Enter two numbers 20 10

1.ADD 2.SUB 3.MUL 4.DIV 5.EXIT

subtration=10

[oslab@localhost~]$sh airthmetic.sh

Enter two numbers 5 2

1.ADD 2.SUB 3.MUL 4.DIV 5.EXIT

multiplication=10

[oslab@localhost~]$sh airthmetic.sh

Enter two numbers 16 2

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

1.ADD 2.SUB 3.MUL 4.DIV 5.EXIT

division=8

RESULT:

Thus the program to perform shell program add, sub, mul, div
using case is executed successfully and the output is verified.
EX NO:4c SHELL PROGRAM- FACTORIAL OF A NUMBER

DATE:

AIM:
To write a c program for factorial of the number.

ALGORITHM:

STEP 1: Get the input number.


STEP 2: Initialize the variables i and fact to one.
STEP 3: Check the condition and print the factorial of the given
number.
STEP 4: Stop.

PROGRAM:
echo "Enter a number"

read num

i=1

fact=1

while [ $i -le $num ]

do

fact=` echo $fact \* $i|bc `

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

i=` expr $i + 1 `

done

echo "Factorial value of $num is $fact"

OUTPUT:
[oslab@localhost~]$sh fact.sh

Enter a number

Factorial value of 5 is 120

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the c program for factorial of a number is executed successfully and the output is verified.

EX NO:4d SHELL PROGRAM- CALCULATE GROSS SALARY


DATE:

AIM:

To write a program for the gross salary.

ALGORITHM:
STEP 1: Get the input basic salary.
STEP 2: Calculate DA and HRA.
STEP 3: Calculate gross salary.
STEP 4: Print the output.

PROGRAM:
echo enter the basic salary

read basic

da=` echo $basic \* 20 / 100 | bc `

hra=` echo $basic \* 10 / 10 | bc `

gs=` echo $basic + $da + $hra | tee a | bc `

echo gross salary= Rs $gs

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:
[oslab@localhost~]$sh gross.sh

enter the basic salary

5000

gross salary= Rs 11000

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for the gross salary is executed successfully and the output is verifie.

EX NO:4eSHELL PROGRAM- SUM OF DIGITS


DATE:

AIM:
To write a program to find sum of the digits.

ALGORITHM:
STEP 1: Get the input number.
STEP 2: Initialise the variable sum to zero.
STEP 3: Check the number is greater than zero.
STEP 4: Take the individual digits and add it
STEP 5: Print the output.

PROGRAM:
echo "Enter the number"

read num

sum=0

while [ $num -gt0 ]

do

d=` expr $num % 10 `

num=` expr $num / 10 `

sum=` expr $sum + $d `

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

done

echo "Sum of Digits is $sum"

OUTPUT:
[oslab@localhost~]$sh sum.sh

Enter the number

143

Sum of Digits is 8

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to find sum of the digits is executed


successfully and the output is verified.

EX NO:5a
FIRST COME FIRSTSERVESCHEDULING
ALGORITHM
DATE:

AIM:
To implement the first come first serve scheduling algorithm us-
ing c program in LINUX.

ALGORITHM:

STEP 1: Get the number of process, along with their arrival time
and
burst time.
STEP 2: Sort the process in the increasing order of the arrival time.
STEP 3: For all the process calculate the waiting time and turn
around time.
STEP 4: Calculate the average of both waiting time and turn around
time.
STEP 5: Display the process number , arrival time , burst time, turn
around time, waiting time , average turn around time and
average waiting time.
STEP 6: Display the gantt chart.
STEP 7: Stop.

PROGRAM:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

#include<stdio.h>

#include<stdlib.h>

main()

int n,a[10],b[10],t[10],w[10],g[10],i;

float att=0,awt=0;

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

a[i]=0;

b[i]=0;

w[i]=0;

g[i]=0;

printf("\n Enter the number of process");

scanf("%d",&n);

printf("\n Enter the burst time");

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

scanf("%d",&b[i]);

printf("\n Enter the arrival time");

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

scanf("%d",&a[i]);

g[0]=0;

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

g[i+1]=g[i]+b[i];

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

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

w[i]=g[i]-a[i];

t[i]=g[i+1]-a[i];

awt=awt+w[i];

att=att+t[i];

awt=awt/n;

att=att/n;

printf("\n\t Process\tWaiting time\tTurn around time");

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

printf("\n\t\tp%d\t\t%d\t\t%d",i+1,w[i],t[i]);

printf("\nAverage waiting time %f",awt);

printf("\nAverageturn around time %f",att);

printf("\n_____________________________________________________\n");

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

printf("\tp%d\t",i+1);

printf("\t\n");

printf("\n_____________________________________________________\n");

printf("\n");

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

printf("\n");

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

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\t%d\t",g[i]);

printf("\n_____________________________________________________\n");

printf("\n");

OUTPUT:
[oslab@localhost~]$ cc fcfs.c

[oslab@localhost~]$ ./a.out

Enter the number of process

Enter the burst time

Enter the arrival time

Process Waiting time Turn around time

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

p1 0 3

p2 3 5

p3 5 9

p4 9 10

Average waiting time 4.250000

Average turn around time 6.750000

GANTT CHART:

_____________________________

p1 | p2 | p3 | p4 |

_____________________________

0 3 5 9 10

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to implement the first come first serve sched-
uling algorithm is executed successfully and the output is verified.

EX NO:5b SHORTEST JOB FIRST SCHEDULING


ALGORITHM
DATE:

AIM:
To implement the shortest job first scheduling algorithm using c -
program.

ALGORITHM:
STEP 1: Get the number of process with their arrival time and burst
time.
STEP 2: Check if all the process arrival time is equal, if true then
sort the process in the increasing order of the burst time
STEP 3: Calculate waiting time and turnaround time for each
process.
STEP 4: Otherwise schedule the process which arrived first.
STEP 5: Check every millisecond whether there is any new
process with shorter burst time.
STEP 6: Then schedule the new process and reduce the burst time
of previous by its executed milliseconds.
STEP 7: Calculate waiting and turnaround time.
STEP 8: Calculate average waiting time and average turnaround
time.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

STEP 9: Display the output.

PROGRAM:
#include<stdio.h>

main()

int n,i,j,temp,p[10],b[10],bu[10],wt[10],tt[10];

float sum=0.0,twt=0.0;

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

b[i]=p[i]=bu[i]=wt[i]=tt[i]=0;

printf("\nEnter the number of process");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\nEnter the burst time for process P%d",i);

scanf("\n%d",&bu[i]);

for(i=1;i<=n;i++)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

b[i]=bu[i];

p[i]=i;

for(i=n;i>=1;i--)

for(j=2;j<=n;j++)

if(b[j-1]>b[j])

temp=b[j-1];

b[j-1]=b[j];

b[j]=temp;

temp=p[j-1];

p[j-1]=p[j];

p[j]=temp;

wt[1]=0;

for(i=2;i<=n;i++)

wt[i]=b[i-1]+wt[i-1];

for(i=1;i<=n;i++)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

twt=twt+wt[i];

tt[i]=b[i]+wt[i];

sum=sum+tt[i];

twt=twt/n;

sum=sum/n;

printf("\nProcess\tBtime\tWT\tTT\n");

for(i=1;i<=n;i++)

printf("\nP%d\t%d\t%d\t%d\n",p[i],b[i],wt[i],tt[i]);

printf("\nAVG waiting time:%f\n",twt);

printf("\nAVGturn around time:%f\n",sum);

printf("\n___________________________________________________\n");

for(i=1;i<=n;i++)

printf("\tP%d\t|",p[i]);

printf("\n");

printf("_____________________________________________________\n");

printf("\n");

for(i=1;i<=n;i++)

printf("%d\t\t",wt[i]);

printf("%d",tt[n]);

printf("\n");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:
[oslab@localhost~]$ cc sjfs.c

[oslab@localhost~]$ ./a.out

Enter the number of process

Enter the burst time for process P1

Enter the burst time for process P2

Enter the burst time for process P3

Enter the burst time for process P4

Process Btime WT TT

P3 1 0 1

P1 4 1 5

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

P4 5 5 10

P2 6 10 16

AVG waiting time: 4.000000

AVG turn around time: 8.000000

GANTT CHART:

____________________________________

P3 | P1 | P4 | P2
____________________________________

0 1 5 10 16

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to implement the shortest job first scheduling


algorithm is executed successfully and the output is verified.
EX NO:5c
PRIORITY SCHEDULING
ALGORITHM
DATE:

AIM:
To implement the priority scheduling and dislay the gantt
chart the priority used also completes average waiting time and
average turn around time.

ALGORITHM:
STEP 1: Get the no of processes with their arrival time , burst time
and priority.
STEP 2: If all the process arrival time is equal then sort the
process in increasing order of priority.
STEP 3: For each process calculate waiting time and turn around
time.
STEP 4: If process have different arrival time the schedule the
process which arrived first.
STEP 5: Check every milliseconds whether there is any new
process with higher priority.
STEP 6: Schedule the new process in the CPU and reduce the
burst time of previous process by its executed
milliseconds .

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

STEP 7: Calculate waiting time and turn around time.


STEP 8: Calculate the average waiting time and average turn around
time.
STEP 9: Display the output.

PROGRAM:
#include<stdio.h>

int main()

int n,i,j,temp,temp1,temp2,b[10],t[10],w[10],p[10],pr[10];

float att=0.0,awt=0.0;

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

b[i]=0;

w[i]=0;

printf("\nEnter the no. of process");

scanf("%d",&n);

printf("\nEnter the burst time");

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

scanf("%d",&b[i]);

p[i]=i+1;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\nEnter the priority ");

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

scanf("%d",&pr[i]);

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

for(j=i;j<n;j++)

if(pr[i]>pr[j])

temp=b[i];

temp1=p[i];

temp2=pr[i];

b[i]=b[j];

p[i]=p[j];

pr[i]=pr[j];

b[j]=temp;

p[j]=temp1;

pr[j]=temp2;

}}}

w[0]=0;

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

w[i+1]=w[i]+b[i];

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

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

t[i]=w[i]+b[i];

awt=awt+w[i];

att=att+t[i];

awt=awt/n;

att=att/n;

printf("\n\tProcess\tWaitingTIme\tTurn around time\t\n");

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

printf("\n\tP%d\t\t%d\t\t%d\n",p[i],w[i],t[i]);

printf("AVG WT is %f",awt);

printf("\nAVGTTis %f",att);

printf("\n\nGANTT Chart");

printf("\n_________________________________________\n");

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

printf("\tP%d\t",p[i]);

printf("\t\n");

printf("\n___________________________________________\n");

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

printf("%d\t\t",w[i]);

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

printf("\n___________________________________________\n");

printf("\n");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:
[cse023@localhost ~]$ cc priority.c

[cse023@localhost ~]$ ./a.out

Enter the no. of process 4

Enter the burst time

Enter the priority

Process WaitingTImeTurn around time

P4 0 2

P2 2 6

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

P1 6 9

P3 9 10

AVG WT is 4.250000

AVG TTis 6.750000

GANTT Chart

____________________________________

P4 | P2 | P1 | P3

____________________________________

0 2 6 9 10

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to implement the priority scheduling and display the
Gantt chart the priority used also completes average waiting time and av-
erage turnaround time is executed successfully and the output is verified.
EX NO:5d
ROUND ROBIN SCHEDULING
ALGORITHM
DATE:

AIM:
To implement round robin scheduling to display the Gantt using
CPU burst time and arrival time.

ALGORITHM:
STEP 1: Get the no. of processes, with their time slice , arrival time
and burst time.
STEP 2: Sort the process in the increasing order of arrival time.
STEP 3: For each process check if the burst time of process is lesser
than time slice, then alter completion schedule the next process.
STEP 4: Else alter time slice expires , schedule the next process.
STEP 5: Calculate waiting time and turn around time of each
process.
STEP 6: Calculate average waiting time and turn around time.
STEP 7: Display the output.

PROGRAM:
#include<stdio.h>

main()

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

struct

int p,bt,rm,flag;

rr[10];

int slice,i,k,n,g[100],cnt=0,pn[100],to,from,sum=0;

int wait[100],turn[100 ],occ[100],tturn=0,twait=0;

float await=0.0,aturn=0.0;

printf("\nEnter the no. of process");

scanf("%d",&n);

printf("\nEnter the time slice");

scanf("%d",&slice);

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

printf("\nEnter the burst time %d:\t",i+1);

scanf("%d",&rr[i].bt);

wait[i]=turn[i]=occ[i]=0;

rr[i].flag=0;

rr[i].rm=rr[i].bt;

sum=sum+rr[i].bt;

k=i=0;

printf("\nFrom\tTO\tProcess\n");

while(k<sum)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

from=k;

if(rr[i].rm>0)

if(rr[i].rm<slice)

to=from+rr[i].rm;

else

to=from+slice;

rr[i].rm=rr[i].rm-(to-from);

k=to;

printf("\n%d\t%d\t%d\n",from,to,i+1);

if((rr[i].rm==0)&&(rr[i].flag==0))

rr[i].flag=1;

wait[i]=from-occ[i];

turn[i]=to;

else

occ[i]=occ[i]+(to-from);

i++;

if(i==n)

i=0;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\n\tProcess\tBurst_time\tWaiting_time\tTurn_around_time");

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

printf("\n\tP%d\t\t%d\t\t%d\t\t%d",i+1,rr[i].bt,wait[i],turn[i]);

twait=twait+wait[i];

tturn=tturn+turn[i];

printf("\n");

printf("\n_____________________________________________________\n");

await=(float)twait/n;

aturn=(float)tturn/n;

printf("\nTotal waiting time=%d\nTotalturn around time=%d",twait,tturn);

printf("\nAverage waiting time=%5.2f",await);

printf("\nAverageturn around time=%5.2f",aturn);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:
[oslab@localhost~]$ cc roundrobin.c

[oslab@localhost~]$ ./a.out

Enter the no. of process 4

Enter the time slice 4

Enter the burst time for P1: 6

Enter the burst time for P2: 2

Enter the burst time for P3: 5

Enter the burst time for P4: 8

From TO Process

0 4 1

4 6 2

6 10 3

10 14 4

14 16 1

16 17 3

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

17 21 4

Process Burst_timeWaiting_timeTurn_around_time

P1 6 10 16

P2 2 4 6

P3 5 12 17

P4 8 13 21

Total waiting time=39

Total turn around time=60

Average waiting time= 9.75

Average turn around time=15.00

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to implement round robin scheduling algorithm is


executed successfully and the output is verified.

EX NO:6
PRODUCER CONSUMER PROBLEM USING
SEMAPHORE
DATE:

AIM:

To implement the producer consumer problem using c - prob-


lem.

ALGORITHM:
STEP 1: Initialize the semaphore mutex, buffer full
and buffer empty.
STEP 2: In producer process, it produces an item in to the buffer.
STEP 3: If buffer is empty, then check the mutex value to enter the
critical section.
STEP 4: If the mutex value is 0, allow the producer to add value in
the temporary variable to buffer.
STEP 5: In consumer process, it should wait if buffer is empty.
STEP 6: If the buffer has item then check the mutex value, if it is
zero remove item from buffer.
STEP 7: Consume the item.
STEP 8: Print the item.
STEP 9: Stop.

PROGRAM:
#include<stdio.h>

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

#defineBsize 10

void produce();

void consumer();

void producer();

inta,b,c,d;

inti,j,k;

intbuf[Bsize],consumed[10];

struct pro

intitem_no;

item[10];

int main()

do

if(j==Bsize-i)

i=0;

j=0;

produce();

consumer();

else

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

producer();

consumer();

printf("\nDo you want to continue(YES-1/NO-0)");

scanf("%d",&a);

}while(a==1);

void producer()

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

if(buf[i]!='\0')

continue;

else

b=Bsize-i;

printf("\nTotal free space is %d",b);

printf("\nDo you want to produce items(YES-1/NO-0)");

scanf("%d",&c);

if(c==1)

produce();

else

break;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

void produce()

printf("\nEnter the no. of items you want to produce");

scanf("%d",&d);

while(j<i+d)

printf("\nItem.no %d",j+1);

scanf("%d",&item[j].item_no);

j++;

for(k=0;k<Bsize;k++)

buf[k]=item[k].item_no;

printf("\nBuffer content is %d",buf[k]);

void consumer()

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

consumed[j]=buf[j];

buf[j]=buf[j-1];

printf("\nConsumed item is %d\n",consumed[j]);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

item[j].item_no=0;

j=0;

OUTPUT:
[oslab@localhost~]$ cc semaphore.c

[oslab@localhost~]$ ./a.out

Total free space is 10

Do you want to produce items(YES-1/NO-0) 1

Enter the no. of items you want to produce 3

Item.no 1 7

Item.no 2 8

Item.no 3 17

Buffer content is 7

Buffer content is 8

Buffer content is 17

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

Buffer content is 0

Total free space is 7

Do you want to produce items(YES-1/NO-0) 1

Enter the no. of items you want to produce 2

Item.no 4 12

Item.no 5 3

Buffer content is 7

Buffer content is 8

Buffer content is 17

Buffer content is 12

Buffer content is 3

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Total free space is 5

Do you want to produce items(YES-1/NO-0) 0

Consumed item is 7

Consumed item is 8

Consumed item is 17

Consumed item is 12

Consumed item is 3

Do you want to continue(YES-1/NO-0) 1

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

Total free space is 10

Do you want to produce items(YES-1/NO-0) 1

Enter the no. of items you want to produce 2

Item.no 1 7

Item.no 2 8

Buffer content is 7

Buffer content is 8

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Buffer content is 0

Total free space is 8

Do you want to produce items(YES-1/NO-0) 0

Consumed item is 7

Consumed item is 8

Do you want to continue(YES-1/NO-0) 0

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for implementing the producer and con-


sumer problem is executed successfully and the output is veri-
fied.
EX NO:7
INTERPROCESS COMMUNICATION
DATE:

AIM:
To write a c program for implementing a shared memory IPC.

ALGORITHM:
STEP 1: Declare an array to store the string.
STEP 2: Declare an array with two element for pipe descriptors.
STEP 3: Enter the string to store in the pipe.
STEP 4: Create pipe on file descriptor using pipe function call.
STEP 5: Using fork system call , create a child process.
STEP 6: Parent process writes the string from array on to the pipe
using write system call.
STEP 7: The child process retrieve the content from the pipe to the
array using read system call.

PROGRAM:

#include<stdio.h>

int main(){

int fd[2],child;

char buf[30];

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\n enter the string to store the pipe:");

scanf("%s",buf);

pipe(fd);

child=fork();

if(!child) {

close(fd[0]);

write(fd[1],buf,5);

wait(0);}

else{

close(fd[1]);

read(fd[0],buf,5);

printf("\n string retrived from the pipe is %s\n",buf);}

return (0);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:

[cse023@localhost ~]$ cc ipc.c

[cse023@localhost ~]$ ./a.out

enter the string to store the pipe:os

string retrived from the pipe is os

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for implementing a shared memory and IPC is


executed successfully and the output is verified.

EX NO:8
BANKERS ALGORITHM
DATE:

AIM:

To implement deadlock avoidance by using bankers algo-


rithm.

ALGORITHM:
STEP 1: Declare an element to store the resources and processes.
STEP 2: Get the total number of resources.
STEP 3: Get the maximum resources to be allocated.
STEP 4: Check the condition for each process.
STEP 5: Print the result.
STEP 6: Stop.

PROGRAM:
#include<stdio.h>

#include<stdlib.h>

int main()

int work[5],av[5],alloc[10][10],l,n,m,i,j,k;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

int need[10][10],avail[10],max[10][10],count,fcount=0,pr[10];

char finish[10]={'f','f','f','f','f','f','f','f','f','f'};

printf("\nEnter the no.of process");

scanf("%d",&n);

printf("\nEnter the no.of resource");

scanf("%d",&m);

printf("\nEnter the total no.of resource");

for(i=1;i<=m;i++)

scanf("%d",&avail[i]);

printf("\nEnter the maximum resource by each process alloc matrix");

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

scanf("%d",&max[i][j]);

printf("\nProcess allocation matrix");

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

scanf("%d",&alloc[i][j]);

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

need[i][j]=max[i][j]-alloc[i][j];

for(i=1;i<=n;i++)

k=0;

for(j=1;j<=m;j++)

k=k+alloc[i][j];

av[i]=av[i]-k;

work[i]=av[i];

for(k=1;k<=n;k++)

for(i=1;i<=n;i++)

count=0;

for(j=1;j<=m;j++)

if((finish[j]=='f')&&(need[i][j]<=work[i]))

count++;

if(count==m)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

for(l=1;l<=m;l++)

work[l]=work[l]+alloc[i][l];

finish[i]='f';

pr[i]=i;

break;

for(i=1;i<=n;i++)

if(finish[i]=='f')

fcount++;

if(fcount==n)

printf("\nThe system in safe state");

for(i=1;i<=n;i++)

printf("\n%d\n",pr[i]);

else

printf("\nThe system is not in safe state");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:
[oslab@localhost~]$ cc dlavoid.c

[oslab@localhost~]$ ./a.out

Enter the no.of process

Enter the no.of resource

Enter the total no.of resource

Enter the maximum resource by each process alloc matrix

Process allocation matrix

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

The system is in safe state


RESULT:

Thus bankers algorithm for deadlock avoidance was executed


successfully.
EX NO:9
DEADLOCK DETECTION ALGORITHM
DATE:

AIM:

To determine whether the process and their request for resources are
in a deadlocked state.

ALGORITHM:
STEP 1: Declare an element to store the resources and processes.
STEP 2: Get the total number of resources.
STEP 3: Get the maximum resources to be allocated.
STEP 4: Check the condition for each process.
STEP 5: Print the result.
STEP 6: Stop.

PROGRAM:
#include<stdio.h>

int main()

int found,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1;

int m[5],r[5],a[5],temp[5],sum=0;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\nEnter total number of process");

scanf("%d",&tp);

printf("\nEnter the total no. of resources");

scanf("%d",&tr);

printf("\nEnter chain(Max.Need)matrix");

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

printf("\nProcess %d",i+1);

for(j=0;j<tr;j++)

scanf("%d",&c[i][j]);

printf("\nEnter the allocation matrix");

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

printf("\nProcess %d",i+1);

for(j=0;j<tr;j++)

scanf("%d",&p[i][j]);

printf("\nEnter the resource vector(Total Resource)");

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

scanf("%d",&r[i]);

printf("\nEnter the availability vector(Available Resource)");

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

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

scanf("%d",&a[i]);

temp[i]=a[i];

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

sum=0;

for(j=0;j<tr;j++)

sum+=p[i][j];

if(sum==0)

m[k]=i;

k++;

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

for(l=0;l<k;l++)

if(i!=m[l])

flag=1;

for(j=0;j<tr;j++)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

if(c[i][j]<temp[j])

flag=0;

break;

if(flag==1)

m[k]=i;

k++;

for(j=0;j<tr;j++)

temp[j]+=p[i][j];

printf("\nDeadlock causing process are\n");

for(j=0;j<tp;j++)

found=0;

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

if(j==m[i])

found=1;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

if(found==0)

printf("\n%d\t",j+1);

OUTPUT:
[oslab@localhost~]$ cc dldetect.c

[oslab@localhost~]$ ./a.out

Enter total number of process 4

Enter the total no. of resources 5

Enter chain(Max.Need)matrix

Process 1

0 1 0 0 1

Process 2

0 0 1 0 1

Process 3

0 0 0 0 1

Process 4

1 0 1 0 1

Enter the allocation matrix

Process 1

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

1 0 1 1 0

Process 2

1 1 0 0 0

Process 3

0 0 0 1 0

Process 4

0 0 0 0 0

Enter the resource vector(Total Resource)

2 1 1 2 1

Enter the availability vector(Available Resource)

0 0 0 0 1

Deadlock causing process are

2 3

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus using given state information deadlock process were deter-


mined.

EX NO:10
IMPLEMENTATION OF DINNING
PHILOSOPHER PROBLEM
DATE:

AIM:

To demonstrate threading and synchronization using mutex.

ALGORITHM:

1. Create five threads.

2. Let the threads share the semaphore variable mutex. 3.Ini-

tialize the semaphore using sem_ init() function. All the


philosophers are in thinking state.
4. State of the philosopher is changed to hungry.
5. After Checking the availability of the fork
the state is changed to eating.
6.Philosopher after eating releases the fork and the

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

state is changed to thinking.

PROGRAM:
#include<stdio.h>

#include<semaphore.h>

#include<pthread.h>

#define N 5

#define THINKING 0

#define HUNGRY 1

#define EATING 2

#define LEFT (ph_num+4)%N

#define RIGHT (ph_num+1)%N

sem_t mutex;

sem_t S[N];

void *philospher(void *num);

void take_fork(int);

void put_fork(int);

void test(int);

int state[N];

int phil_num[N]={0,1,2,3,4};

int main()

int i;

pthread_tthread_id[N];

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

sem_init(&mutex,0,1);

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

sem_init(&S[i],0,0);

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

pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);

printf("\n philospher %d is thinking\n",i+1);

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

pthread_join(thread_id[i],NULL);

void *philospher(void *num)

while(1)

int *i=num;

sleep(1);

take_fork(*i);

sleep(0);

put_fork(*i);

void take_fork(int ph_num)

sem_wait(&mutex);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

state[ph_num]=HUNGRY;

printf("philospher %d is hungry\n",ph_num+1);

test(ph_num);

sem_post(&mutex);

sem_wait(&S[ph_num]);

sleep(1);

void test(int ph_num)

if(state[ph_num]==HUNGRY&&state[LEFT]!=EATING&&state[RIGHT]!=

EATING)

state[ph_num]=EATING;

sleep(2);

printf("philospher %d takes fork %d and %d\


n",ph_num+1,LEFT+1,ph_num+1);

printf("philospher %d is eating\n",ph_num+1);

sem_post(&S[ph_num]);}}

void put_fork(int ph_num)

sem_wait(&mutex);

state[ph_num]=THINKING;

printf("philospher %d putting fork %d and%ddown\


n",ph_num+1,LEFT+1,ph_num+1);

printf("philospher %d is thinking\n",ph_num+1);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

test(LEFT);

test(RIGHT);

sem_post(&mutex);

OUTPUT:
[cse023@localhost ~]$gcc -o c ts.c –pthread

[cse023@localhost ~]$ ./c

Philosopher 1 is thinking

Philosopher 2 is thinking

Philosopher 3 is thinking

Philosopher 4 is thinking

Philosopher 5 is thinking

Philosopher 1 is hungry

Philosopher 1 takes fork 5 and 1

Philosopher 1 is Eating

Philosopher 4 is hungry

Philosopher 4 takes fork 3 and 4

Philosopher 4 is Eating

Philosopher 2 is hungry

Philosopher 3 is hungry

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

Philosopher 5 is hungry

Philosopher 1 putting fork 5 and 1 down

Philosopher 1 is thinking

Philosopher 2 takes fork 1 and 2

Philosopher 2 is Eating

Philosopher 4 putting fork 3 and 4 down

Philosopher 4 is thinking

Philosopher 5 takes fork 4 and 5

Philosopher 5 is Eating

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus concurrent threads were synchronized using mutex lock.


EX NO:11
PROGRAM FOR FIRST FIT,
BEST FIT, WORST FIT
DATE:

AIM:
To implement the first fit, worst fit , best fit algorithm
using c program.

ALGORITHM
STEP 1: Start.
STEP 2: Read the number of partitions and the partition size.
STEP 3: Read the number of processes and their sizes.
STEP 4: Check if the strategy is first fit.
STEP 5: Check if the strategy is best fit;
STEP 6: Check if the strategy is worst fit.
STEP 7: Stop

PROGRAM

#include<stdio.h>

void main()

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

int i,j,temp,b[10],c[10],arr,n,ch,a;

printf("\t\t FIRST FIT, BEST FIT, WORST FIT\n");

printf("Enter the size of no. of blocks:");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("Enter the size of %d block:",i);

scanf("%d",&b[i]);

c[i]=b[i];

printf("\nEnter the size of Arriving block:");

scanf("%d",&arr);

printf("\n1.First fit\n2.Best fit\n3.Worst fit\nEnter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:

for(i=1;i<=n;i++)

if(b[i]>=arr)

printf("\t\t%d",arr);

printf("\nArriving block is allocated to %d block.",i);

break;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

else

printf("%d",b[i]);

continue;

break;

case 2:

for(i=1;i<=n;i++)

if(b[i]>=b[i+1])

temp=b[i];

b[i]=b[i+1];

b[i+1]=temp;

for(i=1;i<=n;i++)

if(b[i]>=arr)

a=b[i];

break;

else

printf("%d",b[i]);

continue;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

for(i=1;i<=n;i++)

if(c[i]==a)

printf("\t\t%d",arr);

printf("\nArriving block is allocated to %d block.",i);

break;

case 3:

for(i=1;i<=n;i++)

if(b[i]>=b[i+1])

temp=b[i];

b[i]=b[i+1];

b[i+1]=temp;

for(i=1;i<n;i++)

printf(" %d",b[i]);

printf("\t%d",arr);

printf("\n Arriving block is allocated to %d block",i);

break;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

default:

printf("Enter the valid choice:");

OUTPUT:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to implement the first fit ,worst fit,best fit algo-
rithm using c program is executed successfully and the output is
verified.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

EX NO:12
MEMORY MANAGEMENT-
PAGING TECHINIQUE
DATE:

AIM:

To write a c program for memory management of paging tech-


niques.

ALGORITHM
STEP 1: Start.
STEP 2: Read the logical memory address
STEP 3: Read the page table along with the offset and page frame
STEP 4: Calculate the physical address
STEP 5: Print the physical address for the corresponding logical
Address
STEP 6: Stop

PROGRAM:

#include<stdio.h>

int main(){

int i,j,arr[100],pt[20],val,pgno,offset,phymem,fs,nf;

printf("\n memory management paging");

printf("\n enter the size of physical memory");

scanf("%d",&phymem);

for(i=20,j=0;i<phymem+20,j<phymem;i++,j++)

arr[i]=j;

printf("\n enter the size of frames or page");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

scanf("%d",&fs);

nf=phymem/fs;

printf("\n no of frames available are\t%d",nf);

printf("\n enter page table");

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

scanf("%d",&pt[i]);

printf("\n enter the page no");scanf("%d",&pgno);

printf("\n enter the offset");scanf("%d",&offset);

val=(fs*pt[pgno])+offset;

printf("\n the physical address is %d\n",arr[val]);

OUTPUT:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

[cse023@localhost ~]$ cc mmp.c

[cse023@localhost ~]$ ./a.out

memory management paging

enter the size of physical memory 100

enter the size of frames or page 10

no of frames available are 10

enter page table 1

10

enter the page no 7

enter the offset 6

The physical address is 86

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for memory management of paging techniques is executed successfully and the
output is verified.

EX NO:13a

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

FIRST IN FIRST OUT PAGE REPLACEMENT


ALGORITHM
DATE:

AIM:
To write a c program to implementation of FIFO page re-
placement algorithm using LINUX.

ALGORITHM:
STEP 1: Start.
STEP 2: Declare the frame size
STEP 3: Make the frames to be empty
STEP 4: The reference is bought into the frames in FIFO manner.
STEP 5: Select the page which the frame are occurs in the first
STEP 6: Repeat the above process until all reference is allocated
STEP 7: Stop

PROGRAM:

#include <stdio.h>

int n,pg [30],fr [10];

int main ()

int i,f,r,s,count,flag,num,psize;

f=0;

r=0;

s=0;

flag=0;

count=0;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf ("\nenter total no.of.pages");

scanf ("%d",&n);

printf("\nenter the sequence");

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

scanf ("%d",&pg [i]);

printf("\nenter size of page frame");

scanf ("%d",&psize);

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

fr[i]=-1;

while(s <n)

flag=0;

num=pg[s];

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

if (num==fr [i])

s++;

flag=1;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

break;

if (flag==0)

if (r <psize)

fr [r]=pg [s];

r++;

s++;

count++;

else

if (f <psize)

fr [f]=pg [s];

r++;

f++;

count++;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

else

f=0;

printf ("\n");

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

printf("%d\t",fr [i]);

printf ("\npage faults=%d\n",count);

getchar ();

OUTPUT:

[cse023@localhost~] $ cc fifopg.c

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

[cse023@localhost~] $ ./a.out

Enter total no.of.pages: 10

Enter sequence:

1
3
2
4
2
3
5
4
1
3
Enter size of page frame: 3
1 -1 -1

1 3 -1

1 3 2

4 3 2

4 3 2

4 3 2

4 5 2

4 5 2

4 5 1

4 5 1

4 5 1

page faults= 7

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to implementation of FIFO page replacement


algorithm is executed successfully and the output is verified.
EX NO:13b
LEAST RECENTLY USED PAGE
REPLACEMENT ALGORITHM
DATE:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

AIM:
To write a program to implementation LRU page replace-
ment algorithm using LINUX.

ALGORITHM:
STEP 1: Start.
STEP 2: Declare the frame size
STEP 3: All the frames is brought into the frames one by one
STEP 4: Select the page which is not recently used
STEP 5: Repeat the above process until all reference is allocated
STEP 6: Stop

PROGRAM:

#include<stdio.h>

main()

int q[20],p[50],c=0,cl,d,f,i,j,k=0,n,r,t,b[20],c2[20];

printf("Enter no. of pages:");

scanf("%d",&n);

printf("Enter the reference string:");

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

scanf("%d",&p[i]);

printf("Enter the no. of frames:");

scanf("%d",&f);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

q[k]=p[k];

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

c++;

k++;

for(i=1;i<n;i++)

cl=0;

for(j=0;j<f;j++)

if(p[i]!=q[j])

cl++;

if(cl==f)

c++;

if(k<f)

q[k]=p[i];

k++;

for(j=0;j<k;j++)

printf("\t%d",q[j]);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\n");

else

for(r=0;r<f;r++)

c2[r]=0;

for(j=i-1;j<n;j--)

if(q[r]!=p[j])

c2[r]++;

else

break;

for(r=0;r<f;r++)

b[r]=c2[r];

for(r=0;r<f;r++)

for(j=r;j<f;j++)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

if(b[r]<b[j])

t=b[r];

b[r]=b[j];

b[j]=t;

}}}

for(r=0;r<f;r++)

if(c2[r]==b[0])

q[r]=p[i];

printf("\t%d",q[r]);

printf("\n");

printf("\n The no.of page faults is %d \n",c);

OUTPUT:

[cse023@localhost ~]$ cc lru.c

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

[cse023@localhost ~]$ ./a.out


Enter no. of pages:10
Enter the reference string:1
3
2
4
2
3
5
4
1
3
Enter the no. of frames:3
1
1 3
1 3 2
4 3 2
5 3 2
5 3 4
5 1 4
3 1 4
The no.of page faults is 8

RESULT:

Thus the program to implementation LRU page replace-


ment algorithm using LINUX is executed successfully and the
output is verified.

EX NO:13c
OPTIMAL PAGE REPLACEMENT

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

ALGORITHM
DATE:

AIM:

To write a c program to implement optimal page replacement al-


gorithm.

ALGORITHM:
STEP 1: Start.
STEP 2: Take the input of pages as an array.
STEP 3: Look for the page allocated is present in near future, if no
then replace the page in the memory with new page .
STEP 4: If page already present increment hit, else increment miss.
STEP 5: Repeat till we reach the last element of the array.
STEP 6: Print the number of hits and misses.
STEP 7: Stop

PROGRAM:

#include<stdio.h>

int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;

int recent[10],optcal[50],count=0;

int optvictim();

int main()

printf("\n optimal page replacement algorithm");

printf("\n................");

printf("\n enter the no of frames");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

scanf("%d",&nof);

printf("\nenter the no refernce string");

scanf("%d",&nor);

printf("\n enter the reference string");

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

scanf("%d",&ref[i]);

printf("\n optimal page replacement algorithm");

printf("\n...........");

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

printf("\n%4d",ref[i]);

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

frm[i]=-1;

optcal[i]=0;

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

recent[i]=0;

printf("\n");

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

flag=0;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\n\tref no %d->\t",ref[i]);

for(j=0;j<nof;j++)

if(frm[j]==ref[i])

flag=1;

break;

if(flag==0)

count++;

if(count<=nof)

victim++;

else

victim=optvictim(i);

pf++;

frm[victim]=ref[i];

for(j=0;j<nof;j++)

printf("%4d",frm[j]);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("\n number of page fault:%d\n",pf);

int optvictim(int index)

int i,j,temp,notfound;

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

notfound=1;

for(j=index;j<nor;j++)

if(frm[i]==ref[j])

notfound=0;

optcal[i]=j;

break;

if(notfound==1)

return i;

temp=optcal[0];

for(i=1;i<nof;i++)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

if(temp<optcal[i])

temp=optcal[i];

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

if(frm[temp==frm[i]])

return i;

return 0;

OUTPUT:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

[cse023@localhost ~]$ cc optimal.c

[cse023@localhost ~]$ ./a.out

OPTIMAL PAGE REPLACEMENT ALGORITHM

.....................................................................................

Enter the no. of frames 3

Enter the no. of reference string 10

Enter the reference string 1

3
OPTIMAL PAGE REPLACEMENT ALGORITHM
......................................................................................
The given string
...........................
1 3 2 4 2 3 5 4 1 3
ref no 1->1 -1 -1
ref no 3-> 1 3 -1

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

ref no 2-> 1 3 2
ref no 4-> 4 3 2
ref no 2->
ref no 3->
ref no 5-> 4 3 5
ref no 4->
ref no 1-> 1 3 5
ref no 3->
Number of page faults:6

RESULT:

Thus the program to implement optimal page replacement


algorithm is executed successfully and the output is verified.

EX NO:14a
SINGLE LEVEL DIRECTORY
DATE:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

AIM:
To write a c program for single level directory.

ALGORITHM:

STEP1: Read the number of directories, files


STEP2: Create a root directory, user define file
STEP3: Link to be created
STEP 4: Print
STEP 5: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#definesx 250
#definesy 200
#define bl 30
#definebw 15
#define sy1 250
int sx1=100;
int n,m[10];
char a[10][10];
int cols[]={2,5,1,3,4,6,8,7,9};
typedef struct
{
char name[10];
struct

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

{
char file[10];
}k[10];
}file_info;
file_infof[10];
void readfiles()
{
int i,j;
printf("enter the no of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the level %d directory:",i+1);
scanf("%s",&f[i].name);
printf("\nenter the no of subdiretories:");
scanf("%d",&m[i]);
for(j=0;j<m[i];j++)
{
printf("\nenter level %d filename:",j+1);
scanf("%s",&f[i].k[j].file);
}
}
}
void fillspace()
{
int x1,y1,i,j;
outtextxy(70,205,"master directory");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

outtextxy(10,255,"userfiles");
for(i=0;i<n;i++)
{
setfillstyle(1,cols[i]);
x1=sx+(i%10)*50;
y1=sy+(i/10)*40;
bar(x1,y1,x1+bl+20,y1+bw);
outtextxy(x1+bl/4,y1+bw/4,f[i].name);
line(x1+bl/2,y1+bw,sx1+50,sy1);
sx1=sx1+50;
for(j=0;j<m[i];j++)
{
setfillstyle(1,cols[i]);
x1=sx1+(j%10)*40;
y1=sy1+(j/10)*40;
bar(x1,y1,x1+bl+10,y1+bw);
outtextxy(x1+bl/4,y1+bw/4,f[i].k[j].file);
line(x1+bl/2,y1+bw,x1+bl/2,y1+50);
circle(x1+bl/2,y1+60,7);
}
if(i==0)
{
line(x1+bl,y1+bw,sx1+160,sy1+60);
}
sx1=sx1+100;
}
}

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
readfiles();
clrscr();
fillspace();
closegraph();}
OUTPUT:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for single level directory is executed


successfully and the output is verified.

EX NO:14b

TWO LEVEL DIRECTORY

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

DATE:

AIM:
To write a c program for two level directory.

ALGORITHM:

STEP1: Read the number of directories, files


STEP2: Create a root directory, user define file
STEP3: Link to be created
STEP 4: Print
STEP 5: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define SX 250
#define SY 200
#define BL 30
#define BW 15
#define SY1 250
int SX1=100;
int SW1=100;
int n,m[10];
char a[10][10];
int cols[]={2,5,1,3,4,6,7,8,9};
typedef struct
{

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

char name[10];
struct
{
char file[10];
}k[10];
}file_info;
file_infof[10];
void readfiles()
{
int i,j;
printf("\nenter the no of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the level %d directory:",i+1);
scanf("%s",&f[i].name);
printf("\n enter the no of subdirectories:");
scanf("%d",&m[i]);
for(j=0;j<m[i];j++)
{
printf("\n enter level %d filename:",j+1);
scanf("%s",&f[i].k[j].file);
}
}
}
void fillspace()
{

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

int X1,Y1,i,j;
outtextxy(70,205,"master directory");
outtextxy(10,255,"userfiles");
for(i=0;i<n;i++)
{
setfillstyle(1,cols[i]);
X1=SX+(i%10)*50;
Y1=SY+(i/10)*40;
bar(X1,Y1,X1+BL+20,Y1+BW);
outtextxy(X1+BL/4,Y1+BW/4,f[i].name);
line(X1+BL/2,Y1+BL,SW1+50,SY1);
SX1=SX1+50;
for(j=0;j<m[i];j++)
{
setfillstyle(1,cols[i]);
X1=SX1+(j%10)*40;
Y1=SY1+(j/10)*40;
bar(X1,Y1,X1+BL+10,Y1+BW);
outtextxy(X1+BL/4,Y1+BW/4,f[i].k[j].file);
line(X1+BL/2,Y1+BW,X1+BL/2,Y1+50);
circle(X1+BL/2,Y1+60,7);
}
if(i==0)
{
line(X1+BL,Y1+BW,SX1+160,SY1+60);
}
SX1=SX1+100;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

}}
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
readfiles();
clrscr();
fillspace();
getch();closegraph();}

OUTPUT:

Enter the no. of directories: 2

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

Enter the level1 directories name: user1


Enter the no. of subdirectories: 2
Enter level 1 directory name: sub1
Enter level 2 directory name: sub2

Enter the level2 directories name: user2


Enter the no. of subdirectories: 2
Enter level 1 directory name: sub3
Enter level 2 directory name: sub4

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for two level file directory is executed


successfully and the output is verified.

EX NO:14c
HIERARCHICAL DIRECTORY
DATE:

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

AIM:
To write a c program for hierarchical directory.

ALGORITHM:

STEP1: Read the number of directories and subdirectories


STEP2: Create a root directory, user define file
STEP3: Link to be created
STEP 4: Print
STEP 5: Stop

PROGRAM:

#include<stdio.h>
#include<graphics.h>
struct tree_ele
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_ele *link[5];
};
typedef struct tree_ele node;
void main()
{
int gm,root,gd=DETECT;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

initgraph(&gd,&gm,"c:\\tc\\bgi");
display(root);
getch();
closegraph();
}
create(node **root,intlev,char *dname,intlx,intrx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("\n enter name of dir(file(under%s):",dname);
fflush(stdin);
gets((*root)->name);
printf("\n enter 1 for dir/2 for file:");
scanf("%ds",&(*root)->ftype);
(*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)
{
printf("\n no. ofsubdir/file(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

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);}
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

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:

Enter name of dir/file (under root): ROOT

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

Enter 1 for dir/2 for file: 1


No. of sub directories/file (for root):3

Enter name of dir/file (under root): USER1


Enter 1 for dir/2 for file: 1
No. of sub directories/file (for user1):2

Enter name of dir/file (under user1): SUB1


Enter 1 for dir/2 for file: 1
No. of sub directories/file (under user1):1

Enter name of dir/file (under sub1): C


Enter 1 for dir/2 for file: 2

Enter name of dir/file (under sub1): SUB2


Enter 1 for dir/2 for file: 1
No. of sub directories/file (for sub2): 1

Enter name of dir/file (under sub2): JAVA


Enter 1 for dir/2 for file: 2
Enter name of dir/file (under root):USER2

Enter 1 for dir/2 for file: 1


No. of sub directories/file (for user2): 2

Enter name of dir/file (under user2): SUB3


Enter 1 for dir/2 for file: 1
No. of sub directories/file (for sub3):1

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

Enter name of dir/file (under sub3): CPP


Enter 1 for dir/2 for file: 2

Enter name of dir/file (under user2): SUB4


Enter 1 for dir/2 for file: 1
No. of sub directories/file (for sub4):1

Enter name of dir/file (under sub4): FILE1


Enter 1 for dir/2 for file: 2

Enter name of dir/file (under root): USER3


Enter 1 for dir/2 for file: 1
No. of sub directories/file (for user3):1

Enter name of dir/file (under user3): SUB


Enter 1 for dir/2 for file: 1
No. of sub directories/file (for user3):1

Enter name of dir/file (under sub): FILE2


Enter 1 for dir/2 for file: 2

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for hierarchical directory is executed


successfully and the output is verified.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

EX NO:14d
DIRECTED ACYCLIC GRAPH
DATE:

AIM:
To write a c program to implement directed acyclic directory in LINUX.

ALGORITHM:
STEP1: Read the name of the root directory.
STEP2: Create user define file
STEP3: Link to be created
STEP 4: Print
STEP 5: Stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
struct tree_ele
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_ele *link[5];
};
typedef struct tree_ele node;
typedef struct
{

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

char from[20];
char to[20];
}link;
link L[10];
int nof1;
node *root;
void main()
{
int gm,gd=DETECT;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
draw_link_lines();
display(root);
getch();
closegraph();
}
read_links()
{
int i;
printf("\n how many links?");
scanf("%d",&nof1);
for(i=0;i<nof1;i++)
{

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

printf("file/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name");
fflush(stdin);
gets(L[i].to);
}
return;
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nof1;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
return;
}
search(node *root,char *s,int *x,int *y)
{
int i;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

if(root!=NULL)
{
if(strcmp(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}
}
return;
}
create(node **root,intlev,char *dname,intlx,intrx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("\n enter name of dir(file(under%s):",dname);
fflush(stdin);
gets((*root)->name);
printf("\n enter 1 for dir/2 for file:");
scanf("%ds",&(*root)->ftype);
(*root)->level=lev;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

(*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)
{
printf("\n no. ofsubdir/file(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
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);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

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

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:

Enter the name of dir/file(under root):ROOT

Enter 1 for dir / 2 for file:1

No.of subdirectories/files(for ROOT):2

Enter the name of dir/file(under root):USER1

Enter 1 for dir / 2 for file:1

No.of subdirectories/files(for USER1):1

Enter the name of dir/file(under USER1):C

Enter 1 for dir / 2 for file:2

Enter the name of dir/file(under root):USER2

Enter 1 for dir / 2 for file:1

No.of subdirectories/files(for USER2):1

Enter the name of dir/file(under USER2):JAVA

Enter 1 for dir / 2 for file:2

How many links?1

file/dir:USER1

user name JAVA

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program for directed acyclc directory is executed


successfully and the output is verified.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

EX NO:15a
SEQUENTIAL FILE ALLOCATION
STRATERGY
DATE:

AIM:

To implement the sequential file allocation in c program using linear file.

ALGORITHM:

STEP1: Declare the starting block and length of files initially

STEP2: Get the input for the starting block and length of the file

STEP3: If the block is not used, then it is allocated for the new one

STEP4: Otherwise, print that the block is already allocated

STEP5: Stop

PROGRAM:
#include<stdio.h>

#include<stdlib.h>

main()

int f[50],i,st,j,len,c,k;

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

f[i]=0;

X:

printf("\nEnter the starting block &length of the file");

scanf("%d%d",&st,&len);

for(j=st;j<(st+len);j++)

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

if(f[j]==0)

f[j]=1;

printf("\n%d->%d",j,f[j]);

else

printf("\nBlock already allocated");

break;

if(j==(st+len))

printf("\nFile is allocated to disk");

printf("\nIf u want to enter more files?(Y-1/N-0)");

scanf("%d",&c);

if(c==1)

goto X;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:
[oslab@localhost~]$ cc seqfilealloc.c

[oslab@localhost~]$ ./a.out

Enter the starting block &length of the file

5->1

6->1

7->1

If u want to enter more files?(Y-1/N-0)1

Enter the starting block &length of the file

Block already allocated

If u want to enter more files?(Y-1/N-0)1

Enter the starting block &length of the file

8->1

9->1

10->1

If u want to enter more files?(Y-1/N-0)0


RESULT:

Thus the program to implement the sequential file allocation using LINUX
file is executed successfully and the output is verified.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

EX NO:15b
LINKED FILE ALLOCATION
STRATERGY
DATE:

AIM:
To implement the index file allocation using c - program on LINUX.

ALGORITHM:

STEP1: Declare the starting index block and length

STEP2: Read the number of blocks that are allocated

STEP3: If entry any file in the block, if the file is exits, print the file in
already allocated

STEP 4: Stop

PROGRAM:
#include<stdio.h>

#include<stdlib.h>

main()

int f[50],p,i,j,k,a,st,len,n,c;

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

f[i]=0;

printf("\nEnter how many blocks that are already alloated");

scanf("%d",&p);

printf("\nEnter the block no. that are already allocated");

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

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

scanf("%d",&a);

f[a]=1;

X:

printf("\nEnter the starting index block & length");

scanf("%d%d",&st,&len);

k=len;

for(j=st;j<(k+st);j++)

if(f[j]==0)

f[j]=1;

printf("\n%d->%d",j,f[j]);

else

printf("\n%d->file is already allocated",j);

k++;

printf("\nIf you want to enter one or more file?(yes-1/no-0)");

scanf("%d",&c);

if(c==1)

goto X;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:
[oslab@localhost~]$ cc linkedfilealloc.c
[oslab@localhost~]$ ./a.out
Enter how many blocks that are already alloated
3
Enter the block no. that are already allocated
467
Enter the starting index block & length
2 4
2->1
3->1
4->file is already allocated
5->1
6->file is already allocated
7->file is already allocated
8->1
If you want to enter one or more file?(yes-1/no-0)1
Enter the starting index block & length
8 2
8->file is already allocated
9->1
10->1
If you want to enter one or more file?(yes-1/no-0)0

RESULT:

Thus the program to implement the index file allocation using c -


program on LINUX is executed successfully and the output is
verified.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

EX NO:15c
INDEXED FILE ALLOCATION STRATERGY
DATE:

AIM:
To implement the linked file allocation using c - program.

ALGORITHM:

STEP1: Declare the starting index block

STEP2: Read the number of files on index that are to be allocated of the
index is empty , the file is allocated the file is allocated to the index is
already allocated

STEP3: Repeated step 2 until the file is allocated

STEP4: Stop

PROGRAM:
#include<stdio.h>

int f[50],i,j,k,inde[50],n,c,count=0,p;

main()

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

f[i]=0;

X:

printf("\nEnter index block");

scanf("%d",&p);

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

if(f[p]==0)

f[p]=1;

printf("\nEnter no. of files on index\t");

scanf("%d",&n);

else

printf("\nBlock already allocated\n");

goto X;

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

scanf("%d",&inde[i]);

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

if(f[inde[i]==1])

printf("\nBlock already allocated");

goto X;

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

f[inde[j]]=1;

printf("\nAllocated");

printf("\nFile indexed");

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

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

printf("\n%d->%d=%d",p,inde[k],f[inde[k]]);

printf("\nEnter 1 to enter more files and 0 to exit");

scanf("%d",&c);

if(c==1)

goto X;

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

OUTPUT:
[oslab@localhost~]$ cc indexfilealloc.c

[oslab@localhost~]$ ./a.out

Enter index block

Enter no. of files on index

Allocated

File indexed

7->6=1

7->7=1

7->8=1

7->17=1

7->2=1

Enter 1 to enter more files and 0 to exit1

Enter index block

Block already allocated

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

RESULT:

Thus the program to implement the linked file allocation using c -


program is executed successfully and the output is verified.

CS3461 OPERATING SYSTEM LAB


NAME: K. MANIKAVALLI REGNO:411621104029

CS3461 OPERATING SYSTEM LAB


NAME REGNO

CS8461 OPERATING SYSTEM LAB


NAME REG NO

CS 3461 OPERATING SYSTEM LABORATORY


NAME REG NO

CS 3461 OPERATING SYSTEM LABORATORY


NAME REG NO

CS 3461 OPERATING SYSTEM LABORATORY

You might also like