0% found this document useful (0 votes)
16 views19 pages

OS Practical

Uploaded by

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

OS Practical

Uploaded by

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

1. Study of Basic commands of Linux.

https://ubuntu.com/tutorials/command-line-for-beginners#1-overview

2. Study the basics of shell programming.

https://www.tecmint.com/understand-linux-shell-and-basic-shell-scripting-language-tips/

Ashell script is a computer program designed to be run by the UNIX shell, a command-line interpreter. The
various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell
scripts include file manipulation, program execution, and printing of text. A script that sets up the
environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.

Identification of shell prompt


You can identify whether the shell prompt on a Linux based computer is a normal or super user by looking at
the symbols of the prompt in the terminal window. The ‘#’ symbol is used for a super user and the ‘$’ symbol
is used for a user with standard privileges.

https://www.opensourceforu.com/2022/05/the-basic-concepts-of-shell-scripting/

3. Write a Shell script to print given numbers sum of all digits.

Algo:
1. Get a number
2. Split each digit from the number using modulo operator.
3. Calculate the sum
4. Print the result.
Script:
echo "Enter a number"
read num

sum=0

while [ $num -gt 0 ]


do
mod=$((num % 10)) #It will split each digits
sum=$((sum + mod)) #Add each digit to sum
num=$((num / 10)) #divide num by 10.
done

echo $sum
4. Write a shell script to validate the entered date. (eg. Date format is:
dd-mm-yyyy).

echo ""
echo "Date validator"
echo ""
#Initializing values of date, month and year
dd=0
mm=0
yy=0
#initializing no of days in a month
days=0
read -p "Enter day (dd) : " dd
read -p "Enter Month (mm) : " mm
read -p "Enter Year (yyyy) : " yy
#checking for invalid month
if [ $mm -le 0 -o $mm -gt 12 ]
then
echo "$mm is invalid month. "
exit 1
fi
#finding out no. of days in a month
case $mm in
1|3|5|7|8|10|12)
days=31
;;
2)
days=28
;;
4|6|9|11)
days=30
;;
*)
days=-1
;;
esac
#checking for leap year
if [ $mm -eq 2 ]
then
a=`expr $yy % 4`
b=`expr $yy % 100`
c=`expr $yy % 400`
if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ]
then
days=29
else
break
fi
fi
if [ $dd -le 0 -o $dd -gt $days ]
then
echo "$dd day is invalid "
exit 3
fi
#No error means date is valid
echo ""
echo "$dd/$mm/$yy is a Valid Date"
echo ""

5. Write a shell script to check entered string is palindrome or not.

#!/bin/bash
echo "Enter a String"
read input
reverse=""

len=${#input}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${input:$i:1}"
done
if [ $input == $reverse ]
then
echo "$input is palindrome"
else
echo "$input is not palindrome"
fi

6. Write a Shell script to say Good morning/Afternoon/Evening as you log in to


system.

h=$(date +"%H")

if [ $h -gt 6 -a $h -le 12 ]

then

echo good morning

elif [ $h -gt 12 -a $h -le 16 ]

then

echo good afternoon

elif [ $h -gt 16 -a $h -le 20 ]


then

echo good evening

else

echo good night

fi

Output

$ good evening

7. Write a C program to create a child process.


#include <stdio.h>

#include <sys/wait.h> /* contains prototype for wait */

int main(void)

int pid;

int status;

printf("Hello World!\n");

pid = fork( );

if(pid == -1) /* check for error in fork */

perror("bad fork");

exit(1);

if (pid == 0)

printf("I am the child process.\n");


else

wait(&status); /* parent waits for child to finish */

printf("I am the parent process.\n");

}
}

8. Finding out biggest number from given three numbers supplied as command
line arguments.

#Check if the number of arguments passed is zero

if [ "$#" = 0 ]

then

#Script exits if no

#arguments passed

echo "No arguments passed."

exit 1

fi

#Initialize maxEle with

#the first argument

maxEle=$1
#Loop that compares maxEle with the

#passed arguments and updates it

for arg in "$@"

do

if [ "$arg" -gt "$maxEle" ]

then

maxEle=$arg

fi

done

echo "Largest value among the arguments passed is: $maxEle"

9. Printing the patterns using for loop.

#include <stdio.h>

