0% found this document useful (0 votes)
8 views112 pages

Os Record

The document outlines a laboratory manual for studying the UNIX operating system, including various experiments and commands related to UNIX and shell programming. It covers topics such as the functions and features of UNIX, the vi editor, basic UNIX commands, and shell programming syntax. The manual serves as a guide for students to understand and practice key concepts in operating systems and programming.

Uploaded by

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

Os Record

The document outlines a laboratory manual for studying the UNIX operating system, including various experiments and commands related to UNIX and shell programming. It covers topics such as the functions and features of UNIX, the vi editor, basic UNIX commands, and shell programming syntax. The manual serves as a guide for students to understand and practice key concepts in operating systems and programming.

Uploaded by

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

INDEX

Name : ……………………… Reg.No:……………….

Branch: ……………………… Year/Sem: ………………..

EX.NO DATE NAME OF THE EXPERIMENT PG NO. MARK SIGNATURE

1(A) STUDY OF UNIX OS

1(B) STUDY OF VI EDITOR AND SHELL


PROGRAMMING

2(A) BASIC UNIX COMMANDS

2(B) DIRECTORY COMMANDS AND PROCESS


MANAGEMENT COMMANDS

3 SHELL PROGRAMS

4 ‘C’ PROGRAMS

5 SYSTEM CALLS

6(A) ROUND ROBIN

6(B) SHORTEST JOB FIRST- SJF

6(C) FIRST COME FIRST

6(D) PRIORITY

7(A) PRODUCER – CONSUMER PROBLEM

7(B) INTER PROCESS COMMUNICATION USING


SHARED MEMORY

7(C) BANKER’S ALGORITHM FOR DEADLOCK


AVOIDANCE
7(D) DEADLOCK DETECTION

7(E) THREADING & SYNCHRONIZATION


APPLICATION
8(A)
FIRST FIT
8(B) BEST FIT

8(C) WORST FIT

8(D) MEMORY MANAGEMENT USING


PAGING TECHNIQUE

9(A) FIFO

9(B) LRU

9(C) LFU

10(A) SINGLE LEVEL DIRECTORY

10(B) TWO LEVEL DIRECTORY

10(C) HIERARCHICAL DIRECTORY

10(D) DAG

11(A) CONTINUOUS MEMORY ALLOCATION

11(B) INDEXED MEMORY ALLOCATION

11(C) LINKED FILE ALLOCATION


STUDY OF UNIX OS

Ex.NO:1 (A)
DATE:

INTRODUCTION
An operating system is software that acts as an interface between the user and the computer
hardware. It is considered as the brain of the computer. It controls and co-ordinates the internal
activities of the computer and provides user interface.

The computer system is built with the following general components


i) Hardware
ii) Application Software
iii) Operating System

(i) Hardware: This includes the physical components such as CPU, Keyboard, Hard disk
and Printer.

(ii) Application Software: These are the programs that are used to accomplish specific tasks.

(iii) Operating System: It is the component or the set of programs to manage and control the
hardware as well as co-ordinate the applications. Each system must have at least have
the hardware and the OS.
Functions of an Operating System

· Command interpretation: The CPU can’t understand the commands keyed in by a user. It is
the function of the OS to make it understand.

· Peripheral Interfaces: The OS also has to take care of the devices attached to the system.

The OS oversees communication between these devices and the CPU.

· Memory management: The OS handles the extremely important job of allocating memory
for various processes running on the system.

· Process management: This is required if several programs must run concurrently. CPU time
would then have to be rationed out by the OS to ensure that no programs get more than its
fair share of the processor time.

Services of an OS:

1. Process Management

2. File Management

3. I/O Management

4. Scheduling

5. Security Management

UNIX OPERARTING SYSTEM:

In the mid-1960s, AT &T Bell Laboratories developed a new OS called Multics. Multics was
intended to supply large scale computing services as a utility; much like electrical power. In 1969
Ken Thompson, Dennis Ritchie and others developed and simulated an initial design for a file
system that later evolved into the UNIX file system. The whole UNIX was rewritten in ‘C’ language
in 1973.Today, UNIX is a giant Os and is much powerful than most of its counter parts.tld

UNIX operating System is like a layer between the hardware and the applications that run on
the computer .It has functions that run on the computer. It has functions that manage the executing
applications.
UNIX system is an OS, which includes the traditional system components. UNIX system
includes a set of libraries and a set of applications.

KERNEL is the heart of UNIX OS that manages the hardware and the executing process. The
UNIX system views each device as a file called a device file. It implements security controls to
protect the safety and privacy of information. The Unix System allocates resources including use of
the CPU and mediates accesses to the hardware.
Application portability is the ability of a single application to be executed on various types of
computer hardware without being modified. This is one of the important advantages of UNIX.

FEATURES OF UNIX:

1. Multitasking
Multitasking is the capability of the Os to perform various tasks simultaneously. i.e. A single
user can run multiple programs concurrently.

2. Multi User Capability

Multi User capability allows several users to use the same computer to perform their tasks.
Several terminals are connected to a single powerful computer and each user can work with their
terminals.

3. Security

Unix allows sharing of data. Every user must have a Login name and a password. So,

accessing another user’s data is impossible without permission.


4. Portability

Unix is portable because it is written in high level languages so it can run on different
computers.

5. Communication

Unix supports communication between different terminals connected to the Unix server and
also between the users of one computer to the users of another computer located elsewhere in the
network.

6. Programming Facility

Unix is highly programmable, the Unix shell programming language has the conditional
statements and control structures and variables.

UNIX ARCHITECTURE

The functioning of Unix OS is handled in 3 ways.

Kernel
It is the core of the OS. It controls all the tasks and carries out all the functions of an OS such
as memory and file management etc., and it keep track of the programs that are executing. It also
handles the information exchange between the terminals such as tape drives and printers etc.,

Functions of Kernel

1. Allocating and deallocating memory to each and every process.


2. Receiving instructions from the shell and carrying them out.
3. Managing files that held on various storage devices.
4. Scheduling, Coordinating and assigning various input/output devices simulataneously.
5. Enforcing security measures.
6. Providing Network services.
7. Providing Utility services.
8. Coordinating each and every process with signal handling.
9. Providing administrative functions or utilities.

Shell

It is the command interpreter of the OS. The commands given from the user are moved to the

shell. The shell analyses and interprets these commands into the machine understandable form. The

commands can be either typed in through the command line or contained in a file called shell script.

Hence, Shell acts as an interface between the user and the kernel.

UNIX has a variety of shells, they are:

(i) Bourne Shell: It is developed by Steve Bourne and it is the most popular shell and
widely used. This shell comes bundled with almost every Unix system.

(ii) Korn shell: It is developed by David G.Korn. This is superset of Bourne shell an
it has more capabilities.
(iii) C Shell: It is developed by Bil Joy. It is similar to the C Programming language.

Features of Shell
1. All communications between user and Kernel takes place through the shell.
2. It allows the tasks to run in the background.
3. It also enables us to construct scripts like a programming language.
4. A group of files can be executed using a single command.

Starting a UNIX session – Logging In

A user of a Unix based system works as a user terminal. After the boot procedure is completed, that is the
operating system is loaded in memory, the following message appears at each user terminal:
Logging

Each user has an identification called the user name are the login name which has to be
entered when the login: message appears. The user is then asked to enter the password. Unix keeps
track of all the Unix user names and the information about identity in a special file. If the login name
entered does not match with any of the user names it displays the login message again. This ensures
that, only authorized people use the system. When a valid user name is entered at the terminal the
dollar symbol is displayed on the screen this is the Unix prompt.

Ending a UNIX session –Logging Out

Once a user has logged into the system the users works session continues until the user
instructs the shell to terminate the session. This is done by pressing the ctrl and ‘d keys’ together or
typing exit at the dollar prompt. Then the systems display the login:

Prompt on the screen.

RESULT:
Ex.NO:1(B) STUDY OF VI EDITOR & SHELL PROGRAMMING

DATE:

An editor is a program that allows users to see a portion of a file on the screen and to modify
characters and lines by simply typing at the cursor position. There are a number of editors that may be
included with the Unix system, including ed, ex, vi and EMACS.

The vi Editor

Vi - vi stands for visual.


Vi is a full screen editor and is widely acknowledged as one of the most powerful editors available .It is a
full screen editor that allows the user to view and edit the entire document at the same time.

The vi editor was written in the University of California at Berkeley by Bill Joy, who is on
of the co-founder of Sun Microsystems.

Features of vi Editor

1. It is very easy to learn and has more powerful and exciting features.
2. It works in great speed.
3. vi is case sensitive.
4. vi has powerful undo features than most other word processors,but it has no formatting
features.

Modes of vi Editor

Vi editor works in three modes of operations specified below:


Command Mode: In this mode, all the keys pressed by the user are interpreted to be editor
commands. No text is displayed on the screen, even if the corresponding key is pressed on the
keyboard.

Insert Mode: This mode permits inserting new text, editing and replacement of existing text. Once

the vi editor is in the insert mode, letters typed in the keyboard are echoed on the screen.
The ex or escape colon ( : ) Mode: This mode allow us to give commands at the command

