Os File 2310991964

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Practical File

Of
Operating System
22CS005
Submitted

in partial fulfillment for the award of the degree

of

BACHELEOR OF ENGINEERING
in

COMPUTER SCIENCE & ENGINEERING

CHITKARA UNIVERSITY

CHANDIGARH-PATIALA NATIONAL HIGHWAY


RAJPURA (PATIALA) PUNJAB-140401 (INDIA)

June, 2024

Submitted To: Submitted By:

Faculty name: Mrs. Meenakshi Malhotra Name: Jai Gupta


Designation: Assistant Professor Roll No.:2310991937
Chitkara University, Punjab Sem: 2, Group: 22
INDEX
Sr. Practical Name Teacher Sign
No.
1 a) Installation: Configuration & Customizations of Linux
b) Introduction to GCC compiler: Basics of GCC, Compilation of
program, Execution of program.

2 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
3 Implement the commands that is used for Creating and Manipulating files:
cat, cp, mv, rm, ls and its options, touch and their options, which is, where
is, what is
4 Implement Directory oriented commands: cd, pwd, mkdir, rmdir, Comparing
Files using diff, cmp, comm.
5 Write a program to create and execute process using fork() and exec() system
calls.
6

10

11

12
EXPERIMENT-1

Linux:

It is an open-source Unix-like operating system kernel initially developed by Linus Torvalds in 1991. It
forms the core of Linux distributions, providing stability, security, and flexibility. Supported by a global
community, Linux powers servers, desktops, smartphones, and embedded systems, offering a wide range
of customization options. Over many years, it has become more user-friendly and supports a lot of
features such as:

1. Reliable when used with servers


2. No need of antivirus
3. A Linux server can run nonstop with the boot for many years.
It has many distributions such as Ubuntu, Fedora, Redhat, Debian but all run on top of Linux server
itself. Installation of every distribution is similar; thus, installation of Ubuntu is explained here.

Virtual Box:

VirtualBox is a free and open-source virtualization software that allows users to run multiple operating
systems simultaneously on a single physical machine. It supports various guest operating systems,
provides features like snapshotting and shared folders, and is widely used for development, testing, and
running legacy applications.

Installation of Virtual Box:

To install VirtualBox, first, go to the download page on the VirtualBox


