0% found this document useful (0 votes)
9 views18 pages

Operating System Lab

The document outlines various experiments related to Linux operating system commands and shell programming. It includes basic commands like ls, cd, and mkdir, along with shell scripts for checking even/odd numbers, swapping numbers, calculating factorials, generating Fibonacci series, and implementing CPU scheduling algorithms in C. Each experiment provides code snippets and expected outputs.

Uploaded by

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

Operating System Lab

The document outlines various experiments related to Linux operating system commands and shell programming. It includes basic commands like ls, cd, and mkdir, along with shell scripts for checking even/odd numbers, swapping numbers, calculating factorials, generating Fibonacci series, and implementing CPU scheduling algorithms in C. Each experiment provides code snippets and expected outputs.

Uploaded by

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

SCHOOL OF COMPUTER SCIENCE AND ENGINEERING

OPERATING SYSTEM
COURSE CODE : R1UC403B

NAME : Sakshi Yadav

ADMISSION NO : 23SCSE1010470

SEC : 46

SUBMITTED TO:
EXPERIMENT -1

AIM : Basic Commands in Linux Operating System

1.ls
Syntax:ls[options][directory]
Lists all files and directories within the specified directory. If no directory is given, it
shows the contents of the current working directory. Various options can be used to
display hidden files, detailed info, or sort the output.

2.cd
Syntax:cd[directory_path]
Changes the current directory to the specified path. It allows navigation through the file
system, such as moving into a folder, going up one level, or switching to the home
directory.

3.pwd
Syntax:pwd
Displays the full absolute path of your current working directory. It's useful for
understanding where you are in the directory structure, especially after moving around
with cd.

4.mkdir
Syntax:mkdir[directory_name]
Creates a new directory with the given name at the current location or specified path. It
helps in organizing files by creating folders for them.

5.rm
Syntax:rm[options][file_or_directory]
Deletes files or directories from the system. This command removes them permanently
and cannot be undone without backup. Use options like -r for recursive deletion and -f to
force deletion.

6.cp
Syntax:cp[options][source][destination]
Copies files or directories from one location to another. It preserves the original while
making a duplicate at the destination. Use the -r option to copy entire directories.
7.mv
Syntax:mv[source][destination]
Moves files or directories from one place to another. It is also used to rename files by
specifying a new name as the destination.

8.touch
Syntax:touch[file_name]
Creates a new, empty file with the given name. If the file already exists, it updates the
file’s last modified timestamp without changing the content.

9.cat
Syntax:cat[file_name]
Displays the content of a text file directly in the terminal. It can also be used to
concatenate multiple files and show their combined content.

10.clear
Syntax:clear
Clears all previous output from the terminal window, giving you a clean screen to work
with. It does not affect running programs or files.

11.chmod
Syntax:chmod[options][permissions][file_or_directory]
Changes the permission settings of a file or directory. Permissions control who can read, write, or
execute a file. Permissions can be set numerically (e.g. chmod 755) or symbolically (e.g. chmod
u+x).
EXPERIMENT - 2

AIM - Write a Shell program to check the given number is even or


odd.
- What is bash ?

Bash stand for BOURNE AGAIN SHELL . It is a command line interpreter or shell
commonly used on Unix and Linux system . It allows user to interact with the operating
system by typing commands.

Code:
#!/bin/bash
# Read number from user
echo "Enter a number:"
read number
# Check if the number is even or odd using modulus
if [ $((number % 2)) -eq 0 ]
then
echo "$number is Even"
else
echo "$number is Odd"
fi
Output:
EXPERIMENT - 3

AIM : Write a Shell program to swap the two numbers


Code:

#!/bin/bash
echo "Enter first number:"
read a
echo "Enter second number:"
read b
# Swapping using a temp variable
temp=$a
a=$b
b=$temp
echo "After swapping:"
echo "First number: $a"
echo "Second number: $b"

Output:
EXPERIMENT – 4

AIM : Write a Shell program to find the factorial of a number


Code:

#!/bin/bash

echo "Enter a number:"


read n
fact=1

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


do
fact=$((fact * i))
done

echo "Factorial of $n is $fact"

Output:
EXPERIMENT – 5

AIM : Write a shell program to generate fibonacci series.


Code:

#!/bin/bash

echo "Enter the number of terms:"


read n

a=0
b=1

echo "Fibonacci Series:"


for (( i=0; i<n; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo

Output:
EXPERIMENT – 6

AIM : Write a C program using the following system calls (fork, exec).
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main() {
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("Child process: executing 'ls'\n");
execl("/bin/ls", "ls", "-l", NULL);
} else {
// Parent process
printf("Parent process: PID = %d\n", pid);
}
return 0;
}

Output:
EXPERIMENT – 7

AIM : Write a C program using the following system calls (get_pid, exit).

Code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main() {
printf("Process ID: %d\n", getpid());
printf("Exiting the program.\n");
exit(0);
}

Output:
EXPERIMENT – 8

AIM : Write a C program using the I/O system calls (open, read, write, etc)

Code
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
char buffer[100];

fd = open("input.txt", O_RDONLY);
if (fd < 0) {
perror("Error opening file");
return 1;
}
read(fd, buffer, sizeof(buffer));
write(1, buffer, sizeof(buffer)); // write to stdout

close(fd);
return 0;
}

Output:
EXPERIMENT – 9

Aim : Write a C program to simulate CPU scheduling algorithms:


FCFS, SJF, and Round Robin.

Code for FCFS:


#include <stdio.h>
struct Process {
int id;
int burst_time;
int waiting_time;
int turnaround_time;
};

void FCFS(struct Process proc[], int n) {


proc[0].waiting_time = 0;
proc[0].turnaround_time = proc[0].burst_time;

for (int i = 1; i < n; i++) {


proc[i].waiting_time = proc[i - 1].waiting_time + proc[i - 1].burst_time;
proc[i].turnaround_time = proc[i].waiting_time + proc[i].burst_time;
}
}
void printProcessInfo(struct Process proc[], int n) {
printf("ProcessID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", proc[i].id, proc[i].burst_time, proc[i].waiting_time,
proc[i].turnaround_time);
}
}

int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
struct Process proc[n];
for (int i = 0; i < n; i++) {
proc[i].id = i + 1;
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &proc[i].burst_time);
}
FCFS(proc, n);
printProcessInfo(proc, n);

return 0;
}

Output for FCFS:


Code for SIF:

#include <stdio.h>
struct Process {
int id;
int burst_time;
int waiting_time;
int turnaround_time;
};
void SJF(struct Process proc[], int n) {
struct Process temp;
// Sort processes by burst time
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (proc[i].burst_time > proc[j].burst_time) {
temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
proc[0].waiting_time = 0;
proc[0].turnaround_time = proc[0].burst_time;
for (int i = 1; i < n; i++) {
proc[i].waiting_time = proc[i - 1].waiting_time + proc[i - 1].burst_time;
proc[i].turnaround_time = proc[i].waiting_time + proc[i].burst_time;
}
}
void printProcessInfo(struct Process proc[], int n) {
printf("ProcessID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", proc[i].id, proc[i].burst_time, proc[i].waiting_time,
proc[i].turnaround_time);
}
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
struct Process proc[n];
for (int i = 0; i < n; i++) {
proc[i].id = i + 1;
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &proc[i].burst_time);
}
SJF(proc, n);
printProcessInfo(proc, n);
return 0;
}

Output for SJF:


Code for Round Robin:

#include <stdio.h>

struct Process {
int id;
int burst_time;
int remaining_time;
int waiting_time;
int turnaround_time;
};

void RoundRobin(struct Process proc[], int n, int quantum) {


int time = 0, done;
while (1) {
done = 1;
for (int i = 0; i < n; i++) {
if (proc[i].remaining_time > 0) {
done = 0;
if (proc[i].remaining_time > quantum) {
proc[i].remaining_time -= quantum;
time += quantum;
} else {
time += proc[i].remaining_time;
proc[i].turnaround_time = time;
proc[i].remaining_time = 0;
}
}
}
if (done) break;
}

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


proc[i].waiting_time = proc[i].turnaround_time - proc[i].burst_time;
}
}

void printProcessInfo(struct Process proc[], int n) {


printf("ProcessID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", proc[i].id, proc[i].burst_time, proc[i].waiting_time,
proc[i].turnaround_time);
}
}

int main() {
int n, quantum;

printf("Enter number of processes: ");


scanf("%d", &n);

struct Process proc[n];


for (int i = 0; i < n; i++) {
proc[i].id = i + 1;
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &proc[i].burst_time);
proc[i].remaining_time = proc[i].burst_time;
}

printf("Enter time quantum: ");


scanf("%d", &quantum);

RoundRobin(proc, n, quantum);
printProcessInfo(proc, n);

return 0;
}

Output for Round Robin :

You might also like