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

Index

Serial
Main Content Date
Number

1. Use of Basic UNIX Shell Commands/Linux Commands.

2. Commands related to inode, I/O redirection and piping, process


control commands, mails
3. Shell Programming

Write a shell script that accepts a file name starting and ending line
4.
numbers as argumentsand display all the lines between given line
no.
5. Write a shell script that deletes all lines containing a specified word.

6. Write a shell script that displays a list of all the files in the current
directory
7. Simulation of Unix commands using C.

8. Implement the following CPU Scheduling Algorithms. (i) FCFS (ii)


Shortest Job First.

9. Implement the following CPU Scheduling Algorithms. (i) Round Robin


(ii) priority based
Use of Basic UNIX Commands

Command Description

ls List directory contents

cd Change directory

pwd Print working directory

mkdir Make directory

rmdir Remove directory

cp Copy file

mv Move file

rm Remove file

cat Concatenate files and print on the standard output

more Page through a text file

less Page through a text file

head Output the first part of files

tail Output the last part of files

wc Print newline, word, and byte counts for each file

sort Sort lines of text files

uniq Report or omit repeated lines

grep Print lines matching a pattern

find Search for files in a directory hierarchy

man An interface to the on-line reference manuals

info An interface to on-line information and documentation

whatis Display one-line manual page descriptions


Create a File
To create a file, use the touch command. For example, to create a file named file1.txt , type:

touch file1.txt

List Files
To list the files in the current directory, use the ls command. For example, to list the files in the current
directory, type:

ls

View Content of a File

To view the content of a file, use the cat command. For example, to view the content of a file named
file1.txt , type:

cat file1.txt

Copy a File
To copy a file, use the cp command. For example, to copy a file named file1.txt to a file named file2.txt
, type:

cp file1.txt file2.txt
Move a File
To move a file, use the mv command. For example, to move a file named file1.txt to a file named
file2.txt , type:

mv file1.txt file2.txt

Remove a File
To remove a file, use the rm command. For example, to remove a file named file1.txt , type:

rm file1.txt
Create a Directory
To create a directory, use the mkdir command. For example, to create a directory named dir1 , type:

mkdir dir1
List Directories
To list the directories in the current directory, use the ls command. For example, to list the directories in the
current directory, type:

ls -d */
Change Directory
To change the current directory, use the cd command. For example, to change the current directory to dir1 ,
type:

cd dir1
Remove a Directory
To remove a directory, use the rmdir command. For example, to remove a directory named dir1 , type:

rmdir dir1
Print Working Directory
To print the current directory, use the pwd command. For example, to print the current directory, type:
Search for Files
To search for files, use the find command. For example, to search for files named file1.txt in the current
directory, type:

find . -name "file1.txt"

Printing
echo "Hello World"

Displaying filename starting with single letter


echo?

Displaying filename starting with two letters


echo??
Displaying Machine Name
uname -n

Displaying Machine Type


uname -m

Displaying Kernel Version


uname -r

Displaying Kernel Release


uname -v
Vim Editor Commands
Command Description

i Insert mode

Esc Normal mode

:w Write (save) the file, but don't exit

:wq Write (save) and quit

:q! Quit and throw away changes

:x Write (save) and quit if file has been changed

:q Quit (fails if there are unsaved changes)

:wq! Write (save) and quit, even if file has been changed
Program 1:

Objective: Commands related to inode, I/O


redirection andpiping, process control commands,
mails

Commands related to inode:


ls -i - displays the inode number of files in the current directory. stat [filename] - displays the

detailed information about a file, including its inode number.

find . -inum [inode-number] - searches for a file with a specific inode number in the current
directory and its subdirectories.

df -i - displays information about inode usage and availability for file systems.

Commands related to I/O redirection and piping:


> - redirects standard output to a file, overwriting the file if it already exists.

>> - redirects standard output to a file, appending the output to the end of the file.

< - redirects standard input from a file.

| - pipes the output of one command as the input of another command.

Process control commands:


ps - displays information about the current processes running on the system. kill

[process-id] - sends a signal to terminate a specific process.

top - displays information about the processes running on the system in real-time.

nice [command] - runs a command with a specific priority level.

Commands related to Mails:


mail - opens the mail interface for the current user to read, compose and send emails. mailq -

displays the current email queue on the system. sendmail - sends email messages from the

command line. mutt - a text-based email client for sending and receiving emails from the

command line.
Shell Programming

Write a shell script that asks the user to enter a number and then calculates its square
using arithmetic.
#!/bin/bash
read -p "Enter a number: " num
result=$((num * num))
echo "The square of $num is $result"

Write a shell script that takes two numbers as command-line arguments and prints
the sum of the two numbers if they are both positive.
#!/bin/bash

if [ "$#" -ne 2 ]; then


echo "Usage: $0 num1 num2"
exit 1 fi

if [ "$1" -gt 0 ] && [ "$2" -gt 0 ];


then sum=$(( $1 + $2 )) echo "The
sum of $1 and $2 is $sum" else echo
"Both numbers must be positive" fi

Write a shell script that uses a while loop to read numbers from a file until the sum of
the numbers is greater than 100.
#!/bin/bash

sum=0

while read num; do sum=$(( $sum +


$num )) if [ $sum -gt 100 ]; then
break fi done < nums.txt

echo "The sum of the numbers is $sum"

Write a shell script that prompts the user to enter their age, and then uses an if-then-
else statement to check if they are old enough to vote.

#!/bin/bash

read -p "Enter your age: " age

if [ $age -ge 18 ]; then echo "You


are old enough to vote" else echo
"You are not old enough to vote" fi
Write a shell script that uses a case statement to prompt the user for their favorite
color and then prints a message based on their answer.
#!/bin/bash

read -p "What is your favorite color? " color

case $color in red) echo "Red


is a passionate color"
;; blue) echo "Blue is
a calming color"
;; green) echo "Green is
a natural color"
;; *) echo "I'm sorry, I don't
know that color"
;;
esac

Write a shell script that uses a for loop to print the numbers 1 to 10.
#!/bin/bash

echo "Counting from 1 to 10:" for i in {1..10}

do
echo $i
done
Program 2:
Objective: Write a shell script that accepts a file
name starting and ending line numbers as
arguments anddisplay all the lines between given
line no.

#!/bin/bash

# check if the correct number of arguments is


passed if [ "$#" -ne 3 ]; then echo "Usage: $0
file_name start_line end_line" exit 1 fi

# check if the file exists and is readable if [ ! -r "$1"


]; then echo "Error: file '$1' does not exist or is not
readable" exit 1 fi

# check if the start and end line numbers are valid if ! [[ "$2" =~ ^[0-
9]+$ ]] || ! [[ "$3" =~ ^[0-9]+$ ]] || [ "$2" -gt "$3"
]; then echo "Error: invalid start or end
line numbers" exit 1 fi

# print the lines between the given line numbers


sed -n "$2,$3p" "$1"

Program 3

Objective:Write a shell script that delete all


linescontaining aspecified word.
#!/bin/bash

# Check if the correct number of arguments is


passed if [ "$#" -ne 2 ]; then echo "Usage: $0
file_name word_to_delete" exit 1 fi

# Check if the file exists and is readable if [ ! -r "$1"


]; then echo "Error: file '$1' does not exist or is not
readable" exit 1 fi

# Delete all lines containing the specified word


sed -i "/$2/d" "$1"

echo "All lines containing '$2' have been deleted from '$1'"

Program 4:
Objective: Write a shell script that displays a list of all the
files in the current directory

#!/bin/bash

ls -al
Objective: Simulation of Unix commands using C

ls command
#include <stdio.h>
#include <dirent.h>

int main(int argc, char *argv[])


{ DIR *dir; struct dirent
*dp;

if (argc < 2) {
dir = opendir(".");
} else { dir =
opendir(argv[1]);
}

while ((dp = readdir(dir)) != NULL) {


printf("%s\n", dp->d_name);
}

closedir(dir);

return 0;
}
cat command
#include <stdio.h>

int main(int argc, char *argv[])


{ FILE *fp; char ch;

if (argc < 2) { printf("Usage:


%s file\n", argv[0]); return 1;
}

fp = fopen(argv[1], "r"); if (fp ==


NULL) { printf("Cannot open file %s\n",
argv[1]);

return 1; }
while ((ch = fgetc(fp)) != EOF)
{ putchar(ch); }

fclose(fp);

return 0;
}

rm command
#include <stdio.h>

int main(int argc, char *argv[]) { if


(argc < 2) { printf("Usage: %s
file\n", argv[0]); return 1;
}

if (remove(argv[1]) == 0) { printf("File %s
deleted successfully\n", argv[1]); return 0;
} else { printf("Cannot delete file %s\n",
argv[1]); return 1;
}
}

mkdir command
#include <stdio.h>
#include <sys/stat.h>

int main(int argc, char *argv[]) { if (argc


< 2) { printf("Usage: %s directory\n",
argv[0]); return 1;
}

if (mkdir(argv[1], 0777) == 0) {
printf("Directory %s created successfully\n", argv[1]);
return 0; } else { printf("Cannot
create directory %s\n", argv[1]); return 1;
}

}
Program 5:
Obejctive: Implement the following CPU
Scheduling Algorithms. (i) First Come First
Serve(FCFS), (ii)Shortest Job First(SJF)

(i) First Come First Serve(FCFS)


#include <stdio.h>

int main() { int num_processes, arrival_time[10], burst_time[10],


wait_time[10], turnaround_time[10], i, j;

float avg_wait_time = 0, avg_turnaround_time = 0;

printf("Enter the number of processes: ");


scanf("%d", &num_processes);

printf("Enter the arrival time and burst time for each process:\n");

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


printf("Process %d:\n", i + 1);

printf("Arrival Time: ");


scanf("%d", &arrival_time[i]);

printf("Burst Time: ");


scanf("%d", &burst_time[i]);
}

wait_time[0] = 0;
turnaround_time[0] = burst_time[0];

for (i = 1; i < num_processes; i++) { wait_time[i] =


wait_time[i - 1] + burst_time[i - 1] - arrival_time[i] +
arrival_time[i - 1]; turnaround_time[i] =
turnaround_time[i - 1] + burst_time[i];
}

printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround


Time\n");
for (i = 0; i < num_processes; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, arrival_time[i],
burst_time[i], wait_time[i], turnaround_time[i]);

avg_wait_time += wait_time[i];
avg_turnaround_time += turnaround_time[i];
}

avg_wait_time /= num_processes;
avg_turnaround_time /= num_processes;

printf("\nAverage Waiting Time: %.2f\n", avg_wait_time);


printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);

return 0;
}
(ii) Shortest Job First(SJF)
#include <stdio.h>

int main() { int num_processes, burst_time[10], wait_time[10],


turnaround_time[10], temp[10], i, j, smallest, time = 0;

float avg_wait_time = 0, avg_turnaround_time = 0;

printf("Enter the number of processes: ");


scanf("%d", &num_processes);

printf("Enter the burst time for each process:\n");

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


printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);

temp[i] = burst_time[i];
}

burst_time[9] = 9999;

for (time = 0; ; ) {
smallest = 9;
for (i = 0; i < num_processes; i++) { if
(burst_time[i] <= burst_time[smallest] && burst_time[i] > 0)
{ smallest =
i;
}
}

if (smallest == 9) {
break;
}

printf("Process %d is running\n", smallest + 1);

burst_time[smallest]--;

if (burst_time[smallest] == 0) {
turnaround_time[smallest] = time + 1;
wait_time[smallest] = turnaround_time[smallest] -
temp[smallest];

avg_wait_time += wait_time[smallest];
avg_turnaround_time += turnaround_time[smallest];
}

time++;
}

avg_wait_time /= num_processes;
avg_turnaround_time /= num_processes;

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

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


printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, temp[i], wait_time[i],
turnaround_time[i]);
}

printf("\nAverage Waiting Time: %.2f\n", avg_wait_time);


printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);

return 0;
}
Program 6
Objective: Implement the following CPU
Scheduling Algorithms. (i) Round Robin, (ii)
Priority Based

(i) Round Robin


#include<stdio.h>

#define MAX 100

typedef struct {
int pid; int
arrival_time; int
burst_time; int
remaining_time; int
completion_time;
int turnaround_time;
int waiting_time;
} Process;

int main() {
Process processes[MAX]; int n, quantum_time,
i, j, time = 0, completed = 0;

float total_turnaround_time = 0, total_waiting_time = 0;

printf("Enter the number of processes: ");


scanf("%d", &n);

printf("Enter the quantum time: ");


scanf("%d", &quantum_time);

printf("Enter the arrival time and burst time for each process:\n");

for (i = 0; i < n; i++) { printf("Process %d: ", i + 1);


scanf("%d%d", &processes[i].arrival_time, &processes[i].burst_time);

processes[i].remaining_time = processes[i].burst_time;
processes[i].completion_time = 0;
processes[i].turnaround_time = 0;
processes[i].waiting_time = 0;
}

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


if (processes[i].remaining_time > 0) { if
(processes[i].remaining_time <= quantum_time) {
time += processes[i].remaining_time;
processes[i].remaining_time = 0;
completed++;
} else { time +=
quantum_time; processes[i].remaining_time
-= quantum_time;
}

if (processes[i].remaining_time == 0) {
processes[i].completion_time = time;
processes[i].turnaround_time = processes[i].completion_time -
processes[i].arrival_time; processes[i].waiting_time =
processes[i].turnaround_time
- processes[i].burst_time; total_turnaround_time +=
processes[i].turnaround_time; total_waiting_time +=
processes[i].waiting_time;
}
}
}
}

float average_turnaround_time = total_turnaround_time / n;


float average_waiting_time = total_waiting_time / n;

printf("\nProcess\tArrival Time\tBurst Time\tCompletion


Time\tTurnaround Time\tWaiting Time\n");

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


printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n", i + 1,
processes[i].arrival_time, processes[i].burst_time,
processes[i].completion_time, processes[i].turnaround_time,
processes[i].waiting_time);
}

printf("\nAverage Turnaround Time: %.2f", average_turnaround_time);


printf("\nAverage Waiting Time: %.2f", average_waiting_time);
return 0;
}

(ii) Priority Based


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

#define MAX 100

typedef struct {
int pid; int
burst_time; int
priority; int
waiting_time; int
turnaround_time;
} Process;

void sort(Process p[], int n) { int i, j;


Process temp; for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) { if
(p[j].priority > p[j + 1].priority) {
temp = p[j]; p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}

int main() {
Process processes[MAX]; int n, i, j; float
total_waiting_time = 0, total_turnaround_time = 0;

printf("Enter the number of processes: ");


scanf("%d", &n);

printf("Enter the burst time and priority for each process:\n");

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


printf("Process %d: ", i + 1); scanf("%d%d",
&processes[i].burst_time, &processes[i].priority);
processes[i].pid = i + 1;
}

sort(processes, n);

processes[0].waiting_time = 0;

for (i = 1; i < n; i++) { processes[i].waiting_time


= 0; for (j = 0; j < i; j++) {
processes[i].waiting_time += processes[j].burst_time; }
total_waiting_time += processes[i].waiting_time;
}

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


processes[i].turnaround_time = processes[i].burst_time +
processes[i].waiting_time; total_turnaround_time +=
processes[i].turnaround_time;
}

printf("\nProcess\tBurst Time\tPriority\tWaiting Time\tTurnaround


Time\n");

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


printf("%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n", processes[i].pid,
processes[i].burst_time, processes[i].priority,
processes[i].waiting_time, processes[i].turnaround_time);
}

float average_waiting_time = total_waiting_time / n;


float average_turnaround_time = total_turnaround_time / n;

printf("\nAverage Waiting Time: %.2f", average_waiting_time);


printf("\nAverage Turnaround Time: %.2f", average_turnaround_time);

return 0;
}

You might also like