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

Os Lab Program Manual

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

Os Lab Program Manual

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

1stprogram(BASIC COMMANDS IN LINUX)

1. WHO COMMAND:

DESCRIPTION:
This command is used to get information about all the users who are currently
logged into the system.
SYNTAX:
$who
OUTPUT:
iicsea55 pts/12 2009-02-19 10:10 (192.168.1.66)
iicsea58 pts/8 2009-02-19 10:10 (192.168.1.49)

2. DATE COMMAND:

DESCRIPTION:
This command tells the Linux system to display the current Date Month Year
Time. SYNTAX:
$date
OUTPUT:
Thu Feb 19 10:43:29 IST 2009

3. CALENDAR COMMAND:

DESCRIPTION:
This command tells the Linux system to display the calendar for current
month. SYNTAX:
$cal
OUTPUT:
February 2009
Su Mo Tu We Th Fr Sa
1234567
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
4. CALENDAR OPTION COMMAND:

DESCRIPTION:
This command tells the Linux system to display the calendar for current
year. SYNTAX:
$cal (year)
OUTPUT:
2000
January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo
Tu We Th Fr Sa 1 1 2 3 4 5 1 2 3 4 2 3 4 5 6 7 8 6 7 8 9 10 11 12 5 6 7 8 9 10 11 9
10 11 12 13 14 15 13 14 15 16 17 18 19 12 13 14 15 16 17 18 16 17 18 19 20 21
22 20 21 22 23 24 25 26 19 20 21 22 23 24 25 23 24 25 26 27 28 29 27 28 29 26
27 28 29 30 31 30 31
….

5. ECHO COMMAND:

DESCRIPTION:
This command is used to display the sentence or string given by
us. SYNTAX:
$echo (string)
OUTPUT:
God is Great

6. MAN COMMAND:

DESCRIPTION:
This command is used to get the complete details for given
command. SYNTAX:
$man (command)
OUTPUT:
CAL(1) BSD General Commands Manual CAL(1) NAME
cal - displays a calendar
SYNOPSIS
cal [-smjy13] [[month] year]
DESCRIPTION
Cal displays a simple calendar. If arguments are not specified, the cur-
rent month is displayed. The options are as follows:
7. CLEAR COMMAND:

DESCRIPTION:
This command is used to clear the screen.
SYNTAX:
$clear
OUTPUT:
The screen is cleared.

8. WHO AM I COMMAND:
This command gives information about current users
SYTNAX:
$who am I
OUTPUT:
Student pts/1 feb 25 14:28(192.168.0.1555)

9. PWD COMMAND:

DESCRIPTION:
This command is used to show the current working
directory SYTNAX:
$pwd
OUTPUT:
/home/student

10. BC COMMAND:

DESCRIPTION:
This command is used to perform basic calculation
SYTNAX:
$bc
OUTPUT:
25+65
90

11. LIST COMMAND

DESCRIPTION:
This command is used to give the list of files in a particular
directory. SYTNAX:
$ls (dir name)
OUTPUT:
File1 file2 today
12. COMMAND: TEE

DESCRIPTION:
It is used to read the contents from standard input or from output of another
command and reproduces the output to both in standard output and direct into output to one
or more files. SYNTAX: tee [options] <file name >
OUTPUT :
$ date | tee dat.txt

13.COMMAND: UNAME
DESCRIPTION:
It is used to display the details about the OS in which we are working.- n for node
name SYNTAX:
uname [options]
OUTPUT:
$ uname –n
administrator –Lenovo-$s510

DIRECTORY MANIPULATION COMMAND

1. MAKEDIR COMMAND
DESCRIPTION:
This command is used to create a new directory
SYTNAX:
$mkdir (dir)
OUTPUT
Required directory is created.

2. RMDIR COMMAND
DESCRIPTION:
This command is used for deleting a directory.
OPTIONS:
-i require a conformation for deletion
-r delete a directory without conformation

SYTNAX:
$rmdir (option) (dir)
OUTPUT:
Given directory is removed.

3. CD COMMAND:
DESCRIPTION:
This command is used to change the directory .
SYTNAX:
$cd (dir)
OUTPUT:
[iicsea19@localhost tariq ~]$
Current working directory is changed.

