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

AL3452 OS Lab Manual (1)

The document outlines a series of experiments and exercises for students at Nadar Saraswathi College of Engineering and Technology, focusing on operating systems and UNIX commands. It includes detailed procedures for installing Windows, utilizing UNIX commands, and writing shell programs. Each section provides aims, commands, syntax, and expected results for various programming tasks and system operations.

Uploaded by

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

AL3452 OS Lab Manual (1)

The document outlines a series of experiments and exercises for students at Nadar Saraswathi College of Engineering and Technology, focusing on operating systems and UNIX commands. It includes detailed procedures for installing Windows, utilizing UNIX commands, and writing shell programs. Each section provides aims, commands, syntax, and expected results for various programming tasks and system operations.

Uploaded by

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

NADAR SARASWATHI COLLEGE

OF ENGINEERING AND
TECHNOLOGY
VADAPUDUPATTI, THENI-625531

NAME : __________________________________________________
REGISTER NUMBER : __________________________________________________
BRANCH : __________________________________________________
YEAR/SEM : __________________________________________________
SUB.CODE/SUB.NAME : ___________________________________________________
PAGE MARKS STAFF
EX.NO DATE NAME OF THE EXPERIMENT
NO OBTAINED SIGNATURE

1 Installation of windows operating


system

2a Illustrate UNIX commands

2b Shell Programming

Process Management using


3 System Calls : Fork, Exit, Getpid,
Wait, Close

Write C programs to implement


4a the various PRIORITY CPU
Scheduling Algorithms

4b ROUND ROBIN SCHEDULING


CPU Scheduling Algorithms

4c FCFS CPU Scheduling


Algorithms

4d SJF SCHEDULING CPU


Scheduling Algorithms

5 Illustrate the inter process


communication strategy

6 Implement mutual exclusion by


Semaphore

Write C programs to avoid


7 Deadlock using Banker’s
Algorithm
Write a C program to Implement
8 Deadlock Detection Algorithm

Write C program to implement


9 Threading

Implement the paging Technique


10 using C program
PAGE MARKS STAFF
EX.NO DATE NAME OF THE EXPERIMENT
NO OBTAINED SIGNATURE

Write C programs to implement


11 a the following Memory Allocation
Methods - FIRST FIT

Write C programs to implement


11 b the following Memory Allocation
Methods - WORST FIT

Write C programs to implement


11 c the following Memory Allocation
Methods - BESTFIT
Write C programs to implement
12 a the various Page Replacement
Algorithms
FIFO
Write C programs to implement
12 b the various Page Replacement
Algorithms
LRU
Write C programs to Implement
13 a the various File Organization
Techniques
SINGLE LEVEL
Write C programs to Implement
13 b the various File Organization
Techniques
TWO LEVEL
Implement the following File
14 a Allocation Strategies using C
programs SEQUENTIAL

Implement the following File


14 b Allocation Strategies using C
programs LINKED

Implement the following File


14 c Allocation Strategies using C
programs INDEXED

Write C programs for the


15 a implementation of various disk
scheduling algorithms
FIRST COME FIRST SERVED

Write C programs for the


15 b implementation of various disk
scheduling algorithms
C-SCAN ALGORITHM

Write C programs for the


16 implementation of various disk
scheduling algorithms
Ex.No:1 INSTALLING WINDOWS 10 IN SYSTEM

AIM :
To install the OS in the system using the USB Media

PROCEDURE

Creating an Installation Disc or Drive

1. Connect a blank USB flash drive or insert a blank writable DVD.

You can install Windows 10 by creating a bootable USB flash drive or DVD that contains the
Windows 10 installation files. You'll need a USB flash drive that's at least 8GB, or any blank DVD to
get started.
If you already have Windows 10 installed on the PC and just want to reinstall it, it'll be easiest to
reinstall it from within Windows 10 instead of creating installation media.

If you want to upgrade from Windows 7 or Windows 8.1, you won't need to create an installation
disc or drive. However, you will need to follow most of this method to start the upgrade.

2. . Make sure you have a product key.


If you bought Windows 10 through Microsoft using your Microsoft account, your product key is
already linked to your account. If you bought Windows 10 from another retailer, you'll have a
25-character product key that you'll need to have handy to activate Windows
If you don't have a product key or you're installing Windows 10 on a new hard drive, make sure
you've linked your Windows 10 digital license to your Microsoft account before you start the
installation.

Head to Settings > Update & Security > Activation from the current installation—if the activation
status says Windows is activated with a digital license, click Add an account and follow the
on-screen instructions to link your Microsoft account.

● If you're upgrading from an earlier version and your PC qualifies for a free upgrade, you
won't need a product key.
Go to https://www.microsoft.com/en-us/software-download/windows10%20. This is the official
download site for Windows 10.
Click Download tool now. This is a blue button in the middle of the page. This downloads the
Media Creation Tool, which you'll use to create your installation media (or start your upgrade.
Double-click the downloaded file. Its name begins with "MediaCreationTool" and ends with
".exe." You'll find it in your default download folder, which is usually called Downloads.
● Click Yes when prompted to allow the installer to run.

Select "Create installation media" and click OK. This option lets you create a Windows
installationdisc or drive that will work on any compatible PC, not just the one you're using now.

If you're updating your PC from an earlier version of Windows, select Upgrade this PC now instead,
and then follow the on-screen instructions to install Windows 10. You're done!
Select

