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

CS3461 OPERATING SYSTEM LABORATORY1

The document outlines a laboratory course on Operating Systems, detailing a list of experiments including installation of Windows and UNIX commands, process management, CPU scheduling algorithms, inter-process communication, mutual exclusion, deadlock avoidance, threading, memory allocation methods, and file organization techniques. It includes step-by-step instructions for installing Windows, basic UNIX commands, and simple shell programming examples. The aim is to provide hands-on experience with various operating system concepts and programming tasks.

Uploaded by

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

CS3461 OPERATING SYSTEM LABORATORY1

The document outlines a laboratory course on Operating Systems, detailing a list of experiments including installation of Windows and UNIX commands, process management, CPU scheduling algorithms, inter-process communication, mutual exclusion, deadlock avoidance, threading, memory allocation methods, and file organization techniques. It includes step-by-step instructions for installing Windows, basic UNIX commands, and simple shell programming examples. The aim is to provide hands-on experience with various operating system concepts and programming tasks.

Uploaded by

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

CS3461 OPERATING SYSTEM LABORATORY

LIST OF EXPERIMENTS:
1. Installation of windows operating system
2. Illustrate UNIX commands and Shell Programming
3. Process Management using System Calls : Fork, Exit, Getpid, Wait, Close
4. Write C programs to implement the various CPU Scheduling Algorithms
5. Illustrate the inter process communication strategy
6. Implement mutual exclusion by Semaphore
7. Write C programs to avoid Deadlock using Banker's Algorithm
8. Write a C program to Implement Deadlock Detection Algorithm
9. Write C program to implement Threading
10. Implement the paging Technique using C program
11. Write C programs to implement the following Memory Allocation Methods
a. First Fit b. Worst Fit c. Best Fit
12. Write C programs to implement the various Page Replacement Algorithms
13. Write C programs to Implement the various File Organization Techniques
14. Implement the following File Allocation Strategies using C programs
a. Sequential b. Indexed c. Linked
15. Write C programs for the implementation of various disk scheduling algorithms
16. Install any guest operating system like Linux using VMware.
Ex.no: 1 Installation of Operating system : Windows
Aim:
To study Installation of Operating system: Windows/ Linux.
1. Creating an Installation Disc or Drive
1Connect 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.[2]

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.[3] 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.

3Go to https://www.microsoft.com/en-us/software-download/windows10%20. This is the official

download site for Windows 10.