FILE MANIPULATION COMMANDS

1. CAT COMMAND:

DESCRIPTION:
This command is used for creating and displaying the content of the
file. SYNTAX:
1.create: $cat>(file name)
2.display: $cat (file name)
OUTPUT:
[student@cse ~]$ cat>India
I love my India.

2. MORE COMMAND:

DESCRIPTION:
This command is used to display the content of the file one screen at a time
SYNTAX:
$more filename
OUTPUT:
$ more Vicky
Operation is performed

3. WC COMMAND:

DESCRIPTION:
This will print number of lines, words, characters in a file.
SYNTAX:
$ wc (file name)
OPTIONS:
l - line
w - word
c –character
OUTPUT:
$ wc –l Vicky
2 vicky
$ wc –w Vicky
8 vicky
$ wc –w Vicky
49 vicky
4. MV COMMAND:

DESCRIPTION:
The command is used to move a file within a directory with different file name SYNTAX:
$ mv (file name1) (file name2)
OUTPUT:
$mv raja tar
The file is renamed.

5. SPELL COMMAND:
DESCRIPTION:
It is used to find spelling errors in a file and display them as standard output in alphabetical
order
SYNTAX:
$spell (file name)
OUTPUT:
$spell raja
cd ppell.

FILE COMPARISON COMMANDS

1. COPY COMMAND:
DESCRIPTION:
Used to copy one or more files.
SYNTAX:
$cp (file name 1) (file name 2)
OUTPUT:
$cp raja vicky
The same file is copied with different name

2. CMP COMMAND:

DESCRIPTION:
This command is used to compare two file and generate the first
difference. SYNTAX:
$cmp (file1) (file2)
OUTPUT:
$cmp s sad
It there
3. COMM COMMAND:

DESCRIPTION:
This is used for comparing 2 files line by line & generates the common items.
SYNTAX:
$comm (file name 1) (file name 2)
OUTPUT:
$ comm S3 s5
Vicky Antonio bendaras

FILTER COMMANDS

1. HEAD COMMAND

DESCRIPTION:
This command is used to display the first ten lines of the file without
option. SYNTAX:
$head (filename)
OUTPUT:
First ten lines of the file are displayed.

2. TAIL COMMAND

DESCRIPTION:
This command is used to display the last ten lines of the file without
option. SYNTAX:
$tail (file name)
OUTPUT:
Last ten lines of the file are displayed.

3. PASTE COMMAND

DESCRIPTION:
This command is used to paste one or more files as vertically and displays the result
in standard output.
SYNTAX:
$paste (file1) (file2)
OUTPUT:
Hi I am Tariq Hi My Name is Tariq
FILE PERMISSION COMMANDS

1. CHMOD COMMAND

DESCRIPTION:
This is used to alter the access permission for owner, group, user or others.
SYNTAX:
$chmod (option) filename
OPTION:
4- read
2- write
1- execute
0- no permission
OUTPUT:
[student@localhost~] chmod 4 sam
---r---w---x---sam

COMMUNICATION COMMANDS

1. MAIL COMMAND:
DESCRIPTION:
This command is an interactive message processing system in which you send
and receive electronically.
SYNTAX:
$ mail (user name)
OUTPUT:
Subject: hai !!! how are you …

2. WRITE COMMAND:

DESCRIPTION:
This command is used to communicate with the users who are currently working
the operating system.
SYNTAX:
$ write user name
OUTPUT:
Message from student @ local host local domain on pts 10 at 12:04

3.WALL COMMAND:

DESCRIPTION:
This command used to broadcast by a message by super user to the entire user
currently logged in.

SYNTAX:
$ wall message
OUTPUT:
Broad cast message from student (pts/0) ( thu feb 26 12:06:30:2009)
4.MESSAGE COMMAND:

DESCRIPTION:
This command is used to permit or deny message coming from the other user write
to talk commands.
SYNTAX:
$mesg y/n
OUTPUT:
Message is accepted.

5. COMMAND:SSH

DESCRIPTION:
Program for logging into a remote machine and for executing commands on a
remote machine.
SYNTAX:
ssh [options] [user]@hostname
EXAMPLE:
ssh ¬X [email protected]