your preferences and click Next. If you're installing Windows on the current PC, you can keep the
default options. If you need to install on a different PC, make sure you choose the language and edition
for which you have a license, and select the architecture (64-bit or 32-bit) that matches the PC you're
going to install on.

If you're not sure about the architecture, you can choose Both from the menu.
Create your installation media. The steps are a little different depending on what you're doing:

Flash drive: Select your flash drive from the list, click Next, and wait for the installation files to install.
When the process is complete, click Finish.
.
Booting from Windows 10 Installation Media

Boot the PC into the BIOS. If your PC is not already set up to boot from your flash or optical drive, rebooting
from your installation media won't work. You'll need to make a quick change in your BIOS to change the boot order.
There are a few ways to get in:
Wait for your computer to restart. Once your computer finishes restarting, you'll see a window here
with your geographical data. You're now ready to begin setting up your Windows 10 installation.
Follow the on-screen instructions to install Windows 10. You'll be asked to perform a few tasks, such
as connecting to Wi-Fi and choosing some preferences. Once the installation is complete, you'll have a
fresh new installation of Windows 10.

If you're upgrading from an earlier version of Windows, you'll be asked if you want to upgrade
the current operating system or do a custom install. If you choose Upgrade, you'll preserve
existing apps and files.

RESULT
Thus the windows Operating system installed successfully
Ex.No:2 a) Basics of UNIX commands.

AIM :
To Study about the UNIX Commands
COMMAND :
1.Date Command :
This command is used to display the current data and time.
Syntax :
$date
$date +%ch
Options : -
a = Abbreviated weekday.
A = Full weekday.
b = Abbreviated month.
B = Full month.
c = Current day and time.
C = Display the century as a decimal number.
d = Day of the month.
D = Day in „mm/dd/yy‟ format
h = Abbreviated month day.
H = Display the hour.
L = Day of the year.
m = Month of the year.
M = Minute.
P = Display AM or PM
S = Seconds
T = HH:MM:SS format u
= Week of the year.
y = Display the year in 2 digit.
Y = Display the full year.
Z = Time zone .
To change the format:
Syntax:
$date „+%H-%M-%S‟

2. Calender Command :
This command is used to display the calendar of the year or the particular month of
calendar year.
Syntax:
a. $cal <year>
b.$cal <month> <year>
Here the first syntax gives the entire calendar for given year & the second Syntax
gives the calendar of reserved month of that year.
3. Echo Command:
This command is used to print the arguments on the screen.
Syntax: $echo <text>

Multi line echo command:


To have the output in the same line , the following commands can be used.\
Syntax: $echo <text\>text

To have the output in different line, the following command can be used.
Syntax: $echo “text
>line2
>line3”

4. Banner Command:
It is used to display the arguments in „#‟ symbol.
Syntax: $banner <arguments>

5. ’who’ Command:
It is used to display who are the users connected to our computer currently.
Syntax: $who – option‟s
Options: -
H–Display the output with headers.
b–Display the last booting date or time or when the system was lastely rebooted.

6. ’who am i’ Command:
Display the details of the current working directory.
Syntax: $who am i

7. ’tty’ Command:
It will display the terminal name.
Syntax: $tty
w
8. ’Binary’ Calculator Command:
It will change the „$‟ mode and in the new mode, arithematic operations such as +,-
,*,/,%,n,sqrt(),length(),=, etc can be performed . This command is used to go to the binary
calculus mode.
Syntax:
$bc operations^d$
Base:
1 base –inputbase
0 base – outputbase are used for base conversions. Decimal = 1

Binary = 2 Octal = 8 Hexa = 16


9. ’CLEAR’ Command:
It is used to clear the screen.
Syntax: $clear

10. ’MA N’ Command:


It help us to know about the particular command and its options & working. It is like
„help‟ command in windows .
Syntax: $man <command name>
Manipulation Command:
It is used to manipulate the screen.
Syntax: $tput <argument>

Arguments:
1. Clear – to clear the screen.
2. Longname – Display the complete name of the terminal.
3. SMSO – background become white and foreground become black color.
4. rmso – background become black and foreground becomes white color.
5. Cop R C – Move to the cursor position to the specified location.
6. Cols – Display the number of columns in our terminals.

11. LIST Command :


It is used to list all the contents in the current working directory.
Syntax: $ ls – options <arguments>
If the command does not contain any argument means it is working in the Current
directory.
Options:
a– used to list all the files including the hidden files. c–
list all the files columnwise.
d- list all the directories.
m- list the files separated by commas.
p- list files include „/‟ to all the directories.
r- list the files in reverse alphabetical order.
f- list the files based on the list modification date.
x-list in column wise sorted order.

DIRECTORY RELATED COMMANDS


1. Present Working Directory Command:
To print the complete path of the current working directory.
Syntax: $pwd

2. MKDIR Command:
To create or make a new directory in a current directory.
Syntax: $mkdir <directory name>
3. CD Command:
To change or move the directory to the mentioned directory.
Syntax: $cd <directory name.

4. RMDIR Command:
To remove a directory in the current directory & not the current directory itself.
Syntax: $rmdir <directory name>

FILE RELATED COMMANDS:


1. CREATE A FILE:
To create a new file in the current directory we use CAT command.
Syntax:
$cat > <filename.
The > symbol is redirectory we use cat command.

2. DISPLAY A FILE:
To display the content of file mentioned we use CAT command without „>‟ operator.
Syntax:
$cat <filename.
Options –s = to neglect the warning /error message.

