0% found this document useful (0 votes)
17 views32 pages

23BCP119 Os Lab File

The document outlines a series of experiments conducted in an Operating System Lab at Pandit Deendayal Energy University for the academic session 2024-25. It includes detailed instructions and code examples for various Linux commands, shell scripting, CPU scheduling algorithms, and page replacement algorithms implemented in C language. Each experiment has specific objectives and tasks aimed at enhancing practical knowledge of operating systems and shell scripting.

Uploaded by

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

23BCP119 Os Lab File

The document outlines a series of experiments conducted in an Operating System Lab at Pandit Deendayal Energy University for the academic session 2024-25. It includes detailed instructions and code examples for various Linux commands, shell scripting, CPU scheduling algorithms, and page replacement algorithms implemented in C language. Each experiment has specific objectives and tasks aimed at enhancing practical knowledge of operating systems and shell scripting.

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE &

ENGINEERING
SCHOOL OF TECHNOLOGY
PANDIT DEENDAYAL ENERGY UNIVERSITY
SESSION 2024-25

SUBMITTED BY

NAME : Nevil Ramesh Desai


ROLL NO. : 23BCP119
DIVISION : 2 (G4)
COURSE NAME : Operating System Lab
COURSE CODE : 20CP207P

SUBMITTED TO

Dr. Shilpa Pandey

Assistant Professor

Department of Computer Science and Engineering

Pandit Deendayal Energy University

****************
Experiment-1
1. Introduction to Linux Commands with all the options available for them like p,f, a,
t,r, S, R, i, g, h, d etc.

a) ls - List Directory Contents

• Displays the files and directories in the current directory.


• Options:
o -a: Show all files, including hidden files.
o -l: Display detailed information about each file.
o -t: Sort files by modification time.
o -r: Reverse the order of the output.
o -S: Sort by file size.
• Output:

b) cd - Change Directory

• Changes the current working directory.


• Options:
o cd ..: Move to the parent directory.
o cd ~: Move to the home directory.
• Output:

c) pwd - Print Working Directory

• Prints the absolute path of the current directory.


• Options: No major options, but useful with scripting.
• Output:
d) mkdir - Make Directory

• Creates a new directory.


• Options:
o -p: Create parent directories as needed.
o -v: Display a message for each created directory.
• Output:

e) rm - Remove Files or Directories

• Deletes files or directories.


• Options:
o -r: Recursively delete directories.
o -f: Force deletion without confirmation.
o -i: Interactive mode to confirm deletion.
• Output:

f) cp - Copy Files or Directories

• Copies files or directories from one location to another.


• Options:
o -r: Copy directories recursively.
o -p: Preserve file attributes.
o -i: Prompt before overwriting files.
• Output:
g) mv - Move or Rename Files

• Moves files or directories or renames them.


• Options:
o -i: Prompt before overwriting.
o -n: Do not overwrite existing files.
• Output:

h) touch - Create Empty File

• Creates an empty file or updates the timestamp of an existing file.


• Options:
o -a: Change access time only.
o -m: Change modification time only.
• Output:

i) cat - Concatenate and Display

• Displays the content of a file or combines multiple files.


• Options:
o -n: Number lines in the output.
o -b: Number non-blank lines.
o -E: Display $ at the end of each line.
• Output:
j) nano - Text Editor

• Opens a terminal-based text editor.


• Options:
o -B: Create a backup of the file.
o -C: Specify a directory for backup files.
• Output:

k) grep - Search Text Patterns

• Searches for patterns within a file or text.


• Options:
o -i: Ignore case.
o -r: Search recursively in directories.
o -v: Invert the match (exclude results).
o -c: Count matching lines.

• Output:
l) sudo - Superuser Do

• Runs commands with administrative privileges.


• Options:
o -v: Extend the sudo session.
o -l: List user permissions.
• Output:

m) apt-get - Package Manager

• Installs, updates, and removes packages in Debian-based systems.


• Options:
o install: Install a package.
o update: Update package lists.
o remove: Remove a package.

• Output:

n) dpkg - Package Manager

• Handles low-level package management in Debian-based systems.