6. COMMAND:SCP

DESCRIPTION:
Secure copy (remote file copy program) -copies files between hosts on a
network SYNTAX:
scp [options] [[user]@host1:file1] [[user]@host2:file2]
EXAMPLE:
scp file1.txt [email protected]:~/Desktop/

ARCHIVAL FILE COMMANDS

Command : tar
Purpose : to archive a file
Syntax:tar [OPTION] DEST SOURCE
Example : tar ¬cvf /home/archive.tar /home/original
tar ¬xvf /home/archive.tar

Command : Zip
Purpose : package and compress (archive) files
Syntax:zip [OPTION] DEST SOURSE
Example : zip original.zip original
Command : unzip
Purpose : list, test and extract compressed files in a ZIP
archive Syntax:unzip filename
Example : unzip original.zip

Command : fdisk
Purpose : partition manipulator
Syntax:sudo fdisk name
Example : sudo fdisk ¬l

Command : mount
Purpose : mount a file system
Syntax:mount ¬t type device dir
Example : mount /dev/sda5 /media/target

Command : Umount
Purpose : unmount file systems
Syntax:umount [OPTIONS] dir | device...
Example : umount /media/target

Command : du
Purpose : estimate file space usage
Syntax: df [OPTION]... [FILE]...
Example : df

Command : Quota
Purpose : display disk usage and limits
Syntax:quota [OPTION]
Example : quota –v

NETWORK COMMANDS

1.ping

DESCRIPTION:
It sends packets of information to the user-defined source. If the packets are received,
the destination device sends packets back. Ping can be used for two purposes

1. To ensure that a network connection can be established.


2. Timing information as to the speed of the connection.

Syntax:ping link name


Example : pink website address
2. ifconfig

DESCRIPTION:
View network configuration, it displays the current network adapter configuration. It
is handy to determine if you are getting transmit (TX) or receive (RX) errors.

syntax : ifconfig
Example : ifconfig

3. netstat

DESCRIPTION:
Most useful and very versatile for finding a connection to and from the host. You
can find out all the multicast groups (network) subscribed by this host by issuing
Syntax:netstat -g
Example : netstat-t,netstat-u

4. telnet
DESCRIPTION:
Connects destination host via the telnet protocol, if telnet connection establishes on
any port means connectivity between two hosts is working fine.

Syntax:telnet hostname port


Example : telnet hostname port

5 Quota
DESCRIPTION:
Display disk usage and limits

Syntax:quota [OPTION]
Example : quota -v

PROCESS COMMANDS

1. PS COMMAND:

DESCRIPTION:
This Command is used to tell the detail about actual process running in the system.
SYNTAX:
$ps (option)
OPTIONS:
1. –f
OUTPUT:
UID PID PPID C STIME TIMECMD
Student 2834 2831 0 10:13 00:00:00 bash
Student 2884 2871 0 10:13 00:00:00 bash

2. SLEEP COMMAND:

DESCRIPTION:
This command is used to suspend the execution of the system for an individual time
SYNTAX:
$sleep (time)
OUTPUT:
$ sleep 5
Operation is performed

3.KILL COMMAND:

DESCRIPTION:
This is used to terminate the process.
SYNTAX:
$kill (processed)
OUTPUT:
$ kill 89
Operation is performed.

4.AT COMMAND:

DESCRIPTION:
It is used to schedule the jobs for later execution at the specified time.
SYNTAX:
$at (time)
OUTPUT:
$ at 10:00
Job 1 at FRI mar 06 10:00:00 2009

2 nd program( SHELL PROGRAMMING)

$ sh filename.sh// Executing the program


Sample Coding:

// Greatest among 3 numbers

echo Enter 3 numbers with spaces in between

read a b c
l=$a
if [ $b -gt $l ]
then
l=$b
fi
if [ $c -gt $l ]
then
l=$c
fi
echo Largest of $a $b $c is $l
Sample Output:

Enter 3 numbers with spaces in between

385

Largest of 3 8 5 is 8

Test cases:

Enter Numbers in different ranges.


Include choices for executing the many concepts in one program

Sample Coding:

// Factorial

echo "enter the number"


read n
fact=1
i=1
while [ $i -le $n ]
do
fact=`expr $i \* $fact`
i=`expr $i + 1`
done
echo "the factorial number of $ni is $fact
Sample Output:

Enter the number :


4
The factorial of 4 is 24.
2nd (additional)

1.#shell script to find sum of n numbers

echo "Enter Size(N)"


read N

i=1
sum=0

echo "Enter Numbers"


while [ $i -le $N ]
do
read num #get number
sum=$((sum + num)) #sum+=num
i=$((i + 1))
done

echo $sum

2. #shell script for factorial of a number


echo "Enter a number"
read num

fact=1

while [ $num -gt 1 ]


do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done

echo $fact

3. HOW TO FIND A NUMBER IS EVEN OR ODD IN SHELL SCRIPT


echo "---- EVEN OR ODD IN SHELL SCRIPT -----"
echo -n "Enter a number:"
read n
echo -n "RESULT: "
if [ `expr $n % 2` == 0 ]
then
echo "$n is even"
else
echo "$n is Odd"
fi

4. Shell Script to print Fibonacci series


echo "Enter the value of n"
read n
a=0
b=1
count=2
echo "Fibonacci series:"
echo $a
echo $b
while [ $count -le $n ]
do
fib=`expr $a + $b`
a=$b
b=$fib
echo $fib
count=`expr $count + 1`
done

5. Shell Script to Print the Multiplication Table

echo "Enter a Number"


read n
echo "Enter Range"
read r
i=0
while [ $i -le $r ]
do
echo " $n x $i = `expr $n \* $i`"
i=`expr $i + 1`
done

6. File manipulation

echo Enter the filename


read file
c=`cat $file | wc -c`
w=`cat $file | wc -w`
l=`grep -c "." $file`
echo Number of characters in $file is $c
echo Number of words in $file is $w
echo Number of lines in $file is $l
7. Palindrome or not
echo "Enter the number"
read n
function pal
{
number=$n
reverse=0
while [ $n -gt 0 ]
do
a=`expr $n % 10 `
n=`expr $n / 10 `
reverse=`expr $reverse \* 10 + $a`
done
echo $reverse
if [ $number -eq $reverse ]
then
echo "Number is palindrome"
else
echo "Number is not palindrome"
fi
}
r=`pal $n`
echo "$r"

8. Positive or negative number.


echo "Enter a number:"
read a

if [ $a -eq 0 ]
then
echo "$a is neither postive nor negative"
elif [ $a -gt 0 ]
then
echo "$a number is positive"
else
echo "$a number is negative"
fi

9.Prime number or not.

echo "enter number"


read num
function prime
{
for((i=2; i<=num/2; i++))
do
if [ $((num%i)) -eq 0 ]
then
echo "$num is not a prime number."
exit
fi
done
echo "$num is a prime number."
}
r=`prime $number`
echo "$r"

10.Area of different shapes.

echo “Enter the side of the square:”


read s
echo “Enter the length and breadth of rectangle:”
read leng
read brea
echo “Enter the radius of the circle:”
read radius
echo “Area of the square is:` expr $s \* $s ` ”
echo “Area of the rectangle is: `expr $leng \* $brea`”
echo “Area of the circle is: `expr 3.14 \* radius \* radius`”

3rd program( IMPLEMENTATION OF UNIX SYSTEM


CALLS)

fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC,
0644); if(fd < 0)
{
printf("File creation
problem\n"); exit(-1);
}
printf("Press Ctrl+D at end in a new line:\n");
while((n = read(0, buf, sizeof(buf))) > 0)
{
len = strlen(buf);
write(fd, buf, len);
}
Close(fd);

Read()
fd = open(argv[1],
O_RDONLY); if(fd == -1)
{

printf("%s file does not exist\n",


argv[1]);
exit(-1);
}
printf("Contents of the file %s is : \n",argv[1]);
while(read(fd, buf, sizeof(buf)) > 0)
printf("%s", buf);
close(fd);

Write()

fd = open(argv[1], O_APPEND|O_WRONLY|O_CREAT,0644);
if (fd < 0)
{
perror(argv[1]);
exit(-1);
}
while((n = read(0, buf, sizeof(buf))) > 0)
{
len = strlen(buf);
write(fd, buf, len);
}
close(fd);
}