3. COPYING CONTENTS:
To copy the content of one file with another. If file doesnot exist, a new file is created
and if the file exists with some data then it is overwritten.
Syntax:
$ cat <filename source> >> <destination
filename>
Options: - Syntax: $ cat <source filename> >> <destination
filename> it is avoid overwriting.

-n content of file with numbers included with


blank lines.

$cat –n <filename>

4. SORTING A FILE:
To sort the contents in alphabetical order in reverse order.
Syntax:
$sort <filename >
Option:
$ sort –r <filename>
COPYING CONTENTS FROM ONE FILE TO ANOTHER:
To copy the contents from source to destination file. So that both contents are same.
Syntax:
$cp <source filename> <destination filename>
$cp <source filename path > <destination filename path>
5. MOVE Command :
To completely move the contents from source file to destination file and to remove the
source file.
Syntax:
$ mv <source filename> <destination filename>

6. REMOVE Command :
To permanently remove the file we use this command.
Syntax:
$rm <filename>

7. WORD Command :
To list the content count of no of lines , words, characters .
Syntax: Options:
$wc<filename>

-c – to display no of characters.
-l – to display only the lines.
-w – to display the no of words.

8. LINE PRINTER:
To print the line through the printer, we use lp command.
Syntax:
$lp <filename>

9. PAGE Command:
This command is used to display the contents of the file page wise & next page can be
viewed by pressing the enter key.
Syntax:
$pg <filename>

10. FILTERS AND PIPES


HEAD: It is used to display the top ten lines of file.
Syntax: $head<filename>
TAIL: This command is used to display the last ten lines of file.
Syntax: $tail<filename>
PAGE: This command shows the page by page a screenfull of information is displayed after
which the page command displays a prompt and passes for the user to strike the enter key to
continue scrolling.
Syntax: $ls –a\p
MORE: It also displays the file page by page .To continue scrolling with more command ,
press the space bar key.
Syntax: $more<filename>
GREP: This command is used to search and print the specified patterns from the file.
Syntax: $grep [option] pattern <filename>
SORT: This command is used to sort the datas in some order.
Syntax: $sort<filename>

RESULT
Thus the Linux command executed successfully
Ex.No:2 b)
Shell Programming

AIM:

To write the shell programming in the command prompt

1. Write a Shell program to check the given number is even or odd

ALGORITHM:

SEPT 1: Start the program. STEP 2: Read the value of n.


STEP 3: Calculate „r=expr $n%2‟.
STEP 4: If the value of r equals 0 then print the number
is even
STEP 5: If the value of r not equal to 0 then print the
number is odd.

PROGRAM:

echo "Enter the Number" read n


r=`expr $n % 2` if [ $r -eq 0 ] then
echo "$n is Even number" else
echo "$n is Odd number" fi

2. Write a Shell program to check the given year is leap year or not

ALGORITHM:

SEPT 1: Start the program.

STEP 2: Read the value of year.

STEP 3: Calculate b=expr $y%4‟.

STEP 4: If the value of b equals 0 then print the year is a leap year

STEP 5: If the value of r not equal to 0 then print the year is not a leap year.
PROGRAM :

echo "Enter the year" read y

b=`expr $y % 4` if [ $b -eq 0 ] then

echo "$y is a leap year" else

echo "$y is not a leap year" fi

3. Write a Shell progrm to find the factorial Number

ALGORITHM:

SEPT 1: Start the program.


STEP 2: Read the value of n.
STEP 3: Calculate „i=expr $n-1‟.

STEP 4: If the value of i is greater than 1 then calculate „n=expr $n \* $i‟ and „i=expr $i – 1‟

STEP 5: Print the factorial of the given number

PROGRAM:

echo "Enter a Number" read n

i=`expr $n - 1` p=1

while [ $i -ge 1 ] do

n=`expr $n \* $i` i=`expr $i - 1` done

echo "The Factorial of the given Number is $n"


4. Write a Shell program to swap the two integers

ALGORITHM:

SEPT 1: Start the program.


STEP 2: Read the value of a,b.
STEP 3: Calculate the swapping of two values by using a temporary variable
temp.
STEP 4: Print the value of a and b.

PROGRAM:

echo "Enter Two Numbers" read a b

temp=$a a=$b b=$temp

echo "after swapping" echo $a $b

RESULT
Thus the Shell command executed successfully
Programs using the following system calls of UNIX operating system fork,
Ex.No:3
exec, getpid, exit, wait, close, stat, opendir, readdir

AIM:
To write C Programs using the following system calls of UNIX operating system
fork, exec, getpid, exit, wait, close, stat, opendir, readdir.

1. PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING SYSTEMS


(OPENDIR, READDIR, CLOSEDIR)

ALGORITHM:

STEP 1: Start the program.


STEP 2: Create struct dirent.
STEP 3: declare the variable buff and pointer dptr.
STEP 4: Get the directory name.
STEP 5: Open the directory.
STEP 6: Read the contents in directory and print it.
STEP 7: Close the directory.

