OS Lab
Assignment #1
Perform the following operations
-
Create three directories named letter, report and assignment.
mkdir letter mkdir report mkdir assignment -
Move to directory letter.
cd letter -
Create two directories named friendly and formal under letter directory.
mkdir friendly formal -
Move to directory report using only one command (from directory letter)
cd ../report -
Create three directories called personal, business, school under directory report (Using one command).
mkdir ../assignment/unit -
Create a directory called unit under the assignment directory without moving from the directory report (Using one command).
mkdir personal business school -
Move to your HOME directory.
cd ~ -
Recursively list all the directories you have created and draw the directory structure.
ls -LR
Assignment #2
Perform the following operations
-
Change the working directory to friendly and use pluma from the command line to create A1.text with the following personal information.
Name: <student_name>
Email: <email_id>
Id: <student_id>
Date: <date>
cd friendly pluma A1.textName: Purbayan Chowdhury Email: pur.cho.99@gmail.com Id: 90 Date: 01/02/2020 -
Exit pluma and copy the new file A1.txt to B1.txt. Verify that both files exist by listing the directory.
cp A1.txt B1.txt ls # A1.txt B1.txt -
Rename the B1.txt file to A.txt, then list the directory, and remove the A1.txt file, and list the directory again.
mv B1.txt A.txt rm A1.txt ls # A.txt -
Set A.txt to have read and write permission for the owner, but keep file private from others.
chmod 600 A.txt -
Now check the file permissions for A.txt.
ls -l # total 4 # -rw------- 1 pur.cho.99 pur.cho.99 0 Jan 2 2020 A.txt -
Move to your HOME directory.
cd ~ -
Recursively list all the directories you have created and draw the directory structure.
ls -LR
Assignment #3
Perform the following operations using date and cal command.
-
Display 3 months from now
cal -A2 -
Display Monday as the first day
ncal -M -
Display September of 1752
cal -m 9 1752 -
Display 3 months for the data around 9th March 2015
ncal -m 3 2015 -A2 -H 2015-03-09 -
Display day of the week
date +%A -
Display week number of the year
date +%V -
Display date on this format - 12th April 1998
date +'%_d %_B %_Y' -
Display date on this format - 12th Jan'95
date +"%_d %_b'%_y" -
Display date on this format - 05/07/98
date +%d/%m/%y -
Display current date with hour minute second
date +%H:%M:%S
Assignment #4
Open a terminal window and show the manual page for the command grep.
Suppose that you have a file abc.txt whose contents are
- Your name in sentence case.
- Your name in capital case.
- Your name in small case.
- Email id.
- Mobile number.
- Phone number.
- Your hobby.
- A blankline.
- Your grade in Data Structures and Algorithms.
- Any special character.
- A string like 112@456.
- Dates in different format.
Write the grep command for the following instructions:
-
Find your name in small letter.
grep "^[a-z]+ [a-z]+$" abc.txt # purbayan chowdhury -
Find your name anywhere in file.
grep -i "^[a-z]+ [a-z]+$" abc.txt # Purbayan Chowdhury # PURBAYAN CHOWDHURY # purbayan chowdhury -
Show two lines before & after your hobby.
grep -A2 -B2 "^Hobby: [a-zA-Z]+$" abc.txt # Hobby: cricket # # A+ -
Show your mail id.
grep -i "^[a-z_]+[a-z0-9._]+@[a-z]{3,}.[a-z]{2,}$" abc.txt # pur.cho.99@gmail.com -
Show all the lines containing @.
grep "@" abc.txt # pur.cho.99@gmail.com # 112@456 -
Show all the lines without @.
grep -v "@" abc.txt # Purbayan Chowdhury # PURBAYAN CHOWDHURY # purbayan chowdhury # 7898765432 # +91789765432 # Hobby: cricket # # A+ # 112@456 # 12/08/2020 -
Find how many times your name is present in the file.
grep -c "^[a-zA-Z]+ [a-zA-Z]+$" abc.txt # 3 -
Find the line number where your name is present in the file in small case or capital.
grep -n "^[a-z]+ [a-z]+$|^[A-Z]+ [A-Z]+$|" abc.txt # 2:PURBAYAN CHOWDHURY # 3:purbayan chowdhury -
Print all lines that contain a mobile number.
grep "^[0-9]{10}$" abc.txt # 7898765432 -
Print all lines that contain a phone number with an extension.
grep "^[0-9]{2,3}[-][0-9]{6,8}$" abc.txt # +91789765432 -
Print all lines containing a vowel.
grep -i "[aeiou]" abc.txt # Purbayan Chowdhury # PURBAYAN CHOWDHURY # purbayan chowdhury # pur.cho.99@gmail.com # Hobby: cricket -
Print all lines that do not begin with a capital S.
grep -v "^S" abc.txt # Purbayan Chowdhury # PURBAYAN CHOWDHURY # purbayan chowdhury # 7898765432 # +91789765432 # Hobby: cricket # # A+ # 112@456 # 12/08/2020 -
Is there any day of a date?
grep -i "^[0-3][0-9]/[0-9]{2}/[0-9]{4}$" abc.txt # 12/08/2020
Assignment #5
-
Write a program to print current process ID, parent process ID and child process ID.
#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid, ppid, cpid; pid = getpid(); ppid = getppid(); cpid = fork(); printf("Process ID: %d\n", pid); printf("Parent Process ID: %d\n", ppid); printf("Child Process ID: %d\n", cpid); return 0; }Process ID: 8 Parent Process ID: 7 Child Process ID: 0 -
Show that the orders of print statement in the child and parent process are different at different times of execution. Find the reason behind it.
#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { if(fork()==0) printf("Child \n"); else printf("Parent \n"); return 0; }Parent ChildOn the execution, both programs (parent and child) will execute independent of each other starting from if statement. In the parent, since pid is non-zero, it prints the parent and on the child, since pid is zero, it prints the child later. -
Show that parent process ID depends on Shell.
gcc -o exe process1.c ./exe # Current process: 3148 # Parent process: 3141gcc -o exe process1.c ./exe # Current process: 3156 # Parent process: 3149The reason for this is that the parent process ID depends on the Shell. -
Write a C program using fork() system call that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in Fibonacci will be output by the childd process. Because the parent and child processes have their own copies of the data, it will be necessary for child to output the sequence.
#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int a=0, b=1, c=a+b, i, ii; pid_t pid; printf("Enter the number of terms: "); scanf("%d", &ii); if(ii<=0) { printf("Please enter a positive number.\n"); return 0; } else { pid = fork(); if(pid == 0) { printf("Child is producing the Fibonacci series.....\n"); for(i=0; i<ii; i++) { printf("%d ", c); a=b; b=c; c=a+b; } printf("\n"); printf("Child ends.\n"); } else { printf("Parent is waiting for child to finish......\n"); wait(NULL); printf("Parent ends.\n"); } } return 0; } /* Output: */Enter the number of terms: 5 Parent is waiting for child to finish...... Child is producing the Fibonacci series..... 0 1 1 2 3 5 Child ends. Parent ends.
Assignment #6
-
Take two numbers through command line argument. Write a shell script to interchange the contents of the variables. Do the same operation without using any third variable.
#!/bin/bash file1=$1 file2=$2 echo "Before swapping: "$file1 $file2 temp=$file1 file1=$file2 file2=$temp echo "After swapping: "$file1 $file2Before swapping: 1 2 After swapping: 2 1#!/bin/bash file1=$1 file2=$2 echo "Before swapping: "$file1 $file2 file1=$((file1 + file2)) file2=$((file1 - file2)) file1=$((file1 - file2)) echo "After swapping: "$file1 $file2Before swapping: 1 2 After swapping: 2 1 -
Write a shell script to check whether a given year is leap year or not.
#!/bin/bash year=$1 y=$((!(year % 4) && (year % 100 || !(year % 400)))) if [ $y -eq 1 ]; then echo "$year is a leap year" else echo "$year is not a leap year" fiEnter the year: 2000 2000 is a leap year -
Write a shell script to find the value of y using
Y(x, n) = {1+x^2 when n=1} {1+x/n when n=2} {1+2x when n=3} {1+nx when n>3 or n<1}#!/bin/bash x=$1 n=$2 if [ $n -lt 1 ]; then echo "Enter a positive number" elif [ "$n" -eq "1" ]; then echo $((1+x*x)) elif [ "$n" -eq "2" ]; then echo $((1+x/n)) elif [ "$n" -eq "3" ]; then echo $((1+2*x)) else echo $((1+n*x)) fi -
Determine grade as per following rules using a switch case:
- If marks are more than 90, grade is O
- If marks are more than 80, grade is E
- If marks are more than 70, grade is A
- If marks are more than 60, grade is B
- If marks are more than 50, grade is C
- If marks are less than 50, grade is F
#!/bin/bash marks=$1 case $marks in [9][0-9]|100) echo "O grade" ;; [8][0-9]|[8][0-9][0-9]) echo "E grade" ;; [7][0-9]|[7][0-9][0-9]) echo "A grade" ;; [6][0-9]|[6][0-9][0-9]) echo "B grade" ;; [5][0-9]|[5][0-9][0-9]) echo "C grade" ;; [4][0-9]|[4][0-9][0-9]|[3][0-9]|[3][0-9][0-9]) echo "F grade" ;; *) echo "Invalid marks" ;; esacEnter the marks: 90 O grade
Assignment #7,8,9
Write a program to implement the following scheduling.
- First Come First Served Scheduling
- Priority Scheduling
- Shortest Job First Scheduling
- Round Robin Scheduling
- Shortest Remaining Time First Scheduling
#include <iostream>
#include <algorithm>
class process
{
int _id, _atime, _ttime, _btime, _wtime, _priority;
public:
process()
{
_id = 0;
_atime = 0;
_ttime = 0;
_btime = 0;
_wtime = 0;
_priority = 0;
}
process(int id, int atime, int btime, int priority = 0)
{
this->_id = id;
this->_atime = atime;
this->_btime = btime;
this->_ttime = 0;
this->_wtime = 0;
this->_priority = priority;
}
void print()
{
printf("%d\t%d\t%d\t%d\t%d\t%d\n", this->_id, this->_btime, this->_atime, this->_ttime, this->_wtime, this->_priority);
};
int id()
{
return this->_id;
}
int atime()
{
return this->_atime;
}
int btime()
{
return this->_btime;
}
int ttime()
{
return this->_ttime;
}
int wtime()
{
return this->_wtime;
}
int priority()
{
return this->_priority;
}
void t_time(int ttime)
{
this->_ttime = ttime;
};
void w_time(int wtime)
{
this->_wtime = wtime;
};
};
void fcfs(process *p, int n)
{
std::sort(p, p + n, [](process p1, process p2)
{ return p1.atime() < p2.atime(); });
int i, ctime = 0, avg_t_time = 0, avg_w_time = 0;
// Turn Around Time Calculation
for (i = 0; i < n; i++)
{
if (ctime - p[i].atime() < 0)
ctime += p[i].atime();
ctime += p[i].btime();
p[i].t_time(ctime - p[i].atime());
avg_t_time += p[i].ttime();
}
avg_t_time /= n;
// Waiting Time
for (i = 0; i < n; i++)
{
p[i].w_time(p[i].ttime() - p[i].btime());
avg_w_time += p[i].wtime();
}
avg_w_time /= n;
std::sort(p, p + n, [](process p1, process p2)
{ return p1.id() < p2.id(); });
printf("Id\tB_Time\tA_Time\tT_Time\tW_Time\n");
for (i = 0; i < n; i++)
{
p[i].print();
}
printf("Average TAT: \t%d\nAverage WT: \t%d\n", avg_t_time, avg_w_time);
}
void psa(process *p, int n)
{
std::sort(p, p + n, [](process p1, process p2)
{ return (p1.priority() < p2.priority()) || (p1.priority() == p2.priority() && p1.atime() < p2.atime()); });
int i, ctime = 0, avg_t_time = 0, avg_w_time = 0;
// Turn Around Time Calculation
for (i = 0; i < n; i++)
{
if (ctime - p[i].atime() < 0)
ctime += p[i].atime();
ctime += p[i].btime();
p[i].t_time(ctime - p[i].atime());
avg_t_time += p[i].ttime();
}
avg_t_time /= n;
// Waiting Time
for (i = 0; i < n; i++)
{
p[i].w_time(p[i].ttime() - p[i].btime());
avg_w_time += p[i].wtime();
}
avg_w_time /= n;
std::sort(p, p + n, [](process p1, process p2)
{ return p1.id() < p2.id(); });
printf("Id\tB_Time\tA_Time\tT_Time\tW_Time\n");
for (i = 0; i < n; i++)
{
p[i].print();
}
printf("Average TAT: \t%d\nAverage WT: \t%d\n", avg_t_time, avg_w_time);
}
void sjf(process *p, int n)
{
std::sort(p, p + n, [](process p1, process p2)
{ return (p1.btime() < p2.btime()) || (p1.btime() == p2.btime() && p1.atime() < p2.atime()); });
int i, ctime = 0, avg_t_time = 0, avg_w_time = 0;
// Turn Around Time Calculation
for (i = 0; i < n; i++)
{
if (ctime - p[i].atime() < 0)
ctime += p[i].atime();
ctime += p[i].btime();
p[i].t_time(ctime - p[i].atime());
avg_t_time += p[i].ttime();
}
avg_t_time /= n;
// Waiting Time
for (i = 0; i < n; i++)
{
p[i].w_time(p[i].ttime() - p[i].btime());
avg_w_time += p[i].wtime();
}
avg_w_time /= n;
std::sort(p, p + n, [](process p1, process p2)
{ return p1.id() < p2.id(); });
printf("Id\tB_Time\tA_Time\tT_Time\tW_Time\tPriority\n");
for (i = 0; i < n; i++)
{
p[i].print();
}
printf("Average TAT: \t%d\nAverage WT: \t%d\n", avg_t_time, avg_w_time);
}
void rr(process *p, int n, int quantum = 4)
{
std::sort(p, p + n, [](process p1, process p2)
{ return (p1.atime() < p2.atime()); });
int i, ctime = 0, avg_t_time = 0, avg_w_time = 0;
int min = -1, st[n], left_jobs = n;
// Turn Around Time Calculation
for (i = 0; i < n; i++)
st[i] = p[i].btime();
while (left_jobs > 0)
{
for (i = 0; i < n; i++)
{
if (st[i] == 0 || ctime < p[i].atime())
continue;
if (st[i] > quantum)
{
ctime += quantum;
st[i] -= quantum;
}
else
{
ctime += st[i];
st[i] = 0;
p[i].t_time(ctime - p[i].atime());
avg_t_time += p[i].ttime();
left_jobs--;
}
}
}
avg_t_time /= n;
// Waiting Time
for (i = 0; i < n; i++)
{
p[i].w_time(p[i].ttime() - p[i].btime());
avg_w_time += p[i].wtime();
}
avg_w_time /= n;
std::sort(p, p + n, [](process p1, process p2)
{ return p1.id() < p2.id(); });
printf("Id\tB_Time\tA_Time\tT_Time\tW_Time\tPriority\n");
for (i = 0; i < n; i++)
{
p[i].print();
}
printf("Average TAT: \t%d\nAverage WT: \t%d\n", avg_t_time, avg_w_time);
}
void srtf(process *p, int n)
{
std::sort(p, p + n, [](process p1, process p2)
{ return (p1.atime() < p2.atime()); });
int i, ctime = 0, avg_t_time = 0, avg_w_time = 0;
int min = -1, st[n], left_jobs = n;
// Turn Around Time Calculation
for (i = 0; i < n; i++)
st[i] = p[i].btime();
while (left_jobs != 0)
{
min = -1;
for (i = 0; i < n; i++)
{
if (st[i] == 0 || ctime < p[i].atime())
continue;
if (min == -1 || st[i] < st[min])
min = i;
}
ctime++;
st[min]--;
if (st[min] == 0)
{
left_jobs--;
p[min].t_time(ctime - p[min].atime());
avg_t_time += p[min].ttime();
}
}
avg_t_time /= n;
// Waiting Time
for (i = 0; i < n; i++)
{
p[i].w_time(p[i].ttime() - p[i].btime());
avg_w_time += p[i].wtime();
}
avg_w_time /= n;
std::sort(p, p + n, [](process p1, process p2)
{ return p1.id() < p2.id(); });
printf("Id\tB_Time\tA_Time\tT_Time\tW_Time\tPriority\n");
for (i = 0; i < n; i++)
{
p[i].print();
}
printf("Average TAT: \t%d\nAverage WT: \t%d\n", avg_t_time, avg_w_time);
}
int main()
{
int n;
n = 5;
process p[n];
// Process p1
p[0] = process(1, 0, 24, 1);
// Process p2
p[1] = process(2, 3, 12, 4);
// Process p3
p[2] = process(3, 2, 6, 3);
// Process p4
p[3] = process(4, 0, 18, 0);
// Process p5
p[4] = process(5, 4, 3, 3);
printf("\nFCFS:\n");
fcfs(p, n);
printf("\nPSA:\n");
psa(p, n);
printf("\nSJF:\n");
sjf(p, n);
printf("\nSRTF:\n");
srtf(p, n);
printf("\nRR:\n");
rr(p, n);
return 0;
}
FCFS:
Id B_Time A_Time T_Time W_Time
1 24 0 24 0 1
2 12 3 57 45 4
3 6 2 46 40 3
4 18 0 42 24 0
5 3 4 59 56 3
Average TAT: 45
Average WT: 33
PSA:
Id B_Time A_Time T_Time W_Time
1 24 0 42 18 1
2 12 3 60 48 4
3 6 2 46 40 3
4 18 0 18 0 0
5 3 4 47 44 3
Average TAT: 42
Average WT: 30
SJF:
Id B_Time A_Time T_Time W_Time Priority
1 24 0 67 43 1
2 12 3 22 10 4
3 6 2 11 5 3
4 18 0 43 25 0
5 3 4 3 0 3
Average TAT: 29
Average WT: 16
SRTF:
Id B_Time A_Time T_Time W_Time Priority
1 24 0 63 39 1
2 12 3 20 8 4
3 6 2 9 3 3
4 18 0 39 21 0
5 3 4 3 0 3
Average TAT: 26
Average WT: 14
RR:
Id B_Time A_Time T_Time W_Time Priority
1 24 0 63 39 1
2 12 3 42 30 4
3 6 2 27 21 3
4 18 0 59 41 0
5 3 4 15 12 3
Average TAT: 41
Average WT: 28

0 comments:
Post a Comment