Execution of the Program:

$ cc fork.c //Compiling the Program

$ ./a.out //Executing the program

Sample Coding:
/* C program to illustrate use of fork() & exec() system call */

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>

int main()
{
pid_t pid;
int ret = 1;
int status;
pid = fork();

if (pid == -1) // pid == -1 means error occured


{
printf("can't fork, error occured\n");
exit(EXIT_FAILURE);
}
else if (pid == 0) // pid == 0 means child process created
{

printf("child process, pid = %u\n",getpid());


printf("parent of child process, pid = %u\n",getppid());

char * argv_list[] = {"ls","-lart","/home",NULL};

execv("ls",argv_list);
exit(0);
}
else // a positive number is returned for the pid of parent process
{
printf("Parent Of parent process, pid = %u\n",getppid());
printf("parent process, pid = %u\n",getpid());

if (waitpid(pid, &status, 0) > 0)


{

if (WIFEXITED(status) && !WEXITSTATUS(status))


printf("program execution successful\n");

else if (WIFEXITED(status) && WEXITSTATUS(status))


{
if (WEXITSTATUS(status) == 127)
{
printf("execv failed\n");
}
else
printf("program terminated normally,"
" but returned a non-zero status\n");
}
else
printf("program didn't terminate normally\n");
}
else {
// waitpid() failed
printf("waitpid() failed\n");
}
exit(0);
}
return 0;}

4.1(FCFS)
Execution of the Program:

$ cc pgm1.c //Compiling the Program

$ ./a.out //Executing the program

Sample Coding:

#include<stdio.h>
int main()
{
int AT[10],BT[10],WT[10],TT[10],n;
int burst=0,cmpl_T;
float Avg_WT,Avg_TT,Total=0;
printf("Enter number of the process\n");
scanf("%d",&n);
printf("Enter Arrival time and Burst time of the process\n");
printf("AT\tBT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&AT[i],&BT[i]);
}

// Logic for calculating Waiting time


for(int i=0;i<n;i++)
{
if(i==0)
WT[i]=AT[i];
else
WT[i]=burst-AT[i];
burst+=BT[i];
Total+=WT[i];
}
Avg_WT=Total/n;

// Logic for calculating Turn around time


cmpl_T=0;
Total=0;
for(int i=0;i<n;i++)
{
cmpl_T+=BT[i];
TT[i]=cmpl_T-AT[i];
Total+=TT[i];
}
Avg_TT=Total/n;

// printing of outputs

printf("Process ,Waiting_time ,TurnA_time\n");


for(int i=0;i<n;i++)
{
printf("%d\t\t%d\t\t%d\n",i+1,WT[i],TT[i]);
}
printf("Average waiting time is : %f\n",Avg_WT);
printf("Average turn around time is : %f\n",Avg_TT);
return 0;
}

4.2 (SJF ALGORITHM)


#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("\nEnter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n;
total=0;

printf("nProcesst Burst Time tWaiting TimetTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t%d\t%d\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n;
printf("\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}

4.3(SRTF ALGORITHM)

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("\nEnter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n;
total=0;

printf("nProcesst Burst Time tWaiting TimetTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%d\t%d\t%d\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}

4.4(PRIORITY ALGORITHM)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
//flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)

{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

4.5(ROUND ROBIN)
#include<stdio.h>

int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes: ");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]", i + 1);

printf("Arrival Time:");

scanf("%d", &arrival_time[i]);
printf("Burst Time:");

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

temp[i] = burst_time[i];
}

printf("\nEnter Time Quantum:");


scanf("%d", &time_quantum);
printf("\nProcess IDttBurst Timet Turnaround Timet Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time_quantum;
total = total + time_quantum;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("\nProcess[%d]\t%d\t%d\nt%d", i + 1, burst_time[i], total -
arrival_time[i], total - arrival_time[i] - burst_time[i]);
wait_time = wait_time + total - arrival_time[i] - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time[i];
counter = 0;
}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}

average_wait_time = wait_time * 1.0 / limit;


average_turnaround_time = turnaround_time * 1.0 / limit;
printf("\nAverage Waiting Time:t%f", average_wait_time);
printf("\nAvg Turnaround Time:t%f", average_turnaround_time);
return 0;
}

5.1( PRODUCER – CONSUMER PROBLEM


USING SEMAPHORES )

#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 the 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);
}
5.2(DINING PHILOSOPHER’S PROBLEM
TO DEMONSTRATE PROCESS
SYNCHRONIZATION.)

#include<stdio.h>

#define n 4

int compltedPhilo = 0,i;

struct fork{
int taken;
}ForkAvil[n];

struct philosp{
int left;
int right;
}Philostatus[n];

void goForDinner(int philID){ //same like threads concept here cases implemented
if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
printf("Philosopher %d completed his dinner\n",philID+1);
//if already completed dinner
else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
//if just taken two forks
printf("Philosopher %d completed his dinner\n",philID+1);

Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that he


completed dinner by assigning value 10
int otherFork = philID-1;

if(otherFork== -1)
otherFork=(n-1);

ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks


printf("Philosopher %d released fork %d and fork
%d\n",philID+1,philID+1,otherFork+1);
compltedPhilo++;
}
else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ //left already
taken, trying for right fork
if(philID==(n-1)){
if(ForkAvil[philID].taken==0){ //KEY POINT OF THIS PROBLEM, THAT
LAST PHILOSOPHER TRYING IN reverse DIRECTION
ForkAvil[philID].taken = Philostatus[philID].right = 1;
printf("Fork %d taken by philosopher %d\n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1);
}
}else{ //except last philosopher case
int dupphilID = philID;
philID-=1;

if(philID== -1)
philID=(n-1);

if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);
}
}
}
else if(Philostatus[philID].left==0){ //nothing taken yet
if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM,
THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by philosopher %d\n",philID,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
}
}else{ //except last philosopher case
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);
}
}
}else{}
}

int main(){
for(i=0;i<n;i++)
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;

while(compltedPhilo<n){
/* Observe here carefully, while loop will run until all philosophers complete dinner
Actually problem of deadlock occur only thy try to take at same time
This for loop will say that they are trying at same time. And remaining status will print by go
for dinner function
*/
for(i=0;i<n;i++)
goForDinner(i);
printf("\nTill now num of philosophers completed dinner are %d\n\n",compltedPhilo);
}

return 0;
}