int main() {

int i, j, rows;

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

scanf("%d", &rows);

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

for (j = 1; j <= i; ++j) {

printf("* ");

printf("\n");

}
return 0;

#include <stdio.h>

int main() {

int i, j, rows;

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

scanf("%d", &rows);

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

for (j = 1; j <= i; ++j) {

printf("%d ", j);

printf("\n");

return 0;

}
#include <stdio.h>

int main() {

int i, j, rows;

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

scanf("%d", &rows);

for (i = rows; i >= 1; --i) {

for (j = 1; j <= i; ++j) {

printf("* ");

printf("\n");

return 0;

}
10. Shell script to determine whether given file exist or not.

https://www.geeksforgeeks.org/bash-scripting-how-to-check-if-file-exists/

#!/bin/bash

# using [ expression ] syntax and in place

# of File.txt you can write your file name

if [ -f "File.txt" ];

then

# if file exist the it will be printed

echo "File is exist"

else

# is it is not exist then it will be printed

echo "File is not exist"


fi

Now save and run the file using the following command
$ chmod +x ./FirstFile.sh
$ ./FirstFile.sh

11. Write a program for process creation using C. (Use of gcc compiler.

https://medium.com/@luischaparroc/compilation-process-with-gcc-and-c-programs-344445180ac
8

12. Implementation of FCFS &Round Robin Algorithm.

//C++ Program to implement Round Robin

//Scheduling CPU Algorithm

#include <iostream>

#include <vector>

/*at = Arrival time,

bt = Burst time,

time_quantum= Quantum time

tat = Turn around time,

wt = Waiting time*/

using namespace std;

int main(){

int i,n,time,remain,temps=0,time_quantum;

int wt=0,tat=0;
cout<<"Enter the total number of process="<<endl;

cin>>n;

remain=n;

// assigning the number of process to remain variable

vector<int>at(n);

vector<int>bt(n);

vector<int>rt(n);

//dynamic array declaration using vector method of (STL)

//STL standard template library of C++

cout<<"Enter the Arrival time, Burst time for All the processes"<<endl;

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

cin>>at[i];

cin>>bt[i];

rt[i]=bt[i];

cout<<"Enter the value of time QUANTUM:"<<endl;

cin>>time_quantum;
cout<<"\n\nProcess\t:Turnaround Time:Waiting Time\n\n";

for(time=0,i=0;remain!=0;)

if(rt[i]<=time_quantum && rt[i]>0)

time += rt[i];

//Addition using shorthand operators

rt[i]=0;

temps=1;

else if(rt[i]>0)

rt[i] -= time_quantum;

//Subtraction using shorthand operators

time += time_quantum;

//Addition using shorthand operators

if(rt[i]==0 && temps==1)

remain--;

//Desplaying the result of wating, turn around time:

printf("Process{%d}\t:\t%d\t:\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
cout<<endl;

wt += time-at[i]-bt[i];

tat += time-at[i];

temps=0;

if(i == n-1)

i=0;

else if(at[i+1] <= time)

i++;

else

i=0;

cout<<"Average waiting time "<<wt*1.0/n<<endl;

cout<<"Average turn around time "<<tat*1.0/n<<endl;;

return 0;

}
13. Implementation of Banker's Algorithm.

//C++ program for Banker's Algorithm

#include <iostream>

using namespace std;

int main()

// P0, P1, P2, P3, P4 are the names of Process

int no_of_processes, no_of_resources, i, j, k;

no_of_processes = 5; // Number of processes

no_of_resources = 3; //Number of resources


int allocate[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix

{ 2, 0, 0 }, // P1

{ 3, 0, 2 }, // P2

{ 2, 1, 1 }, // P3

{ 0, 0, 2 } }; // P4

int max[5][3] = { { 7, 5, 3 }, // P0 // MAX matrix representing max need

{ 3, 2, 2 }, // P1

{ 9, 0, 2 }, // P2

{ 4, 2, 2 }, // P3

{ 5, 3, 3 } }; // P4

int available[3] = { 3, 3, 2 }; // Available Resources

int finish[no_of_processes]={0}, safe_seq[no_of_processes], index = 0;

int need[no_of_processes][no_of_resources];

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

for (j = 0; j < no_of_resources; j++)

need[i][j] = max[i][j] - allocate[i][j]; //calculating need of resources

int y = 0;

for (k = 0; k < 5; k++) {


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

if (finish[i] == 0) { //unfinished process

bool flag = true;

for (j = 0; j < no_of_resources; j++) {

if (need[i][j] > available[j]){ //not enough resources

flag = false;

break;

if (flag == true) { //resources are available

safe_seq[index++] = i;

for (y = 0; y < no_of_resources; y++)

available[y] += allocate[i][y]; //allocating


resources

finish[i] = 1; //process is finished

cout<<"Th SAFE Sequence is: \n";

for (i = 0; i < no_of_processes - 1; i++)

cout<<" P->"<<safe_seq[i];
cout<<" P->"<<safe_seq[no_of_processes - 1];

return 0;

https://www.educative.io/answers/how-to-implement-the-bankers-algorithm

You might also like