• Options:
o -i: Install a package.
o -r: Remove a package.
o -l: List installed packages.

• Output:
o) chmod - Change File Permissions

• Modifies file permissions for users, groups, and others.


• Options:
o -R: Apply changes recursively.
o -v: Display details of changes.
• Output:

p) chown - Change File Owner

• Changes the owner of a file or directory.


• Options:
o -R: Change ownership recursively.
o --reference: Change ownership to match a reference file.
• Output:
q) tar - Tape Archive

• Archives files into a single file or extracts them.


• Options:
o -c: Create an archive.
o -x: Extract an archive.
o -z: Compress using gzip.
• Output:

r) uname - Print System Information

• Displays system and kernel details.


• Options:
o -a: Display all information.
o -r: Display the kernel version.
• Output:

s) date - Display Date and Time

• Displays or sets the system date and time.


• Options:
o +%Y: Display the year.
o +%H:%M:%S: Display time in HH:MM:SS format.
• Output:
t) shutdown - Shutdown or Restart System

• Powers off or restarts the system.


• Options:
o -r: Restart the system.
o -h: Halt the system.
• Output:

u) ps - Process Status

• Displays information about active processes.


• Options:
o -e: Display all processes.
o -f: Show full format listing.
• Output:

v) top - Display System Activity

• Shows real-time system resource usage.


• Options:
o -o: Sort by a specific field.
o -d: Delay between updates.
• Output:
w) du - Disk Usage

• Estimates file or directory size.


• Options:
o -h: Human-readable format.
o -s: Display total size.
• Output:

x) df - Disk Free

• Displays available disk space.


• Options:
o -h: Human-readable format.
o -T: Show file system type.
• Output:
y) history

• Shows a list of previously executed commands.


• Options:
o -c: Clear the history.
• Output:

z) clear

• Clears the terminal screen.


• No options available.
• Output:

aa) ipconfig

• Displays network interface configuration (on some systems, use ifconfig).


• Options:
o -a: Show all interfaces.
o -s: Show summary.
• Output:
bb) echo

• Outputs text to the terminal.


• Options:
o -n: Do not append a newline.
• Output:

cc) cal

• Displays a calendar.
• Options:
o -y: Display the calendar for the whole year.
• Output:

dd) passwd

• Changes the user’s password.


• Options:
o --status: Display account status.
• Output:
ee) free

• Displays memory usage.


• Options:
o -h: Human-readable format.
o -m: Show memory usage in MB.
• Output:

ff) comm

• Compares two sorted files line by line.


• Options:
o -1: Suppress lines unique to the first file.
o -2: Suppress lines unique to the second file.
• Output:

gg) group

• Displays groups the user belongs to.


Experiment-2
Objective: Introduction to shell scripting and its basics Different shell introduction,
variables, constants, strings, arrays, printing variables, strings and constants, shell file
creation and execution (touch, vi)

a. Write a shell script to print your name


Code and Output:

b. Write a shell script to find the simple interest


Code and Output:

c. Write a shell script that changes text to upper case


Code and Output:
Experiment-3
Objective: Introduction to shell scripting with control statements Conditional statements,
Loops, break, continue

a. Write a shell script to find whether a number is even or odd.


Code and Output:

b. Write a script to print a table of a given number.


Code and Output:
d. Write a shell script to check whether a given no. is prime or not.

Code and Output:

d. Write a shell script to find sum of n numbers.


Code and Output:
e. Write a shell script to find the largest number of three numbers.

Code and Output:

f. Write a menu driven shell script will point the following menu and execute the given
task.
▪ Display calendar of current month
▪ Display today’s date and time
▪ Display username those are currently logged in the system
▪ Display your name at given x,y position.
▪ Display your terminal number.
Code and Output:
Experiment-4

Objective: Introduction to shell scripting with functions and command line arguments

a. Write a shell script and create functions to find the largest of three numbers and also
find the total average.
Code and Output:
b. Write a shell script which print “invalid no. of arguments” if more than 5 command line
arguments otherwise print “valid no. of arguments”.

Code and Output:

