OS CSL404 Lab Manual
OS CSL404 Lab Manual
Sr. Mapped
Name of Experiment
No CO
To explore the internal commands of linux like ls, cp, rm, mv, chdir, mkdir, cat, more, CSL404.1
1
date, cal, chmod, etc CSL404.2
To explore the internal commands of linux like head, tail, touch, pwd, wc, who, tty, ps, CSL404.1
2
kill, echo, etc CSL404.2
3 To write shell scripts to display current logged in user and log name CSL404.3
To write shell scripts to Display current shell, home directory, operating system type,
4 CSL404.3
current path setting, current directory
5 To illustrate the use of sort, grep, awk, etc
CSL404.1
CSL404.3
Write a program to for System calls of Linux operating system for creating parent & CSL404.2
6
child process using fork, getpid, exit, etc CSL404.6
7 Write a program to implement CPU scheduling algorithms for FCFS CSL404.4
8 Write a program to implement CPU scheduling algorithm for Round Robin CSL404.4
9 Write a program to implement dynamic partitioning placement algorithms for First-fit CSL404.5
10 Write a program to implement dynamic partitioning placement algorithms for Best Fit CSL404.5
Operating System
Experiment No.: 1
student@project-OptiPlex-360:~/dir_1$ncal
January 2018
Su 7 14 21 28
Mo 1 8 15 22 29
Tu 2 9 16 23 30
We 3 10 17 24 31
Th 4 11 18 25
Fr 5 12 19 26
Sa 6 13 20 27
student@project-OptiPlex-360:~/dir_1$ncal -mfebruary
February 2018
Su 4 11 18 25
Mo 5 12 19 26
Tu 6 13 20 27
We 7 14 21 28
Th 1 8 15 22
Fr 2 9 16 23
Sa 3 10 17 24
student@project-OptiPlex-360:~/dir_1$ncal -y2020 ncal: invalid option -- '2'
Usage: cal [general options] [-hjy] [[month] year] cal [general options] [-hj] [-m
month] [year] ncal [general options] [-bhJjpwySM] [-s country_code] [[month] year]
ncal [general options] [-bhJeoSM] [year]
General options: [-NC31] [-A months] [-B months] For debug the highlighting: [-H yyyy-
mm-dd] [-d yyyy-mm]
student@project-OptiPlex-360:~/dir_1$ncal -y 2019
2019
January February March April
Su 6 13 20 27 3 10 17 24 3 10 17 24 31 7 14 21 28
Mo 7 14 21 28 4 11 18 25 4 11 18 25 1 8 15 22 29 Tu 1 8 15 22 29
5 12 19 26 5 12 19 26 2 9 16 23 30 We 2 9 16 23 30 6 13 20 27 6 13
20 27 3 10 17 24
Th 3 10 17 24 31 7 14 21 28 7 14 21 28 4 11 18 25
Fr 4 11 18 25 1 8 15 22 1 8 15 22 29 5 12 19 26
Sa 5 12 19 26 2 9 16 23 2 9 16 23 30 6 13 20 27
7. Conclusion :understood how the Linux system call and commands are run
Operating System
Experiment No.: 2
5. Theory:
Commands used: head, tail, touch, pwd, wc, who, tty, ps, kill, echo, man etc
Description with examples:
1) head: ‘head’ by default, prints the first 10 lines of each FILE to standard output.
With more than one FILE, it precedes each set of output with a header identifying
the file name. If no FILE is specified, or when FILE is specified as a dash ("-"), head
reads from standard input
2) tail: ‘tail’ prints the last 10 lines of each file to standard output. If you specify
more than one file, each set of output is prefixed with a header showing the file name
3) touch: ‘touch’ command is used to create a file(not directory)
4) pwd: ‘pwd’ command is used to print working directory, it displays the directory
in which we are currently working.
5) wc: ‘wc’ stands for word count. It is used to find out Number of limes, word
count, byte & character count in the files specified in the file arguments
6) who: the ‘who’ command displays all the users who have logged into system
currently 7)tty: the ‘tty’ command is used to print the filename of the terminal
connected to standard input
8) ps: this command is used for viewing the process running on the system. It
provides snapshot of the current processes along with detailed information like
userid, cpu usage, memory usage, command name,etc
9) kill: this command is used to terminate process without having to logout or reboot
10) echo: ‘echo’ is a built in command for bash that is typically used in
scripting language & batch files to display a line of text/string on standard output
6. Procedure/Program/Output:
student@project-OptiPlex-360:~/dir_1$ cat file_3
this is the third file
demonstratingwc command
student@project-OptiPlex-360:~/dir_1$ wc file_3
2 8 48 file_3
student@project-OptiPlex-360:~/dir_1$ who student tty7 2016-02-11 21:59 (:0)
student@project-OptiPlex-360:~/dir_1$ who -l LOGIN tty1 2016-02-11 21:58 1348
id=tty1
student@project-OptiPlex-360:~/dir_1$ who -r run-level 5 2016-02-11 21:58
student@project-OptiPlex-360:~/dir_1$ w
23:36:24 up 1:38, 1 user, load average: 0.77, 0.59, 0.42
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
student tty7 :0 21:59 731days 1:36 0.19s /sbin/upstart -
student@project-OptiPlex-360:~/dir_1$ tty
/dev/pts/0
student@project-OptiPlex-360:~/dir_1$ ps
PID TTY TIME CMD
9849 pts/0 00:00:00 bash
11194 pts/0 00:00:00 cat
11221 pts/0 00:00:00 cat
11234 pts/0 00:00:00 cat
11247 pts/0 00:00:00 cat
11248 pts/0 00:00:00 cat
11332 pts/0 00:00:00 cat
11480 pts/0 00:00:00 cat 11585 pts/0 00:00:00 ps
student@project-OptiPlex-360:~/dir_1$ echo hello hi everyone hello hi everyone
student@project-OptiPlex-360:~/dir_1$ echo –e
student@project-OptiPlex-360:~/dir_1$ echo have a nice day have a nice day
student@project-OptiPlex-360:~/dir_1$ echo have \na \nnice \nday have nannicenday
student@project-OptiPlex-360:~/dir_1$ echo -e have \na \nnice \nday have
nannicenday
student@project-OptiPlex-360:~/dir_1$ kill -l
7. Conclusion: understood how the Linux system call and commands are run
Operating System
Experiment No.: 3
6. Procedure/ Program:
#!/bin/sh
echo "Logged User : $(echo $USER)"
echo "Current date and time : $(date)"
7. Results:
Logged User : student
Current date and time : Mon Mar 5 12:24:10 IST 2018
8. Conclusion : understood how to write and execute the shell scripts run
Operating System
Experiment No.: 4
Experiment No. 4
1. Aim: To write shell scripts to Display current shell, home directory, operating system
type, current path setting, current directory
2. Objectives : To introduce basic concepts and functions of operating systems, and how to
write scripts and shell commands using kernel APIs
3. Outcomes: Student will learn to write shell script to Display current shell, home
directory, operating system type, current path setting, current directory
4. Hardware / Software Required: Computer with Ubuntu
5. Theory:
A shell is special user program which provide an interface to user to use operating system
services. Shell accept human readable commands from user and convert them into
something which kernel can understand. It is a command language interpreter that
execute commands read from input devices such as keyboards or from files. The shell
gets started when the user logs in or start the terminal. Shell is broadly classified into two
categories –
Command Line Shell
Graphical shell
Shell can be accessed by user using a command line interface. A special program called
Terminal in linux/macOS or Command Prompt in Windows OS is provided to type in the
human readable commands such as “cat”, “ls” etc. and then it is being execute. The result
is then displayed on the terminal to the user.
6. Procedure/ Program:
#!/bin/sh
_mydir="`pwd`"
echo "dir: $_mydir"
echo "MY shell: $SHELL"
cat /etc/os-release
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
echo $SCRIPTPATH
7. Results:
dir: /home/student MY shell: /bin/bash
NAME="Ubuntu"
VERSION="16.04.2 LTS (XenialXerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.2 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
/home/student
8. Conclusion :Understood how access system variables using the shell scripts
Operating System
Experiment No.: 5
Experiment No. 5
1. Aim: To illustrate the use of sort, grep, awk, etc
2. Objectives: To introduce basic concepts and functions of operating systems, and how to
write scripts and shell commands using kernel APIs
3. Outcomes: Student will learn the use of sort, grep, awk, etc
4. Hardware / Software Required: Computer with Ubuntu
5. Theory:
1) Sort:
Name:
sort - sort lines of text files
Synopsis:
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
Description:
Write sorted concatenation of all FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too. Ordering
options: -b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order
consider only blanks and alphanumeric characters
-f, --ignore-case
fold lower case to upper case characters
-g, --general-numeric-sort
compare according to general numerical value
-i, --ignore-nonprinting consider only printable characters -M, --month-sort
compare (unknown) < 'JAN' < ... < 'DEC'
-h, --human-numeric-sort
compare human readable numbers (e.g., 2K 1G)
-n, --numeric-sort
compare according to string numerical value
-R, --random-sort
shuffle, but group identical keys. See shuf(1)
2) GREP
Name:
grep, egrep, fgrep, rgrep - print lines matching a pattern
Synopsis:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN]... [-f FILE]... [FILE...]
Description:
grep searches the named input FILEs for lines containing a match to the given.
By default, grep prints the matching lines.
In addition, the variant programs egrep, fgrep and rgrep are the same as grep - E, grep -F,
and grep -r, respectively.
These variants are deprecated, but are provided for backward compatibility.
Options:
Generic Program Information
--help Output a usage message and exit.
-V, --version
Output the version number of grep and exit.
Matcher Selection
-E, --extended-regexp
3) AWK
Name: gawk - pattern scanning and processing language
Synopsis:
gawk [ POSIX or GNU style options ] -f program-file [ -- ] file ...
gawk [ POSIX or GNU style options ] [ -- ] program-text file ...
Description:
Gawk is the GNU Project's implementation of the AWK programming
language. It conforms to the definition of the language
in the POSIX 1003.1 Standard. This version in turn is based on the description in The
AWK Programming Language, by Aho,
Kernighan, and Weinberger. Gawk provides the additional features found in the current
version of Brian Kernighan's awk and a number of GNU-specific extensions.
The command line consists of options to gawk itself, the AWK program text (if not
supplied via the -f or --file options), and values to be made available in the ARGC and
ARGV pre-defined AWK variables.
When gawk is invoked with the --profile option, it starts gathering profiling statistics
from the execution of the program. Gawk runs more slowly in this mode, and
automatically produces an execution profile in the file awkprof.out when done.
6. Procedure/ Program/Output:
student@project-OptiPlex-360:~/Desktop$ cat list1
2233|a.k.shukla |g.m |sales |12/12/52
2365|barun sengupta |director|production|11/05/47
9876|keval mhatre |chairman|admin |12/03/56 8752|varun gupta |d.g.m |personnel |
19/04/43
student@project-OptiPlex-360:~/Desktop$ sort -t"|" -k 1 list1
2233|a.k.shukla |g.m |sales |12/12/52
2365|barun sengupta |director|production|11/05/47
8752|varun gupta |d.g.m |personnel |19/04/43 9876|keval mhatre |chairman|admin |
12/03/56
student@project-OptiPlex-360:~/Desktop$ sort -t"|" -k 2 list1
2233|a.k.shukla |g.m |sales |12/12/52
2365|barun sengupta |director|production|11/05/47
9876|keval mhatre |chairman|admin |12/03/56 8752|varun gupta |d.g.m |personnel |
19/04/43
student@project-OptiPlex-360:~/Desktop$ sort -t"|" -k 3 list1
9876|keval mhatre |chairman|admin |12/03/56
8752|varun gupta |d.g.m |personnel |19/04/43
2365|barun sengupta |director|production|11/05/47
2233|a.k.shukla |g.m |sales |12/12/52
student@project-OptiPlex-360:~/Desktop$ sort -t"|" -k 4 list1
9876|keval mhatre |chairman|admin |12/03/56
8752|varun gupta |d.g.m |personnel |19/04/43
2365|barun sengupta |director|production|11/05/47
2233|a.k.shukla |g.m |sales |12/12/52
\
student@project-OptiPlex-360:~/Desktop$ sort -t"|" -k 5 list1
2365|barun sengupta |director|production|11/05/47
9876|keval mhatre |chairman|admin |12/03/56
2233|a.k.shukla |g.m |sales |12/12/52
8752|varun gupta |d.g.m |personnel |19/04/43
Experiment No.: 6
2) getpid:
Name:
getpid, getppid - get process identification
Synopsis:
#include <sys/types.h> #include <unistd.h>pid_tgetpid(void); pid_tgetppid(void);
Description:
getpid() returns the process ID of the calling process. (This is often used by routines that
generate unique temporary filenames.) getppid() returns the process ID of the parent of
the calling process.
3) exit:
Name:
exit - cause normal process termination
Synopsis:
#include <stdlib.h>
void exit(int status);
Description:
The exit() function causes normal process termination and the value of status & 0377 is
returned to the parent (see wait(2)).
All functions registered with atexit(3) and on_exit(3) are called, in the reverse order of
their registration. (It is possible for one of these functions to use atexit(3) or on_exit(3) to
register an addi- tional function to be executed during exit processing; the new regis-
tration is added to the front of the list of functions that remain to be called.) If one of
these functions does not return (e.g., it calls _exit(2), or kills itself with a signal), then
none of the remaining functions is called, and further exit processing (in particular, flush-
ing of stdio(3) streams) is abandoned. If a function has been regis- tered multiple times
using atexit(3) or on_exit(3), then it is called as many times as it was registered.
6. Procedure/ Program:
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
int main(void)
{
pid_tpid;
printf("Before fork\n");
pid=fork();
if(pid>0)
{
sleep(1);
printf("PARENT--PID:%d PPID: %d,CHILD PID: %d\n",getpid(),getppid(),pid);
}
else if(pid==0)
printf("CHILD -- PID :%d\n",getpid(),getppid());
else
{
printf("Fork error\n");
exit(1);
}
printf("Both processes continue from here\n");
exit(0);
}
7. Results:
Before fork
CHILD -- PID :2818
Both processes continue from here
PARENT--PID:2817 PPID: 2772,CHILD PID: 2818
Both processes continue from here
8. Conclusion :Understood how to use theprocess management commands
Operating Systems
Experiment No.: 7
Experiment No. 7
Implement and analyze different process
1. Aim: Write a program to implement CPU scheduling algorithms for FCFS
2. Objectives: Implement and analyze different process scheduling algorithms
3. Outcomes: Student will learn to write a program to implement CPU scheduling
algorithms for FCFS
4. Hardware / Software Required: Computer with Ubuntu
5. Theory:
First Come First Served (FCFS) is a Non-Preemptive scheduling algorithm. FIFO (First
In First Out) strategy assigns priority to process in the order in which they request the
processor. The process that requests the CPU first is allocated the CPU first. This is
easily implemented with a FIFO queue for managing the tasks. As the process come in,
they are put at the end of the queue. As the CPU finishes each task, it removes it from the
start of the queue and heads on to the next task.
FCFS provides an efficient, simple and error-free process scheduling algorithm that saves
valuable CPU resources. It uses non-preemptive scheduling in which a process is
automatically queued and processing occurs according to an incoming request or process
order. FCFS derives its concept from real-life customer service.
For e.g.:
Suppose there are three processes in a queue: P1, P2 and P3. P1 is placed in the
processing register with a waiting time of zero seconds and 10 seconds for complete
processing. The next process, P2, must wait 10 seconds and is placed in the processing
cycle until P1 is processed. Assuming that P2 will take 15 seconds to complete, the final
process, P3, must wait 25 seconds to be processed. FCFS may not be the fastest process
scheduling algorithm, as it does not check for priorities associated with processes. These
priorities may depend on the processes' individual execution times.
6. Procedure/ Program:
#include<stdio.h>
int main()
{
intn,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):"); scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0 //calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j>i; j++);
wt[i]+=b[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
7. Results:
Enter Process Burst Time
P[1]:24
P[2]:3
P[3]:3
Process Burst Time Waiting Time Turnaround Time
P[1] 24 0 24
P[2] 3 24 27
P[3] 3 27 30
Average Waiting Time:17
Average Turnaround Time:27
8. Conclusion : Understood and implemented the CPU scheduling algorithms for FCFS
Operating System
Experiment No.: 8
Experiment No. 8
1. Aim: Write a program to implement CPU scheduling algorithm for Round Robin
2. Objectives: Implement and analyze different process scheduling algorithms
3. Outcomes: Student will learn to write a program to implement CPU scheduling
algorithm for Round Robin
4. Hardware / Software Required: Computer with Ubuntu
5. Theory:
Round Robin Scheduling Algorithm:
1. The queue structure in ready queue is of First In First Out (FIFO) type.
2. A fixed time is allotted to every process that arrives in the queue. This fixed time is
known as time slice or time quantum.
3. The first process that arrives is selected and sent to the processor for execution. If it is
not able to complete its execution within the time quantum provided, then an interrupt is
generated using an automated timer.
4. The process is then stopped and is sent back at the end of the queue. However, the state
is saved and context is thereby stored in memory. This helps the process to resume from
the point where it was interrupted.
5. The scheduler selects another process from the ready queue and dispatches it to the
processor for its execution. It is executed until the time Quantum does not exceed.
6. The same steps are repeated until all the process are finished.
The round robin algorithm is simple and the overhead in decision making is very low. It
is the best scheduling algorithm for achieving better and evenly distributed response time.
6. Procedure/ Program:
#include<stdio.h>
int main()
{
intcount,j,n,time,remain,flag=0,time_quantum;
intwait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|TurnaroundTime|Waiting Time\n\n"); for(time=0,count=0;remain!
=0;)
{
if(rt[count]<=time_quantum&&rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1) count=0;
else if(at[count+1]<=time)
count++;
else count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
7. Results:
Enter Total Process: 4
Enter Arrival Time and Burst Time for Process Process Number 1 :0
9
Enter Arrival Time and Burst Time for Process Process Number 2 :1
5
Enter Arrival Time and Burst Time for Process Process Number 3 :2
3
Enter Arrival Time and Burst Time for Process Process Number 4 :3 4
Enter Time Quantum: 5
Process |Turnaround Time|Waiting Time
P[2] | 9 | 4
P[3] | 11 | 8
P[4] | 14 | 10
P[1] | 21 | 12
Average Waiting Time= 8.500000
Avg Turnaround Time = 13.750000
8. Conclusion : Understood and implemented the CPU scheduling algorithm for Round
Robin
Operating System
Experiment No.: 9
Write a program to implement dynamic
partitioning placement algorithms for
First-fit
Experiment No. 9
1. Aim: Write a program to implement dynamic partitioning placement algorithms for First-
fit
2. Objectives: Implement and analyze different memory management algorithms
3. Outcomes: Student will learn to write a program to implement dynamic partitioning
placement algorithms for First-Fit
4. Hardware / Software Required: Computer with Ubuntu
5. Theory:
In this scheme we check the blocks in a sequential manner which means we pick the first
process then compare it’s size with first block size if it is less than size of block it is
allocated otherwise we move to second block and so on.
When first process is allocated we move on to the next process until all processes are
allocated. In the first fit, partition is allocated which is first sufficient from the top of
Main Memory.
Example :
Input :
blockSize[] = {100, 500, 200, 300, 600}; processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 2
2 417 5
3 112 2
4 426 Not Allocated
• Its advantage is that it is the fastest search as it searches only the first block i.e. enough
to assign a process.
• It may problems of not allowing processes to take space even if it was possible to
allocate. Consider the above example, process number 4 (of size 426) does not get
memory. However it was possible to allocate memory if we had allocated using best fit
allocation {block number 4 (of size 300) to process 1, block number 2 to process 2, block
number 3 to process 3 and block number 5 to process 4}.
6. Procedure/ Program:
#include<stdio.h>
void main()
{
intbsize[10], psize[10], bno, pno, flags[10], allocation[10], i,j;
for(i = 0; i< 10; i++)
{
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("\nEnter size of each block: ");
for(i = 0; i<bno; i++)
scanf("%d", &bsize[i]);
printf("\nEnter no. of processes: ");
scanf("%d", &pno);
printf("\nEntersizeof each process:”);
for(i = 0; i<pno; i++)
scanf("%d", &psize[i]);
for(i = 0; i<pno; i++) //allocation as per first fit
for(j = 0; j <bno; j++)
if(flags[j] == 0 &&bsize[j] >= psize[i])
{
allocation[j] = i;
flags[j] = 1;
break;
}
//display allocation details
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i<bno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
}
}
7. Results:
Enter no. of blocks: 3
Enter size of each block: 12
7
4
Enter no. of processes: 3
Enter size of each process: 7
4
9
Block no. size process no. size
1 12 1 7
2 7 2 4
3 4 Not allocated
8. Conclusion :Understood and implemented dynamic partitioning placement algorithms
for First-fit
Operating System
Experiment No.: 10
6. Procedure/ Program:
void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
staticintbarray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n");
for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=1;i<=np &&parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}
7. Results:
Enter the number of blocks:5
Enter the number of processes:4
Enter the size of the blocks:- Block no.1:10
Block no.2:15
Block no.3:5
Block no.4:9
Block no.5:3
Enter the size of the processes :-
Process no.1:1
Process no.2:4
Process no.3:7
Process no.4:12
Process_noProcess_sizeBlock_noBlock_size Fragment
1 1 5 3 2
2 4 3 5 1
3 7 4 9 2
4 12 2 15 3
8. Conclusion :understood and implemented dynamic partitioning placement algorithms for
Best Fit