line.The bottom line of the vi editor is called the command line.vi uses the command line to display
messages and commands.
Starting with Vi editor

Syntax: vi filename

Moving the cursor

The cursor movement commands are:

Command Action

H or backspace Left one character


l or spacebar Right one character
K or - Up one line
J or + Down one line
I Moves forward a word

#b Moves back a word


#e Moves to the last character in the word
F[character] Moves right to the specified character in a line
T[character] Moves right and places it one character before the specified
character
0 or ^ Moves to the beginning of the file
#$ Moves to the end of the file
L Moves to the last line of the file
G Moves to the specified line number

Editing the file

¨
¨ Open the file using $ vi filename

¨ To add text at the end of the file, position the cursor at the last character of the file.

¨ Switch from command mode to text input mode by pressing ‘a’.


¨
¨ Here ‘a’ stands for append.

¨ Inserting text in the middle of the file is possible by pressing ‘i’.

¨ The editor accepts and inserts the typed character until Esc key is pressed.

Command Purpose

I Inserts text to the left of the cursor


I Inserts text at the beginning of the line
A Append text to the right of the cursor
A Appends text at the end of the line
O Appends a new line below
O Appends a line above

Deleting Text

For deleting a character, move the cursor to the character , press ‘x’. The character will
disappear.

Command Purpose

X Deletes one character


Nx Deletes n number of characters
#x Deletes on character at the cursor position
#X Deletes on the character before the cursor position
D$ or d Deletes a line from the cursor position to the end of the line
D0 Deletes from the cursor position to the starting of the line
#dd Deletes the current line where the cursor is positioned
#dw Deletes the word from the cursor position to the end of the word

The undo features

u-undo the recent changes


U- undo all changes in the current line

Saving text

:w – save the file and remains in edit mode


:wq – save the file and quits from edit mode
:q – quit without changes from edit mode

Quitting vi

Press zz or ‘:wq’ in command mode.

SHELL PROGRAMMING

The format for the various conditional statements and looping statements and the relational
operators used for those conditions are given below.

if condition

if condition then

execute commands
fi

if – else condition

if condition then

execute commands
else
execute commands
fi

if – elif condition – multi way branching

if condition then

execute commands elif condition


then
execute commands
else
execute commands
fi
Relational Operators

Operat Meaning
or
-eq Equal to

-ne Not equal to

-gt Greater than

-ge Greater than or equal


to
-lt Less than
-le Less than or equal to

Case condition
case expression in

pattern1) execute commands ;; pattern2) execute commands ;;


pattern3) execute commands ;;

……………..
esac while looping

While expression Do
execute commands
done

until : while’s complement until [ condition ]

for looping

for variable in list do


execute commands done

RESULT:
Ex.NO:2(A) BASIC UNIX COMMANDS

DATE:

AIM:

FILE MANIPULATION COMMANDS

Command : Cat

Purpose : It is used to display the contents of the file as well as used to create a new

file.

Syntax : cat <file name >

Example : $ cat devi

Command : More

Purpose : It is used to display the contents of the file on the screen at a time.

Syntax : more <file name >

Example : $ more devi

Command : Wc

Purpose : It is used to count the number of lines ,words and characters in a file or

group of files.

Syntax : wc [options] <file name >

Example : $ wc –l devi
Command : File
Purpose : It is used to determine the type of the file.

Syntax : file <file name >

Example : $ file devi

Command : Spell

Purpose : It is used to find the spelling errors in the file.

Syntax : spell [options] <file name >

Example : $ spell -b devi

Command : Split

Purpose : It is used to split the given file into smaller pieces of given size.

Syntax : split –size <file name > < splitted file name >

Example : $ split –2 devi de

Command : Cp

Purpose : It is used to copy one or more files.

Syntax : cat <source file name > <destination file name>

Example : $ cp devi latha

Command : Mv

Purpose : It is used to move a file within a directory with different names and also used to
move a file to different directory with its original name.

Syntax : mv <source file name > <destination file name>

Example : $ mv devi jeya


Command : Rm

Purpose : It is used to remove a file from the disk.

Syntax : rm <file name >

Example : $ rm devi

GENERAL PURPOSE COMMANDS

Command : Banner

Purpose : It is used to display its argument in large letters.

Syntax : banner < string >

Example : $ banner BOOM

Command : Who

Purpose : It is used to get the information about all the users currently working in
the system.

Syntax : who

Example : $ who

Command : Who am i

Purpose : It is used to know in which terminal the user is currently logged on.

Syntax : who am i

Example : $ who am I

Command : Date

Purpose : It is used to display the system date and time.

Syntax : date

Example : $ date
Command : Cal

Purpose : It prints the calender for the specified year and month.

Syntax : cal <month> <year>

Example : $ cal 05 2003

Command : Id

Purpose : It is used to display the login name.

Syntax : id

Example : $ id

Command : Clear

Purpose : It is used to clear the screen.

Syntax : clear

Example : $ clear

Command : Tput

Purpose : It is used to manipulate the screen.

Syntax : tput < attributes >

Example : $ tput rmso

Command : Uname

Purpose : It is used to display the details about the OS in which we are working.

Syntax : uname [options]

Example : $ uname –n

Command : Tty

Purpose : It is used to know the terminal name on which we work.


Syntax : tty

Example : $ tty

Command : Pwd

Purpose : It is used to display the absolute pathname of current working directory.


Syntax : pwd

Example : $ pwd
Command : Bc

Purpose : It is used to perform simple mathematical calculations.

Syntax : bc <operation>

Example : $ bc 3+5 8 ^d

Command : Ls

Purpose : It is used to display the files in the current working directory.

Syntax : ls [options] <arguments>

Example : $ ls –p

Command : Echo

Purpose : It echoes the argument on the standard output device.

Syntax : echo [options] <string>

Example : $ echo ‘BOOM’

Command : Man

Purpose : It gives details about the unix commands.

Syntax : man < command name >

Example : $ man echo


COMMAND GROUPING & FILTER COMMANDS

Command : Head

Purpose : It is used to display the top portion of the file.

Syntax : head [options] <file name>

Example : $ head -5 devi

Command : Tail

Purpose : It is used to display the bottom portion of the file.

Syntax : tail [options] <file name >

Example : $ tail –5 devi

Command : Pr

Purpose : It is used to display the contents of the file by separating them into pages
and each page begins with the header information.

Syntax : pr [options] <file name >

Example : $ pr devi

Command : Cut

Purpose : It is used to extract selected fields or columns from each line of one or
more files and display them on the standard output device.

Syntax : cut [options] <file name >

Example : $ cut –c5 devi

Command : Paste

Purpose : It concatenates the line from each input file column by column with tab
characters in between them.

Syntax : paste [options] <file name >


Example : $ paste f1 f2

Command : Join

Purpose : It is used to extracts common lines from two sorted files and there should
be the common field in both file.

Syntax : join [options] <file name1 > <file name 2>

Example : $ join –a1 f1 f2

Command : Uniq

Purpose : It compares adjacent lines in the file and displays the output by
eliminating duplicate adjacent lines in it.

Syntax : uniq [options] <file name >

Example : $ uniq -c devi

Command : Sort

Purpose : It sorts one or more files based on ASCII sequence and also to merge the
file.

Syntax : sort [options] <file name >

Example : $ sort -r devi

Command : Nl

Purpose : It is used to add the line numbers to the file.

Syntax : nl [options] [filename]

Example : $ nl devi

Command : Tr

Purpose : It is used to translate or delete a character or a string from the standard


input to produce the required output.
Syntax : tr [options] <string1> <string2>

Example : $ tr –t ‘a’ ‘b’ < devi>

Command : Tee

Purpose : It is used to read the contents from standard input or from output of another
command and reproduces the output to boyh in standard output and direct into
output to one or more files.

Syntax : tee [options] <file name >

Example : $ tee date dat.txt

Command : grep

Purpose : It is used to search the specified pattern from one or more files.

Syntax : grep [options] <pattern> <file name >

Example : $ grep “anand” devi

RESULT:
Ex.NO:2 (B) DIRECTORY COMMANDS AND PROCESS MANAGEMENT
COMMANDS
DATE:

AIM:

Command : mkdir

Purpose : It is used to create new directory or more than one directory.

Syntax : mkdir <directory name >

Example : $ mkdir riya

Command : rmdir

Purpose : It is used to remove the directory if it is empty.

Syntax : rmdir <directory name >

Example : $ rmdir riya

Command : cd

Purpose : It is used to change the control from one working directory to another
specified directory.

Syntax : cd <directory name >

Example : $ cd riya

Command : cd ..

Purpose : It is used to quit from current directory and move to the previous
directory.

Syntax : cd ..

Example : $ cd ..
Process Commands

Command : echo $$

Purpose : It is used to display the process number of the current shell.

Syntax : echo $$

Example : $ echo $$

Command : ps

Purpose : It is used to display the attributes of a process.

Syntax : ps

Example : $ ps
$ ps –f ( Display the ancestry of a process )
$ ps –u ( Display the activities of a user )
$ ps –a ( Lists processes of all users but not the system processes )