c. Write a shell script and create functions to find the max. and min. number from the
given data set passed by command line argument.

Code and Output:


d. Write a shell script and create functions to find reverse of given number

Code and Output:

e. Write a shell script and create functions which will generate first n Fibonacci numbers
like :1,1,2,3,5,13….

Code and Output:


f. Write a shell script and create functions to find whether a given year is a leap year or
not

Code and Output:


Experiment-5

Objective: Implement CPU Scheduling algorithms in C Language:

i) FCFS

Explanation: First come – First served (FCFS), is the simplest scheduling algorithm.
FIFO simply queues processes according to the order they arrive in the ready queue. In this
algorithm, the process that comes first will be executed first and next process starts only
after the previous gets fully executed.

Code:
#include <stdio.h>

int main() {
int n, i;
printf("Enter number of processes: ");
scanf("%d", &n);

int bt[n], wt[n], tat[n];


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

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

for(i = 0; i < n; i++)


tat[i] = wt[i] + bt[i];

printf("\nProcess\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++)
printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]);

return 0;
}
Output:

ii) RR

Explanation: Round Robin Scheduling is a method used by operating systems to manage


the execution time of multiple processes that are competing for CPU attention. It is called
"round robin" because the system rotates through all the processes, allocating each of them
a fixed time slice or "quantum", regardless of their priority.
The primary goal of this scheduling method is to ensure that all processes are given an
equal opportunity to execute, promoting fairness among tasks.

Code:
#include <stdio.h>

int main() {
int i, j, n, time, remain, flag = 0, tq;
printf("Enter number of processes: ");
scanf("%d", &n);
remain = n;

int at[n], bt[n], rt[n];


for(i = 0; i < n; i++) {
printf("Enter Arrival and Burst time for P%d: ", i+1);
scanf("%d %d", &at[i], &bt[i]);
rt[i] = bt[i];
}

printf("Enter Time Quantum: ");


scanf("%d", &tq);

int wt = 0, tat = 0;
for(time = 0, i = 0; remain != 0;) {
if(rt[i] > 0 && at[i] <= time) {
if(rt[i] <= tq) {
time += rt[i];
rt[i] = 0;
flag = 1;
} else {
rt[i] -= tq;
time += tq;
}
if(flag) {
remain--;
wt += time - at[i] - bt[i];
tat += time - at[i];
flag = 0;
}
}
i = (i+1)%n;
}

printf("Average Waiting Time: %.2f\n", (float)wt/n);


printf("Average Turnaround Time: %.2f\n", (float)tat/n);
return 0;
}

Output:
Experiment-6

Objective: Implement Page replacement algorithms in C Language: FIFO, LRU

i) FIFO

Explanation: This is the simplest page replacement algorithm. In this algorithm, the
operating system keeps track of all pages in the memory in a queue, the oldest page is in
the front of the queue. When a page needs to be replaced page in the front of the queue is
selected for removal.

Code:
#include <stdio.h>

void fifo(int input[], int n, int capacity) {


int queue[capacity];
int fault = 0, index = 0;

for (int i = 0; i < capacity; i++)


queue[i] = -1;

printf("Page\tFrames\t\tPage Fault\n");
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < capacity; j++) {
if (input[i] == queue[j]) {
found = 1;
break;
}
}

if (!found) {
queue[index] = input[i];
index = (index + 1) % capacity;
fault++;
}

printf("%d\t", input[i]);
for (int k = 0; k < capacity; k++) {
if (queue[k] != -1)
printf("%d ", queue[k]);
else
printf("- ");
}
printf("\t\t%s\n", found ? "No" : "Yes");
}

printf("\nTotal Page Faults = %d\n", fault);


}

int main() {
int input[] = {1, 3, 0, 3, 5, 6};
int n = sizeof(input) / sizeof(input[0]);
int capacity = 3;

fifo(input, n, capacity);

return 0;
}

Output:

ii) LRU

Explanation: In this algorithm, page will be replaced which is least recently used.

Code:
#include <stdio.h>

int findLRU(int time[], int f) {


int min = time[0], pos = 0;
for (int i = 1; i < f; i++) {
if (time[i] < min) {
min = time[i];
pos = i;
}
}
return pos;
}
int main() {
int n, f, i, j, pos, faults = 0;

printf("Enter number of pages: ");


scanf("%d", &n);
int pages[n];
printf("Enter page reference string: \n");
for (i = 0; i < n; i++) scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);
int frames[f], time[f];
for (i = 0; i < f; i++) {
frames[i] = -1; // Initialize frames as empty
time[i] = -1; // Initialize time as -1 (invalid)
}

for (i = 0; i < n; i++) {


int found = 0;
for (j = 0; j < f; j++) {
if (frames[j] == pages[i]) {
found = 1;
time[j] = i; // Update last access time
break;
}
}

if (!found) {
// Find the LRU frame (smallest time)
pos = findLRU(time, f);
frames[pos] = pages[i];
time[pos] = i; // Set new access time
faults++;
}
}
printf("Total Page Faults = %d\n", faults);
return 0;
}
Output:
Experiment-7

Objective: Implement Disk Scheduling algorithms in C Language: FCFS, C-Scan

i) FCFS

Explanation: FCFS is the simplest disk scheduling algorithm. As the name suggests, this
algorithm entertains requests in the order they arrive in the disk queue. The algorithm
looks very fair and there is no starvation (all requests are serviced sequentially) but
generally, it does not provide the fastest service.

Code:
#include <stdio.h>
#include <stdlib.h>

int main() {
int n, head, i, total = 0;
printf("Enter number of disk requests: ");
scanf("%d", &n);
int req[n];
printf("Enter request sequence: \n");
for(i = 0; i < n; i++) scanf("%d", &req[i]);
printf("Enter initial head position: ");
scanf("%d", &head);

for(i = 0; i < n; i++) {


total += abs(req[i] - head);
head = req[i];
}

printf("Total head movement: %d\n", total);


return 0;
}
Output:

ii) C-Scan

Explanation: The Circular SCAN (C-SCAN) Scheduling Algorithm is a modified version


of the SCAN Disk Scheduling Algorithm that deals with the inefficiency of the SCAN
algorithm by servicing the requests more uniformly. Like SCAN (Elevator Algorithm), C-
SCAN moves the head from one end servicing all the requests to the other end. However,
as soon as the head reaches the other end, it immediately returns to the beginning of the
disk without servicing any requests on the return trip (see chart below) and starts servicing
again once reaches the beginning. This is also known as the “Circular Elevator Algorithm”
as it essentially treats the cylinders as a circular list that wraps around from the final
cylinder to the first one.

Code:
#include <stdio.h>
#include <stdlib.h>

void cscan(int request[], int n, int head, int disk_size) {


int i, j;
int seek_count = 0;
int d, current;

int seek_sequence[n + 2];


int index = 0;

for (i = 0; i < n - 1; i++) {


for (j = 0; j < n - i - 1; j++) {
if (request[j] > request[j + 1]) {
int temp = request[j];
request[j] = request[j + 1];
request[j + 1] = temp;
}
}
}

int pos;
for (i = 0; i < n; i++) {
if (head < request[i]) {
pos = i;
break;
}
}

for (i = pos; i < n; i++) {


current = request[i];
seek_sequence[index++] = current;
d = current - head;
seek_count += d;
head = current;
}

seek_count += (disk_size - 1) - head;


seek_sequence[index++]=disk_size-1;
seek_count += (disk_size - 1);
seek_sequence[index++]=0;
head = 0;

for (i = 0; i < pos; i++) {


current = request[i];
seek_sequence[index++] = current;
d = current - head;
seek_count += d;
head = current;
}

printf("Seek Sequence: ");


for (i = 0; i < index; i++) {
printf("%d ", seek_sequence[i]);
}

printf("\nTotal Seek Operations = %d\n", seek_count);


}

int main() {
int request[] = {82, 170, 43, 140, 24, 16, 190};
int n = sizeof(request) / sizeof(request[0]);
int head = 50;
int disk_size = 200;

cscan(request, n, head, disk_size);


return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy