1. Use the cat commands tp create a file containing the following data. Call it Mytable.
txt use
tabs to separate the fields.
[Link]:
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
a. Use the cat command to display the file. [Link]?
Output: sec-e@vignan:~$ cat [Link]
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
b. Use the Vicomm and to correct any errors in the [Link]?
Output:
c. Use the sort command to sort the file [Link] according to the first field?
Output: sec-e@vignan:~$ sort [Link]
1425 ravi 15.65
1450 raju 21.86
4320 ramu 26.27
6830 sita 36.15
d. Call the sorted file [Link] (different name)
Output: sec-e@vignan:~$ sort [Link] > [Link]
sec-e@vignan:~$ sort -o [Link] [Link]
sec-e@vignan:~$ cat [Link]
1425 ravi 15.65
1450 raju 21.86
4320 ramu 26.27
6830 sita 36.15
e. Print the file [Link]?
Output: sec-e@vignan:~$ ip [Link]
1425 ravi 15.65
4320 ramu 26.27
6830 sita 36.15
1450 raju 21.86
f. Use the cut & paste commands to swap fields 2 nd and 3 rd in [Link]. Call it
[Link]?
Output:
vignan@vignan:~$ cut -f1,3 [Link] > cat
vignan@vignan:~$ cut -f2 [Link] | paste cat -> result
vignan@vignan:~$ cat result
1425 15.65 ravi
4320 26.27 ramu
6830 36.15 sita
1450 21.86 raju
2. Write a shell script that displays a list of all the files in the current directory to which the
user has read, write and execute permissions.
Source Code:
#!/bin/bash
echo "List of files in current directory having read,write and execute permissions:"
for file in *
doif [ -f $file ]
then
if [ -r $file -a -w $file -a -x $file ]
then
ls -l $file
fi
fi
done
Output:
List of files in current directory having read,write and execute permissions:
-rwxrw-r-- 1 vignan vignan 37 Feb 3 08:40 a
-rwxrw-r-- 1 vignan vignan 111 Feb 3 08:43 d
-rwxrwxrwx 1 vignan vignan 139 Feb 3 08:59 [Link]
-rwxrwxrwx 1 vignan vignan 114 Feb 3 08:57 [Link]~
3. Write a shell script that computes the total and average marks of a student according to the
following;
● Ifaveragemarks≥69thenresultis-
● Ifaveragemarks≥59and≤
● Ifaveragemarks≥49and≤
● Note that any subject marks ≤ 40then result is-Fail ‖ .
Accept student name and six subject marks through the keyboard
Source code:
echo "Enter Student name"
read name
echo "Enter Student Registered Number"
read reg
echo "Enter the marks of five subjects : "
read m1 m2 m3 m4 m5
let total=$m1+$m2+$m3+$m4+$m5
let per=(total/5)
echo "Name :$name"
echo "Registered number :$reg"
echo "student total marks:$total"
echo "Student percentage :$per"
if [ $per -ge 69 ]
then
echo "grade :Distinction"
elif [ $per -ge 59-a $per -lt 69 ]
then
echo "Grade :First class"
elif [ $per -ge 49-a $per -lt 59 ]
then
echo "Grade :Pass"
else
echo "Grade :Fail"
fi
Output:
vignan@vignan:~/os$ ./[Link]
Enter Student name
abc
Enter Student Registered Number
4200
Enter the marks of five subjects :
95 98 78 96 99
Name :abc
Registered number :4200
student total marks:466
Student percentage :93
grade :Distinction
4. Simulate the following non-pre-emptive CPU scheduling algorithms to find turnaround
time and waiting time. a. FCFS b. SJF c. Round Robin (pre-emptive).
A. FCFS:
echo -n "Enter process number: "
read n1
BurstTime=()
WaitingTime=()
TurnAroundTime=()
for i in $(seq 1 1 $n1)
do
echo -n "Enter Burst Time for process:"
read bt
BurstTime+=($bt)
done
WaitingTime[0]=0
TurnAroundTime+=${BurstTime[0]
TotalWaitingTime=0
TotalTurnAroundTime=${
for i in $(seq 1 1 $((n1-1)))
do
WaitingTime[$i]=$((${
TurnAroundTime[$i]=$((${
TotalWaitingTime=$(($
TotalTurnAroundTime=$(($
doneAvgWaitingTime=$(($
AvgTurnAroundTime=$(($
echo "PROCESS BURSTTIME WAITINGTIME TURNAROUND TIME"
for(( i=0; i<$n1; i++))
do
echo "p:${i} ${BurstTime[$i]} ${WaitingTime[$i]} $
{TurnAroundTime[$i]}"
done
echo Average Waiting Time : $AvgWaitingTime
echo Average Turn Around Time : $AvgTurnAroundTime
vignan@vignan:~/os$ bash [Link]
Enter process number: 3
Enter Burst Time for process:9
Enter Burst Time for process:6
Enter Burst Time for process:8
PROCESS BURSTTIME WAITINGTIME TURNAROUND TIME
p:0 9 0 9
p:1 6 9 15
p:2 8 15 23
Average Waiting Time : 8
Average Turn Around Time : 15
B. SJF:
readarray fileDat < $1
quantum=${fileDat[${#fileDat[@
unset fileDat[${#fileDat[@]}-1]
processCount=${#fileDat[@]}
if [ $quantum -lt 3 ] || [ $quantum -gt 10 ] ; then
echo "ERROR: Quantum must be between 3 to 10"
exit
fi
function printProcess {
process=(${fileDat[$1]})
if [ -z $process ] ; then
return
fi
processName=${process[0]}
arrival=${process[1]}
burst=${process[2]}priority=${process[3]}
echo Process Name: $processName
echo Arrival Time: $arrival
echo Burst Time: $burst
echo Priority: $priority
echo
}
count=0
let end=processCount-1
until [ $count -gt $end ]; do
printProcess $count
let count=count+1
done
echo Quantum: $quantum
echo
echo "~~~ Shortest Job First (SJF) Scheduling ~~~"
echo
sjfDat=("${fileDat[@]}")
shortestBurstIdx=0
currentTime=0
totalTurnaroundTime=0
waitingTime=0
echo "Grantt Chart: "
echo -n $currentTime' '
while [ ${#sjfDat[@]} -gt 0 ] ; do
shortestBurst=99999
count=0
until [ $count -gt $processCount ]; do
process=(${sjfDat[$count]})
if [ -z $process ] ; then
let count=count+1
continue
fi
burst=${process[2]}
if [ $burst -lt $shortestBurst ]; then
shortestBurst=$burst
shortestBurstIdx=$count
filet count=count+1
done
chosenProcess=(${sjfDat[$
processName=${chosenProcess[0]
arrival=${process[1]}
burst=${chosenProcess[2]}
let currentTime=currentTime+burst
echo -n [$processName] $currentTime' '
let turnaroundTime=currentTime-
let waitingTime=waitingTime+
let totalTurnaroundTime=
unset sjfDat[$shortestBurstIdx]
done
let avgWaitingTime=waitingTime/
let avgTurnAroundTime=
echo "Total Turnaround Time :" $totalTurnaroundTime
echo "Average Turnaround Time :" $avgTurnAroundTime
echo "Total Waiting Time :" $waitingTime
echo "Average Waiting Time :" $avgWaitingTime
[Link]:
P1 2 6 7
P2 1 8 1
P3 18 4 2
P4 2 2 5
4
vignan@vignan:~/os$ ./[Link] [Link]
Process Name: P1
Arrival Time: 2
Burst Time: 6
Priority: 7
Process Name: P2
Arrival Time: 1
Burst Time: 8
Priority: 1
Process Name: P3
Arrival Time: 18
Burst Time: 4
Priority: 2Process Name: P4
Arrival Time: 2
Burst Time: 2
Priority: 5
Quantum: 4
~~~ Shortest Job First (SJF) Scheduling ~~~
Grantt Chart:
0 [P4] 2 [P3] 6 [P1] 12 [P2] 20 Total Turnaround Time : 40
Average Turnaround Time : 10
Total Waiting Time : 20
Average Waiting Time : 5
C. Round Robin:
echo Enter number of process:
read n
echo Enter quantum time:
read qt
echo Enter the burst time for each process:
for i in $(seq 1 1 $n)
do
echo -n Process $i : burst time:
read bt[i]
rbt[i]=${bt[i]}
done
p=$n
pt=0
while [[ $p>0 ]]
do
for i in $(seq 1 1 $n)
do
if [[ ${rbt[i]} -gt 0 ]]
then
if [[ ${rbt[i]} -le $qt ]]
then
pt=$((pt+rbt[i]))
rbt[i]=0
tat[i]=$pt
wt[i]=$((pt-bt[i]))
p=$((p-1))else
rbt[i]=$((rbt[i]-qt))
pt=$((pt+qt))
fi
fi
done
done
for i in $(seq 1 1 $n)
do
echo process $i :waiting time ${wt[i]} turnaround time ${tat[i]}
done
vignan@vignan:~/os$ bash [Link]
Enter number of process:
3
Enter quantum time:
2
Enter the burst time for each process:
Process 1 : burst time:10
Process 2 : burst time:6
Process 3 : burst time:3
process 1 :waiting time 9 turnaround time 19
process 2 :waiting time 9 turnaround time 15
process 3 :waiting time 8 turnaround time 11
5. Simulate multi-level queue scheduling algorithm considering the following scenario.
All the processes in the system are divided into two categories - system processes and
user processes. system processes are to be given higher priority than user processes.
Use FCFS scheduling for the processes in each queue.
Ans:
#include<stdio.h>
int main()
{
int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;printf("Enter the Burst Time of Process%d:", i);
scanf("%d",&bt[i]);
printf("System/User Process (0/1) ? ");
scanf("%d", &su[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(su[i] > su[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=su[i];
su[i]=su[k];
su[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\t SYSTEM/USER PROCESS \tBURST TIME\tWAITING
TIME\tTURNAROUND TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],su[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
return 0;
}
Output:
vignan@vignan:~$ gedit mlq.c
vignan@vignan:~$ cc [Link]@vignan:~$ ./[Link]
Enter the number of processes:3
Enter the Burst Time of Process0:12
System/User Process (0/1) ? 0
Enter the Burst Time of Process1:18
System/User Process (0/1) ? 0
Enter the Burst Time of Process2:15
System/User Process (0/1) ? 1
PROCESS
SYSTEM/USER PROCESS
TURNAROUND TIME BURST TIMEWAITING TIME
0 0 12 0 12
1 0 18 12 30
2 1 15 30 45
Average Waiting Time is --- 14.000000
Average Turnaround Time is --- 29.000000vignan@vignan:~$
6. Simulate the following file allocation strategies.
a. Sequential
Ans:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TOTAL_DISK_BLOCKS 32
#define TOTAL_DISK_INODES 8
#ifndef MAX
#define MAX 15
#endif
int blockStatus[TOTAL_DISK_BLOCKS]; // free = 0
int blockStart;
struct file_table {
char fileName[20];int startBlock;
int fileSize;
int allotStatus;
};
struct file_table fileTable[TOTAL_DISK_BLOCKS - TOTAL_DISK_INODES];
int AllocateBlocks(int Size) {
int i = 0, count = 0, inList = 0, nextBlock = 0;
int allocStartBlock = TOTAL_DISK_INODES;
int allocEndBlock = TOTAL_DISK_BLOCKS - 1;
// check whether sufficient free blocks are available
for (i = 0; i < (TOTAL_DISK_BLOCKS - TOTAL_DISK_INODES); i++)
if (blockStatus[i] == 0)
count++;
if (count < Size)
return 1; // not enough free blocks
count = 0;
while (count < Size) {
nextBlock = (rand() % (allocEndBlock - allocStartBlock + 1)) + allocStartBlock;
for (i = nextBlock; i < (nextBlock + Size); i++) {
if (blockStatus[i] == 0)
count = count + 1;
else {
count = 0;
break;
}
}
}
blockStart = nextBlock;
for (int i = 0; i < Size; i++) {
blockStatus[blockStart + i] = 1;
}
if (count == Size)return nextBlock; // success
else
return 1; // not successful
}
void main() {
int i = 0, j = 0, numFiles = 0, nextBlock = 0, ret = 1, totalFileSize = 0;
char s[20];
//-- -
char *header[] = {"FILE_fileName", "FILE_SIZE", "BLOCKS_OCCUPIED"};
printf("File allocation method: SEQUENTIAL\n");
printf("Total blocks: %d\n", TOTAL_DISK_BLOCKS);
printf("File allocation start at block: %d\n", TOTAL_DISK_INODES);
printf("File allocation end at block: %d\n", TOTAL_DISK_BLOCKS - 1);
printf("Size (kB) of each block: 1\n\n");
printf("Enter no of files: ");
scanf("%d", &numFiles);
//numFiles = 3;
for (i = 0; i < numFiles; i++) {
//-- -
printf("\nEnter the name of file #%d: ", i+1);
scanf("%s", fileTable[i].fileName);
printf("Enter the size (kB) of file #%d: ", i+1);
scanf("%d", &fileTable[i].fileSize);
//strcpy(fileTable[i].fileName, "testfile");
srand(1234);
ret = AllocateBlocks(fileTable[i].fileSize);
//-- -
if (ret == 1) {
exit(0);
} else {
fileTable[i].startBlock = ret;
}
}//-- -
printf("\n%*s %*s %*s\n", -MAX, header[0], -MAX, header[1], MAX, header[2]);
//Seed the pseudo-random number generator used by rand() with the value seed
srand(1234);
//-- -
for (j = 0; j < numFiles; j++) {
printf("\n%*s %*d ", -MAX, fileTable[j].fileName, -MAX, fileTable[j].fileSize);
for(int k=0;k<fileTable[j].fileSize;k++) {
printf("%d%s", fileTable[j].startBlock+k, (k == fileTable[j].fileSize-1) ? "" : "-");
}
}
printf("\nFile allocation completed. Exiting.\n");
}
Output:
vignan@vignan:~$ ./[Link]
File allocation method: SEQUENTIAL
Total blocks: 32
File allocation start at block: 8
File allocation end at block: 31
Size (kB) of each block: 1
Enter no of files: 1
Enter the name of file #1: A
Enter the size (kB) of file #1: 24
FILE_fileName FILE_SIZE
A
24
29-30-31
BLOCKS_OCCUPIED
8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-
File allocation completed. Exiting.
b. Indexed
Ans:
#include<stdio.h>
#include<stdlib.h>
void main()
{int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n",
ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Output:
vignan@vignan:~$ ./[Link]
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
1
2
File in the index is already allocated
Enter another file indexed5
Allocated
File Indexed
6-------->5 : 1
Do you want to enter more file(Yes - 1/No – 0)0
vignan@vignan:~$
c. LinkedAns:
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);printf("Do you want to enter more file(Yes -
1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
Output:
vignan@vignan:~$ ./[Link]
Enter how many blocks already allocated: 3
Enter blocks already allocated: 1 3 5
Enter index starting block and length: 2 2
2-------->1
3 Block is already allocated
4-------->1
Do you want to enter more file(Yes - 1/No - 0)0
vignan@vignan:~$
7. Implementation of producer consumer problem.
Program:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces"
"item %d",
x);
++mutex;
}void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
for (i = 1; i > 0; i++) {
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)
&& (empty != 0)) {
producer();
}
else {
printf("Buffer is full!");
}
break;
case 2:
if ((mutex == 1)
&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!");}
break;
case 3:
exit(0);
break;
}
}
}
Output:
vignan@vignan:~$ ./[Link]
1. Press 1 for Producer
2. Press 2 for Consumer
3. Press 3 for Exit
Enter your choice:2
Buffer is empty!
Enter your choice:1
Producer producesitem 1
Enter your choice:1
Producer producesitem 2
Enter your choice:1
Producer producesitem 3
Enter your choice:2
Consumer consumes item 3
Enter your choice:2
Consumer consumes item 2
Enter your choice:2
Consumer consumes item 1
Enter your choice:2
Buffer is empty!
Enter your choice:2
Buffer is empty!Enter your choice:
8. Implementation of Banker’s algorithm for Deadlock avoidance.
Source code:
#include <stdio.h>
int m, n, i, j, al[10][10], max[10][10], av[10], need[10][10], temp, z, y, p, k;
void main() {
printf("\n Enter no of processes : ");
scanf("%d", &m); // enter numbers of processes
printf("\n Enter no of resources : ");
scanf("%d", &n); // enter numbers of resources
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\n Enter instances for al[%d][%d] = ", i,j); // al[][] matrix is for allocated instances
scanf("%d", &al[i][j]);
// al[i][j]=temp;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\n Enter instances for max[%d][%d] = ", i,j); // max[][] matrix is for max instances
scanf("%d", &max[i][j]);
}
}
for (i = 0; i < n; i++) {
printf("\n Available Resource for av[%d] = ",i); // av[] matrix is for available instances
scanf("%d", &av[i]);
}
// Print allocation values
printf("Alocation Values :\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf(" \t %d", al[i][j]); // printing allocation matrix
}
printf("\n");
}printf("\n\n");
// Print max values
printf("Max Values :\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf(" \t %d", max[i][j]); // printing max matrix
}
printf("\n");
}
printf("\n\n");
// Print need values
printf("Need Values :\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
need[i][j] = max[i][j] - al[i][j]; // calculating need matrix
printf("\t %d", need[i][j]); // printing need matrix
}
printf("\n");
}
p = 1; // used for terminating while loop
y = 0;
while (p != 0) {
for (i = 0; i < m; i++) {
z = 0;
for (j = 0; j < n; j++) {
if (need[i][j] <= av[j] &&
(need[i][0] != -1)) { // comparing need with available instance and
// checking if the process is done
// or not
z++; // counter if condition TRUE
}
}
if (z == n) { Efor (k = 0; k < n; k++) {
av[k] += al[i][k];
}
printf("\n SS process %d", i);
need[i][0] = -1;
y++;
}
} // end for loop
if (y == m) { // if all done then
p = 0; // exit while loop
}
} // end while
printf("\n");
}
Output:
vignan@vignan:~$ ./[Link]
Enter no of processes : 3
Enter no of resources : 2
Enter instances for al[0][0] = 1
Enter instances for al[0][1] = 2
Enter instances for al[1][0] = 1
Enter instances for al[1][1] = 2
Enter instances for al[2][0] = 1
Enter instances for al[2][1] = 2Enter instances for max[0][0] = 1
Enter instances for max[0][1] = 2
Enter instances for max[1][0] = 1
Enter instances for max[1][1] = 2
Enter instances for max[2][0] = 1
Enter instances for max[2][1] = 2
Available Resource for av[0] = 2
Available Resource for av[1] = 2
Alocation Values :
12
12
12
Max Values :
12
12
12
Need Values :
00
00
00
SS process 0
SS process 1
SS process 2