Command :&

Purpose : It is shell operator which is used to run a process in the background.

Syntax : <command> &

Example : $ sort emp.txt &

Command : nohup

Purpose : It permits the execution of the process even after the user has logged out.

Syntax : nohup <command>

Example : $ nohup sort emp.txt ( result is available on nohup.out )

Command : kill

Purpose : It is used to terminate the process.


Syntax : kill <PID>
Example : $ kill 105

Command : kill $!

Purpose : $! is the system variable which stores the process id of the last background
job. The command kill $! is used to kill the last process.

Syntax : kill $!

Example : $ kill $!

Command : at

Purpose : It is used to execute the process at the time specified.

Syntax : echo <time>

Example : $ at 14:08 (or) $ at 3 PM (or) $ at 4 :50 AM

RESULT:
Ex.No. 3 (A) SIMPLE SHELL PROGRAMS
DATE:
Getting and displaying the academic and personal details

AIM:

ALGORITHM:

1. Get name, age, and address from the user.


2. Print that message as similar.
3. Get mark1, mark2, and mark3 from the user.
4. Print that message as similar.

PROGRAM:

echo –n “Enter the name” read s

echo –n “Enter the age” read a

echo –n “Enter the address” read adr

echo –n “The name is $s”


echo –n “The age is $a”

echo –n “The address is $adr”


echo –n “Enter the mark1” read m1

echo –n “Enter the mark2” read m2

echo –n “Enter the mark3” read m3

echo –n “The mark1 is $m1”


echo –n “The mark2 is $m2”
echo –n “The mark3 is $m3”

RESULT:
Ex No. 3 (B)
DATE: ARITHMETIC OPERATION

AIM:

ALGORITHM:

1. Read two numbers from the user.


2. Add both the numbers.
3. Print the summation of two numbers.
4. Subtract the second number from the first number
5. Print the result.
6. Multiply the two numbers
7. Print the result.
8. Divide the first number by second number.

PROGRAM:

echo enter the numbers


read n1 n2
n3=`expr $n1 + $n2`
n4=`expr $n1 - $n2`
n5=`expr $n1 \* $n2`
n6=`expr $n1 / $n2`
echo summation of $n1 and $n2 is $n3
echo difference of $n1 and $n2 is $n4
echo multiplication of $n1 and $n2 is $n5
echo division of $n1 by $n2 is $n6

RESULT:
PROGRAMS USING CONDITIONAL STATEMENTS

Ex. No .3(C) ODD (or) EVEN


DATE:

AIM:

ALGORITHM:

1. Get a number from the user.


2. Divide the number by 2.
3. Check the remainder of the division.
4. If it is zero then the given number is even.
5. Otherwise the given number is odd.

Program

echo –n “enter the number”


read num
x = `expr $num % 2`
if [ $x –eq 0 ]
then
echo “$x is an even number”
else
echo “$x is a odd number”
fi

RESULT:
Ex. No. 3(D) POSITIVE OR NEGATIVE NUMBER
DATE:

AIM:

ALGORITHM:

1. Get a number from the user.


2. Check the number is greater than or less than zero or equal to zero.
3. If it is greater than zero then the given number is +ve.
4. If it is less than zero then the given number is –ve. Otherwise the given number is
zero.

PROGRAM:

echo –n “enter the number”


read num
if [ $num –gt 0 ] then
echo “$num is a positive number”
elif [ $num –lt 0 ]
then
echo “$num is a negative number”
else
echo “$num is zero”
fi

RESULT:
Ex. No: 3(E) BIGGEST AMONG THREE NUMBERS
DATE:

AIM:

ALGORITHM:

1. Get three numbers from the user.


2. If the 1 st number is greater than other two numbers then the 1 st number is a biggest
numbers.
3. Otherwise if the 2 nd number is greater than 3 rd number then the 2 nd number is a
biggest number.
4. Otherwise 3 rd number is a biggest number.

PROGRAM:

echo -n "Enter the numbers"


read num1 num2 num3

if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]


then
echo "$num1 is a biggest number"
elif [ $num2 -gt $num3 ] then
echo "$num2 is a biggest number"
else
echo "$num3 is a biggest number"
fi

RESULT:
Ex No.3(F) ARITHMETIC OPERATION - SWITCH CASE
DATE:

AIM:

ALGORITHM:

1. Get two numbers from the user.


2. Select the options .
3. Execute arithmetic operations using case statements..
4. Print the output

PROGRAM:

echo “Enter two numbers” read a b


echo “menu” echo “1.add” echo
“2.sub” echo “3.mul” echo “ 4. div”

echo “Enter your option” read option


case $option in
1) c=`expr $a + $b`;;
2) c=`expr $a - $b`;;
3) c=`expr $a \* $b`;;
4) c=`expr $a / $b`;;
esac
echo $c

RESULT:
Ex No.3(G) FINDING AREA OF CIRCLE, SQUARE ,RECTANGLE AND TRIANGLE
DATE:

AIM:

ALGORITHM:

1. Get two numbers from the user.


2. Select the option.
3. Execute the statement to find the area using case statements..
4. Print the output

PROGRAM:

echo 1.circle
echo 2. rectangle
echo 3.square
echo 4.triangle

echo enter your choice


read opt
case $opt in
1) echo enter the radius
read r
area=`expr $r \* $r \* 22 / 7`
echo $area;;
2) echo enter the length and breadth
read l b
area=`expr $l \* $b`
echo $area;
3) echo enter the side
read s
area=`expr $s \* $s`
echo $area;;
4) echo enter the height and breadth
read b h
area=`expr $b \* $h \* 1 / 2`
echo $area;;
TESTING AND LOOPS

Ex. No.3(H)
DATE: Fibonacci Series

AIM:

ALGORITHM:

1. Get the value n from the user.


2. Initial numbers of fibonacci series is 0,1.
3. Using the sum of last adjacent two numbers generate a next number in the series.
4. Repeat the step 3 until n numbers are generated.

PROGRAM:

echo -n "enter the limit"


read n
echo "the fibonacci series is : "
b=0
c=1
i=0
if [ $n -ge 2 ] then
echo -n " $b $c"
n=`expr $n - 2`
while [ $i -lt $n ] do
a=`expr $b + $c`
b=$cc=$a
echo -n " $c"
i=`expr $i + 1`
done
fi

RESULT:
Ex. No.3(I)

DATE: ARMSTRONG NUMBER

AIM:

ALGORITHM:

1. Get the number from the user.


2. Extract each and every digit of the given number.
3. Find the cube of every digit and calculate the sum of all cubes.
4. Check whether the sum is equal to the given number, if it is equal, the given
number is Armstrong number otherwise not.

PROGRAM:

echo -n "enter the number"


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

RESULT:
Ex. No.3(J)
DATE: SUM OF N EVEN NUMBERS

AIM:

ALGORITHM:

1. Start the program


2. Read the number
3. Initialize i=2
4. Calculate sum=sum+1
5. Print the sum
6. Stop the program execution

PROGRAM:

echo “enter the number of upper limit”


read n
I=2
C=0
while [ $I –lt $n ] do
echo “number is $I”
I=`expr $I + 2’
C=`expr $I + $C’
done
echo “ the sum is $c”

RESULT:
Ex. No: 3(K)
DATE: COMBINATIONS OF 1 2 3

AIM:

ALGORITHM:

1. Assign values 1 2 3 to first for loop


2. Form another for loop within the first for loop and assign values 1 2 3
3. Within the second for loop execute another for loop with values 1 2 3
4. Close all for loops
5. Display the outputs

PROGRAM:

for I in 1 2 3 do
for j in 1 2 3 do
for k in 1 2 3 do
echo $I $j $k
done
done
done

RESULT:
Ex. No: 3(L) Nth POWER OF THE GIVEN NUMBER
DATE:

AIM:

ALGORITHM:

1. Get the value for the power &amp; number to be computed.


2. Initialize the value for I as 2.
3. Set the variable ans as a.
4. Repeat the steps 5,6 until the value of I ie less than or equal to the power value.
5. Multiply the value of ans and a.
6. Increment the I value.
7. Display the value ans as result

program
echo enter the number and its power
read a pow
i=2
ans=$a
while [ $i -le $pow ] do
ans=`expr $ans \* $a`
i=`expr $i + 1`
done
echo $ans

RESULT:
COMMAND LINE SUBSTITUTION
Ex. No: 3(M) Palindrome checking
DATE:

AIM:

ALGORITHM:

1. Get the filename and string in a command line.


2. Reverse the original string.
3. If the original string is equal to the reversed string then display a given string is
palindrome.
4. Otherwise, displaying a given string is not palindrome.

PROGRAM:

