EXPERIMENT 1
Linux commands
1. Who- Displays who is on the system. The who utility displays information about
currently logged-in users. By default, this includes the login name, tty name, date, and
time of login and remote hostname if not local.
Syntax: who
Output:
a. -s: Shows the name, line and time fields only. This is the default.
Syntax: who -s
Output:
b. -q: “Quick mode”: Lists the names and number of logged-in users in columns. All
other command-line options are ignored.
Syntax: who -q
Output:
c. who am I: Shows information about the terminal attached to standard input only.
Syntax: who am I
Output:
2. Date- When invoked without arguments, the date utility displays the current date and
time. Otherwise, depending on the options specified, date will set the date and time or
print it in a user-defined way.
Syntax: date
Output:
a. DD-MM-YY: It displays the current system date in a custom format.
Syntax: date +"%d-%m-%y"
Output:
b. HH-MM-SS: Displays the current system time in hours–minutes–seconds format.
Syntax: date +"%H-%M-%S"
Output:
c. -R: Displays the current date and time in RFC 5322 format.
Syntax: date -R
Output:
3. cal- Displays the calendar of the current month.
Syntax: cal
Output:
a. -y: Displays the calendar of the entire current year.
Syntax: cal -y
Output:
b. -j: Displays the calendar with Julian day numbers (day of the year: 1–365/366).
Syntax: cal -j
Output:
c. -3: Displays three months at a time: previous month, current month, next month.
Syntax: cal -3
Output:
4. banner- Prints the given text in a large ASCII-art style on the terminal.
Syntax: banner [text]
Output:
a. -w: Prints the text in large ASCII letters and wraps it to the specified width.
Syntax: banner -w [width] [text]
Output:
5. clear- Clears the terminal screen, removing all previous commands and output.
Syntax: clear
Output:
6. echo- Displays text, variables, or command output on the terminal.
Syntax: echo [options] [text/string/variables]
Output:
a. -n: Prints text without a newline at the end.
Syntax: echo -n [text]
Output:
7. login- Allows a user to log in to a Linux system by starting a new login session.
Syntax: login
Output:
8. logout- Logs the user out of the current login session.
Syntax: logout
Output:
9. exit- Closes the current shell or terminal session.
Syntax: exit
Output:
10. ls: Lists files and directories in the current or specified directory.
Syntax: ls [options] [directory]
Output:
a. -l: Shows files in long listing format (permissions, owner, size, date).
Syntax: ls -1
Output:
b. -t: Lists files and directories sorted by time of last modification.
Syntax: ls -t
Output:
c. -r: Displays files and directories in reverse order.
Syntax: ls -r
Output:
11. mkdir: Creates a new directory (folder).
Syntax: mkdir [directory name]
Output:
a. -v: “verbose”- Shows a message for every action performed by the command.
Syntax: mkdir -v [directory name]
Output:
b. -p: Creates parent directories automatically if they do not exist.
Syntax: mkdir -p
Output:
c. -m: Sets permissions for a file or directory at the time of creation.
Syntax: mkdir -m
Output:
12. cd- Changes the current working directory.
Syntax: cd
Output:
a. [name of directory]: Moves into a specific directory.
Syntax: cd [directory]
Output:
b. [b. . . ]: Moves the user one level up to the parent directory.
Syntax: cd ..
Output:
c. [ / ]: Changes the current directory to the root directory of the Linux file system.
Syntax: cd /
Output:
13. rm- Deletes files and directories permanently.
Syntax: rm file_or_directory
Output:
a. -r: Deletes a directory and all its contents (files and subdirectories) recursively.
Syntax: rm -r file name
Output:
b. -i: Deletes a file only after asking for confirmation from the user.
Syntax: rm -i file name
Output:
c. -v: Deletes a file and displays a message for each file removed (verbose mode).
Syntax: rm -v file name
Output:
14. mv: Used to move files or directories from one place to another, or to rename them.
Syntax: mv source destination
a. to rename a file.
Syntax: mv old_filename new_filename
Output:
b. -i: Renames or moves a file after asking for confirmation before overwriting an
existing file.
Syntax: mv -i source destination
Output:
c. -n: Moves or renames a file without overwriting an existing file.
Syntax: mv -n source destination
Output:
15. cat: Used to display file contents, create files, or concatenate multiple files.
Syntax: cat file name
Output:
a. To display multiple files.
Syntax: cat file name1 file name2
Output:
b. To create new file.
Syntax: cat > file name
Output:
c. -n: Displays the contents of a file and numbers all lines, including blank lines.
Syntax: cat -n file name
Output:
EXPERIMENT 2
Linux Commands
CHMOD COMMAND
Theory of chmod
chmod: It performs modification of file or directory permissions in Linux.
Using the octal (numeric) method, permissions are assigned using numbers that represent
read, write and execute access.
Permissions are given to:
User (Owner)
Group
Others
Permission Values (Octal)
Read (r) = 4
Write (w) = 2
Execute (x) = 1
Permission Order:
User → Group → Others
Table: chmod Octal Number Commands
Octal User Group Others Meaning
Value Permission Permission Permission
777 rwx rwx rwx Full permission to all users
755 rwx r-x r-x Full permission to user, read
& execute to group and others
754 rwx r-x r-- Full permission to user, read
& execute to group, read to
others
700 rwx --- --- All permissions only to user
666 rw- rw- rw- Read and write permission to
all users
644 rw- r-- r-- Read and write to user, read-
only to group and others
640 rw- r-- --- Read and write to user, read to
group
600 rw- --- --- Read and write only to user
555 r-x r-x r-x Read and execute permission
to all users
444 r-- r-- r-- Read-only permission to all
users
000 --- --- --- No permission to anyone
CHMOD COMMANDS
Type 1: Full Permission to Everyone
chmod 777: It performs assigning read, write and execute permission to user, group and
others.
Syntax:
chmod 777 file_name OR chmod u+rwx,g+rwx,o+rwx file_name
Output:
Type 2: Read and Execute Permission for Everyone
chmod 555: It performs assigning read and execute permission to user, group and others.
Syntax:
chmod 555 file_name OR chmod u+rx,g+rx,o+rx file_name
Output :
Type 3: Read and Write Permission for Everyone
chmod 666: It performs assigning read and write permission to user, group and others.
Syntax:
chmod 666 file_name OR chmod u+rw,g+rw,o+rw file_name
Output :
Type 4: Read-Only Permission for Everyone
chmod 444: It performs assigning read-only permission to user, group and others.
Syntax:
chmod 444 file_name OR chmod u+r,g+r,o+r file_name
Output :
Type 5: Full Permission to User Only
chmod 700: It performs assigning read, write and execute permission only to the user.
Syntax:
chmod 700 file_name OR chmod u+rwx,go-rwx file_name
Output:
Type 6: Read & Write to User, Read to Group
chmod 640: It performs assigning read and write permission to user, read permission to
group and no permission to others.
Syntax:
chmod 640 file_name OR chmod u+rw,g+r,o-rwx file_name
Output:
Type 7: Full Permission to User, Read & Execute to Group and Others
chmod 755: It performs assigning full permission to user and read-execute permission to
group and others.
Syntax:
chmod 755 file_name OR chmod u+rwx,go+rx file_name
Output:
Type 8: Read & Write to User, Read-Only to Group and Others
chmod 644: It performs assigning read-write permission to user and read-only permission to
group and others.
Syntax:
chmod 644 file_name OR chmod u+rw,go+r file_name
Output:
Type 9: Execute-Only Permission for Everyone
chmod 111: It performs assigning execute-only permission to user, group and others.
Syntax:
chmod 111 file_name OR chmod a+x file_name
Output:
Type 10: Write-Only Permission for Everyone
chmod 222: It performs assigning write-only permission to user, group and others.
Syntax:
chmod 222 file_name OR chmod a+w file_name
Output:
Type 11: Read-Only Permission for User
chmod 400: It performs assigning read-only permission to user and no permission to group
and others.
Syntax:
chmod 400 file_name OR chmod u+r,go-rwx file_name
Output:
Type 12: Read & Execute Permission for User Only
chmod 500: It performs assigning read and execute permission to user and no permission to
group and others.
Syntax:
chmod 500 file_name OR chmod u+rx,go-rwx file_name
Output:
Linux Commands: grep and sort
Step 1: Create / Overwrite File Using cat >
cat >: It performs writing of text into a file from the terminal input.
Syntax:
cat > file_name
Output:
Step 2: Verify File Content Using cat
cat: It performs displaying of file contents.
Syntax:
cat file_name
Output:
GREP COMMAND
Theory of grep
grep: It performs searching of specific text or patterns from a file and displays only matching
lines.
Type 1: Search a Word in File
grep: It performs searching of a given word in a file.
Syntax:
grep pattern file_name
Output:
Type 2: Case-Insensitive Search
grep -i: It performs searching without considering uppercase or lowercase letters.
Syntax:
grep -i pattern file_name
Output:
Type 3: Count Matching Lines
grep -c: It performs counting of lines that match the given pattern.
Syntax:
grep -c pattern file_name
Output:
Type 4: Display Line Numbers with Match
grep -n: It performs displaying of line numbers along with matching lines.
Syntax:
grep -n pattern file_name
Output:
SORT COMMAND
Theory of sort
sort: It performs arranging of file contents in alphabetical order.
Type 1: Sort Alphabetically
sort: It performs sorting of file lines in alphabetical order.
Syntax:
sort file_name
Output:
Type 2: Sort in Reverse Order
sort -r: It performs sorting of file lines in reverse alphabetical order.
Syntax:
sort -r file_name
Output:
Type 3: Remove Duplicate Lines While Sorting
sort -u: It performs sorting and removes duplicate lines.
Syntax:
sort -u file_name
Output:
Type 4: Sort Output of grep Command
grep + sort: It performs searching first and then sorts the matching lines.
Syntax:
grep pattern file_name | sort
Output:
EXPERIMENT 3
Shell Programming
1. Write a script to swap two numbers.
Code:
echo "Enter first number:"
read a
echo "Enter second number:"
read b
temp=$a
a=$b
b=$temp
echo "After swapping:"
echo "a = $a"
echo "b = $b"
Output:
2. Write a script to find the greatest number among 3 numbers.
Code:
echo "Enter first number:"
read a
echo "Enter second number:"
read b
echo "Enter third number:"
read c
if [ $a -ge $b ] && [ $a -ge $c ]; then
echo "$a is the greatest number"
elif [ $b -ge $a ] && [ $b -ge $c ]; then
echo "$b is the greatest number"
else
echo "$c is the greatest number"
fi
Output:
3. Write a script by using condition statement and loop to create menu driven calculator
that performs operations like division.
Code:
while true
do
echo "----------------------"
echo " MENU DRIVEN CALCULATOR"
echo "----------------------"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Exit"
echo "Enter your choice:"
read choice
if [ "$choice" -eq 5 ]; then
echo "Exiting calculator..."
break
fi
echo "Enter first number:"
read a
echo "Enter second number:"
read b
if [ "$choice" -eq 1 ]; then
result=$((a + b))
echo "Result = $result"
elif [ "$choice" -eq 2 ]; then
result=$((a - b))
echo "Result = $result"
elif [ "$choice" -eq 3 ]; then
result=$((a * b))
echo "Result = $result"
elif [ "$choice" -eq 4 ]; then
if [ "$b" -eq 0 ]; then
echo "Division by zero is not allowed"
else
result=$((a / b))
echo "Result = $result"
fi
else
echo "Invalid choice"
fi
done
Output:
4. Write a script to find factorial of a number using loop statement .
Code:
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 4
Aim – To implement the following CPU Scheduling Algorithms:
1. FCFS
2. Shortest Job First
1. FCFS (First Come First Serve)
Language Used: C
Theory: First Come First Serve (FCFS) is an operating system scheduling algorithm that
automatically executes queued requests and processes in order of their arrival. It is the easiest
and simplest CPU scheduling algorithm. In this type of algorithm, processes which requests
the CPU first get the CPU allocation first.
Code:
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int pid[n], at[n], bt[n], ct[n], tat[n], wt[n];
float avg_tat = 0, avg_wt = 0;
for(int i = 0; i < n; i++) {
printf("\nEnter Arrival Time for P%d: ", i+1);
scanf("%d", &at[i]);
printf("Enter Burst Time for P%d: ", i+1);
scanf("%d", &bt[i]);
pid[i] = i + 1;
}
int current_time = 0;
for(int i = 0; i < n; i++) {
if(current_time < at[i])
current_time = at[i];
ct[i] = current_time + bt[i];
current_time = ct[i];
}
for(int i = 0; i < n; i++) {
tat[i] = ct[i] - at[i];
wt[i] = tat[i] - bt[i];
avg_tat += tat[i];
avg_wt += wt[i];
}
avg_tat /= n;
avg_wt /= n;
printf("\nProcesses ArrivalTime BurstTime CompletionTime TurnAroundTime
WaitingTime\n");
for(int i = 0; i < n; i++) {
printf(" %d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
pid[i], at[i], bt[i], ct[i], tat[i], wt[i]);
}
printf("\nAverage Turn Around Time is %.2f\n", avg_tat);
printf("Average Waiting Time is %.2f\n", avg_wt);
return 0;
}
Output:
2. SJF (Shortest Job First)
Language Used: C
Theory: Shortest job first (SJF), is a scheduling policy that selects the waiting process with
the smallest execution time to execute next. SJF is a non-pre-emptive algorithm. Shortest Job
first has the advantage of having a minimum average waiting time among all scheduling
algorithms. It may cause starvation if shorter processes keep coming. This problem can be
solved using the concept of ageing.
Code:
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int pid[n], at[n], bt[n], ct[n], tat[n], wt[n];
int completed[n];
float avg_tat = 0, avg_wt = 0;
for(int i = 0; i < n; i++) {
pid[i] = i + 1;
completed[i] = 0;
printf("\nEnter Arrival Time for P%d: ", i+1);
scanf("%d", &at[i]);
printf("Enter Burst Time for P%d: ", i+1);
scanf("%d", &bt[i]);
}
int current_time = 0, completed_count = 0;
while(completed_count < n) {
int min_bt = 9999;
int selected = -1;
for(int i = 0; i < n; i++) {
if(at[i] <= current_time && completed[i] == 0) {
if(bt[i] < min_bt) {
min_bt = bt[i];
selected = i;
}
}
}
if(selected == -1) {
current_time++;
continue;
}
ct[selected] = current_time + bt[selected];
tat[selected] = ct[selected] - at[selected];
wt[selected] = tat[selected] - bt[selected];
current_time = ct[selected];
completed[selected] = 1;
completed_count++;
}
for(int i = 0; i < n; i++) {
avg_tat += tat[i];
avg_wt += wt[i];
}
avg_tat /= n;
avg_wt /= n;
printf("\nProcesses ArrivalTime BurstTime CompletionTime TurnAroundTime
WaitingTime\n");
for(int i = 0; i < n; i++) {
printf(" %d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
pid[i], at[i], bt[i], ct[i], tat[i], wt[i]);
}
printf("\nAverage Turn Around Time is %.2f\n", avg_tat);
printf("Average Waiting Time is %.2f\n", avg_wt);
return 0;
}
Output: