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

Os Final Lab Meet

The document outlines practical exercises for a B-Tech CSE course focused on Linux commands and shell scripting. It includes commands for navigating directories, file management, and process handling, as well as examples of shell scripts for tasks like validating dates and checking for palindromes. Additionally, it covers C programming for process creation and scheduling algorithms like FCFS and Round Robin.

Uploaded by

meet
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 views23 pages

Os Final Lab Meet

The document outlines practical exercises for a B-Tech CSE course focused on Linux commands and shell scripting. It includes commands for navigating directories, file management, and process handling, as well as examples of shell scripts for tasks like validating dates and checking for palindromes. Additionally, it covers C programming for process creation and scheduling algorithms like FCFS and Round Robin.

Uploaded by

meet
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/ 23

Faculty of Engineering and Technology

Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

PRACTICAL -1
Aim – Study the basics of Linux Command
What is Linux
?
Linux is an open-source, Unix-like operating system kernel that serves as the foundation for
many operating systems, known as Linux distributions or distros. It was created by Linus
Torvalds in 1991 and has since become one of the most widely used operating systems in the
world, especially in servers, embedded systems, and desktop environments.

1. Pwd : This command in Unix/Linux systems is used to display the full path of
the current working directory in the terminal.

2. Cd : This cd (change directory) command is used to navigate between directories


in the filesystem.

3. Ls -lt : This command is used to list the contents of a directory sorted by


modification time, in descending order, with detailed information about each file or

directory.

Name: Meet Madhani


Enrolment no: 2403031057044 1
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

4. Ls –ltr : It is used to list the contents of a directory in long format (-l), sorted by
modification time (-t), and in reverse order (-r). This means the oldest files are
displayed first.

5. Ls -lh: It is used to lists the contents of a directory in long format (-l)


with humanreadable file sizes (-h).

Name: Meet Madhani


Enrolment no: 2403031057044 2
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

6. Date : The date command displays the current date and time.
• +%H : To display the hour in 24-hour format (00-23).
• +%M : To display the minute of the current time (00-59).
• +%H:%M:%S : To displays the current time in the format
of hour:minute:second (24-hour format).
• +%A : To display the full name of the weekday (e.g., Sunday, Monday, etc.).
• +%D : To display the date in MM/DD/YY format (month/day/year).
• +%Y / +%G : Both displays the year in four-digit format.
• +%h : To display the abbreviated month name (e.g., Jan, Feb, Mar, etc.).
• +%m : To display the month as a two-digit number (01-12).
• +%d : To display the day of the month as a two-digit number (01-31). • +%y
/+%g : To display the last two digits of the year.

7. Vi : vi editor is a powerful text editor used in Unix/Linux systems.


8. Nano : It is often preferred for quick file edits because of its ease of use, as it does
not require the user to switch between different modes like vi.

9. Touch : This touch command is used to create empty files or update the timestamp
of existing files.

Name: Meet Madhani


Enrolment no: 2403031057044 3
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

10. Ps : It is used to display information about running processes.cat

11. Cat : It is used to display the contents of a file, combine multiple files, or create a
new file.
12. Clear : It is used to clear the terminal screen.
13. Echo : It is used to display a line of text or output the value of a variable to
the terminal.

Name: Meet Madhani


Enrolment no: 2403031057044 4
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

PRACTICAL – 2
Aim – Study the basics of Shell
What is a shell ? Programming

An Operating is made of many components, but its two prime components are

 Kernel
 Shell
A Kernel is at the nucleus of a computer. It makes the communication between the hardware
and software possible. While the Kernel is the innermost part of an operating system, a shell is
the outermost one.
A shell in a Linux operating system takes input from you in the form of commands, processes
it, and then gives an output. It is the interface through which a user works on the programs,
commands, and scripts. A shell is accessed by a terminal which runs it.
When you run the terminal, the Shell issues a command prompt (usually $), where you can
type your input, which is then executed when you hit the Enter key. The output or the results
is the thereafter displayed on the terminal.
The shell wraps around the delicate interior of an operating system protecting it from
accidental damage. Hence the name Shell.

Types of Shell:

1. Bottom shell

2. C Shell

Let us understand the steps in creating a Shell Script:

1. Create a file using a vi editor (or any other editor). Name script file with extension .sh 2.

Start the script with #!/bin/sh

3. Write some code.

4. Save the script file as filename.sh

5. For executing the script type bash filename.sh

“ #! ” is an operator called shebang which directs the script to the interpreter location. So, if
we use “#!/bin/sh” the script gets directed to the bourne-shell.

Name: Meet Madhani


Enrolment no: 2403031057044 5
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

• Echo function is use for output the statement.


• Read function use for input the data.
There is 3 types to create file.

1. Touch command
2. Cat command
3. Text editor

Name: Meet Madhani


Enrolment no: 2403031057044 6
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

PRACTICAL -3
Aim – Write a Shell script to Print given numbers Sum of all Digits.

Program Code:

echo "Enter a number:"


read number
sum=0

while [ $number -gt 0 ]; do


mod=$(expr $number % 10)
sum=$(expr $sum + $mod)
number=$(expr $number / 10)
done

echo "Sum of all digits is: $sum"

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 7
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical No. 4
Aim: Write a shell script to validate the entered date.
(eg. date format is: dd-mm-yyyy).
Program Code:

#!/bin/bash

echo "Enter date"


read d
echo "Enter month"
read m
echo "Enter year"
read y

echo "$d/$m/$y"

if [ $m -eq 4 ] || [ $m -eq 6 ] || [ $m -eq 9 ] || [ $m -eq 11 ]; then


if [ $d -gt 0 ] && [ $d -lt 31 ]; then
echo "valid"
else
echo "not valid"
fi
elif [ $m -eq 2 ]; then
if [ $d -gt 0 ] && [ $d -le 29 ]; then
echo "valid"
else
echo "not valid"
fi
elif [ $m -ge 1 ] && [ $m -le 12 ]; then
if [ $d -gt 0 ] && [ $d -le 31 ]; then
echo "valid"
else
echo "not valid"
fi
else
echo "not valid"
fi
Output:

Name: Meet Madhani


Enrolment no: 2403031057044 8
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 5
Aim: Write a shell script to print whether the number is palindrome or not

Program Code:

echo "Enter the number:"


read a
c=$a
sum=0

while [ $a -ne 0 ]; do
b=$((a % 10))
sum=$((sum * 10 + b))
a=$((a / 10))
done

echo "$sum"

if [ $c -eq $sum ]; then


echo "It is a palindrome: $sum"
else
echo "It is not a palindrome."
fi

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 9
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 6
Aim: Write a shell script to say Good morning/ Good afternoon/ good evening
as you log in to system.

Program Code:
current_time=$(date +%H)

if [ "$current_time" -lt 12 ]; then

echo "Good morning!"

elif [ "$current_time" -lt 17 ]; then

echo "Good Afternoon!"

else

echo "Good Evening!"

fi

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 10
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 7

Aim: Write a C program to create a child process.

Program Code:
#include <stdio.h>
#include <windows.h>

int main() {
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

if (!CreateProcess(
NULL,
"cmd /c echo Child Process Running",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi)
){
printf("CreateProcess failed (%d).\n", GetLastError());
return 1;
}

printf("Parent Process: PID = %lu, Child Process ID = %lu\n", GetCurrentProcessId(),


pi.dwProcessId);

WaitForSingleObject(pi.hProcess, INFINITE);

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

return 0;
}

Name: Meet Madhani


Enrolment no: 2403031057044 11
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 12
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 8

Aim: Finding the biggest number from given three numbers supplied as command
line arguments.
Program Code:
echo "Enter the first number "
read a
echo "Enter the second number"
read b
echo "Enter the third number"
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "First number is greater "
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo "Second number is greater "
else
echo "Third number is greater "
fi

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 13
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 9

Aim: Print pattern using for loop.


Program Code:

#!/bin/bash

rows=5

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


do
for ((j=1; j<=i; j++))
do
echo -n "*"
done
echo ""
done

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 14
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 10

Aim: Shell script to detrermine whether given file exist or not.

Program Code:

#!/bin/bash

echo "Enter the file name:"


read filename

if [ -e "$filename" ]; then
echo "File '$filename' exists."
else
echo "File '$filename' does not exist."
fi

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 15
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 11

Aim: Write a program for process creation using C. (Use of gcc compiler)
Program Code:

#include <stdio.h>
#include <windows.h>

int main() {
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

if (!CreateProcess(
NULL,
"cmd /c echo Child Process Running",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi)
){
printf("CreateProcess failed (%d).\n", GetLastError());
return 1;
}

printf("Parent Process: PID = %lu, Child Process ID = %lu\n", GetCurrentProcessId(),


pi.dwProcessId);

WaitForSingleObject(pi.hProcess, INFINITE);

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

return 0;
}

Name: Meet Madhani


Enrolment no: 2403031057044 16
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 17
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 12

Aim: Implementation of FCFS & Round Robin Algorithm.

Program Code:
#include <stdio.h>

void findWaitingTimeFCFS(int processes[], int n, int bt[], int wt[]) {


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

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}

void findAvgTimeFCFS(int processes[], int n, int bt[]) {


int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTimeFCFS(processes, n, bt, wt);
findTurnAroundTime(processes, n, bt, wt, tat);

printf("Processes Burst time Waiting time Turnaround time\n");


for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf("%d\t\t%d\t \t%d\t \t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
printf("Average waiting time = %.2f\n", (float)total_wt / n);
printf("Average turnaround time = %.2f\n", (float)total_tat / n);
}

void roundRobin(int processes[], int n, int bt[], int quantum) {


int rem_bt[n], wt[n], tat[n], total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];

int t = 0;
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = 0;
if (rem_bt[i] > quantum) {
t += quantum;
rem_bt[i] -= quantum;
} else {
Name: Meet Madhani
Enrolment no: 2403031057044 18
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

t += rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == 1)
break;
}

findTurnAroundTime(processes, n, bt, wt, tat);


printf("Processes Burst time Waiting time Turnaround time\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf("%d\t\t%d\t \t%d\t \t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
printf("Average waiting time = %.2f\n", (float)total_wt / n);
printf("Average turnaround time = %.2f\n", (float)total_tat / n);
}

int main() {
int processes[] = {1, 2, 3};
int n = sizeof(processes) / sizeof(processes[0]);
int burst_time[] = {10, 5, 8};
int quantum = 2;

printf("FCFS Scheduling:\n");
findAvgTimeFCFS(processes, n, burst_time);

printf("\nRound Robin Scheduling (Quantum = %d):\n", quantum);


roundRobin(processes, n, burst_time, quantum);

return 0;
}

Name: Meet Madhani


Enrolment no: 2403031057044 19
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 20
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Practical no. 13

Aim: Implementation of Banker's Algorithm


Program Code:

#include <stdio.h>
#include <stdbool.h>

void calculateNeed(int need[][10], int max[][10], int alloc[][10], int P, int R) {


for (int i = 0; i < P; i++)
for (int j = 0; j < R; j++)
need[i][j] = max[i][j] - alloc[i][j];
}

bool isSafe(int processes[], int avail[], int max[][10], int alloc[][10], int P, int R) {
int need[P][10];
calculateNeed(need, max, alloc, P, R);

bool finish[P];
int safeSeq[P], work[10];
for (int i = 0; i < P; i++) finish[i] = false;
for (int i = 0; i < R; i++) work[i] = avail[i];

int count = 0;
while (count < P) {
bool found = false;
for (int p = 0; p < P; p++) {
if (!finish[p]) {
int j;
for (j = 0; j < R; j++)
if (need[p][j] > work[j])
break;

if (j == R) {
for (int k = 0; k < R; k++)
work[k] += alloc[p][k];
safeSeq[count++] = processes[p];
finish[p] = true;
found = true;
}
}
}
if (!found) {
printf("\nUnsafe State\n");
return false;
}
}
Name: Meet Madhani
Enrolment no: 2403031057044 21
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

printf("\nSafe State. Safe Sequence: ");


for (int i = 0; i < P; i++)
printf("%d ", safeSeq[i]);
printf("\n");
return true;
}

int main() {
int P, R;
printf("Enter number of processes: ");
scanf("%d", &P);
printf("Enter number of resources: ");
scanf("%d", &R);

int processes[P], avail[10], max[P][10], alloc[P][10];

printf("\nEnter Allocation Matrix:\n");


for (int i = 0; i < P; i++) {
printf("Process %d: ", i + 1);
for (int j = 0; j < R; j++)
scanf("%d", &alloc[i][j]);
}

printf("\nEnter Maximum Matrix:\n");


for (int i = 0; i < P; i++) {
printf("Process %d: ", i + 1);
for (int j = 0; j < R; j++)
scanf("%d", &max[i][j]);
}

printf("\nEnter Available Vector:\n");


for (int i = 0; i < R; i++)
scanf("%d", &avail[i]);

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


processes[i] = i;

isSafe(processes, avail, max, alloc, P, R);

return 0;
}

Name: Meet Madhani


Enrolment no: 2403031057044 22
Faculty of Engineering and Technology
Operating System(303105252)
B-Tech CSE 4th sem and 2nd Year

Output:

Name: Meet Madhani


Enrolment no: 2403031057044 23

You might also like