website(https://www.virtualbox.org/wiki/Downloads).
Select and download the correct package for your primary OS (the host OS, in virtualization
terminology).
Once the package is installed, install VirtualBox as you would typically install a program. Feel free to
use the default installation options.

Virtual Machine:

In VirtualBox, a virtual machine (VM) is a software emulation of a physical computer that operates
within a host operating system. It allows users to run multiple independent instances of operating systems
simultaneously, enabling testing, development, and isolation of environments on a single physical
machine.

Creating VM:

Once VirtualBox is installed on your machine, start the program and you should see the
VirtualBox Manager window open.
In this screen you will see options to create and add VMs to VirtualBox, as well as any VMs you have
previously created.

Select the “New” button to create a new VM. The prompt will ask for a name for the VM, a file
path for where to save the VM files. There are options for the “type” of OS you would like to install
on your VM, such as Windows, macOS, or Linux. It will also ask for the “version” of the OS you
will install, such as Ubuntu or Debian.

The following screen will ask to select the memory size to allocate for your machine. This will set
aside a certain amount of RAM on your host machine to be used for your VM when it is running.
Now select the Disk Size for virtual hard disk
Ubuntu:

Download Ubuntu:
In order to install an OS to a VM you will need to install the OS just like you would on a regular
computer. You’ll need to download a copy of the installation.

Now you will be asked What apps would you like to install to start with Linux? The two options are
‘Normal installation’ and ‘Minimal installation’.
Derive Selection:

Select the drive for installation of OS to be completed. Select “Erase Disk and install
Ubuntu” in case you want to replace the existing OS otherwise select “Something else”
option and click INSTALL NOW.

A small panel will ask for confirmation. Click Continue in case you don’t want to change any
information provided. Select your location on the map and install Linux.

Provide the login details.

Click on create option and then click on the START button to start the virtual box and browse to the
location of the .iso file of the OS.
Now Linux OS will start, Click on install option.

You are almost done. It should take 10-15 minutes to complete the installation. Once the installation
finishes, restart the system.
EXPERIMENT-2

Theory: The term compiler refers to a piece of software that converts our source code from a high-level
programming language to a low-level programming language (machine-level code) to build an
executable program file and in Linux Operating Systems and compile C program in Linux, we'll need to
install the GCC Compiler. In Ubuntu repositories, GCC Compiler is a part of the build-essential package,
and this package is exactly what we will be installing in our Linux Operating System. If you're interested
in learning more about the build-essential meta-package, You can refer here.

GCC Compiler (GNU Compiler Collection) is a collection of compilers and libraries for the programs
written in C, C++, Ada, GO, D, Fortran, and Objective-C programming languages and is distributed
under the GNU General Public License.

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 it is not installed, follow the
steps below to install it:
Step1: Update the package list.
To update the package list, execute the following command: 1. sudo apt update It will ask for the system
administrative password, enter the password. It will start updating the system package. Consider the
below snap of

Step2: Install the build-essential package.


It contains various packages such as gcc, g++, and make utility. Execute the below command to install
it: 1. sudo apt install build-essential
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 snap of
Step3: Verify the installation.
To verify the installation, execute the gcc -version command as follows:
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

Here, we have successfully installed the GCC utility. Let's understand how to use it. We will create and
execute some c programs using GCC.

Run the first C program by gcc Create a basic c program "Hello world!".

Create a file "First.c" and put the code below in it:


1. #include<stdio.h>
2. int main () {
3. printf("Hello, world! \n");
4. return 0;
5. }
EXPERIMENT-2

STATEMENT:
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

INTRODUCTION:
Linux is a powerful and flexible family of operating systems that are free to use and share. It was created
by a person named Linus Torvalds in 1991. What’s cool is that anyone can see how the system works
because its source code is open for everyone to explore and modify. Linux is based on a kernel, which
is the core program that manages how the computer interacts with its hardware and resources. The kernel
is combined with a collection of software packages and utilities, which are together called Linux
distributions.

Su command:
The su command (switch user) is used to run a command as a different user in Linux. To write the su
command in a Linux terminal

Sudo command:
The sudo command in Linux is used to run a command as a different user, usually the superuser or root.
It allows users to perform administrative tasks without logging in as root.
Man command:
The man command in Linux is used to access the reference manual of a utility or a command run in the
terminal. It provides detailed information about the command’s usage, options
Help command:
The help command in Linux is used to display information about shell built-in commands. It provides a
short description, usage, options, and arguments for each command.

History command:
The history command in Linux is used to display, search, and manipulate the list of commands that you
have previously entered in the terminal.
Who command:
The who command is a simple and effective way to display information about currently logged-in users.
By typing who in the terminal, you will receive a list of usernames, terminal IDs, login times, and
originating IP addresses if applicable.

Whoami command:
The whoami command in Linux is a simple utility that displays the username of the current user. It is
useful for verifying your identity or checking the privileges of your account.

ID command:
The id command in Linux is used to display information about the user and group IDs of the current user
or any other user on the system.

Uname command:
The uname command in Linux is used to display information about the system, such as the kernel name,
version, release, machine hardware name, and operating system.
Uptime command:
The uptime command in Linux is used to display information about how long the system has been
running, how many users are logged in, and what the system load average is.
EXPERIMENT-3

STATEMENT:
Implement the commands that is used for Creating and Manipulating files: cat, cp, mv, rm, ls and its
options, touch and their options, which
Introduction
Linux is famous for its powerful commands. To use Linux effectively, all users should know how to
use terminal commands. Although the OS has a GUI, many functionalities work faster when run as
commands through the terminal. This guide showcases basic Linux commands all users should
know
Prerequisites
• A system running Linux.
• Access to the command line/terminal.
Basic Linux Commands
All Linux commands fall into one of the following four categories:
• Shell builtins - Commands built directly into the shell with the fastest execution.
• Shell functions - Shell scripts (grouped commands).
• Aliases - Custom command shortcuts.
• Executable programs - Compiled and installed programs or scripts
1. pwd command
The pwd command (print working directory) is a shell builtin command that prints the current location.
The output shows an absolute directory path, starting with the root directory.

2. ls command
The ls command (list) prints a list of the current directory's contents. Run the following
Additional options provide flexibility with the display output. Typical usage includes combining
* ls-l: show as list
* ls-la: show as list and include hidden files
* ls-lah: show sizes in human readable form

3.cd command
The cd command (change directory) is a shell builtin command for changing the current working
directory:
For example, to move to the Document directory, run

The working directory changes in the terminal interface. In a non-default interface, use the pwd
command to check the current directory.

The working directory changes in the terminal interface. In a non-default interface, use the pwd
command to check the current directory.
Use cd without any parameters to return to home directory

4. cat command
The cat command (concatenate) displays the contents of a file in the terminal (standard output or
stdout).
To use the command, provide a file name from the current directory:
5. touch command The touch command's primary purpose is to modify an existing file's timestamp. To
use the

The command creates an empty file if it does not exist. Due to this effect, touch is also a
quick way to make a new file

6. cp command
The main way to copy files and directories in Linux is through the cp (copy) command.
Try the command

The source and target files must have different names since the command copies in the same directory.
Provide a path before the file name to copy to another location

7.mv command
Use the mv (move) command to move files or directories from one location to another. For example, to
move a file from the current directory to ~/Documents, run:
EXPERIMENT-4

STATMENT:
Implement Directory oriented commands: cd, pwd, mkdir, rmdir, Comparing Files using diff, cmp,
comm.
Managing Directories:
mkdir, rmdir, ls, cd, and pwd You can create and remove your own directories, as well as change your
working directory, with the mkdir, rmdir, and cd commands. Each of these commands can take as its
argument the pathname for a directory. The pwd command displays the absolute pathname of your
working directory. In addition to these commands, the special characters represented by a single dot, a
double dot, and a tilde can be used to reference the working directory, the parent of the working directory,
and the home directory, respectively. Taken together, these commands enable you to manage your
directories. You can create nested directories, move from one directory to another, and use pathnames
to reference any of your directories. Those commands commonly used to manage directories
mkdir command
The mkdir (make directory) command creates a new directory in the provided location. Use the command
in the following form

rmdir command
Use the rmdir (remove directory) command to delete an empty directory. For example:

Pwd command:
Displays the pathname of the working directory
EXPERIMENT-5
STATEMENT:
Write a program to create and execute process using fork () and exec () system calls.
THEORY:
Fork system call is used for creating a new process, which is called child process, which
runs concurrently with the process that makes the fork() call (parent process). After a new
child process is created, both processes will execute the next instruction following the
fork() system call. A child process uses the same pc(program counter), same CPU
registers, same open files which use in the parent process. It takes no parameters and
returns an integer value. Below are different values returned by fork(). Negative Value:
creation of a child process was unsuccessful. Zero: Returned to the newly created child
process. Positive value: Returned to parent or caller. The value contains process ID of
newly created child process.
EXPERIMENT- 1(c)
STATEMENT:
Timestamping in Linux.
THEORY:
What are timestamps in linux?
Timestamps are records for the times in which actions are performed on files. A
timestamp is useful because it keeps records of when a file was accessed, modified, or
added.

Linux’s files have 3 timestamps recorded by the computer:

1. atime: Access timestamp (atime) refers to the last time a file was read by a user.
That is, a user displayed the contents of a file using any suitable program, but did
not necessarily modify anything. To view an access timestamp using ls command,
we use -lu option followed by the file name.
Syntax: ls -lu [filename]

2. mtime: Modified timestamp (mtime) indicates the last time the contents of a file
were modified. For example, if new contents were added, deleted, or replaced in a
file, the modified timestamp is changed. To view the modified timestamp, we can
simple use the ls command with -l option.
Syntax: ls -l [filename

3. ctime: Unlike mtime, which is only related to the contents inside a file, changed
timestamp indicates the last time some metadata of a file was changed. ctime refers
to the last time when a file’s metadata.
Syntax: ls -lc [filename]

stat command: stat command can be used to see all timestamps of a file simultaneously.
Syntax: stat [filename]
EXPERIMENT – 1(d)

STATEMENT:
Automating the execution using Make file.
THEORY:
What is Makefile?
A Makefile is a file used by the ‘make’ command on Unix-based systems to specify
dependencies and build instructions for a project. It contains rules for building executable
programs, libraries, or other targets from source code files.

Syntax: target: dependencies


Commands

Explanation:
1. target: This is the name of the target being built, such as an executable file.
2. dependencies: These are the files or other targets that the target depends on. If any
of these dependencies are newer than the target, make will run the associated
commands to rebuild the target.
3. commands: These are the commands that make executes to build the target. They
are usually shell commands and are preceded by a tab character.

Firstly, we will create a simple Makefile for printing Hello World:


1. Create a directory named ‘practical’ using mkdir command and open your linux
terminal.

2. Now, using nano create a file named ‘Makefile’ and enter the below content.
3. Now, run the file by typing make and see the output.

If in the above case you only want to show Hello World use @echo Hello World.

Again run the file by using make and notice the change.

If we create many targets and run make only the first target will execute and to override
this issue we can use special phony targets:
1. .DEFAULT_GOAL: - This allows to override the sequence of execution.
Now again run this using make.
EXPERIMENT – 6

STATEMENT:
Write a program to implement process concepts using c language by printing process id.
THEORY:
In an operating system, every running program is represented by a process. These processes are
managed by the operating system's kernel, which assigns each process a unique identifier called a
Process ID (PID).

The PID is essential for the operating system to keep track of each process, manage their execution,
allocate system resources, and facilitate communication between processes.

When a new process is created, the operating system assigns it a PID, usually incrementing from the
previous PID. As processes terminate, their PIDs become available for reuse by new processes.

The getpid() used to get the process id of the current process. Every process has a parent process.
The process ID of the parent process can be found using getppid(). The getpid() and getppid() are
declared under <unistd.h>. If we don't include <unistd.h> then a warning occurs. To fix this
warning <unistd.h> must be included.

PROCEDURE:
EXPERIMENT – 7

STATEMENT:
Write C program to implement FCFS (First Come First Serve) and SJF (Shortest Job First)
scheduling algorithms.
THEORY:
First Come, First Served (FCFS) also known as First In, First Out (FIFO) is the CPU scheduling
algorithm in which the CPU is allocated to the processes in the order they are queued in the ready
queue. FCFS follows non-preemptive scheduling which means once the CPU is allocated to a
process it does not leave the CPU until the process will not get terminated or may get halted due to
some I/O interruption.

Shortest Job First (SJF) is an algorithm in which the process having the smallest execution time
is chosen for the next execution. This scheduling method can be preemptive or non-preemptive. It
significantly reduces the average waiting time for other processes awaiting execution. The full
form of SJF is Shortest Job First.

PROCEDURE:

FCFS(First Come First Serve):


SJF(Shortest Job First):
EXPERIMENT – 8

STATEMENT:
Write a C program to implement Priority Scheduling and RR(Round-Robin) scheduling
algorithms.
THEORY:
Priority Scheduling is a method of scheduling processes that is based on priority. In this algorithm,
the scheduler selects the tasks to work as per the priority. The processes with higher priority should
be carried out first, whereas jobs with equal priorities are carried out on a round-robin or FCFS basis.
Priority depends upon memory requirements, time requirements, etc.

Round Robin Scheduling comes from the round-robin principle, where each person gets an
equal share of something in turns. It is the oldest, simplest scheduling algorithm, which is mostly
used for multitasking. In Round-robin scheduling, each ready task runs turn by turn only in a
cyclic queue for a limited time slice. This algorithm also offers starvation free execution of
processes.
PROCEDURE:

1.Priority Scheduling
2. Round Robin:
EXPERIMENT – 10

STATEMENT:
Write program in c language to implement Disk Scheduling algorithms (FCFS (First
Come First Serve), SSTF (Shortest Seek Time First), Look and C-Look).
THEORY:
FCFS (First-Come, First-Served) disk scheduling is a simple algorithm used to
manage requests for accessing disk blocks. In FCFS, the requests are serviced in the
order they arrive. This means the request that comes first gets serviced first, regardless
of its location on the disk. However, FCFS can suffer from the "convoy effect," where
one long request can hold up all subsequent requests behind it, leading to inefficient
disk access times.

SSTF (Shortest Seek Time First) disk scheduling is an algorithm where the request with
the shortest seek time (the distance the disk arm needs to move to reach the requested
track) is serviced first. This approach aims to minimize the total seek time, potentially
reducing disk access latency and improving overall performance compared to FCFS.
However, SSTF may suffer from starvation, where requests located far from the disk arm's
current position may never get serviced if there are always closer requests arriving.
LOOK disk scheduling, like SCAN, moves the disk arm in one direction servicing
requests until there are no more requests in that direction. However, unlike SCAN, LOOK
doesn't necessarily go all the way to the end of the disk. Instead, it reverses direction when
there are no more requests in that direction, servicing requests as it moves back towards
the starting position. This helps to prevent unnecessary traversal of the entire disk,
reducing seek time and improving efficiency.

C-LOOK (Circular LOOK) is a disk scheduling algorithm that is similar to LOOK but
with a slight difference. Instead of reversing direction when reaching the end of the disk,
C-LOOK jumps back to the beginning of the disk and continues servicing requests in the
same direction. This creates a circular path, hence the name "C-LOOK." This approach
helps to reduce the average seek time by avoiding unnecessary traversal of the entire disk,
especially when there are no requests beyond the current position of the disk arm.

PROCEDURE:
1.FCFS
2.SSTF
3.LOOK
4.CLOOK
EXPERIMENT – 11

STATEMENT:
Write a program and execute the same to demonstrate how to use terminal commands in
c program (using system () function).
THEORY:
The system () function in C provides a convenient way to execute terminal commands
directly from a C program. It allows developers to integrate shell commands seamlessly
into their C programs, enabling them to automate tasks, interact with the operating system,
and run external programs or scripts.

Parameters:
- command: A pointer to a null-terminated string containing the command to be executed
by the system. This command can be any valid shell command, including built-in
commands, external programs, or scripts.

PROCEDURE:

You might also like