Padosi Osfile
Padosi Osfile
of
Operating System
22CS005
Submitted
the degreeof
BACHELEOR OF ENGINEERING
in
CHITKARA UNIVERSITY
CHANDIGARH-PATIALA NATIONAL HIGHWAY
RAJPURA (PATIALA) PUNJAB-140401 (INDIA).
June, 2024
2
Program – 1
AIM: Installation: Configuration & Customizations of Linux.
Introduction to GCC compiler: Basics of GCC, Compilation of program,Execution
of program, Time stamping, Automating the execution using Make file.
By default, it comes with the most Linux distributions. We can verify it by executing
the below command:
1. gcc -version
The above command will display the installed version of the GCC tool. If itis not
installed, follow the below steps to install it:
It will ask for the system administrative password, enter the password. Itwill
start updating the system package. Consider the below snap of output:
It contains various packages such as gcc, g++, and make utility. Execute the
below command to install it:
The above command will install the required packages for the GCC utility.Now,
we can use the GCC utility in our machine. Consider the below snapof output:
Step 3: Verify the installation.
1. gcc --version
It will display the installed version of GCC utility. To display the more specific
details about the version, use the '-v' option. Consider the below output:
Here, we have successfully installed the GCC utility. Let's understand to use it.
We will create and execute some c programs using GCC.
Create a basic c program "Hello world!". Create a file 'hello.c" and put below
code in it:
1. #include <stdio.h>
2. int main() {
3. printf("Hello, world!\n");
4. return 0;
5. }
Now, compile the hello.c as follows:
4
1. gcc hello.c
If we directly run the hello.c, it will throw the error. Make it executable, the
default executable file for the Linux system is a.out. To execute the file, execute
the chmod command as follows:
3. ./a.out
Consider the below output:
By default, the gcc command creates the object file as 'a.out.' If you want to
change the default output file name, use the '-o' option.
1. gcc hello.c
The above command will generate the object file 'a.out.' To specify theobject
file name, execute the command as follows:
1. gcc hello.c -o hello
It will generate the output file 'hello.' Consider the below output:
5
o Enable all Warnings
To enable all warnings in the output, use the '-Wall' option with the gcccommand.
Let's create a variable in the main function of hello.c. Consider the below code:
hello.c:
1. #include <stdio.h>
2. int main() {
3. int a;
4. printf("Hello, world!\n");
5. return 0;
6. }
If we compile the above code using -Wall option. It will throw the
warnings. Execute the below command to compile the file:
The above command will display the warnings. Consider the below output:
We can produce only the preprocess output by using the '-E' option. Consider
the below command:
6
Produce the assembly code
To produce the assembly code, execute the command with the '-S' option.
Consider the below command:
The above command will generate the 'hello.s.' file that contains the
assembly code. Consider the below output:
We can produce all the intermediate files of the compilation process byusing
the '-save-temp' option. Consider the below output:
3. gcc -save-temps hello.c
The above command will generate all the intermediate files as well as
executable files at once. Consider the below output:
7
We have discussed some most useful examples of the gcc command. Since the
gcc command facilitates with a huge number of options, you can get stuck
anywhere while using it. Let's see how to get help by yourself from the
terminal.
If you get stuck anywhere while using the gcc command, you can take help
from your terminal. To access the manual from the command line, execute the
man command as follows:
man gcc
8
Program - 2
AIM: Implement Process concepts using C language by printing processId,execute Linux
command as sub process, creating and executing process using fork and exec system calls.
The title of our practical contains two different functions of the C language that occur while
running any program in our system. Unfortunately, there is zero possibility of occurring
more than one program at a time in C. Onlya single task can happen at a particular time,
which means it doesn’t allowconcurrent programs to run. It causes you to wait for the
completion of one process to execute the other one. To avoid this troubleshooting problem,
you may need to develop yourprogram in a good way, being a good developer. Linux fork () is
used to overcomethe waiting and to allowconcurrency in your system. It helps in duplicating a
new process and creates a new one by calling the duplicated process. The new process isthe
child process, and the previous one is called the parent process.
Whereas, exec () function is used to change the current existing programwith the new one.
This replacement is done by making amendments to the content of a program or a file. So,
the dissimilarity between fork and exec is that fork creates a new process from the existing
process, and exec is used to replace the existing program by creating a new one.
Prerequisites
To execute any c program on your Linux system, we need to install some prerequisites on it.
Go to the terminal by using the shortcut method Ctrl+Alt+T. Now write the following
commands to install the man pages.
sudo apt install manpages-dev
9
It will install all the corresponding pages.
Moving forward, to run a program on Linux, you need to install a codecompiler. That is used
to compile the code and execute it. For this purpose, we will install GCC repositories in our
system.
$ man exec
10
Now in the above-cited image, you can observe the types of exec.These are the family of
exec functions. Each one is for a different function following the same base, “exec.”
Example: Now, moving further, we will describe the functionality of execwith the help of
an example. We will take one function of exec to demonstrate its working, which is
“execv.” Firstly, we will create two fileswith the extension of “.c.” After their creation, we
will write respective codes in them and execute them to see the result.
Consider a file name “sample4.c”. Please open it and use the followingcode. In this code, we
have used execv in a particular manner cited below.
First of all, we have printed the current process’s id. Secondly, we havecreated a character
array having NULL in the end for the termination.
Thirdly we have called the sample4copy function.
Sample4copy.c
When we call the exec function(), the process image is changed. Below cited image
below shows the code of sample4copy.c.
Here we have used only print statements to get the id of the currentprocess.
11
The output of the respective codes can be obtained by using thefollowing commands.
As we have described earlier, the “GCC” word is used to compile thecode, and after
compiling, the code is executed successfully.
According to the image, The PID of sample4.c file is shown first as it was declared before the
exec call. Then after the exec() function is called, both the print statements of file
sample4copy.c is executed where getpid() is used to obtain the process’s id.
Coding with a fork in c
The fork() function creates the child process from the parent process. It also
contains two headers, including the fork information in it.
Syntax:
Pid_t fork(void);
We can use the man page for help in the usage
$ man fork
12
Example: Now consider an example by creating a file “sample3.c”. We will enter the
code inside the file. According to the code, we have set thefork statusas forkrank.
Sample3.c
We have used the “if-else” statement to apply the condition. Simple print commands are
declared here to help in the understanding of the fork() concept. Forkrank is first
declared as 0 and then -1. With a fork(), there are now two processes that are working
concurrently. Output can be obtained by using the same code, as used above in exec
example.
$ GCC –o sample3.c
$./sample3
The output shows that the child process is executed earlier than the parent when the
parent process was waiting. The wait function implies that it causes the parent function
to wait unless one of all the child processes is terminate.
13
Fork and Exec system calls Collectively
Here we will take two files named “sample1.c” and “sample2.c”. First, open the
file sampl1.c and write the code that is appended below in theimage. We have
used the fork() system-call here; when the child process is created, p will be
assigned with 0. While using exec system-call, the sample1.c will be replaced
with sample2.c.
Sample1.c
Sample2.c
14
Similar to the examples discussed above, the sample2 file will
contain the printf statements in it. In sample1.c, the first print
command is executed first, then the fork function is called, when
p== 0, then child portion is executed and sample2.c file will be run.
The output will contain GCC to compile both files. Here parent
sample1.c id and sample2.c id isdifferent because they are parent
and child.
Conclusion
In this experiment, we have used both fork and exec separately and
collectively to understand the usage and concept easily.
15
Program–3
AIM: Implement FCFS, SJF, priority scheduling, and RR schedulingalgorithms in
C language.
Turn Around Time (TAT): Time Difference between completion time andarrival
time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time (WT): Time Difference between turn around time and bursttime.
Waiting Time = Turn Around Time – Burst Time
16
Program Code
P1 0 4 4 4 0
P2 1 3 7 6 3
P3 2 1 8 6 5
P4 3 2 10 7 5
P5 4 5 15 11 6
Average WT = 3.800000
These algorithms schedule processes in the order in which the shortest job is done
first. It has a minimum average waiting time.
1. Burst Time
2. Average waiting time
3. Average turnaround time
18
Non-Preemptive Shortest Job First
Here is an example
TurnAround
Process Id Burst Time Waiting Time
Time
4 3 0
1 6 3
3 7 9
2 8 16
19
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("n Process Burst Time Waiting Time Turnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt); printf("nAverage
Turnaround Time=%fn",avg_tat);
}
Output:
20
In the above program, we calculate the average waiting and average turn
around times of the jobs. We first ask the user to enter the numberof processes
and store it in n. We then accept the burst times from the user. It is stored in the
bt array.
After this, the burst times are sorted in the next section so the shortest one can
be executed first. Here selection sort is used to sort the array of burst time bt.
Waiting time of the first element is zero, the remaining waiting time is calculated
by using two for loop that runs from 1 to in that controls the outer loop and the
inner loop is controlled by another for loop that runs from j=0 to j<i. Inside the
loop, the waiting time is calculated by adding the burst time to the waiting time.
1for(i=1;i<n;i++)
2 {
3 wt[i]=0;
4 for(j=0;j<i;j++)
5 wt[i]+=bt[j];
6 total+=wt[i];
7}
Total is the addition of all the waiting time together. The average waitingtime is
calculated:
avg_wt=(float)total/n;
and it is printed.
Next, the turnaround time is calculated by adding the burst time and thewaiting
time
1for(i=0;i<n;i++)
2{
3 tat[i]=bt[i]+wt[i];total+=tat[i];
4 printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
5}
6
Again, here the for loop is used. And the total variable here holds the totalturnaround
time. After this the average turnaround time is calculated. This is how Non-
Preemptive scheduling takes place
21
Code for Pre-emptive SJF Scheduling
#include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end; float average_waiting_time,
average_turnaround_time; printf("nEnter the Total Number of Processes:t");
scanf("%d", &limit);
printf("nEnter Details of %d Processesn", limit);
for(i = 0; i < limit; i++)
{
printf("nEnter Arrival Time:t"); scanf("%d",
&arrival_time[i]); printf("Enter Burst Time:t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] <burst_time[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[small
turnaround_time = turnaround_time + end - arriva
}
}
average_waiting_time = wait_time / limit; average_turnaround_time =
turnaround_time / limit; printf("nnAverage Waiting Time:t%lfn",
average_waiting_time) printf("Average Turnaround Time:t%lfn",
average_turnaround_treturn 0;
}
22
Output:
The only difference in preemptive and non-preemptive is that when two burst
times are same the algorithm evaluates them on first come first serve basis. Hence
there is an arrival time variable.
In the priority scheduling algorithm, each process has a priority associated with it and
as each process hits the queue, it is stored based on its priority so that processes with
higher priority is dealt with first. It should be noted that equal priority processesare
scheduled in FCFS order.
To prevent high priority processes from running indefinitely the scheduler may
decrease the priority of the currently running process at each clock tick (i.e., at each
clock interrupt). If this action causes its priority to drop below that of the next highest
process, a process switch occurs. Alternatively, each process may be assigned a
maximum time quantum that it is allowed to run. When this quantum is used up, the
next highest priority process is given a chance to run.
23
Example:
P3 2 6 4
P4 4 4 3
P5 6 2 5
In this example, there are 5 processes with their arrival time, burst time, and
priority. The execution order, waiting time, andturnaround time for each process
will be as given below.
P1 5 3 8
P4 4 8 12
P3 6 12 18
P5 2 18 20
The problem occurs when the operating system gives a particular task a very low
priority, so it sits in the queue for a larger amount of time, not being dealt with by the
CPU. If this process is something the user needs, there could be a very long wait, this
process is known as “Starvation” or “Infinite Blocking”.
Solution
Many operating systems use a technique called “aging”, in which a low priority process
slowly gains priority over time as it sits in the queue. Even if, the priority of
24
Code for Priority Scheduling
1 #include<stdio.h>
2int main()
3{
4 int
5bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
6 printf("Enter Total Number of Process:");
7 scanf("%d",&n);
8 printf("\nEnter Burst Time and Priority\n");
9 for(i=0;i<n;i++)
10 {
11 printf("\nP[%d]\n",i+1);
12 printf("Burst Time:");
13 scanf("%d",&bt[i]);
14 printf("Priority:");
15 scanf("%d",&pr[i]);
16 p[i]=i+1; //contains process number
17 }
18//sorting burst time, priority and process number in ascending order using
19selection sort
20 for(i=0;i<n;i++)
21 {
22 pos=i;
23 for(j=i+1;j<n;j++)
24 {
25 if(pr[j]<pr[pos])
26 pos=j;
27 }
28 temp=pr[i];
29 pr[i]=pr[pos];
30 pr[pos]=temp;
31 temp=bt[i];
32 bt[i]=bt[pos];
33 bt[pos]=temp;
34 temp=p[i];
35 p[i]=p[pos];
36 p[pos]=temp;
37 }
38 wt[0]=0; //waiting time for first process is zero
39 //calculate waiting time
40 for(i=1;i<n;i++)
41 {
42 wt[i]=0;
43 for(j=0;j<i;j++)
44 wt[i]+=bt[j];
25
45
total+
=wt[i]; 46
}
47 avg_wt=total/n; //averagewaiting time
48 total=0;
49 printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
50 for(i=0;i<n;i++)
51 {
52 tat[i]=bt[i]+wt[i]; //calculate turnaround time
53 total+=tat[i];
54 printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
55 }
56 avg_tat=total/n; //average turnaround time
57 printf("\n\nAverage Waiting
58 Time=%d",avg_wt); printf("\nAverage
Turnaround Time=%d\n",avg_tat);
return 0;
}
OUTPUT:
26
ROUND ROBIN SCHEDULING
Round Robin CPU Scheduling is the most important CPU Scheduling Algorithm which is ever
used in the history of CPU Scheduling Algorithms. Round Robin CPU Scheduling uses Time
Quantum (TQ). The Time Quantum is something which is removed from the Burst Time and
Time Sharing is the main emphasis of the algorithm. Each step of this algorithm is carried out
cyclically. The system defines a specific time slice, known as a time quantum.
First, the processes which are eligible to enter the ready queue enter the ready queue. After
entering the first process in Ready Queue is executed for a Time Quantum chunk of time. After
execution is complete, the process is removed from the ready queue. Even now the process
requires some time to complete its execution, then the process isadded to Ready Queue.
The Ready Queue does not hold processes which already present in the Ready Queue. The
Ready Queue is designed in such a manner that it does not hold non unique processes. By
holding same processes Redundancy of the processes increases.
After, the process execution is complete, the Ready Queue does not take the completed process
for holding.
27
Advantages
Disadvantages
Ready Queue:
P1, P2, P3, P4, P5, P6, P1, P3, P4, P5, P6, P3, P4, P5
28
Gantt chart:
29
Program-4
AIM: Implement the basic and user status commands like: su, sudo, man, help, history,
who, whoami, id, uname, uptime, free, tty, cal, date, hostname, reboot, clear.
1. man command:
man command in Linux is used to display the user manual of any command that we
can run on the terminal.
Manual:
30
• man intro: Gives introduction to user commands.
2. hostname command:
Type hostname at the command line. This will print your computer name on the
next line.
31
1. who command:
who command is a tool print information about users who are currently logged in.
who command only see a real user who logged in.
• Who -p -H: To display information about all active processes that are
spawned by the NIT process.
• who -m -H:Command to display the hostname and user associated with the
input/output like keyboard.
1. whoami command:
The 'whoami' command in Linux is a built-in utility that displays the username of
the current user. It is used with the syntax, whoami [option] . It's a quick and easy
way to confirm your identity within the system. In this example, we've used the
'whoami' command in the terminal.
32
2. uptime command:
The Linux uptime command shows the system's uptime, how long it has been
running, and when it was last booted. Depending on the options, the command
prints the number of users and system load averages.
• uptime –help: Display all commands that can be used with uptime.
• uptime -s: Shows the time from which the system is up.
33
• uptime -v: It shows the output version information and exit.
3. cal command:
The cal command displays a calendar of the specified year or month. The Year
parameter names the year for which you want a calendar.
4. date command:
The "date" command in Linux is a simple but powerful tool used to display the
current date and time, as well as set the system date and time. This command is
extremely useful for troubleshooting and system administration tasks, and is a
vital tool in understanding any Linux user.
1. id command :
The id command is a basic Linux command used to confirm the identity of a
specified Linux user.
34
2. uname command:
The uname command writes to standard output the name of the operating system
that you are using.
35
4. sudo command:
The sudo command allows you to run programs with the security privileges of
another user.
5. history command:
The history command in Linux provides a chronological list of previously
executed commands, along with corresponding command numbers. This feature
allows users to recall, reuse, and modify commands without having to retype
them.
36
6. tty command:
The tty command in Linux is a built-in utility that displays the file name of the
terminal connected to the standard input.
tty
37
• tty –help: It will display the help message and exit.
38
Program – 5
Setup
To simulate deadlock in the system we will create the above shown situation.
P1 and P2 will be represented by two thread one and two.The two resources R1 and R2
will be represented by the two lock variables first_mutex and second_mutex
First thread one will acquire lock first_mutex and then thread two will acquirelock
second_mutex. Hence, both the threads have acquired oneresource each.
Next, thread one will try to acquire lock second_mutex while the second thread, thread two
will try to acquire lock first_mutex. Since the resources are already occupied by the other
thread, both the threads will get into a deadlock.
39
Program to create Deadlock Using C in Linux using Mutex Locks andthreads
#include<stdio.h>
#include<pthread.h>
#include<unistd.h> void
*function1(); void
*function2();
pthread_mutex_t first_mutex; //mutex lockpthread_mutex_t
second_mutex;
int main() {
pthread_mutex_init(&first_mutex,NULL); //initialize the lock
pthread_mutex_init(&second_mutex,NULL);
pthread_t one, two;
pthread_create(&one, NULL, function1, NULL); // create thread
pthread_create(&two, NULL, function2, NULL); pthread_join(one,
NULL);
pthread_join(two, NULL);
printf("Thread joined\n");
}
void *function1( ) {
sleep(1); pthread_mutex_lock(&second_mutex);
pthread_mutex_unlock(&first_mutex);
40
void *function2( ) { pthread_mutex_lock(&second_mutex);
printf("Thread TWO acquired second_mutex\n");sleep(1);
pthread_mutex_lock(&first_mutex); printf("ThreadTWO
acquired first_mutex\n");
pthread_mutex_unlock(&first_mutex); printf("Thread TWO
released first_mutex\n");
pthread_mutex_unlock(&second_mutex); printf("Thread
TWO released second_mutex\n");
}
41
Program-6
AIM: Implement the command that is used for Creating and Manipulating files
cat,cp,mv,rm,ls and its options,touch and their options,whichis ,whereis, whatis .
1. Cat: This command outputs the contents of a text file. You can use it to read brief
files or to concatenate files together:
• To append file1 onto the end of file2,enter:
Syntax: cat file1>>file2
• To view the contents of a file named myfile, enter:
Syntax: cat myfile
42
• Syntax:rm[FILE]
• Example:Delete file1:
$rm t1.txt
5. Is: A Linux command has the following basic syntax: ls [ Options ][File]
• Is -a list all files including hidden file starting with '.'.
• Is -1 list with long format - show permissions.
• Is-r list in reverse order.
• Is -t sort by time & date.
• Is -n It is used to print group ID and owner ID instead of their names.
• Is -m A list of entries separated by commas should fill the width.
• Is -g This allows you to exclude the owner and group information
43
1. Touch: It is used to create a file without any content.
• Touch command to create multiple files:
Syntax: touch File1-name File2-name File3-name
2. Cat: The cat command reads each file parameter in sequence and writes it to
standard output.
Syntax: cat file-name
44
Program – 7
1. mkdir: The mkdir (make directory) command is used to create a new directory.
• To Create a single directory
Syntax: mkdir Directory Name
• To Delete directory
Syntax: rmdir file
45
1. Pwd: The pwd(print working directory) command is used to print out the current
working directory we are in and they all are shell built in.
Syntax: cd [directory]
46
Program – 8
AIM: Implementing File system commands: Comparing files using diff,cmp,comm
the different ways involved for comparing two files.
• Example: Add write permission for user, group and others for filel.
• Example: Only show column-3 that contains lines common between file! and
file2
47
3. diff: This command is used to compare two files line by line.
• Description: The output indicates how the lines in each file are different,
and the steps invoved to change d1.txt to d2.txt The 'patch' command
can be used to make the suggested changes. The output is formatted as
blocks of:
48
Program – 9
AIM: File system: Introduction to File system, File system Architecture
and FileTypes.
Digital file systems and files are named for and modeled after paper- based
filing systems using the same logic-based method of storing and retrieving
documents.
Along with the file itself, file systems contain information such as the size of the
file, as well as its attributes, location and hierarchy in the directory in the
metadata. Metadata can also identify free blocks of available storage on the
drive and how much space is available.
49
A file system also includes a format to specify the path to a file through the
structure of directories. A file is placed in a directory -- or a folder in
Windows OS
-- or subdirectory at the desired place in the tree structure. PC and mobile
OSes have file systems in which files are placed somewhere in a hierarchical
tree structure.
Before files and directories are created on the storage medium, partitions
shouldbe put into place. A partition is a region of the hard disk or other storage
that theOS manages separately. One file system is contained in the primary
partition, and some OSes allow for multiple partitions on one disk. In this
situation, if onefile system gets corrupted, the data in a different partition will
be safe.
• Date created.
• Date modified.
Metadata is stored separately from the contents of the file, with many file
systems storing the file names in separate directory entries. Some metadata
maybe kept in the directory, whereas other metadata may be kept in a
structure called an inode.
File permissions such as access or capability control lists can also be used to
moderate file system access. These types of mechanisms are useful to prevent
access by regular users, but not as effective against outside intruders.
Encrypting files can also prevent user access, but it is focused more on
protecting systems from outside attacks. An encryption key can be applied to
unencrypted text to encrypt it, or the key can be used to decrypt encrypted
text. Only users with the key can access the file. With encryption, the file
system does not need to know the
encryption key to manage the data effectively.
Global file system (GFS) is a file system for the Linux OS, and it is a
shared disk file system. GFS offers direct access to shared block storage
and can be used as a local file system.
GFS2 is an updated version with features not included in the original GFS,
such as an updated metadata system. Under the terms of the GNU General
Public License, both the GFS and GFS2 file systems are available as free
software.
Hierarchical file system (HFS) was developed for use with Mac operating
systems. HFS can also be referred to as Mac OS Standard, and it wassucceeded
by Mac OS Extended. Originally introduced in 1985 for floppy andhard disks,
HFS replaced the original Macintosh file system. It can also be used on CD-
ROMs.
The NT file system -- also known as the New Technology File System(NTFS
- is the default file system for Windows products from Windows NT
3.1 OS onward. Improvements from the previous FAT file system include
better metadata support, performance and use of disk space. NTFS is also
supported in the Linux OS through a free, open-source NTFS driver. Mac
OSes have read- only support for NTFS.
Universal Disk Format (UDF) is a vendor-neutral file system used on optical
media and DVDs. UDF replaces the ISO 9660 file system and is the official
file system for DVD video and audio as chosen by the DVD Forum.
The centralized structure of a DBMS allows for easier file sharing than a file
system and prevents anomalies that can occur when separate changes are
made to files in a file system.
There are methods to protect files in a file system, but for heavy-duty security,
a DBMS is the way to go. Security in a file system is determined by the OS,
andit can be difficult to maintain over time as files are accessed and
authorization is granted to users.
The term file system can also refer to the part of an OS or an add-on
program that supports a file system. Examples of such add-on file systems
include the Network File System (NFS) and the Andrew File System
(AFS).
In addition, the term has evolved to refer to the hardware used for nonvolatile storage, the software application
that controls the hardware and architecture of both hardware and software
53
Program –10
Aim: Mass Storage structure :Overview, Disk structure, Disk Attachment, Disk
Scheduling, Disk Management.
Solution:
1) Primary Memory
A processor or computer initially or directly accesses primary memory while using a
computer. It enables a processor to access programs and services that are now in use
and temporarily stored in a particular area of memory. An operating system (OS),
user interface, and any user-installed and running software utilities are all loaded into
main memory as soon as a computer turns on.
2) Secondary Memory
Secondary memory is non-volatile, permanent computer memory that is not directly
accessible by a computer or processor. Data that can be quickly and easily retrieved,
transmitted, and utilized by apps and services can be stored by the user and then used
in this manner. Read-only memory (ROM), flash drives, hard disk drives (HDD),
magnetic tapes, and other forms of internal and external storage media are all
considered secondary memory.
The basic idea of Mass Storage is to create a Data Backup or Data Recovery System. The
Mass Storage Structure Devices are:
1. Magnetic Disks
2. Solid State Disks
3. Magnetic Tapes
Disk Structure
Disk structure in operating system refers to the physical and logical arrangement of data on
a hard disk drive. The physical structure of a hard disk drive consists of one or more
surfaces, each of which contains several tracks, each of which is divided into sectors. The
logical structure of a hard disk drive is divided into partitions, which are treated by the
operating system as separate disks.
Hard disk is organized into two different ways like as physical structure and logical
structure; below shown each one in detail, you can check them:
• Physical Structure of Hard Disk
The physical division of a hard disk refers to the way data is stored on the disk at a low
level. Here are the main parts of the physical structure of hard disk, including:
Platters: The disks that make up the hard disk drive. Each platter has an upper and lower
oxide-coated surface.
Read/Write Heads: The heads that float above the surface of the platters and read and
write data as the platters spin around. There is at least one head per surface.
Tracks: The concentric circles on the surface of the platters. Each track is divided into
sectors.
Sectors: The smallest physical storage units on the disk. Sectors are the unit of information
transfer and are mapped with a logical block on the disk.
Cylinders: The set of tracks that have the same track value on all platters. A cylinder is
made up of rings on the upper and lower surfaces of all of the platters.
Spindle: Help to connects all the platters and is connected to a motor. The motor of the
spindle rotates with a constant speed, causing the disk platter to spin at a constant speed.
Arm Assembly: It holds the read/write heads. The arm assembly is moved in or out to
position a head on a desired track.
Master Boot Record (MBR): The MBR contains a small program to load and start the
active (or bootable) partition from the hard disk drive. The MBR contains information about
all four primary partitions on the hard disk drive such as the starting sector, ending sector,
size of the partition, etc.
DOS Boot Record (DBR): The DBR is the first sector of a partition and contains the boot
loader code that is executed when the partition is booted.
File Allocation Table (FAT): The FAT is a table that keeps track of which clusters are free
and which are in use. It is used by the file system to locate files on the disk.
Root Directory: The root directory is the top-level directory on a disk. It contains all the
files and directories that are stored directly on the disk.
Clusters: A cluster is a group of sectors that are allocated to a file. Clusters are used by the
file system to read and write data.
Disk Attachment
The disk attachment is a special feature that allows you to attach a disk (such as a USB
flash drive) to your computer’s hard drive. Users can use this feature to store files in the
cloud or transfer files between computers on their network. This can be done in number of
ways like:
1) Host-Attached Storage
Host-attached storage (HAS) is a form of internal computer storage that can be
attached to a host computer, such as a PC or server. Host-attached devices are often
used for backup purposes and can include tape drives, optical drives, hard disk drives
(HDDs), solid state drives (SSDs), USB flash drives, and other similar media.
Disk Scheduling
Disk scheduling is done by operating systems to schedule I/O requests arriving for the disk.
Disk scheduling is also known as I/O Scheduling.
Programs in C language to implement Disk Scheduling algorithms (FCFS (First Come First
Serve), SSTF (Shortest Seek Time First), SCAN (Elevator Algorithm) and C-SCAN
(Circular SCAN)).
Program Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_REQUESTS 100
// Sort requests
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
// Move head to the end of the queue in the direction it was moving
if (direction == 1)
total_seek_time += max_cylinder - head;
else
total_seek_time += head;
// Reverse direction
direction = -direction;
int total_seek_time = 0;
int direction = 1; // 1: Moving towards higher cylinders, -1: Moving towards lower cylinders
// Sort requests
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
// Move head to the end of the queue in the direction it was moving
if (direction == 1)
total_seek_time += max_cylinder - head;
else
total_seek_time += head;
int main() {
int head, n, max_cylinder;
return 0;
}
Output :
Disk Management
The operating system is responsible for various operations of disk management. Modern
operating systems are constantly growing their range of services and add-ons, and all
operating systems implement four essential operating system administration functions.
These functions are as follows:
1) Process Management
2) Memory Management
3) File and Disk Management
4) I/O System Management