6 TH ( IMPLEMENTATION OF BANKERS
ALGORITHM)
#include<stdio.h>
#include<conio.h>
void main() {
int
k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[
10],j,rz, count=0;

printf("\n Enter the number of resources : ");


scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for (i=0;i<rz;i++) {
avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");
for (i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for (i=0;i <pno;i++) {
P[i]=i;
printf("P[%d] ",P[i]);
for (j=0;j<rz;j++) {
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}
printf("\nEnter the MAX matrix \n ");
for (i=0;i<rz;i++) {
printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for (i=0;i <pno;i++) {
printf("P[%d] ",i);
for (j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
}
printf("\n");
A: d=-1;
for (i=0;i <pno;i++) {
count=0;
t=P[i];
for (j=0;j<rz;j++) {
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz) {
output[k++]=P[i];
for (j=0;j<rz;j++)
avail[j]+=allocated[t][j];
} else
P[++d]=P[i];
}
if(d!=-1) {
pno=d+1;
goto A;
}
printf("\t <");
for (i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
getch();
}

7 TH (MEMORY ALLOCATION AND


MANAGEMENT TECHNIQUES
(BEST FIT, WORST FIT, FIRST FIT)
WORST FIT

#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];

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 \tFragment");

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

BEST FIT

#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];

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

FIRST FIT

#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];

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

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

8 TH(IMPLEMENTATION OF PAGE
REPLACEMENT ALGORITHMS)

8.1 FIFO PAGE REPLACEMENT

#include <stdio.h>
int main()
{
int referenceString[10], pageFaults = 0, m, n, s, pages, frames;
printf("\nEnter the number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter reference string values:\n");
for( m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &referenceString[m]);
}
printf("\n What are the total number of frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(referenceString[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = referenceString[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = referenceString[m]; }
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}

8.2 LRU PAGE REPLACEMENT

#include<stdio.h>
int 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);
}
8.3 OPTIMAL PAGE REPLACEMENT
#include<stdio.h>
int main()
{
int fr[5],i,j,k,t[5],p=1,flag=0,page[25],psz,nf,t1,u[5];
printf("enter the number of frames:");
scanf("%d",&nf);
printf("\n enter the page size");
scanf("%d",&psz);
printf("\nenter the page sequence:");
for(i=1; i<=psz; i++)
scanf("%d",&page[i]);
for(i=1; i<=nf; i++)
fr[i]=-1;
for(i=1; i<=psz; i++)
{
if (full(fr,nf)==1)
break;
else
{
flag=0;
for(j=1; j<=nf; j++)
{
if(page[i]==fr[j])
{
flag=1;
printf(" \t%d:\t",page[i]); break;
}
}
if(flag==0)
{
fr[p]=page[i];
printf(" \t%d:\t",page[i]); p++;
}
for(j=1; j<=nf; j++)
printf(" %d ",fr[j]);
printf("\n");
}
}
p=0;
for(; i<=psz; i++)
{
flag=0;
for(j=1; j<=nf; j++)
{
if(page[i]==fr[j])
{
flag=1;
break;
}
}
if(flag==0)
{
p++;
for(j=1; j<=nf; j++)
{
for(k=i+1; k<=psz; k++) {
if(fr[j]==page[k])
{
u[j]=k;
break;
}
else
u[j]=21;
}
}
for(j=1; j<=nf; j++)
t[j]=u[j];
for(j=1; j<=nf; j++)
{
for(k=j+1; k<=nf; k++) {
if(t[j]<t[k])
{
t1=t[j];
t[j]=t[k];
t[k]=t1;
}
}
}
for(j=1; j<=nf; j++)
{
if(t[1]==u[j])
{
fr[j]=page[i];
u[j]=i;
}
}
printf("page fault\t");
}
else
printf(" \t");
printf("%d:\t",page[i]);
for(j=1; j<=nf; j++)
printf(" %d ",fr[j]);
printf("\n");
}
printf("\ntotal page faults: %d",p+3); // getch();
}
int full(int a[],int n)
{
int k;
for(k=1; k<=n; k++)
{
if(a[k]==-1)
return 0;
}
return 1;
}

9TH(IMPLEMENTATION OF DISK
SCHEDULING ALGORITHMS)

9.1 FCFS SCHEDULING ALGORITHM

#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
// logic for FCFS disk scheduling
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Total head moment is %d",TotalHeadMoment);
return 0;
}
9.2 SSTF (SHORTEST SEEK TIME FIRST)
ALGORITHM )

#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
// logic for sstf disk scheduling
/* loop will execute until all process is completed*/ while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}
printf("Total head movement is %d",TotalHeadMoment); return 0;
}
9.3 SCAN SCHEDULING ALGORITHM

# include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1); initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0); initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment); return 0;
}

9.4 C-SCAN SCHEDULING ALGORITHM

# include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for C-Scan disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1); /*movement
max to min disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0); initial=0;
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}

// if movement is towards low value


{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0); /*movement min
to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}
}

9.5 LOOK DISK SCHEDULING ALGORITHM

#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for look disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment); return 0;
}

9.6 C-LOOK DISK SCHEDULING ALGORITHM

# include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
// logic for C-look disk scheduling
/*logic for sort the request array */
for(i=0;i<n;i++)
{
for( j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i];
}}
printf("Total head movement is %d",TotalHeadMoment); return 0;
}

10 .IMPLEMENTATION OF FILE
ORGANIZATION TECHNIQUES

10.1 CONTIGUOUS (SEQUENTIAL) FILE


ALLOCATION ALGORITHM

#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)

c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}

10.2 LINKED FILE ALLOCATION ALGORITHM

#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}
f[10];
int main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}

10(3)
#include<stdio.h>
#include<conio.h>
int main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d",x);
i=x-1;
printf("Index is:%d\n",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}

You might also like