#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
printf("Enter a string \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}
[it@localhost ~]$ ./a.out
Enter a string
how

RESULT:
Ex. No:3(N) CONCATENATION OF TWO STRINGS
DATE:

AIM:

ALGORITHM:

1. Get the two strings in command line.


2. Copy the two arguments in two variables from the command line.
3. Concatenate the two strings using the built – in concatenate function.
4. Display the resultant string.

PROGRAM:

#include <stdio.h>
#include <string.h>
main(int argc, char *argv[])

{
char str1[10],str2[20],str3[10];
strcpy(str1,argv[1]);
strcpy(str2,argv[2]);
printf("%s",strcat(str1,str2));
}

RESULT:
C PROGRAMMING ON UNIX

Ex.no.4(A) BIGGEST AMONG ‘N’ NUMBERS USING FUNCTIONS


DATE:

AIM:

ALGORITHM:

1. Get the value of n as input.


2. Get the list of numbers in an array of size n.
3. Set the initial value as a biggest value.
4. Set the value of I is 1.
5. Compare it with other elements in an array.
6. If the biggest element is less than array element then interchange both the values.
7. Repeat steps 4 &amp; 5 until I reaches n-1.
8. Display the biggest value.

PROGRAM:

#include<stdio.h>
#include<conio.h>
float largest(float a[],int n);
void main()
{
int n;
float value[10];
clrscr();

printf("enter the value of n\n");


scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%f",&value[i]);
}
printf("The Largest number among set of numbers is\n");
printf("%f",largest(value,n));
getch();
}
float largest(float a[],int n)
{
int i; float max;
max=a[0];

for(i=1;i<n;i++)
{
if(max<a[i])
{
max= a[i];
}
}
return(max);
}

RESULT:
Ex.No.4(B) SWAPPING OF TWO NUMBERS USING POINTERS
DATE:

AIM:

ALGORITHM:

1. Declare the two pointer variables.


2. Get the value for both the variables.
3. Swap the values.
4. Print the result.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void swap1(int,int);
void main()
{
int a,b,c,d;
printf("Enter the values of a and b:= ");
scanf("%d %d",&a,&b);
printf("Enter the values of c and d:= ");
scanf("%d %d",&c,&d);
printf("\n BEFORE SWAPPING : ");
printf("\n The value of a and b is : %d\t %d ",a,b);
printf("\n The value of c and d is : %d\t %d ",c,d);
printf("\n AFTER SWAPPING : ");
swap(a,b);
swap1(&c,&d);
printf("\n Method is:-Call by Value");
printf("\n **************************");
printf("\n The value of a and b is : %d\t %d",a,b);
printf("\n Method is:-Call by Address or Reference");
printf("\n ***************************");
printf("\n The value of c and d is : %d\t %d",c,d);
}
void swap(int *c,int *d)
{ int t; t=*c; *c=*d; *d=t; }
void swap1(int a,int b)
{ int t; t=a; a=b; b=a; }
Ex.No.5(A) Is- SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the directory name.
3. Check the directory in unix.
4. Display the directory name.
5. Stop.

PROGRAM:

#include<dirent.h>
#include<stdio.h>
main()
{
char dirname[10];
DIR *p;
struct dirent *d;
printf("Enter directory name");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find dir.");
exit(-1);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
}

RESULT:
Ex.No.5(B) grep – SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the file name.
3. Read the pattern to be searched.
4. Check whether the pattern present in the input file or not.
5. If present, display the line which has the pattern.
6. Stop.

PROGRAM:

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

RESULT:
Ex.No.5(C) execl – SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Assign fork() to pid.
3. If pid equals to zero, then it becomes child process.
4. Else cannot create process.
5. Else it is parent.
6. Stop.

PROGRAM:

#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf(“child process”);
execl(“/bin/ls”,”ls”,”-r”,(char*)0);
}
else if(pid<0)
{
printf(“cannot create process”);
}
else
{
printf(“parent process”);
}
}

RESULT:
Ex.No.5(D) execv – SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Assign fork() to pid.
3. If pid equals to zero the display the files in long format.
4. Else display parent or cannot create process.
5. Stop.

PROGRAM:

#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
char *argv[]={“ls”,”-l”,”-r”,(char*)0};
execv(“bin/ls”,argv);
printf("child process");
}
else if(pid<0)
{
printf("cannot create");
}
else
{
printf("parent process");
}
}

RESULT:
Ex.No.5(E) wait – SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Assign pid as fork().
3. If it equals to zero then print child process.
4. Else wait for some time and display parent process.
5. Stop

PROGRAM:

#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("child process");
}
else if(pid<0)
{
printf("cannot create");
}
else
{wait();
printf("parent process");
}
}

RESULT:
Ex.No.5(F) getpid – SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Assign fok() to pid.
3. If pid equals to zero, then call sleep with 3 minutes and print the pid for parnt and child
process.
4. Else print the original process id and their parent id.
5. Stop.

PROGRAM:

#include<stdio.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
sleep(3);
printf(“parent process id is %d”,getppid());
printf("child process id is %d”,getpid());
}
else if(pid<0)
{
printf("cannot create");
}
else
{ printf(“parent process id is %d”,getppid());
printf("child process id is %d”,getpid());
}
}

RESULT:
Ex.No.5(G) lstat – SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Read file name to display the status.
3. Display file name, uid,gid, file size and inode number.
4. Stop.

PROGRAM:

#include<stdio.h>
#include<sys/stat.h>
main(int argc,char **argv)
{
struct stat statbuf;
if(lstat(argv[1],&statbuf)==-1)
printf("Error : cannot find file information ");
else
{
printf("\n File %s ", argv[1]);
printf("\n Inode number %d",statbuf.st_ino);
printf("\n UID %d",statbuf.st_uid);
printf("\n GID %d",statbuf.st_gid);
printf("\n File size in bytes %d",statbuf.st_size);
}
}

RESULT:
Ex.No.5(H) OPEN,READ AND CLOSE DIRECTORY - SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the directory name.
3. Display the directory name with ino.
4. Stop.

PROGRAM:

#include<stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
{
printf("a single argument (the directory name) is required\n");
exit(1);
}
if ( (dp = opendir(argv[1])) == NULL)
{
printf("can't open %s\n",argv[1]);
exit(1);
}
while ( (dirp = readdir(dp)) != NULL)
printf("%s %d\n",dirp->d_name,dirp->d_ino);
closedir(dp);
exit(0);
}

RESULT:
Ex.No.5(I) OPEN,READ AND WRITE - SYSTEM CALLS
DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the source and destination file name.
3. Open source file and copy the contents into destination file.
4. Open the destination file in write mode and copy the contents.
5. Close both files
6. Stop.

PROGRAM:

#include<stdio.h>
#include<fcntl.h>
main()
{
char buf[1000],fn1[10],fn2[10];
int fd1,fd2,n;
printf("Enter source file name ");
scanf("%s",fn1);
printf("Enter destination file name “):
scanf("%s",fn2);
fd1=open(fn1,O_RDONLY);
n=read(fd1,buf,1000);
fd2=open(fn2,O_CREAT|0666);
n=write(fd2,buf,n);
close(fd1);
close(fd2);
}

RESULT:
Ex.No 6(A) ROUND ROBIN SCHEDULING
DATE;

AIM:

ALGORITHM:

1. Start.
2. Read the no.of process, time quantum.
3. Read the processes arrival time and burst time.
4. Based on the round robin scheduling display the job scheduling process.
5. Stop.

PROGRAM:
#include<stdio.h>
int main()
{
int g[20],p[20],w[20],t[20],a1[20],tq,a=0,q[20],wait_tot=0,turn_tot=0,tot_brust=0,
s=0,n,i,j,p1[20];
float wait_avg,turn_avg;
printf("\nROUND ROBIN SCHEDULING\n");
printf("\nEnter the no of process : ");
scanf("%d",&n);
printf("\nEnter the time quantum : ");
scanf("%d",&tq);
for(i=1;i<=n;i++)
{
printf("\nEnter the burst time for p%d : ",i);
scanf("%d",&p[i]);
printf("\nEnter the arrival time for p%d : ",i);
scanf("%d",&a1[i]);
tot_brust+=p[i];
p1[i]=p[i];
}
g[0]=0;
w[1]=0;
do
{
for(i=1;i<=n;i++)
{
if(p[i]>tq)
{
p[i]=p[i]-tq;
a=a+1;
g[a]=g[a-1]+tq;
q[a]=i;
}
else if((p[i]<=tq)&&(p[i]!=0))
{
a=a+1;
g[a]=g[a-1]+p[i];
q[a]=i;
w[i]=g[a]-p1[i];
t[i]=g[a];
p[i]=0;
s++;
} }
}while(s!=n);
for(i=1;i<=n;i++)
{
w[i]=w[i]-a1[i];
t[i]=t[i]-a1[i];
wait_tot+=w[i];
turn_tot+=t[i];
}
wait_avg=wait_tot/n;
turn_avg=turn_tot/n;
printf("\nGANTT CHART");
printf("\n----------------- \n");
for(i=1;i<=a;i++)
printf("\t\tp%d",q[i]);
printf("\n0\t");
for(i=1;i<=a;i++)
printf("\t\t%d",g[i]);
printf("\nPROCESS\tBURSTTIME\tARRIVALTIME\tWAITINGTIME\tTURNAROUNDTIME\n");
for(i=1;i<=n;i++)
printf("\np%d\t%9d\t%11d\t%11d\t%14d",i,p1[i],a1[i],w[i],t[i]);
printf("\nAverage waiting time : %f ms",wait_avg);
printf("\nAverage turn around time : %f ms",turn_avg);
}

RESULT:
Ex.No. 6(B) SHORTEST JOB FIRST SCHEDULING

DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the no.of process.
3. Read the processes arrival time and burst time.
4. Based on the shortest job first scheduling display the job scheduling process.
5. Stop.

PROGRAM:

#include<stdio.h>
main()
{
int i,j,temp,index[20],b[20],t[20],w[20],tot_burst=0,tot_wait=0,tot_turn=0;
float avg_wait,avg_turn,n;
int a[20],count=0,enter[20],c_wait[20],time=0,name[20];
printf("Enter the no of process : ");
scanf("%f",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time for p%d : ",i+1);
scanf("%d",&b[i]);
printf("Enter the arrival time for p%d : ",i+1);
scanf("%d",&a[i]);
tot_burst+=b[i];
enter[i]=0;
}
for(i=0;i<n;i++)
{
temp=0;
for(j=0;j<n;j++)
{
if((i!=j)&&(b[i]>=b[j]))
{
temp++;
}
if((i<j)&&(b[i]==b[j]))
{
temp--;
}

}
index[temp]=i;

for(i=0;count<n;i++)
{
for(j=0;j<n;j++)
{
if((time>=a[index[j]])&&(enter[index[j]]!=1))
{
enter[index[j]]=1;
time+=b[index[j]];
name[count]=index[j];
count++;
break;
}

}
}
c_wait[name[0]]=w[name[0]]=0;
tot_turn=t[name[0]]=b[name[0]];

for(i=1;i<n;i++)
{ c_wait[name[i]]=c_wait[name[i-1]]+b[name[i-1]];
w[name[i]]=c_wait[name[i]]-a[name[i]];
t[name[i]]=w[name[i]]+b[name[i]];
tot_turn+=t[name[i]];
tot_wait+=w[name[i]];
}
avg_wait=tot_wait/n;
avg_turn=tot_turn/n;
printf("\nGANTT CHART\n");
for(i=0;i<n;i++)
{
printf("\t\tp%d",name[i]+1);
}

printf("\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",c_wait[name[i]]);
}
printf("%d",tot_burst);
printf("\nProcess\tBurst Time\tArrival Time\tWait Time\tTurnaround Time\n");
for(i=0;i<n;i++)
{
printf("p%d\t%10d\t%12d\t%9d\t%15d\n",i,b[i],a[i],w[i],t[i]);
}
printf("Average Waiting Time = %f ms\nAverage Turnaround Time=%f
ms\n",avg_wait,avg_turn);
}

RESULT:
Ex.No. 6(C) FIRST COME FIRST SERVE
DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the no.of process.
3. Read the processes arrival time and burst time.
4. Based on the first come first scheduling display the job scheduling process.
5. Stop.

PROGRAM:

#include <stdio.h>
int wt[10],bt[10],at[10],tat[10],n;
float awt,atat;
void input()
{
printf("Enter Number of processes:");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("Enter Burst Time of process %d:",i+1);
scanf("%d",&bt[i]);
printf("Enter Arrival Time of process %d:",i+1);
scanf("%d",&at[i]);
}
}
void calculate()
{
wt[0]=0;
atat=tat[0]=bt[0];
int btt=bt[0];//to store total burst time sum
int i;
for(i=1;i<n;i++){
wt[i]=btt-at[i];
btt+=bt[i];
awt+=wt[i];
tat[i]= wt[i]+bt[i];
atat+=tat[i];
}
atat/=n;
awt/=n;
}
void display()
{
int i;
printf("PROCESS NO\tARRIVAL TIME.\tBURST TIME.\tWAITING TIME\tTURN AROUND
TIME.\n");
for(i=0;i<n;i++)
{
printf("%3d\t%11d\t%11d\t%11d\t%11d\n",i+1,at[i],bt[i],wt[i],tat[i]);
}
printf("Average Waiting Time: %f\nAverage Turn Around Time:%f",awt,atat);
}
int main()
{
printf("FCFS CPU Scheduling Algorithm\n");
input();
calculate();
display();
}

RESULT:
Ex.No. 6(D) PRIORITY SCHEDULING

DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the no.of process.
3. Read the processes Priority, arrival time and burst time.
4. Based on the Priority scheduling display the job scheduling process.
5. Stop.

PROGRAM:

#include<stdio.h>
main()
{
int i,j,temp,index[20],b[20],t[20],w[20],tot_wait=0,tot_turn=0;
float avg_wait,n,avg_turn;
int a[20],count=0,pri[20],enter[20],c_wait[20],time=0,tot_burst=0,name[20];
printf("Enter the no process:");
scanf("%f",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time for p%d : ",i+1);
scanf("%d",&b[i]);
printf("Enter the arrival time for p%d : ",i+1);
scanf("%d",&a[i]);
printf("Enter the priority for p%d : ",i+1);
scanf("%d",&pri[i]);
tot_burst+=b[i];
enter[i]=0;
}
for(i=0;i<n;i++)
{ temp=0;
for(j=0;j<n;j++)
{

if((i!=j)&&(pri[i]>=pri[j]))
{
temp++;
}
if((i<j)&&(pri[i]==pri[j]))
{
temp--;
}

}
index[temp]=i;

}
for(i=0;count<n;i++)
{
for(j=0;j<n;j++)
{
if((time>=a[index[j]])&&(enter[index[j]]!=1))
{
enter[index[j]]=1;
time+=b[index[j]];
name[count]=index[j];
count++;
break;
}

}
}
c_wait[name[0]]=w[name[0]]=0;
tot_turn=t[name[0]]=b[name[0]];

for(i=1;i<n;i++)
{ c_wait[name[i]]=c_wait[name[i-1]]+b[name[i-1]];
w[name[i]]=c_wait[name[i]]-a[name[i]];
t[name[i]]=w[name[i]]+b[name[i]];
tot_turn+=t[name[i]];
tot_wait+=w[name[i]];
}
avg_wait=tot_wait/n;
avg_turn=tot_turn/n;
printf("\nGANTT CHART\n");
for(i=0;i<n;i++)
{
printf("\t\tp%d",name[i]+1);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",c_wait[name[i]]);
}
printf("%d",tot_burst);
printf("\nProcess\tBurst Time\tArrival Time\tPriority\tWait Time\tTurnaround Time\n");
for(i=0;i<n;i++)
{
printf("p%d\t%10d\t%12d\t%8d\t%9d\t%15d\n",i,b[i],a[i],pri[i],w[i],t[i]);
}
printf("Average Waiting Time = %f \nAverage Turnaround Time=%f\n",avg_wait,avg_turn);

RESULT:
Ex.No. 7 (A) PRODUCER – CONSUMER PROBLEM USING SEMAPHORE

DATE:

AIM:

ALGORITHM:

1. Start
2. create semaphore using semget( ) system call
3. if successful it returns positive value
4. create two new processes
5. first process will produce
6. until first process produces second process cannot consume
7. End.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
#define num_loops 2
int main(int argc,char* argv[])
{
int sem_set_id;
int child_pid,i,sem_val;
struct sembuf sem_op;
int rc;
struct timespec delay;
clrscr();
sem_set_id=semget(ipc_private,2,0600);
if(sem_set_id==-1)
{
perror(“main:semget”);
exit(1);
}
printf(“semaphore set created,semaphore setid‘%d’\n ”, sem_set_id);
child_pid=fork();
switch(child_pid)
{
case -1: perror(“fork”);
exit(1);
case 0: for(i=0;i<num_loops;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(“producer:’%d’\n”,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++)
{
printf(“consumer:’%d’\n”,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14));
{
delay.tv_sec=0; delay.tv_nsec=10; nanosleep(&delay,null);
}
}
break;
}
return 0;
}

RESULT:
Ex.No. 7(B) INTER PROCESS COMMUNICATION USING SHARED MEMORY

DATE:

AIM:

ALGORITHM:

1. Start
2. create shared memory using shmget( ) system call
3. if success full it returns positive value
4. attach the created shared memory using shmat( ) system call
5. write to shared memory using shmsnd( ) system call
6. read the contents from shared memory using shmrcv( ) system call
7. End .

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<sys/shm.h>
#define shm_size 1024
int main(int argc,char * argv[])
{
key_t key;
int shmid;
char *data;
int mode;
if(argc>2)
{
fprintf(stderr,”usage:stdemo[data_to_writte]\n”);
exit(1);
}
if((shmid=shmget(key,shm_size,0644/ipc_creat))==-1)
{
perror(“shmget”);
exit(1);
}
data=shmat(shmid,(void *)0,0);
if(data==(char *)(-1))
{
perror(“shmat”);
exit(1);
}
if(argc==2)
printf(writing to segment:\”%s”\”\n”,data);
if(shmdt(data)==-1)
{
perror(“shmdt”);
exit(1);
}
return 0;
}

RESULT:
Ex.No. 7(C) BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE

DATE:

AIM:

ALGORITHM:

1. Start
2. Read the no.of process
3. Read the n no.of process allocated resources and maximum resources
4. Read the available vector.
5. Based on banker’s algorithm check whether the state is safe or not.
6. End.

PROGRAM:

#include<stdio.h>
Struct process
{
int allocation[3];
int max[3];
int need[3];
int finish;
}p[10];
int main()
{
int n,i,j,avail[3],work[3],flag,count=0,sequence[10],k=0;
printf(“\n Enter the number of process:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the %dth process allocated resources”,i);
scanf(“%d%d%d”,&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);
printf(“\n Enter the %d process maxium resources:”,i);
scanf(“%d%d%d”,&p[i].max[0],&p[i].max[1],&p[i].max[2]);
p[i].finish=0;
p[i].need[0]=p[i].max[0]-p[i].allocation[0];
p[i].need[0]=p[i].max[1]-p[i].allocation[1];
p[i].need[0]=p[i].max[2]-p[i].allocation[2];
}
printf(“\n Enter the available vector:”);
scanf(“%d%d%d”,&avail[1],&avail[2]);
for(i=0;i<3;i++);
work[i]=avail[i];
while(count!=n)
{
count=0
for(i=0;i<n;i++)
{
flag=1;
if(p[i].finish==0)
if(p[i].need[0]<=work[0])
if(p[i].need[1]<=work[1])
if(p[i].need[2]<=work[2])
{
for(j=0;j<3;j++)
work[j]+=p[i].allocation[j]
p[i].finish=1;
sequence[k++]=I;
flag=0;
}
if(flag==1)
count++;
}
}
for(i=0;i<n;i++)
if(p]i].finish==1)
count++;
printf(“\n The safe sequence is:\t”);
if(count++n)
for(i=0;i<k;i++)
printf(“ %d\t”,sequence[i]);
else
printf(“SYSTEM IS NOT IN A SAFE STATE \n\n”);
return 0;

RESULT:
Ex.No. 7(D) DEADLOCK DETECTION

DATE:

AIM:

ALGORITHM:

1. Start
2. Raed the total no of processes.
3. Enter claim and allocation matrix for the n no.of processes.
4. Read resource vector & available vector.
5. Based on deadlock avoidance check whether deadlock are caused or not.
6. Display the deadlocked process.
7. End.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int found,flag,l,p[4][5],tp,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("enter total no of processes");
scanf("%d",&amp;tp);
printf("enter claim matrix");
for(i=1;i&lt;=4;i++)
for(j=1;j&lt;=5;j++)
{
scanf("%d",&amp;c[i][j]);
}
printf("enter allocation matrix");
for(i=1;i&lt;=4;i++)
for(j=1;j&lt;=5;j++)
{
scanf("%d",&amp;p[i][j]);
}
printf("enter resource vector:\n");
for(i=1;i&lt;=5;i++)
{
scanf("%d",&amp;r[i]);
}
printf("enter availability vector:\n");
for(i=1;i&lt;=5;i++)
{
scanf("%d",&amp;a[i]);
temp[i]=a[i];
}
for(i=1;i&lt;=4;i++)
{
sum=0;
for(j=1;j&lt;=5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i&lt;=4;i++)
{
for(l=1;l&lt;k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j&lt;=5;j++)
if(c[i][j]&gt;temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j&lt;=5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j&lt;=tp;j++)
{
found=0;
for(i=1;i&lt;k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}
RESULT:
Ex.No. 7(E) THREADING & SYNCHRONIZATION APPLICATION DATE

AIM:

ALGORITHM:

1. Start the program.


2. Print the thread creating using no.of threads assign in the main program.
3. Call printhello () and display the thread statement
4. Else print error message.
5. Stop the program.

PROGRAM:
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
RESULT:
Ex.No. 8(A) MEMORY ALLOCATION MANAGEMENT – FIRST FIT
DATE:

AIM:

PROGRAM:
#include<stdio.h>
struct mainblock
{
int size,occupied;
struct mainblock *next;
}*blocks=NULL,*first=NULL;
struct process
{
int size,blockused,accomodated;
}p[10];
main()
{
int noblocks,noprocess,i,j,t,count=0;
struct mainblock *temp;
printf("\nEnter the no of blocks :");
scanf("%d",&noblocks);
blocks=(struct mainblock*)malloc(noblocks);
printf("\nEnter the size of blocks\n");
for(i=0;i<noblocks;i++)
{
scanf("%d",&t);
if(first==NULL)
{
first=(struct mainblock*)malloc(1);
first->size=t;
first->occupied=0;
first->next=NULL;
blocks=first;
}
else
{
temp=(struct mainblock*)malloc(1);
temp->size=t;
temp->occupied=0;
temp->next=NULL;
blocks->next=temp;
blocks=temp;
}
}
printf("\nEnter the no of process :");
scanf("%d",&noprocess);
printf("\nEnter the size of process \n");
for(i=0;i<noprocess;i++)
{
scanf("%d",&p[i].size);
p[i].accomodated=0;
}
for(i=0;i<noprocess;i++)
{
for(j=0,temp=first;j<noblocks&&p[i].accomodated!=1;j++,temp=temp->next)
{
if(p[i].size<=temp->size&&temp->occupied==0)
{
count++;
temp->occupied=1;
p[i].accomodated=1;
p[i].blockused=j+1;
printf("%d",i+1);
}
}
}
if(count>0)
{
printf("\nAccomodated process\t\tblock number");
for(i=0;i<noprocess;i++)
{
if(p[i].accomodated==1)
printf("\n\t%d\t\t\t%d",i+1,p[i].blockused);
}
}
if(count!=noprocess)
{
printf("\nProcess not accomodated");
for(i=0;i<noprocess;i++)
if(p[i].accomodated==0)
printf("%d",i+1);
}
}

RESULT:
Ex.No. 8(B) MEMORY ALLOCATION MANAGEMENT– BEST FIT
DATE:

AIM:

PROGRAM:
#include<stdio.h>
struct mainblock
{
int size,occupied;
struct mainblock *next;
}*block=NULL,*best=NULL;
struct process
{
int size,blockused,accommodated;
}p[10];
main()
{
int noblocks,noprocess,i,j,t1,t2,t3,count=0;
struct mainblock *temp1,*temp2;
printf(“\nEnter no of blocks :”);
scanf(“%d”,&noblocks);
blocks=(struct mainblock*)malloc(noblocks);
printf(“\nEnter the size of the blocks :\n”);
for(i=0;i<noblocks;i++)
{
scanf(“%d”,&t1);
if(best==NULL)
{
best=(struct mainblock*)malloc(1);
best->size=t1;
best->occupied=0;
best->next=NULL;
blocks=first;
}
else
{
temp1=(struct mainblock*)malloc(1);
temp1->size=t1;
temp1->occupied=0;
temp1->next=NULL;
blocks->next=temp1;
blocks=temp1;
}
}
printf(“\nEnter the no of process :”);

scanf(“%d”,&noprocess);
printf(“\nEnter the size of the process :”);
for(i=0;i<noprocess;i++)
{
scanf(“%d”,&p[i].size);
p[i].accomodated=0;
}
for(i=0;i<noprocess;i++)
{
for (j=0,temp1=best,t2=0;j<noblocks;j++,temp1=temp1->next)
{
if(p[i].size<=temp1->size&& temp1->occupied==0)
{
t2++;
if(t2==1)
{
t3=j+1;
temp2=temp1;
}
if(temp1->size<temp2->size)
{
temp2=temp1;
t3=j+1;
}
}
` if(t2>0)
{
count++;
temp2->occupied=1;
p[i].accomodated=1;
p[i].blockused=t3;
}
}
}
if(count>0)
{
printf(“\nAccomodated process\t\tBlock number”);
for(i=0;i<noprocess;i++)
if(p[i].accomodated==1)
printf(“\n%d\t\t%d”,i+1,p[i].blockused);
}
if(count!=noprocess)

{
printf(“\nProcess not accommodated”);
for(i=0;i<noprocess;i++)
if(p[i].accomodated==0)
printf(“\n%d”,i+1);
}
}

RESULT:
Ex.No. 8(C) MEMORY ALLOCATION MANAGEMENT – WORST FIT

DATE:

AIM:

PROGRAM:
#include<stdio.h>
struct mainblock
{
int size,occupied;
struct mainblock *next;
}*block=NULL,*worst=NULL;
struct process
{
int size,blockused,accommodated;
}p[10];
main()
{
int noblocks,noprocess,i,j,t1,t2,t3,count=0;
struct mainblock *temp1,*temp2;
printf(“\nEnter no of blocks :”);
scanf(“%d”,&noblocks);
blocks=(struct mainblock*)malloc(noblocks);
printf(“\nEnter the size of the blocks :\n”);
for(i=0;i<noblocks;i++)
{
scanf(“%d”,&t1);
if(worst==NULL)
{
worst=(struct mainblock*)malloc(1);
worst->size=t1;
worst->occupied=0;
worst->next=NULL;
blocks=worst;
}
else
{
temp1=(struct mainblock*)malloc(1);
temp1->size=t1;
temp1->occupied=0;

temp1->next=NULL;
blocks->next=temp1;
blocks=temp1;
}
}
printf(“\nEnter the no of process :”);
scanf(“%d”,&noprocess);
printf(“\nEnter the size of the process :”);
for(i=0;i<noprocess;i++)
{
scanf(“%d”,&p[i].size);
p[i].accomodated=0;
}
for(i=0;i<noprocess;i++)
{
for (j=0,temp1=worst,t2=0;j<noblocks;j++,temp1=temp1->next)
{
if(p[i].size<=temp1->size&& temp1->occupied==0)
{
t2++;
if(t2==1)
{
t3=j+1;
temp2=temp1;
}
if(temp1->size>temp2->size)
{
temp2=temp1;
t3=j+1;
}
}
` if(t2>0)
{
count++;
temp2->occupied=1;
p[i].accommodated=1;
p[i].blockused=t3;
}
}
}
if(count>0)
{

printf(“\nAccomodated process\t\tBlock number”);


for(i=0;i<noprocess;i++)
if(p[i].accomodated==1)
printf(“\n%d\t\t%d”,i+1,p[i].blockused);
}
if(count!=noprocess)
{
printf(“\nProcess not accommodated”);
for(i=0;i<noprocess;i++)
if(p[i].accomodated==0)
printf(“\n%d”,i+1);
}
}

RESULT:
Ex.No. 8(D) MEMORY MANAGEMENT USING PAGING TECHNIQUE
DATE:

AIM:

ALGORITHM:

1. Start the program.


2. Read the base address, page size, number of pages and memory unit.
3. If the memory limit is less than the base address display the memory limit is less
than limit.
4. Create the page table with the number of pages and page address.
5. Read the page number and displacement value.
6. If the page number and displacement value is valid, add the displacement value
with the address corresponding to the page number and display the result.
7. Display the page is not found or displacement should be less than page size.
8. Stop the program.

PROGRAM:

#include<stdio.h>
#include<unistd.h>
void main()
{
int b[20],n,i,pa,p,a,d;
printf("\nProgram for paging");
printf("\n enter the no.of pages");
scanf("%d",&n);
printf("\nEnter the base address:");
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
printf("\nEnter the logical address:");
scanf("%d",&d);
printf("\n enter the page no");
scanf("%d",&p);
for(i=1;i<=n;i++)
{
if(i==p)
{
pa=b[i]+d;
a=b[i];
printf("\n\tPageNo.\t BaseAdd. PhysicalAdd. \n\t %d \t %d \t %d \t ",p,a,pa);
exit(0);
}
}
printf("\nInvalid page");
}

RESULT:
Ex.No. 9(A) PAGE REPLACEMENT ALGORITHM – FIFO
DATE:

AIM:

ALGORITHM:

1. Start
2. Read no.of pages, frames and page size for each pages.
3. Check whether the page is already in frame or not.
4. If no pages fount then increase the page fault by replacing the pages based on fifo order.
5. Display the no.of page faults.
6. End.

PROGRAM:

#include<graphics.h>
#include<stdlib.h>
void main()
{
//declare pages for store page nos, frames to store frame details
int pages[10], frames[10][10],ppos[10],fillcount=0,least;
//no_p stands for no of pages, no_f stands for no of frames
int i,j,k,m,pos=0,no_p,no_f,faults=0;
int gd=DETECT,gm,no;
int x1=20,y1=100;
float ratio;
char found,
str[30],ch;
clrscr();
//GRAPHICS initialise method
initgraph(&gd,&gm,"..//bgi");
setbkcolor(BLUE);
puts("Enter no of pages do u have");
scanf("%d",&no_p);
puts("Enter no of frames do u have");
scanf("%d",&no_f);
//initializing each frame with 0
for(i=0;i<no_f;i++)
for(j=0;j<no_p;j++)
frames[i][j]=0;
for(i=0;i<no_p;i++)
{
puts("Enter page num");
scanf("%d",&pages[i]);
clrscr();
cleardevice();
x1=20,y1=100;
found='f';
for(j=0;j<no_f;j++)
{
if(i!=0) frames[j][i]=frames[j][i-1];
//checking whether page is there in frames or not
if(frames[j][i]==pages[i])
found='t';
}
//if PAGE is not there in frames
if(found=='f')
{
faults++;
fillcount++;
if(fillcount<=no_f)
{
frames[pos][i]=pages[i];
pos++;
}
else
{
for(j=0;j<no_f;j++)
ppos[j]=0;
for(j=0;j<no_f;j++)
{
for(k=i-3;k<i;k++)
{
if(frames[j][i]==pages[k])
ppos[j]=k;
}
}
least=ppos[0];
for(j=0;j<no_f;j++)
{
if(ppos[j]<least)
least=ppos[j];
}
for(j=0;j<no_f;j++)
if(pages[least]==frames[j][i])
pos=j;
frames[pos][i]=pages[i];
}
}
//printing frames each time we enter a no
settextstyle(2,0,6);
for(k=0;k<no_f;k++)
{
for(j=0;j<=i;j++)
{
rectangle(x1,y1,x1+40,y1+45);
if(frames[k][j]!=0)
{
//changing text color in case of replacement
if(j==i&&frames[k][j]==pages[i]&&found=='f')
setcolor(MAGENTA);
else
setcolor(WHITE);
itoa(frames[k][j],str,10);
outtextxy(x1+15,y1+15,str);
}
else
outtextxy(x1+10,y1+10,"");
setcolor(WHITE);
x1+=55;
}
y1+=45;
x1=20;
}
}
//printing page fault ratio
printf("/n/n page fault ratio=%f",(float)faults/(float)no_p);
getch();
}

RESULT:
Ex.No. 9(B) PAGE REPLACEMENT ALGORITHM– LRU

DATE:

AIM:

ALGORITHM:

1. Start
2. Read no.of pages, frames and page size for each pages.
3. Check whether the page is already in frame or not.
4. If no pages fount then increase the page fault by replacing the pages based on LRU order.
5. Display the no.of page faults.
6. End.

PROGRAM:

#include<graphics.h>
#include<stdlib.h>
void main()
{
//declare pages for stores page nos, frames to store frame details
int pages[10],frames[10][10],ppos[10],fillcount=0,least;
//no_pstands for no of pages, no_f stands for no of frames
int i,j,k,m,pos=0;
no_p,no_f,faults=0;
gd=DETECT,gm,no;
int x1=20,y1=100;
float ratio;
char found,
str[30],ch,occur;
clrscr();
//GRAPHICS initialise method
initgraph(&gd,&gm,"..//bgi");
setbkcolor(BLUE);

puts("Enter no of pages do u have");


scanf("%d",&no_p);
puts("Enter no of frames do u have");
scanf("%d",&no_f);
//initializing each frame with 0
for(i=0;i<no_f;i++)
for(j=0;j<no_p;j++)
frames[i][j]=0;
for(i=0;i<no_p;i++)
{
puts("Enter page num");
scanf("%d",&pages[i]);
clrscr();
cleardevice();
x1=20,y1=100;
found='f';
for(j=0;j<no_f;j++)
{
if(i!=0) frames[j][i]=frames[j][i-1];
//checking whether page is there in frames or not
if(frames[j][i]==pages[i])
found='t';
}
//if PAGE is not there in frames
if(found=='f')
{
faults++;
fillcount++;
if(fillcount<=no_f)
frames[pos][i]=pages[i];
pos++;
}
else
{
for(j=0;j<no_f;j++)
ppos[j]=0;
for(j=0;j<no_f;j++)
{
for(k=0;k<i;k++)
{
if(frames[j][i]==pages[k])
ppos[j]++;
}
}
least=ppos[0];
for(j=0;j<no_f;j++)
{
if(least>ppos[j])
least=ppos[j];
}
occurs='n';
for(j=0;j<1&&occur=='n';j++)
{
for(k=0;k<no_f;k++)
{
if(pages[j]==frames[k][i]&&ppos[k]==least)
{
pos=k;
occur='y';
}
}
}
frames[pos][i]=pages[i];
}
}
settextstyle(2,0,6);
for(k=0;k<no_f;k++)
{
for(j=0;j<=i;j++)
{
rectangle(x1,y1,x1+40,y1+45);
if(frames[k][j]!=0)
{
if(j==i&&frames[k][j]==pages[i]&&found=='f')
setcolor(MAGENTA);
else
setcolor(WHITE);
itoa(frames[k][j],str,10);
outtextxy(x1+15,y1+15,str);
}
else
outtextxy(x1+10,y1+10,"");
setcolor(WHITE);
x1+=55;
}
y1+=45;
x1=20;
}
}
printf("page fault ration %f",(float)faults/(float)no+p);
getch();
}

RESULT:
Ex.No. 9(C) PAGE REPLACEMENT ALGORITHM – LFU

DATE:

AIM:

ALGORITHM:

1. Start
2. Read no.of pages, frames and page size for each pages.
3. Check whether the page is already in frame or not.
4. If no pages fount then increase the page fault by replacing the pages based on LFU order.
5. Display the no.of page faults.
6. End.

PROGRAM:

#include<graphics.h>

#include<stdlib.h>
void main()
{
//declare pages for stores page nos, frames to store frame details
int pages[10],frames[10][10];
//no_pstands for no of pages, no_f stands for no of frames
int i,j,k,m,pos=0;
no_p,no_f,faults=0;
gd=DETECT,gm,no;
int x1=20,y1=100;
float ratio;
char found,
str[30],ch;
clrscr();
//GRAPHICS initialise method
initgraph(&gd,&gm,"..//bgi");
setbkcolor(BLUE);
puts("Enter no of pages do u have");
scanf("%d",&no_p);

puts("Enter no of frames do u have");


scanf("%d",&no_f);
//fill all frames with 0
for(i=0;i<no_f;i++)
for(j=0;j<no_p;j++)
frames[i][j]=0;
for(i=0;i<no_p;i++)
{
puts("Enter page num");
scanf("%d",&pages[i]);
clrscr();
cleardevice();
x1=20,y1=100;
found='f';
for(j=0;j<no_f;j++)
{
if(i!=0) frames[j][i]=frames[j][i-1];
//checking whether page is there in frames or not
if(frames[j][i]==pages[i])
found='t';
}
//if PAGE is not there in frames
if(found=='f')
{
frames[pos][i]=pages[i];
faults++;
if(pos<no_f)
pos++;
else
pos=0;
}
//printing frames each time we enter a no
settextstyle(2,0,6);
for(k=0;k<no_f;k++)
{
for(j=0;j<=i;j++)
{
rectangle(x1,y1,x1+40,y1+45);
if(frames[k][j]!=0)
{
//changing the text color when page is replaced
if(j==i&&frames[k][j]==pages[i]&&found=='f')
setcolor(MAGENTA);
else
setcolor(WHITE);
itoa(frames[k][j],str,10);
outtextxy(x1+15,y1+15,str);
}
else
outtextxy(x1+10,y1+10,"");
setcolor(WHITE);
x1+=55;
}
y1+=45;
x1=20;
}
}
//printing page fault ratio
printf("page fault ration %f",(float)faults/(float)no_p);
Ex.No. 10(A) FILE ORGANIZATION TECHNIQUE – SINGLE LEVEL DIRECTORY
DATE:

AIM:

ALGORITHM:

1. Start
2. Read the no of files and store it as in count variable.
3. Read the root directory
4. Read the files to be inserted.
5. Display the structure.
6. End.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
cleardevice();
setbkcolor(GREEN);
puts("Enter no of files do u have?");
scanf("%d",&count);
for(i=0;i<count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("Enter file %d name",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"Root Directory");
setcolor(BLUE);
for(j=0;j<=i;j++,cir_x+=mid)
{
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[j]);
}
getch();
}
}

RESULT:
Ex.No. 10(B) FILE ORGANIZATION TECHNIQUE – TWO LEVEL DIRECTORY

DATE:

AIM:

ALGORITHM:

1. Start
2. Read the root and first level directory
3. Read the respective files to be stored in root and first level directories.
4. Display the two level directory structures.
5. End.

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,"c:\tc\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
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");
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;
}
}
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);
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]);
}
}}

RESULT:
Ex.No. 10(C) FILE ORGANIZATION TECHNIQUE –HIERARCHICAL

DIRECTORY

DATE:

AIM:

ALGORITHM:

1. Start
2. Read the root, first and second level directory
3. Read the respective files to be stored in root, first, second level directories.
4. Display the hierarchical structure for giving the input.
5. End.

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,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
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);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*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("No of sub directories/files(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;
}
}
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);
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]);
}
}
}
Ex.No. 10(D) FILE ORGANIZATION TECHNIQUE – DAG

AIM:

ALGORITHM:

1. Start
2. Read the root, first, second and three level directories.
3. Read the respective files to be stored in root, first, second and three level directories.
4. Display the direct acyclic graph structures.
5. End.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.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;
typedef struct
{
char from[20];
char to[20];
}link;
link L[10];
int nofl;
node * root;
void main()
{
int gd=DETECT,gm;
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("how many links");
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
{
printf("File/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name:");
fflush(stdin);
gets(L[i].to);
}
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nofl;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);
}
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmpi(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);
}}}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
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);
printf("enter 1 for dir/ 2 for file:");
scanf("%d",&(*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("no of sub directories /files (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,l x+gap*i+gap, lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
/* displays the constructed tree in graphics
mode */ 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);
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]); }}}
Ex.No. 11(A) CONTINUOUS MEMORY ALLOCATION
DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the no.of blocks.
3. Read the processes size and name to insert.
4. Check the continuous availability of blocks.
5. Allocate the process continuously.
6. Display the allocated process.
7. Stop.

PROGRAM:

#include<stdio.h>
#include<string.h>
struct block
{
int b_id;
int b_alloted;
};
struct block b[50];
void main()
{
int i,no,n,bname,j,count=0,eblock,sblock,ch,num;
int psize,flag=1,pname;
printf("Enter no of blocks");
scanf("%d",&n);
for(i=0;i<n;i++)
{
b[i].b_id=i;
b[i].b_alloted=0;
}
do
{

printf("\n 1.Insert 2.Delete 3.Exit");


scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter process name:");
scanf("%d",&pname);
printf("\nEnter process size:");
scanf("%d",&psize);
for(i=0;i<n;i++)
{
if(b[i].b_alloted==0)
{
count++;
if(count==1)
{
sblock=i;
}
if(count==psize)
{
eblock=i;
for(j=sblock;j<=eblock;j++)
b[j].b_alloted=pname;
printf("\n Process %d is alloted to blocks from %d to %d \n",pname,sblock,eblock);
i=n+1;
flag=1;
}
}
else
{
count=0;
flag=0;
}
}
if(flag==0)
printf("\nProcess not alloted");
for(i=0;i<n;i++)
printf("\n b[%d]-- %d\n",b[i].b_id,b[i].b_alloted);
break;
case 2:
printf("\nEnter the process no to be deleted");
scanf("%d",&num);
b[num].b_alloted=0;
for(i=0;i<n;i++)
printf("\n b[%d]--%d\n",b[i].b_id,b[i].b_alloted);
break;
case 3:
exit(0);
}
}
while(ch!=3);
}