4Click 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).
5Double-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.`
6Click Accept to accept the license. It's in the bottom-right corner of the window.

7 Select "Create installation media" and click OK. This option lets you create a Windows installation
disc 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!

8 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.

9 Choose an installation type and click Next. An ISO file is a type of file that can be burned to a DVD,
so choose that option if you plan to create a DVD. Otherwise, choose the USB flash drive option.
10Create 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.

11 DVD/ISO: Click Save to save the ISO file to your computer—it may take a while because
the file is large and has to be downloaded. Once downloaded, you'll see a progress screen
that monitors the download. When the download is complete, click Open DVD burner on
the "Burn the ISO file to a DVD" screen, select your DVD burner, and then click Burn to
create your DVD.

RESULT: Thus installation of Operating system ( Windows) was installed successfully.


Ex. No.2(a) BASIC UNIX COMMANDS
AIM:
To study the Basic UNIX Commands and various UNIX editors such as vi, ed, ex and EMACS.
COMMANDS:
a) Display Commands
1) Command : date
Purpose : To check the date and time
Syntax : $date
Example : $date
2) Command : month
Purpose : To display only month
Syntax : $+%m
Example : $+%m
3) Command : Month Name
Purpose : To display month name
Syntax : $+%h
Example : $+%h
4) Command : Month Day
Purpose : To display day of month
Syntax : $+%d
Example : $+%d
5) Command : year
Purpose : To display last two digits of years
Syntax : $+%y
Example : $+%y
6) Command : hour
Purpose : To display hours
Syntax : $+%H
Example : $+%H
7) Command : Minutes
Purpose : To display minutes
Syntax : $+%M
Example : $+%M
8) Command : Seconds
Purpose : To display seconds
Syntax : $+%S
Example : $+%S
b) Command : cal
Purpose : To display the calendar
Syntax : $cal
Example : $cal
c) Command : echo
Purpose : To print the message on the screen.
Syntax : $echo “text”
Example : $echo HELLO
d) Command : ls
Purpose : To list the files. Your files are kept in a directory.
Syntax : $ls
Example : $ls
lsls–s All files (include files with prefix)
ls–l Lodetai (provide file statistics)
ls–t Order by creation time
ls– u Sort by access time (or show when last accessed together with –l) ls–s Order by size
ls–r Reverse order
ls–f Mark directories with /,executable with* , symbolic links with @, local sockets with =, named
pipes(FIFOs)with
ls–s Show file size
ls– h “ Human Readable”, show file size in Kilo Bytes & Mega Bytes (h can be used together with –l
or)
e) Command : man
Purpose : To provide manual help on every UNIX commands. Syntax : $man unix command
Example : $man cat
f) Command : who
Purpose : To displays data about all users who have logged into the system currently.
Syntax : $who
Example : $ who -H. To show only hostname
$ who -m. To show active processes spawned by init
$ who -p. To show user's message status as
$ who -T. Show or list users logged in.
$ who -u. Show time of last system boot.
g) Command : whoami
Purpose : To displays about current user only
Syntax : $whoami
Example : $whoami
h) Command : uptime
Purpose : To tells you how long the computer has been running since its last reboot or power-off.
Syntax : $uptime
Example : $uptime
i) Command : uname
Purpose : To displays the system information such as hardware platform, system name and
processor, OS type.
Syntax : $uname–a
Example : $uname–a
j) Command : hostname
Purpose : To displays and set system host name
Syntax : $ hostname
Example : $ hostname
k) Command : bc
Purpose : To stands for “best calculator”
Syntax : $bc
Example : $bc
l) Command : id
Purpose : To display the login name.
Syntax : $id
Example : $id
m) Command : clear
Purpose : To clear the screen.
Syntax : $clear
Example : $clear
n) Command : finger
Purpose : To gathers and displays information about the users which includelogin name, name of
user, home directory etc..
Syntax : $finger username
Example : $finger student
FILE MANIPULATION COMMANDS
a) Command : cat
Purpose : To create, view, and edit files.
Syntax : CREATE :$cat>filename
VIEW: $cat filename
EDIT $cat>>filename
Example : $ cat>aaa
cat aaa
cat>> aaa
b) Command : concatenate
Purpose : To add two file content into new file
Syntax : $cat file1file2>file3
Example : $cat aaa bbb>ccc
c) Command : grep
Purpose : To search a particular word or pattern related to that word from the file.
Syntax : $grep search word filename
Example : $grep anu student
d) Command : rm
Purpose : To deletes a file from the file system
Syntax : $rm filename
Example : $rm student
e) Command : touch
Purpose : To create a blank file.
Syntax : $touch filename
Example : $touch student
f) Command : cp
Purpose : To copies the files or directories
Syntax : $cp source file destination file
Example : $cp student stud
g) Command : mv
Purpose : To rename the file or directory
Syntax : $mv old file new file
Example : $$mv student stu
h) Command : cut
Purpose : To cuts or pickup a given number of character or fields of the file. Syntax :
$cut<option><filename>
Example : $cut –c filename (-c cutting columns)
$cut–c1-10emp
$cut–f 3,6emp (-f cutting fields)
$ cut –f 3-6 emp
i) Command : wc
Purpose : To counts the number of lines, words, character in a specified file(s) with the options as –
l,-w,-c
Syntax : $wc filename
Example : $$wc student -l
$$wc student -w
$$wc student -c
DIRECTORY COMMANDS
a) Command : mkdir
Purpose : To create a directory
Syntax : $mkdir <directory name>
Example : $mkdir student
b) Command : rmdir
Purpose : To delete a directory
Syntax : $rmdir <directory name>
Example : $rmdir student
c) Command : cd
Purpose : To change the current directory
Syntax : $cd
Example : $cd ~ (changes path to your home directory) cd (changes parent directory)
d) Command : pwd (Print working Directory)
Purpose : To display the absolute pathname of current working directory. Syntax : $pwd
Example : $pwd
PROCESS COMMANDS
a) Command : exit
Purpose : To terminate a process
Syntax : $exit
Example : $exit
b) Command : kill
Purpose : To terminates or send a signal to process
Syntax : $kill
Example : $kill
c) Command : passwd
Purpose : To create or change a password
Syntax : $passwd
Example : $passwd
d) Command : semicolon (;)
Purpose : To execute more than one command at a time Syntax : $ ;
Example : $who; date;
FILTER COMMANDS
a) Command : head
Purpose : To display lines from the head(top)of a given file Syntax : $head filename
Example : $head student
$head -2student (To display the top two lines:)
b) Command : tail
Purpose : To display last 10 lines of the file
Syntax : $tail filename
Example : $tail student
$tail -2filename(To display the bottom two lines)
c) Command : chmod
Purpose : To change the permissions of a file or directory.
Syntax : $ch mod category operation permission file
(Category–is the user type, Operation–is used to assign or
remove permission, Permission–is the type of permission,
File–are used to assign or remove permission all)
Example : $ch modu+rw,g+rwstudent
Assigns read and write permission for users and groups
$chmodg=rwx student
Assigns absolute permission for groups of all read, write and execute permissions
$chmodu-wx student
Removes write and execute permission for users.
b).PROGRAM FOR SIMULATION OF CP UNIX COMMANDS
ALGORITHM:
STEP 1: Start the program
STEP 2:Declare the variables ch, *fp, sc=0
STEP 3: Open the file in read mode
STEP 4: Get the character
STEP 5: If ch== “ “ then increment sc value by one
STEP 6: Print no of spaces
STEP 7:Close the file
PROGRAM:
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
main(int argc,char *argv[])
{
FILE *fp;
char ch;
int sc=0;
fp=fopen(argv[1],"r");
if(fp==NULL)
printf("unable to open a file",argv[1]);
else
{
while(!feof(fp))
{
ch=fgetc(fp);
if(ch==' ')
sc++;
}
printf("Number of spaces: %d",sc);
printf("\n");
fclose(fp);
}
}

c). PROGRAM FOR SIMULATION OF LS UNIX COMMANDS


ALGORTIHM:
STEP 1 : Start the program
STEP 2 : Open the directory with directory object dp STEP 3 : Read the directory content and print it.
STEP 4: Close the directory.
PROGRAM:
#include<stdio.h>
#include<dirent.h>
main(int argc, char **argv)
{
DIR *dp;
struct dirent *link;
dp=opendir(argv[1]);
printf(“\n Contents of the directory %s are: \n”, argv[1]); while((link=readdir(dp))!=0)
printf(“%s”,link->d_name);
closedir(dp);
}
OUTPUT:

d). PROGRAM FOR SIMULATION OF GREP UNIX COMMANDS


ALGORITHM
STEP 1: Start the program
STEP 2: Declare the variables fline[max], count=0, occurrences=0 and pointers *fp,*newline.
STEP 3: Open the file in read mode.
STEP 4: In while loop check fgets(fline,max,fp)!=NULL
STEP 5: Increment count value.
STEP 6: Check newline=strchr(fline, „\n ) ‟
STEP 7: print the count,fline value and increment the occurrence value. STEP 8: Stop the program
PROGRAM:
#include<stdio.h>
#include<string.h>
#define max 1024
void usage()
{
printf(“usage:\t. /a.out filename word \n “);
}
int main(int argc, char *argv[])
{
FILE *fp;
char fline[max];
char *newline;
int count=0;
int occurrences=0;
if(argc!=3)
{
usage();
exit(1);
}
if(!(fp=fopen(argv[1],”r”)))
{
printf(“grep: couldnot open file : %s \n”,argv[1]);
exit(1);
}
while(fgets(fline,max,fp)!=NULL)
{
count++;
if(newline=strchr(fline, „\n )) ‟
*newline= \0 ; ‟ ‟
if(strstr(fline,argv[2])!=NULL)
{
printf(“%s: %d %s \n”, argv[1],count, fline);
occurrences++;
}
}
}
OUTPUT:
RESULT:
Thus the Basic UNIX Commands using various editors are executed and programs for simulation of
cp, ls, grep commands were written and executed.

Ex.No.2B SIMPLE SHELL PROGRAMMING


AIM:
To write simple shell programs by using conditional, branching and looping statements.
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
OUTPUT:
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
OUTPUT:

3. Write a Shell program to find the factorial of a 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
OUTPUT:

5.Write a Shell program for Academic and Personal Details


ALGORITHM:
STEP 1. Get name, age, and address from the user. STEP 2. Print that message as similar.
STEP 3. Get mark1, mark2, and mark3 from the user. STEP 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”
OUTPUT:
6.Write a Shell program for Greatest Among Three Numbers
ALGORITHM:
STEP 1. Start the program.
STEP 2. Enter any three numbers.
STEP 3. Read the values as a, b and c.
STEP 4. If a greater than b and greater than c, print the value of a as the Greatest number.
STEP 5. Else if b is greater than c, print the value of b as greatest number. STEP 6. Else print the value
of c as the greatest number. STEP 7. Stop the program.
PROGRAM:
echo “Greatest of Three numbers”
echo “Enter the numbers:”
read a
read b
read c
if test $a -gt $b -a $a -gt $c
then
echo “$a is greater”
elif test $b -gt $c
then
echo “$b is greater”
else
echo “$c is greater”
fi
OUTPUT:

7.Write a Shell program to Check Prime Number


ALGORITHM:
STEP 1. Start the program
STEP 2. Input number n
STEP 3. Is i>2, repeat the following steps.
STEP 4. p=n%i.
STEP 5. Is p=0 then increment t value by 1.
STEP 6. Decrement i value by one
STEP 7. Is t value > zero then print number is prime STEP 8. Otherwise print number is not a prime
STEP 9. Stop the program
PROGRAM:
echo "Enter a Number:"
read n
i=`expr $n - 1`
t=0
while [ $i -ge 2 ]
do
p=`expr $n % $i`
if [ $p -eq 0 ]
then
t=`expr $t + 1`
fi
i=`expr $i - 1`
doneif [ $t -gt 0 ]
then
echo "The Number $n is not a Prime Number" else
echo "The Number $n is a Prime Number"
fi
OUTPUT:

8.Write a Shell program for Sum of N Numbers


ALGORITHM:
STEP 1. Start the program
STEP 2. Get the limit number ‘n’ from the user
STEP 3. Initialize sum=0 and i=1
STEP 4. If I is less than or equal to n get the number to be summed from the user and increment I
value by i
STEP 5. Add the number to the sum value
STEP 6. Repeat step 4 and 5
STEP 7. Print the sum value
STEP 8. Stop the program.
PROGRAM:
echo "Enter the limit:"
read n
echo "Enter the $n numbers:"
sum=0
i=1
while test $i -le $n
do
read num
sum=`expr $num + $sum`
i=`expr $i + 1`
done
echo "The sum of the numbers are $sum"
OUTPUT:

9.Write a Shell program for Fibonacci Series


ALGORITHM:
STEP 1. Input the range(n)
STEP 2. Initialize b=1,a=0,s=0
STEP 3. Do the following in a loop, until s less than or equal to n a=b;b=s
Print the Fibonacci series value i.e.(s)
Let s=a+b
STEP 4. Stop.
PROGRAM:
echo "Enter the range to be displayed:"
read n
a=0
b=1
s=0
echo "Fibonacci series"
while test $s -le $n
do
a=$b
b=$s
echo $s
s=`expr $a + $b`
done
OUTPUT:

10.Write a Shell program for Armstrong Number


ALGORITHM:
STEP 1. Start the program.
STEP 2. Get the input value(num)
STEP 3. Assign value of num to x
STEP 4. Assign value of sum equal to zero.
STEP 5. Repeat the following steps till the num greater than zero STEP 6. Find y equal to num
modulus 10.Find z equal to cube of y. Then find num equal to num divided by 10.
STEP 7. If x equal to sum then print the value is Armstrong Otherwise print the number is not
armstrong
STEP 8. Stop the program.
PROGRAM:
echo "Enter a 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]
thenecho "$x is an Armstrong Number"
else
echo "$x is not an Armstrong Number"
fi
OUTPUT:

11.Write a Shell program for Arithmetic Operations Using Switch Case


ALGORITHM:
STEP 1. Enter the input
STEP 2. Enter the choice
STEP 3. Perform corresponding arithmetic operation on the inputs STEP 4. Display the result
STEP 5. Stop
PROGRAM:
echo "Performing Arithmetic Manipulation"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" echo -n "Enter the first number:"
read a
echo -n "Enter the second number:"
read b
echo "Arithmetic Operation - Menu"
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "0.Exit"
echo -n "Enter your Choice:"
read ch
case $ch in
1) echo " Option 1 Performs Addition" ;
c=`expr $a + $b`;
echo "The sum of $a and $b is $c";
exit ;;
2) echo " Option 2 Performs Subtraction" ;
c=`expr $a - $b`;
echo "The difference of $a and $b is $c";
exit ;;
3) echo " Option 3 Performs Multiplication" ;
c=`expr $a \* $b`;
echo "The product of $a and $b is $c";
exit ;;
4) echo " Option 4 Performs Division" ;
c=`expr $a / $b`;echo "The division of $a by $b is $c";exit ;; 0) echo "You are exiting from the
Arithmetic Manipulation" ; echo "Thankyou. Visit again"
exit ;;
esac
OUTPUT:
12.Write a Shell program for Palindrome Number
ALGORITHM:
STEP 1. Start the program.
STEP 2. Get the input value(a)
STEP 3. Assign value of a to d
STEP 4. Assign value of c equal to zero and b equal to one.
STEP 5. Repeat the following steps till the value of a greater than zero STEP 6. Find b equal to a
modulus 10.Find c equal to c *10 + b . Then find a equal to a divided by 10.
STEP 7. If d equal to c then print the value is Palindrome Otherwise print the number is not
Palindrome
STEP 8. Stop the program.
PROGRAM:
echo "Enter a number:"
read a
d=$a
c=0
b=1
while [ $a -gt 0 ]
do
b=`expr $a % 10`
c=`expr $c \* 10 + $b`
a=`expr $a / 10`
done
if [ $d -eq $c ]
then
echo "PALINDROME"
else
echo "NOT PALINDROME"
fi
OUTPUT:

RESULT
Thus the shell programs by using conditional, branching and looping statements was written and
executed.
Ex.no.3 Process Management using System Calls: Fork, Exec, Getpid, Exit, Wait, Close
Aim:
To Process Management using System Calls : Fork, Exec, Getpid, Exit, Wait, Close.
Algorithm:
Step 1: Declare a pid_t variable to store the process ID
Step 2: Call fork() to create a child process
Step 3: If fork() returns a negative value, print an error message and exit with an error code
Step 4: If fork() returns 0, the process is the child:
Step 4.1: Get the process ID of the child using getpid()
Step 4.2: Execute a command using execlp()
Step 5: If fork() returns a positive value, the process is the parent:
Step 5.1: Get the process ID of the parent using getpid()
Step 5.2: Call wait() to wait for the child process to complete
Step 5.3: Print a message to the console indicating that the child process has completed
Step 6: Call exit() to exit the program
Program :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
// Fork a child process
pid = fork();

if (pid < 0) { // Error handling


fprintf(stderr, "Fork failed\n");
exit(1);
}
else if (pid == 0) { // Child process
printf("I am the child process, pid=%d\n", getpid());
// Execute a command using exec
execlp("/bin/ls", "ls", NULL);
}
else { // Parent process
printf("I am the parent process, pid=%d\n", getpid());
// Wait for the child process to complete
wait(NULL);
printf("Child process s has completed\n");
}
// Close the program
printf("Exiting program\n");
exit(0);
}
Output:
Result:
Thus the C Programs using the system calls of UNIX operating system was written
and executed.
Ex No :4 CPU SCHEDULING ALGORITHMS
AIM:
To write a C program for implementation of scheduling algorithms such as FCFS, SJF,
Priority and Round Robin Scheduling.
1. FIRST COME FIRST SERVED (FCFS) SCHEDULING
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. 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 c alculate 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("\n Enter the number 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].btime;
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); }
OUTPUT:

2. SHORTEST JOB FIRST (SJF) SCHEDULING


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. 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 scheduling ..\n");
printf("Enter the number of processes");
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;
}
for(i=0;i<n;i++)
for(j=j+1,j<n;j++)
{
if(p[i].btime>p[j].btime)
{
t=p[i];
p[i]=p[j];p[j]=t;
}
}
printf("\n Process scheduling\n");
printf("\n Process \tBurst time \twaiting time \tTurn around time"); 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); }
OUTPUT:
3. PRIORITY SCHEDULING
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. 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,totttime=0;
sp *p,t;
printf("\n PRIORITY SCHEDULING.\n");
printf("\n Enter number of process....\n");
scanf("%d",&n);
p=(sp*)malloc(sizeof(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\Turn around 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 Average turnaround time:%f",(float)totttime/n); }
OUTPUT:
4. ROUND ROBIN SCHEDULING
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. 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 number 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;
}
}

OUTPUT:

RESULT:
Thus the C program for implementation of scheduling algorithms such as FCFS, SJF,
Priority and Round Robin Scheduling was written and executed.

Ex.no.5 Illustrate the inter process communication strategy

AIM:
To write a C program for inter process communication.
ALGORITHM
Step 1: Start the program.
Step 2: Define the key.
Step 3: Attach the client to the shared memory created by the server. Step 4:
Read the content from the shared memory.
Step 5: Display the content on the screen.
Step 6: Stop
PROGRAM:
MESSAGE QUEUE FOR WRITER PROCESS
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 10
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
key = ftok("progfile", 65);
msgid = msgget(key, 0666 | IPC_CREAT); message.mesg_type = 1;
printf("Write Data : ");
fgets(message.mesg_text,MAX,stdin);
msgsnd(msgid, &message, sizeof(message), 0);
printf("Data send is : %s \n", message.mesg_text); return 0;
}
MESSAGE QUEUE FOR READER PROCESS
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
key = ftok("profile", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
msgrcv(msgid, &message, sizeof(message), 1, 0);
printf("Data received is : %s \n", message.mesg_text);
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
OUTPUT:
RESULT:
Thus the C program for inter process communication was written and
executed..

Ex.no.6 Implement mutual exclusion by Semaphore

AIM:
To write a C-program to implement the producer – consumer problem using
semaphores (Mutual Exclusion).
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>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter Your Choice:");
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;
}
}
return 0;
}
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 Item %d",x); mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer Consumes Item %d",x); x--;
mutex=signal(mutex);
}

OUTPUT:
RESULT:
Thus the C-program to implement the producer – consumer problem using
semaphores (Mutual Exclusion) was written and executed.

Ex.no.7 Write C programs to avoid Deadlock using Banker's Algorithm

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 <stdlib.h>
int main()
{
int p, r, i, j, process, count;
int completed[20], Max[20][20], alloc[20][20], need[20][20], safeSequence[20],
avail[20];
count = 0;
printf("Enter the number of Processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the number of Resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each Process : ");
for(i = 0; i < p; i++)
{
printf("\nFor Process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each Process : ");
for(i = 0; i < p; i++)
{
printf("\nFor Process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max Matrix:\tAllocation Matrix:\n"); for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to Completion!", process + 1); safeSequence[count] =
process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j]; alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1); if(count == p)
{
printf("\nThe system is in a Safe State!!\n"); printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]); printf(">\n");
}
else
printf("\nThe system is in an Unsafe State!!"); }
OUTPUT:
RESULT:
Thus the C program to implement banker’s algorithm for deadlock avoidance was
written and executed.

Ex.no.8 Write a C program to Implement Deadlock Detection Algorithm

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>
static int mark[20];
int i,j,np,nr;
int main()
{
alloc[10][10],request[10][10],avail[10],r[10],w[10];
printf("\nEnter the number of Processes: ");
scanf("%d",&np);
printf("\nEnter the number of Resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the Request Matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
printf("\nEnter the Allocation Matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}
for(i=0;i<np;i++)
{
int count=0;
for(j=0;j<nr;j++)
{
if(alloc[i][j]==0)
count++;
else
break;
}
if(count==nr)
mark[i]=1;
}
for(j=0;j<nr;j++)
w[j]=avail[j];
for(i=0;i<np;i++)
{
int canbeprocessed=0;
if(mark[i]!=1)
{
for(j=0;j<nr;j++) {
if(request[i][j]<=w[j]) canbeprocessed=1; else
{
canbeprocessed=0; break;
}
}
if(canbeprocessed)
{
mark[i]=1;
for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
}
}
}
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;
if(deadlock)
printf("\n Deadlock Detected");
else
printf("\n No Deadlock Possible");
}
OUTPUT:
RESULT: Thus the C program to implement algorithm for deadlock detection was
written and executed.
Ex.no.9 Write C program to implement Threading

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];
int counter;
void* trythis(void *arg)
{
unsigned long i = 0;
counter += 1;
printf("\n Job %d has Started………..\n", counter);
for(i=0; i<(0xFFFFFFFF);i++);
printf("\n Job %d has Finished………………\n", counter);
return NULL;
}
int main(void)
{
int i = 0;
int error;
while(i < 2)
{
error = pthread_create(&(tid[i]), NULL, &trythis, NULL);
if (error != 0)
printf("\nThread can't be created : [%s]", strerror(error));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
OUTPUT:
RESULT:
Thus the c program to implement Threading and Synchronization Applications.

Ex.no.10 Implement the paging Technique using C program

AIM:
To write a c program to implement Paging technique for memory management.
ALGORITHM:
Step 1: Start the process
Step 2: Declare page number, page table, frame number and process size.
Step 3: Read the process size, total number of pages
Step 4: Read the relative address
Step 5: Calculate the physical address
Step 6: Display the address
Step 7: Stop the process
PROGRAM:
#include<stdio.h>
main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
int i;
int choice=0;
printf("\nYour Memory Size is %d ",memsize);
printf("\nEnter Page Size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the Frame of Page: %d",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a Logical Address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
}
OUTPUT:
RESULT:
Thus the c program to implement Paging technique for memory management was
written and executed.
Ex.no.11 Write C programs to implement the following Memory Allocation
Methods a. First Fit b. Worst Fit c. Best Fit

AIM:
To Write C programs to implement the following Memory Allocation Methods
a. First Fit b. Worst Fit c. Best Fit

a. 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>
#define max 25
main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
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)
{
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]);
}
OUTPUT:
b.WORST FIT
AIM:
To write a C program for implementation memory allocation methods for fixed
partition using worst 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 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>
#define max 25
main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n\t Memory 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) //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]);
}
OUTPUT:
c BEST FIT MEMORY ALLOCATION:
To write a C program for implementation memory allocation methods for fixed
partition using best 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 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>
#define max 25
main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - Best 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)
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]);
}
OUTPUT:

RESULT:
Thus the C program for implementation memory allocation methods for fixed
partition using best fit were written and executed.

Ex No :12(a) IMPLEMENTATION OF FIFO PAGE REPLACEMENT


ALGORITHM
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,j,n,a[50],frame[10],no,k,avail,count=0;
printf("FIFO Page Replacement \n");
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault is %d",count);
return 0;
}
OUTPUT:
RESULT:
Thus the C program for implementation of FIFO page replacement algorithm was
written and executed.

Ex No :12b IMPLEMENTATION OF LRU PAGE REPLACEMENT


ALGORITHM
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
PROGRAM:
#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("LRU Page Replacement \n");
printf("Enter Number of Pages:");
scanf("%d",&n);
printf("Enter the Reference String:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter Number 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]); 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 number of Page Faults is %d",c);
}
OUTPUT:

RESULT:
Thus the c program to implement LRU page replacement algorithm was written and
executed.
Ex No :12c IMPLEMENTATION OF OPTIMAL PAGE REPLACEMENT
ALGORITHM
AIM:
To write C program to implement optimal 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 frequently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2,
flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j)
{
if(temp[j] > max)
{
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
OUTPUT:

RESULT:
Thus the C program to implement optimal page replacement algorithm was written
and executed.
Ex.No.13a IMPLEMENTATION OF FILE ORGANIZATION -
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>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\
n Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}
OUTPUT:

RESULT:
Thus the C program to organize the file using single level directory was written and
executed.
Ex.No.13b IMPLEMENTATION OF FILE ORGANIZATION -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>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File"); printf("\n4. Search
File\t\t5. Display\t6. Exit\t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- "); scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- "); scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:
exit(0);
}
}
}
OUTPUT:

RESULT:
Thus the C program to organize the file using two level directory was written and
executed.
Ex.No.14a IMPLEMENTATION OF SEQUENTIAL FILE ALLOCATION
AIM:
To write a C program for sequential file allocation for 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>
main()
{
int f[50], i, st, len, j, c, k, count = 0;
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
}

OUTPUT:

RESULT:
Thus the C program for sequential file allocation for the student information was
written and executed.
Ex.No.14b IMPLEMENTATION OF LINKED FILE ALLOCATION
AIM:
To write a C program for implementation of linked file allocation. 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<stdlib.h>
main()
{
int f[50], p,i, st, len, j, c, k, a;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
}
OUTPUT:

RESULT:
Thus the C program for implementation of linked file allocation was written and
executed.

Ex.No.14c) IMPLEMENTATION OF INDEXED FILE ALLOCATION


AIM:
To write a C program for implementation of indexed file allocation.
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<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \
n",ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf(“Do you want to enter more files(yes-1/n0-0); scanf(“%d”,&c);
if(c==1)
goto x;
else
exit(0);
}

OUTPUT:

RESULT:
Thus the C program for implementation of indexed file allocation was written and
executed.
Ex.No.15 IMPLEMENTATION OF DISK SCHEDULING
ALGORITHM (FCFS)
AIM:
To write a C program for implementation of FCFS Disk Scheduling Algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Let Request array represents an array storing indexes of tracks that have
been requested in ascending order of their time of arrival. ‘head’ is the position
of disk head.
Step 3: Let us one by one take the tracks in default order and calculate
the absolute distance of the track from the head.
Step 4: Increment the total seek count with this distance.
Step 5: Currently serviced track position now becomes the new head position.
Step 6: Go to step 3 until all tracks in request array have not been serviced.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
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",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
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;
}
OUTPUT:

RESULT:
Thus the C program for implementation of FCFS Disk Scheduling Algorithm was
written and executed.

You might also like