PROGRAM:
#include<stdio
.h>
#include<diren
t.h> struct
dirent *dptr;
int main(int argc, char *argv[])
{
char
buff[10
0]; DIR
*dirp;
printf(“\n\n ENTER DIRECTORY
NAME”); scanf(“%s”, buff);
if((dirp=opendir(buff))==NULL)
{printf(“The given directory does not exist”); exit(1);
}while(dptr=readdir(dirp)){
printf(“%s\n”,dptr->d_name);
}closedir(dirp);
2. PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING
SYSTEM (fork, getpid, exit)

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables pid,pid1,pid2. STEP 3: Call fork() system call to create
process. STEP 4: If pid==-1, exit.
STEP 5: Ifpid!=-1 , get the process id using getpid(). STEP 6: Print the process id.
STEP 7:Stop the program

PROGRAM:
\#include<stdio.h> #include<unistd.h> main()
{
int pid,pid1,pid2; pid=fork(); if(pid==-1)
{
printf(“ERROR IN PROCESS CREATION \n”);
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf(“\n the parent process ID is %d\n”, pid1);
}
else
{
pid2=getpid();
printf(“\n the child process ID is %d\n”, pid2);}
RESULT
Thus the UNIX operating Program was executed successfully
CPU SCHEDULING ALGORITHMS
Ex.No:4 a)
PRIORITY

AIM:
To write a C program for implementation of Priority scheduling algorithms.

ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer, totwtime and totttime is equal
to zero. Step 3: Get the value of „n‟ assign p and allocate the memory.
Step 4: Inside the for loop get the value of burst time
and priority. Step 5: Assign wtime as zero .
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and assign as
turnaround time. Step 8: Stop the program.

PROGRAM:
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int pno; int pri; int pri; int btime;
int wtime;
}sp;
int main()
{
int i,j,n;
int
tbm=0,totwtime=0,tottti
me=0; sp *p,t;
printf("\n PRIORITY SCHEDULING.\n");
printf("\n enter the no of process. \n");
scanf("%d",&n);
p=(sp*)malloc(siz
eof(sp));
printf("enter the burst time and
priority:\n"); for(i=0;i<n;i++)
{
printf("process%d:”,i+1);
scanf("%d%d",&p[i].btime,
&p[i].pri);
p[i].pno=i+1;
p[i].wtime=0;
}
for(i=0;i
<n-1;i++
)
for(j=i+1
;j<n;j++)
{
if(p[i].pri>p[j].pri)
{
t=p[i]; p[i]=p[j]; p[j]=t;
}
}
printf("\n process\tbursttime\twaiting time\tturnaround
time\n"); for(i=0;i<n;i++)
{
totwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pno,p[i
].btime);
printf("\t\t%d\t\t%d",p[i].wtime,p[i].wtime+p[i].btime);
}
totttime=tbm+totwtime;
printf("\n total waiting time:%d",totwtime);
printf("\n average waiting
time:%f",(float)totwtime/n); printf("\n total
turnaround time:%d",totttime); printf("\n avg
turnaround time:%f",(float)totttime/n);
}
\

RESULT
Thus the Priority Scheduling Program was executed successfully
CPU SCHEDULING ALGORITHMS
Ex.No:4.b
ROUND ROBIN SCHEDULING

AIM:

To write a C program for implementation of Round Robin scheduling algorithms.

ALGORITHM:

Step 1: Inside the structure declare the variables.


Step 2: Declare the variable i,j as integer, totwtime and totttime is
equal to zero. Step 3: Get the value of „n‟ assign p and allocate the
memory.
Step 4: Inside the for loop get the value of burst time and priority and read the time
quantum. Step 5: Assign wtime as zero.
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and assign as
turnaround time. Step 8: Stop the program.

PROGRAM:

#include<stdio.h> #include<stdlib.h> struct rr


{
int pno,btime,sbtime,wtime,lst;
}p[10];
int main()
{
int pp=-1,ts,flag,count,ptm=0,i,n,twt=0,totttime=0; printf("\n round robin scheduling ");
printf("enter no of processes:"); scanf("%d",&n);
printf("enter the time slice:"); scanf("%d",&ts); printf("enter the burst time");
for(i=0;i<n;i++)
{
printf("\n process%d\t",i+1); scanf("%d",&p[i].btime);
p[i].wtime=p[i].lst=0; p[i].pno=i+1; p[i].sbtime=p[i].btime;printf("scheduling \n");
do
{
flag=0; for(i=0;i<n;i++)
{
count=p[i].btime; if(count>0)
{
flag=-1; count=(count>=ts)?ts:count; printf("\n process %d",p[i].pno); printf("from%d",ptm);
ptm+=count; printf("to%d",ptm);
p[i].btime-=count; if(pp!=i)
{
pp=i;
p[i].wtime+=ptm-p[i].lst-count; p[i].lst=ptm;
}
}

RESULT
Thus the Round Robin scheduling algorithms. Program was executed
successfully
CPU SCHEDULING ALGORITHMS
Ex.No:4.c
FCFS

AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.

ALGORITHM:

Step 1: Inside the structure declare the variables.


Step 2: Declare the variable i,j as integer,totwtime and totttime is
equal to zero. Step 3: Get the value of „n‟ assign pid as I and get the
value of p[i].btime.
Step 4: Assign p[0] wtime as zero and tot time as btime and inside the loop calculate
wait time and turnaround time.
Step 5: Calculate total wait time and total turnaround time by dividing by total
number of process.
Step 6: Print total wait time and total
turnaround time. Step 7: Stop the program.

PROGRAM:

#include<stdio.h> #include<stdlib.h> struct fcfs


{
int pid; int btime; int wtime; int ttime;
} p[10];
int main()
{
int i,n;
int towtwtime=0,totttime=0; printf("\n fcfs scheduling...\n"); printf("enter the no of process");
scanf("%d",&n); for(i=0;i<n;i++)
{
p[i].pid=1;
printf("\n burst time of the process”); scanf("%d",&p[i].btime);
}#include<stdio.h> #include<stdlib.h> struct fcfs
{
int pid; int btime; int wtime; int ttime;
} p[10];
int main()
{
int i,n;
int towtwtime=0,totttime=0; printf("\n fcfs scheduling...\n"); printf("enter the no of process");
scanf("%d",&n); for(i=0;i<n;i++)
{
p[i].pid=1;
printf("\n burst time of the process”); scanf("%d",&p[i].btime);
}
p[0].wtime=0;
p[0].ttime=p[0
].btime;
totttime+=p[i].
ttime;
for(i=0;i<n;i+
+)
{
p[i].wtime=p[i-1].wtime+p[i-1].btim
p[i].ttime=p[i].wtime+p[i].bti
me; totttime+=p[i].ttime;
towtwtime+=p[i].wtime;
}
for(i=0;i<n;i++)
{{
printf("\n waiting time for
process”); printf("\n turn around
time for process”); printf("\n");
}}
printf("\n total waiting time :%d", totwtime );
printf("\n average waiting time
:%f",(float)totwtime/n); printf("\n total turn
around time :%d",totttime);
printf("\n average turn around time: :%f",(float)totttime/n);
}

RESULT
Thus the FCFS Program was executed successfully
CPU SCHEDULING ALGORITHMS
Ex.No:4.d
SJF SCHEDULING

AIM:
To write a C program for implementation of SJF scheduling algorithms.

ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer,totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of p[i].btime.
Step 4: Assign p[0] wtime as zero and tot time as btime and inside the loop calculate wait time
and turnaround time.
Step 5: Calculate total wait time and total turnaround time by dividing by total number of
process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program.

PROGRAM:

#include<stdio.h>
#include<stdlib.h
> typedef struct
{
int pid;
int btime;
int wtime;
}
sp;
int main()
{
int i,j,n,tbm=0,towtwtime=0,totttime
sp*p,t;
printf("\n sjf schaduling ..\n");
printf("enter the no of processor");
scanf("%d",&n);
p=(sp*)malloc(sizeof(sp));
printf("\n enter the burst time");
for(i=0;i<n;i++)
{
printf("\n process %d\t",i+1);
scanf("%d",&p[i].btime);
p[i].pid=i+1;
p[i].wtime=0;
printf("\n process scheduling\n");
printf("\n process \tburst time \t w
for(i=0;i<n;i++)
{
towtwtime+=p[i].wtime=tbm;
tbm+=p[i].btime;
printf("\n%d\t\t%d",p[i].pid,p[i].bt
printf("\t\t%d\t\t%d",p[i].wtime,p[i
}
totttime=tbm+towtwtime;
printf("\n total waiting time :%d", totwtime );
printf("\n average waiting time
:%f",(float)totwtime/n); printf("\n total turn around
time :%d",totttime);
printf("\n average turn around time: :%f",(float)totttime/n);
}

RESULT
Thus the SJF Scheduling Program was executed successfully
Ex.No:5 INTERPROCESS COMMUNICATION

AIM
To write a C program to implement shared memory and inter process communication.

ALGORITHM:

Step 1: Start the Program


Step 2:Obtain the required data through char and int
datatypes. Step 3:Enter the filename,index block.
Step 4: Print the file name index loop.
Step 5:Fill is allocated to the unused index blocks
Step 6: This is allocated to the unused linked
allocation.
Step 7: Stop the execution

PROGRAM

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h> int
main()
{
int id,semid,count=0,i=1,j; int
*ptr;
id=shmget(8078,sizeof(int),IPC_CREAT|0666); ptr=(int
*)shmat(id,NULL,0);
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
}u;
struct sembuf sem;
semid=semget(1011,1,IPC_CREAT|0666); ushort
a[1]={1};
u.array=a; semctl(semid,0,SETALL,u);
while(1)
{
sem.sem_num=0;
sem.sem_op=-1;
sem.sem_flg=0;
semop(semid,&sem,1);
*ptr=*ptr+1;
printf("process id:%d countis :%d \n",getpid(),*ptr);
for(j=1;j<=1000000;j++)
{
sem.sem_num=0; sem.sem_op=+1;
sem.sem_flg=0;
semop(semid,&sem,1);
}
}
shmdt(ptr);
}

RESULT:
Thus the Interprocess communication program was executed Successfully
Ex.No:6 PRODUCER CONSUMER PROBLEM USING SEMAPHORES

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

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the required variables.
Step 3: Initialize the buffer size and get maximum item you want to produce.
Step 4: Get the option, which you want to do either producer, consumer or exit from the
operation.
Step 5: If you select the producer, check the buffer size if it is full the producer should not
produce the item or otherwise produce the item and increase the value buffer size.
Step 6: If you select the consumer, check the buffer size if it is empty the consumer should not
consume the item or otherwise consume the item and decrease the value of buffer size.
Step 7: If you select exit come out of the program.
Step 8: Stop the program.

PROGRAM:

#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1) {
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n);
switch(n)
{ case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("BUFFER IS FULL"); break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("BUFFER IS EMPTY");
break;
case 3:
exit(0);
break;
}
}
}
int wait(int s) {
return(--s); }
int signal(int s) {
return(++s); }
void producer() {
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex); }
void consumer() {
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex); }
RESULT:

Thus the SEMAPHORES program was executed Successfully


Ex.No:7 BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE

AIM:
To write a C program to implement banker‟s algorithm for deadlock avoidance.

ALGORITHM:

Step-1: Start the program.


Step-2: Declare the memory for the process.
Step-3: Read the number of process, resources, allocation matrix and available matrix.
Step-4: Compare each and every process using the banker‟s algorithm.
Step-5: If the process is in safe state then it is a not a deadlock process otherwise it is a
deadlock process
Step-6: produce the result of state of process
Step-7: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Baner's Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
\ scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t
Available\t"); for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}}}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}}
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}}}}}}
for(i=0;i<n;i++)
{
if(finish[i]==1)
{
c1++;
}
else
{printf("P%d->",i);
}}
if(c1==n)
{printf("\n The system is in safe state");
}
Else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}}

RESULT
Thus the Banker Algorithm Program was executed successfully
Ex.No:8 ALGORITHM FOR DEADLOCK DETECTION

AIM:
To write a C program to implement algorithm for deadlock detection.

ALGORITHM:

Step-1: Start the program.


Step-2: Declare the memory for the process.
Step-3: Read the number of process, resources, allocation matrix and available matrix.
Step-4: Compare each and every process using the banker‟s algorithm.
Step-5: If the process is in safe state then it is a not a deadlock process otherwise it is a
deadlock process
Step-6: produce the result of state of process
Step-7: Stop the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t"); scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t
Available\t"); for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}}}
void cal()
{ int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}}
while(flag)
{flag=0;
for(i=0;i<n;i++)
{int c=0;
for(j=0;j<r;j++)
{if((finish[i]==0)&&(need[i][j]<=avail[j]))
{c++;
if(c==r)
{
for(k=0;k<r;k++)
{avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}//printf("\nP%d",i);
if(finish[i]==1)
{i=n;
}}}}}}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{dead[j]=i;
j++;
flag=1;
}}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{printf("P%d\t",dead[i]);
}}
else
{
printf("\nNo Deadlock Occur"); }}
RESULT
Thus the Deadlock detection Program was executed successfully
Ex.No:9 THREADING & SYNCHRONIZATION APPLICATIONS

AIM:
To write a c program to implement Threading and Synchronization Applications.

ALGORITHM:

Step 1: Start the process


Step 2: Declare process thread, thread-id.
Step 3: Read the process thread and thread state.
Step 4: Check the process thread equals to thread-id by using if condition.
Step 5: Check the error state of the thread.
Step 6: Display the completed thread
process. Step 7: Stop the process

PROGRAM:

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* doSomeThing(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();

if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}

for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
int main(void)
{
int i = 0;
int err; while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL); if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err)); else
printf("\n Thread created successfully\n");

i++;
}

sleep(5); return 0;
}

RESULT
Thus the Threading and Sync Program was executed successfully
Ex.No:10 IMPLEMENTATION OF PAGING TECHNIQUE OF MANAGEMENT

AIM:
To write a C program to implement the concept of Paging

ALGORITHM:

Step 1: The Semaphore mutex, full & empty are initialized.


Step 2: In the case of producer process
i) Produce an item in to temporary variable.
ii) If there is empty space in the buffer check the mutex value for enter into the critical
section.
iii) If the mutex value is 0, allow the producer to add value in the temporary
variable to the buffer.
Step 3: In the case of consumer process
i) It should wait if the buffer is empty
ii) If there is any item in the buffer check for mutex value, if the mutex==0, remove
item from buffer
iii) Signal the mutex value and reduce the empty value by 1.
iv) Consume the
item.
Step 4: Print the
result

PROGRAM

#include<stdio.h
> main()
{
int
i,j,arr[100],pt[20],val,pgno,offset,phymem,fs,nf;
printf("\n Memory Management paging\n");
printf("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 size of frame or
page"); scanf("%d",&fs);
nf=phymem/fs;
printf("No of frame available
are\t%d",nf); printf("\n Enter the 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("the physical address is:%d\n",arr[val]);
}

RESULT:

Thus the program is executed successfully


MEMORY ALLOCATION METHODS FOR FIXED PARTITION
Ex.No:11.a
FIRST FIT

AIM:
To write a C program for implementation memory allocation methods for fixed partition
using first fit.

ALGORITHM:

Step 1:Define the max as 25.


Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0, bf[max],ff[max].
Step 3: Get the number of blocks,files,size of the blocks using for loop.
Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check highest<temp,if so assign ff[i]=j,highest=temp
Step 6: Assign frag[i]=highest, bf[ff[i]]=1,highest=0
Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment.
Step 9: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
RESULT
Thus the FIRST FIT Program was executed successfully
MEMORY ALLOCATION METHODS FOR FIXED PARTITION
Ex.No:11.b
WORST FIT

AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.

ALGORITHM:

Step 1:Define the max as 25.


Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0, bf[max],ff[max].
Step 3: Get the number of blocks,files,size of the blocks using for loop.
Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check temp>=0,if so assign ff[i]=j break the for loop.
Step 6: Assign frag[i]=temp,bf[ff[i]]=1;
Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment.
Step 9: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst
Fit"); printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
RESULT
Thus the WORST FIT Program was executed successfully
MEMORY ALLOCATION METHODS FOR FIXED PARTITION
Ex.No:11.c
BEST FIT

AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.

ALGORITHM:

Step 1:Define the max as 25.


Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0, bf[max],ff[max].
Step 3: Get the number of blocks,files,size of the blocks using for loop.
Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check lowest>temp,if so assign ff[i]=j,highest=temp
Step 6: Assign frag[i]=lowest, bf[ff[i]]=1,lowest=10000
Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment.
Step 9: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i]; if(temp>=0) if(lowest>temp)
{
ff[i]=j;

lowest=temp;
}
}
}
frag[i]=lowest; bf[ff[i]]=1; lowest=10000;
}printf("\nFile No\tFile Size \tBlock No\tBlock
Size\tFragment"); for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
RESULT
Thus the Best Fit Program was executed successfully
PAGE REPLACEMENT ALGORITHMS
Ex.No:12.a
FIFO

AIM:

To write a C program for implementation of FIFO page replacement algorithm.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the necessary variables.
Step 3: Enter the number of frames.
Step 4: Enter the reference string end with zero.
Step 5: FIFO page replacement selects the page that has been in memory the longest time and
when the page must be replaced the oldest page is chosen.
Step 6: When a page is brought into memory, it is inserted at the tail of the queue.
Step 7: Initially all the three frames are empty.
Step 8: The page fault range increases as the no of allocated frames also increases.
Step 9: Print the total number of page faults.
Step 10: Stop the program.

PROGRAM:

#include<stdio.h>
int main()
{
int i=0,j=0,k=0,i1=0,m,n,rs[30],flag=1,p[30];
system("clear");
printf("FIFO page replacement algorithm. \\n");
printf("enter the no. of frames:");
scanf("%d",&n);
printf("enter the reference string:");
while(1)
{
scanf("%d",&rs[i]);
if(rs[i]==0)
break;
i++;
}
m=i;
for(j=0;j<n;j++)
p[j]=0;
for(i=0;i<m;i++)
{
flag=1;
for(j=0;j<n;j++)
if(p[j]==rs[i])
{
printf("data already in page \n");
flag=0;
break;
}
if(flag==1)
{
p[i1]=rs[i];
i1++;
k++;
if(i1==n)
i1=0;
for(j=0;j<n;j++)
{
printf("\n page %d:%d",j+1,p[j]);
if(p[j]==rs[i])
printf("*");
}
printf("\n\n");
}
}
printf("total no page faults=%d",k);
}
RESULT
Thus the FIFO Program was executed successfully
PAGE REPLACEMENT ALGORITHMS
Ex.No:12.b
LRU

AIM:
To write a c program to implement LRU page replacement algorithm.

ALGORITHM:

Step 1: Start the process


Step 2: Declare the size
Step 3: Get the number of pages to be inserted
Step 4: Get the value
Step 5: Declare counter and stack
Step 6: Select the least recently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the process

ROGRAM:

#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++) scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{c++;
if(k<f)
{q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{for(r=0;r<f;r++)
{c2[r]=0;
for(j=i-1;j<n;j--)
{if(q[r]!=p[j])
c2[r]++;
else
break;
}}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}}}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}}}
printf("\nThe no of page faults is %d",c);
}
RESULT
Thus the LRU Program was executed successfully
FILE ORGANIZATION TECHNIQUE
Ex.No:13.a
SINGLE LEVEL DIRECTORY

AIM:
To write C program to organize the file using single level directory.

ALGORITHM:

Step-1: Start the program.


Step-2: Declare the count, file name, graphical interface.
Step-3: Read the number of files
Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a single level
Step-7: Display the files
Step-8: Stop the program

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);
printf("enter number of files");
scanf("&d",&count);
if(i<count)
// for(i=0;i<count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("enter %d file
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);
i++;
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[i]);
}}
getch();
}

RESULT
Thus the SIngle Level Program was executed successfully
FILE ORGANIZATION TECHNIQUE
Ex.No:13.b
TWO LEVEL DIRECTORY

AIM:
To write C program to organize the file using two level directory.

ALGORITHM:

Step-1: Start the program.


Step-2: Declare the count, file name, graphical interface.
Step-3: Read the number of files
Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a single level
Step-7: Display the files
Step-8: Stop the program

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 the name of dir file name %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:
Thus the Two level directory Program was executed successfully
FILE ALLOCATION STRATEGIES
Ex.No:14.a
SEQUENTIAL

AIM:
To write a C program for sequential file for processing the student information.

ALGORITHM:

Step-1: Start the program.


Step-2: Get the number of records user want to store in the system.
Step-3: Using Standard Library function open the file to write the data into the
file. Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h
> typedef struct
{int sno;
char name[25];int m1,m2,m3;
}STD;
void display(FILE *);
int search(FILE *);
void main()
{int i,n,sno_key,opn;
FILE *fp;
clrscr();
printf(“How many records ?”);
scanf(“%d”,&n);
fp=fopen(“stud.dat”,”w”);
for(i=0;i<n;i++)
{printf(“Enter the student information : %d(sno,Name,M1,M2,M3):”,i+1);
scanf(“%d%s%d%d%d,&s.sno,s.name,&s.m1,&s.m2,&s.m3);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
fp=fopen(“stdu.dat”,”r”);
do
{printf(“1-DISPLAY\n2.SEARCH\n 3.EXIT\n YOUR OPTION: “);
scanf(“%d”,&open);
switch(opn)
{
case 1:
printf(“\n Student Records in the file \n”);
display(fp);
break;
case 2:
printf(“Read sno of the student to be searched :”);
scanf(“%d”,&sno_key);
if(search(fp,sno_key)){
printf(“success!! Record found in the file\n”);
printf(“%d\t%s\t%d\t%d\t%d\n”, s.sno,s.name,s.m1,s.m2,s.m3);
}
else
printf(“Failure!! Record %d not found\n”,sno_key);
break;
case 3:
printf(“Exit !! press key”);
getch();
break;
default:
printf(“Invalid option!!! Try again!!\n”);
break;
}
}while(opn!=3);
fclose(fp);
}
Void display(FILE *fp)
{rewind(fp);
while(fread(&s,sizeof(s),1,fp))
printf(“%d\t%s\t%d\t%d\t%d\n”,s.sno,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp,int sno_key)
{rewind(fp);
while(fread(&s,sizeof(s),1,fp))
If(s.sno==sno_key)
return 1;
return 0;
}
RESULT
Thus the SEQUENTIAL Program was executed successfully
FILE ALLOCATION STRATEGIES
Ex.No:14.b
LINKED

AIM:
To write a C program for random access file for processing the employee details.

ALGORITHM:

Step-1: Start the program.


Step-2: Get the number of records user want to store in the system.
Step-3: Using Standard Library function open the file to write the data into the
file. Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct record
{
char empname[20];
int age;
float salary;
};
typedef struct record person
FILE *people;
void main()
{
person employee;
int I,n;
FILE *fp;
printf(“How many records:”);
scanf(“%d”,&n);
fp=fopen(“PEOPLE.txt”,”w”);
for(i=0;i<n;i++)
{
printf(“Enter the employee information :%d(EmpName, Age,Salary):”,i+1);
scanf(“%s%d%f”,employee.empname,&employee.age,& employee.salary);
fwrite(,&employee.sizeof(employee,1,people);
}
fclose(fp);
int rec,result;
people=fopen(“PEOPLE.txt”,”r”);
printf(“Which record do you want to read from file?);
scanf(“%d”,&rec);
while(rec>=0)
{
fseek(people,rec*sizeof(employee),SEEK_SET);
result=fread(&em[ployee,sizeof(employee),1,people)
if(result==1)
{
printf(“\n RECORD %d\n”,rec);
printf(“Given name:%s\n”, employee.empname);
printf(“Age:%d years\n”,employee.age);
printf(“Current salary:$ %8.2f\n\n”,employee.salary);
}
else
printf( “\n RECORD %d not found !\n\n”,rec);
printf(“Which record do you want(0to3)”):
scanf(“%d”<&rec):
}
fclose(people):
getch():
}
RESULT
Thus the Linked Management Program was executed successfully
FILE ALLOCATION STRATEGIES
Ex.No:14.c
INDEXED

AIM:
To write a C program for random access file for processing the employee details.

ALGORITHM:

Step-1: Start the program.


Step-2: Get the number of records user want to store in the system.
Step-3: Using Standard Library function open the file to write the data into the
file. Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program.

PROGRAM:
#include
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:
printf("enter index block\t");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("enter no of files on index\t");
scanf("%d",&n);
}
else
{
printf("Block 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("Block already allocated");
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1; printf("\n
allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d->%d:%d",p,inde[k],f[inde[k]]); printf("
Enter 1 to enter more files and 0 to exit\t");
scanf("%d",&c);
if(c==1) goto x; else exit();getch();
}
RESULT
Thus the Indexed Management Program was executed successfully
DISK SCHEDULING
Ex.No:15 a)
FIRST COME FIRST SERVED

AIM:
Write a C program to simulate disk scheduling algorithms FCFS

ALGORITHM

Step 1 : Start at the disk head's current position.


Step 2 : Serve the I/O request that is closest to the current head position.
Step 3 : Move the head to the location of the next I/O request in the queue.
Step 4 : Serve the next request in the queue.
Step 5 : Repeat steps 3 and 4 until all pending I/O requests have been served.
Step 6 : Stop

PROGRAM

#include<stdio.h>
main()
{
int t[20], n, I, j, tohm[20], tot=0;float avhm; clrscr();
printf(“enter the no.of tracks”); scanf(“%d”,&n);
printf(“enter the tracks to be traversed”); for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+); for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i]; if(tohm[i]<0) tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i]; avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”); for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+); printf("\nAverage header movements:%f",avhm); getch();
}
RESULT
Thus the First come First served Program was executed successfully
DISK SCHEDULING
Ex.No:15 b)
C-SCAN ALGORITHM

AIM:
Write a C program to simulate disk scheduling algorithms C-SCAN

ALGORITHM:

Step 1 : Start at the disk head's current position.


Step 2 : Check the pending I/O requests and determine the closest request in the current
direction of the head movement.
Step 3 :Move the head in the direction of the closest pending I/O request and serve it.
Step 4 :Continue moving the head in the same direction and serving all pending I/O requests
along the way.
Step 5 :When the head reaches the end of the disk, wrap it around to the beginning and
continue moving in the same direction.
Step 6 :Stop moving the head when all pending I/O requests have been served.

PROGRAM

#include<stdo.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20], temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n"); scanf("%d",&max);
printf("Enter the initial head position\n"); scanf("%d",&head);
printf("Enter the size of queue request\n"); scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n"); for(i=1;i<=n;i++)
{
scanf("%d",&temp); if(temp>=head)
{
queue1[temp1]=temp; temp1++;
queue2[temp2]=temp; temp2++;
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i]; queue1[i]=queue1[j]; queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i]; queue2[i]=queue2[j]; queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++) queue[i]=queue1[j]; queue[i]=max; queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++) queue[i]=queue2[j]; queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]); seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek); avg=seek/(float)n;
printf("Average seek time is %f\n",avg); return 0;
}
RESULT
Thus the C-Scan Algorithm Program was executed successfully

You might also like