RESULT:
Ex.No. 11(B) INDEXED MEMORY ALLOCATION

DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the no.of blocks.
3. Read the allocated blocks and yet to be allocated.
4. Read the processes size and name to insert.
5. Check the free blocks.
6. Index the free blocks with availability.
7. Display the allocated process.
8. Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
int b_allocated[30];
void display(int n)
{
int i,count=0;
printf("\n\n");
for(i=0;i<n;i++)
{
if(count<3)
{
printf("\tb[%2d]--%d",i,b_allocated[i]);
count=count+1;
}
else
{
printf("\n\tb[%2d]--%d",i,b_allocated[i]);
count=1;
}
}
}
main()
{
int j=0,index[50],indexloc,i,n,no,psize,pname,block,freespace=0,count=0;
printf("Enter the no of blocks");
scanf("%d",&n);
freespace=n;
printf("Enter the no of blocks allocated");
scanf("%d",&no);
for(i=0;i<no;i++)
{
b_allocated[i]=0;
}
printf("Enter the no of blocks that are allocated");
for(i=0;i<no;i++)
{
scanf("%d",&block);
b_allocated[block]=1;
freespace=freespace-1;
}
printf("\nFreespace=%d\n",freespace);
display(n);
printf("Enter the name of the process");
scanf("%d",&pname);
printf("Enter the size of the process");
scanf("%d",&psize);
if(psize<freespace)
{
for(i=0;i<n;i++)
{
if(b_allocated[i]==0 && count<=psize)
{
if(count==psize)
{
b_allocated[i]=pname;
indexloc=i;
break;
}
else
{
b_allocated[i]=1;
index[i]=0;
index[j]=i;
j=j+1;
count=count+1;
}
}
}
display(n);
printf("\n\nBlocks stored are\n");
for(i=0;i<j;i++)
printf("%d",index[i]);
printf("\nDirectory structure");
printf("\nProcess \t Index\n");
printf("\n%d\t%d",pname,indexloc);
}
else
{
printf("Process cannot be allocated");
}
getch();
}

RESULT:
Ex.No. 11(C) LINKED FILE ALLOCATION

DATE:

AIM:

ALGORITHM:

1. Start.
2. Read the no.of blocks.
3. Read the allocated blocks and yet to be allocated.
4. Read the processes size and name to insert.
5. Check the free blocks.
6. Index the free blocks with availability.
7. Display the allocated process.
8. Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n;
for(i=0;i<50;i++)
f[i]=0;
printf("enter how many blocks already allocated");
scanf("%d",&p);
printf("\nenter the blocks nos");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("enter index starting block & length"); scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)
{
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++;
}
}
}
else
printf("\nif u enter one more (yes-1/no-0)"); scanf("%d",&c);
if(c==1) goto X; else exit();
getch( );
}

RESULT:

You might also like