OS & Inux Lab Manual for Print (1)

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

OPERATING SYSTEMS & LINUX LAB MANUAL

List of Experiments
1. Simulate the following CPU scheduling algorithms: a. Round Robin (b) SJF (c)
FCFS (d) Priority

ROUND ROBIN CPU SCHEDULING ALGORITHM

For round robin scheduling algorithm, read the number of processes/jobs in the system, their
CPU burst times, and the size of the time slice. Time slices are assigned to each process in
equal portions and in circular order, handling all processes execution. This allows every
process to get an equal chance. Calculate the waiting time and turnaround time of each of the
processes accordingly.

#include<stdio.h>
void main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];

for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");

for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getch();
}

INPUT
Enter the no of processes – 3
Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 -- 3

Enter the size of time slice – 3

OUTPUT
The Average Turnaround time is – 15.666667
The Average Waiting time is -- 5.666667

PROCESS BURST TIME WAITING TIME TURNAROUND TIME


1 24 6 30
2 3 4 7
3 3 7 10

SJF CPU SCHEDULING ALGORITHM

For SJF scheduling algorithm, read the number of processes/jobs in the system, their CPU
burst times. Arrange all the jobs in order with respect to their burst times. There may be two
jobs in queue with the same execution time, and then FCFS approach is to be performed. Each
process will be executed according to the length of its burst time. Then calculate the waiting
time and turnaround time of each of the processes accordingly

#include<stdio.h>
#include<conio.h>
main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);

for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}

for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}

wt[0] = wtavg = 0;
tat[0] = tatavg = 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("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);

printf("\nAverage Waiting Time -- %f", wtavg/n);


printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}

INPUT
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3

OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
Average Turnaround Time -- 13.000000

FCFS CPU SCHEDULING ALGORITHM

For FCFS scheduling algorithm, read the number of processes/jobs in the system, their CPU
burst times. The scheduling is performed on the basis of arrival time of the processes
irrespective of their other parameters. Each process will be executed according to its arrival
time. Calculate the waiting time and turnaround time of each of the processes accordingly.

#include<stdio.h>
#include<conio.h>
void main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);

for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = 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("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}

INPUT
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3

OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30

Average Waiting Time-- 17.000000


Average Turnaround Time -- 27.000000

PRIORITY CPU SCHEDULING ALGORITHM

For priority scheduling algorithm, read the number of processes/jobs in the system, their CPU
burst times, and the priorities. Arrange all the jobs in order with respect to their priorities.
There may be two jobs in queue with the same priority, and then FCFS approach is to be
performed. Each process will be executed according to its priority. Calculate the waiting time
and turnaround time of each of the processes accordingly.

#include<stdio.h>
main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}

for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[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\tPRIORITY\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],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
getch();
}

INPUT
Enter the number of processes -- 5
Enter the Burst Time & Priority of Process 0 --- 10 3
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2

OUTPUT
PROCESS PRIORITY BURST TIME WAITING TIME TURNAROUND TIME
1 1 1 0 1
4 2 5 1 6
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19
Average Waiting Time is --- 8.200000

Average Turnaround Time is --- 12.000000


2. Multiprogramming-Memory Management- Implementation of fork(), wait(), exec() and
exit()

example.c
#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

void main(void) {

pid_t pid = 0;

int status;

printf("We are in Example.c\n");

printf("PID of Example.c = %d\n", getpid());

pid = fork();

if (pid == 0) {

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

char *args[] = {"Hello", "C", "Programming", NULL};

execv("./hello", args);

if (pid > 0) {

printf("I am the parent, my id is %d ,the child is %d.\n", getpid(),pid);

pid = wait(&status);

printf("End of process %d: ", pid);

if (WIFEXITED(status)) {

printf("The process ended with exit(%d).\n", WEXITSTATUS(status));


}

if (WIFSIGNALED(status)) {

printf("The process ended with kill -%d.\n", WTERMSIG(status));

if (pid < 0) {

perror("In fork():");

exit(0);

hello.c
#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

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

int i;

printf("We are in Hello.c\n");

printf("PID of hello.c = %d\n", getpid());

printf("Arguments passed from parent process are:\n");

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

printf("Argument %d = %s \n",i,argv[i]);

return 0;

}
OUTPUT
sacet@sacet-ThinkCentre-M73:~$ gcc -o example example.c

sacet@sacet-ThinkCentre-M73:~$ gcc -o hello hello.c

sacet@sacet-ThinkCentre-M73:~$ ./example

We are in Example..c

PID of Example.c = 11830

I am the parent, my id is 11830 ,the child is 11831.

I am the child.

We are in Hello.c

PID of hello.c = 11831

Arguments passed from parent process are:

Argument 0 = Hello

Argument 1 = C

Argument 2 = Programming

End of process 11831: The process ended with exit(0).


sacet@sacet-ThinkCentre-M73:~$
3. Write a program to implement first fit, best fit and worst fit algorithm for memory
management.

FIRST-FIT

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2

Enter the size of the


blocks:-Block 1: 5
Block 2: 2
Block 3: 7

Enter the size of the files:-


File 1: 1
File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3

b) Best-fit

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;

lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enter the number of blocks:
3Enter the number of files: 2

Enter the size of the blocks:-


Block 1: 5
Block 2: 2
Block 3: 7

Enter the size of the files:-


File 1: 1
File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
c) Worst-fit
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{

for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enter the number of
blocks: 3 Enter the
number of files: 2

Enter the size of the


blocks:-Block 1: 5
Block 2: 2
Block 3: 7

Enter the size of the


files:-File 1: 1
File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment
1 1 3 7 6
2 4 1 5 1
4. Simulate Bankers Algorithm for Dead Lock Avoidance

#include <stdio.h>

int arrmax[100][100];

int alloc[100][100];

int need[100][100];

int avail[100];

int n, r;

void input()

int i, j;

printf("Enter the no of Processes\t");

scanf("%d",&n);

printf("Enter the no of resource instances\t");

scanf("%d",&r);

printf("Enter the Max Matrix\n");

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

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

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

printf("Enter the Allocation Matrix\n");

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

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

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

printf("Enter the available Resources\n");

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

scanf("%d",&avail[j]);

void show()

int i, j;

printf("Process\t Allocation\t Max\t Available\t");

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

printf( "\nP%d\t",i + 1);

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

printf("%d ",alloc[i][j]);

printf("\t\t");

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

printf("%d ",arrmax[i][j]);
}

printf("\t ");

if (i == 0)

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

printf("%d ",avail[j]);

void cal()

int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;

int dead[100];

int safe[100];

int i, j;

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

finish[i] = 0;

//find need matrix

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

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

need[i][j] = arrmax[i][j] - alloc[i][j];

}
}

while (flag)

flag = 0;

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

int c = 0;

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

if ((finish[i] == 0) && (need[i][j] <= avail[j]))

c++;

if (c == r)

for (k = 0; k < r; k++)

avail[k] += alloc[i][j];

finish[i] = 1;

flag = 1;

//cout<<"\nP%d",i;

if (finish[i] == 1)

i = n;

}
}

j = 0;

flag = 0;

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

if (finish[i] == 0)

dead[j] = i;

j++;

flag = 1;

if (flag == 1)

printf("\n\nSystem is in Deadlock and the Deadlock process are\n");

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

printf("P%d \t",dead[i]);

else

printf("\nNo Deadlock Occur");


}

int main()

int i, j;

printf("********** Deadlock Detection and Avoidance Algorithm ************\n");

input();

show();

cal();

return 0;

OUTPUT 1:

********** Deadlock Detection and Avoidance Algorithm ************

Enter the no of Processes 3

Enter the no of resource instances 3

Enter the Max Matrix

368

433

344

Enter the Allocation Matrix

333

203

124
Enter the available Resources

120

Process Allocation Max Available

P1 333 368 120

P2 203 433

P3 124 344

System is in Deadlock and the Deadlock process are

P0 P1 P2

OUTPUT 2:

********** Deadlock Detection and Avoidance Algorithm ************

Enter the no of Processes 5

Enter the no of resource instances 3

Enter the Max Matrix

753

322

902

222

433

Enter the Allocation Matrix

010

302

302

211

002
Enter the available Resources

230

Process Allocation Max Available

P1 010 753 230

P2 302 322

P3 302 902

P4 211 222

P5 002 433

No Deadlock Occur
5. Simulate Bankers Algorithm for Dead Lock Prevention

#include<stdio.h>

#include<conio.h>

void main()

int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15];

int pno,rno,i,j,prc,count,t,total;

count=0;

clrscr();

printf("\n Enter number of process:");

scanf("%d",&pno);

printf("\n Enter number of resources:");

scanf("%d",&rno);

for(i=1;i<=pno;i++)

flag[i]=0;

printf("\n Enter total numbers of each resources:");

for(i=1;i<= rno;i++)

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

printf("\n Enter Max resources for each process:");

for(i=1;i<= pno;i++)

printf("\n for process %d:",i);

for(j=1;j<= rno;j++)

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

printf("\n Enter allocated resources for each process:");

for(i=1;i<= pno;i++)

printf("\n for process %d:",i);

for(j=1;j<= rno;j++)

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

printf("\n available resources:\n");

for(j=1;j<= rno;j++)

avail[j]=0;

total=0;

for(i=1;i<= pno;i++)

total+=allocated[i][j];

avail[j]=tres[j]-total;

work[j]=avail[j];

printf(" %d \t",work[j]);

do

for(i=1;i<= pno;i++)

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

need[i][j]=max[i][j]-allocated[i][j];

printf("\n Allocated matrix Max need");

for(i=1;i<= pno;i++)

printf("\n");

for(j=1;j<= rno;j++)

printf("%4d",allocated[i][j]);

printf("|");

for(j=1;j<= rno;j++)

printf("%4d",max[i][j]);

printf("|");

for(j=1;j<= rno;j++)

printf("%4d",need[i][j]);

prc=0;
for(i=1;i<= pno;i++)

if(flag[i]==0)

prc=i;

for(j=1;j<= rno;j++)

if(work[j]< need[i][j])

prc=0;

break;

if(prc!=0)

break;

if(prc!=0)

printf("\n Process %d completed",i);

count++;

printf("\n Available matrix:");

for(j=1;j<= rno;j++)

work[j]+=allocated[prc][j];

allocated[prc][j]=0;
max[prc][j]=0;

flag[prc]=1;

printf(" %d",work[j]);

}while(count!=pno&&prc!=0);

if(count==pno)

printf("\nThe system is in a safe state!!");

else

printf("\nThe system is in an unsafe state!!");

getch();

OUTPUT:

Enter number of process:5

Enter number of resources:3

Enter total numbers of each resources:10 5 7

Enter Max resources for each process:

for process 1:7 5 3

for process 2:3 2 2

for process 3:9 0 2

for process 4:2 2 2

for process 5:4 3 3


Enter allocated resources for each process:

for process 1:0 1 0

for process 2:3 0 2

for process 3:3 0 2

for process 4:2 1 1

for process 5:0 0 2

available resources:

2 3 0

Allocated matrix Max need

0 1 0| 7 5 3| 7 4 3

3 0 2| 3 2 2| 0 2 0

3 0 2| 9 0 2| 6 0 0

2 1 1| 2 2 2| 0 1 1

0 0 2| 4 3 3| 4 3 1

Process 2 completed

Available matrix: 5 3 2

Allocated matrix Max need

0 1 0| 7 5 3| 7 4 3

0 0 0| 0 0 0| 0 0 0

3 0 2| 9 0 2| 6 0 0

2 1 1| 2 2 2| 0 1 1

0 0 2| 4 3 3| 4 3 1

Process 4 completed

Available matrix: 7 4 3

Allocated matrix Max need

0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0

3 0 2| 9 0 2| 6 0 0

0 0 0| 0 0 0| 0 0 0

0 0 2| 4 3 3| 4 3 1

Process 1 completed

Available matrix: 7 5 3

Allocated matrix Max need

0 0 0| 0 0 0| 0 0 0

0 0 0| 0 0 0| 0 0 0

3 0 2| 9 0 2| 6 0 0

0 0 0| 0 0 0| 0 0 0

0 0 2| 4 3 3| 4 3 1

Process 3 completed

Available matrix: 10 5 5

Allocated matrix Max need

0 0 0| 0 0 0| 0 0 0

0 0 0| 0 0 0| 0 0 0

0 0 0| 0 0 0| 0 0 0

0 0 0| 0 0 0| 0 0 0

0 0 2| 4 3 3| 4 3 1

Process 5 completed

Available matrix: 10 5 7

The system is in a safe state!!


6.. Write a C program to simulate page replacement algorithms a) FIFO b) LRU c) LFU

DESCRIPTION Page replacement is basic to demand paging. It completes the separation


between logical memory and physical memory. With this mechanism, an enormous virtual
memory can be provided for programmers on a smaller physical memory. There are many
different page-replacement algorithms. Every operating system probably has its own
replacement scheme.

First In First Out (FIFO): This is the simplest page replacement algorithm. In this algorithm,
the operating system keeps track of all pages in the memory in a queue; the oldest page is in the
front of the queue. When a page needs to be replaced page in the front of the queue is selected
for removal.

Least Recently Used(LRU): In this algorithm, page will be replaced which is least recently
used. If the recent past is used as an approximation of the near future, then the page that has not
been used for the longest period of time can be replaced. This approach is the Least Recently
Used (LRU) algorithm. LRU replacement associates with each page the time of that page's last
use.

Least frequently used (LFU): In the LFU page replacement algorithm, the page with the least
visits in a given period of time is removed. It replaces the least frequently used pages. If the
frequency of pages remains constant, the page that comes first is replaced first.

/*FIFO PAGE REPLACEMENT ALGORITHM*/

#include<stdio.h>

#include<conio.h>

void main()

int i, j, k, f, pf=0, count=0, rs[25], m[10], n;

clrscr();

printf("\n Enter the length of reference string -- ");

scanf("%d",&n);

printf("\n Enter the reference string -- ");

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

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

printf("\n Enter no. of frames -- ");

scanf("%d",&f);

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

m[i]=-1;
printf("\n The Page Replacement Process is -- \n");

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

for(k=0;k<f;k++)

if(m[k]==rs[i])

break;

if(k==f)

m[count++]=rs[i];

pf++;

printf("page %d:\tFrames ---",rs[i]);

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

printf("\t%d",m[j]);

if(k==f)

printf("\tPF No. %d",pf);

printf("\n");

if(count==f)

count=0;

printf("\n The number of Page Faults using FIFO are %d",pf);

getch() ;

INPUT
Enter the length of reference string – 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter no. of frames -- 3
OUTPUT
The Page Replacement Process is –
7 -1 -1 PF No. 1
7 0 -1 PF No. 2
7 0 1 PF No. 3
2 0 1 PF No. 4
2 0 1
2 3 1 PF No. 5
2 3 0 PF No. 6
4 3 0 PF No. 7
4 2 0 PF No. 8
4 2 3 PF No. 9
0 2 3 PF No. 10
0 2 3
0 2 3
0 1 3 PF No. 11
0 1 2 PF No. 12
0 1 2
0 1 2
7 1 2 PF No. 13
7 0 2 PF No. 14
7 0 1 PF No. 15

The number of Page Faults using FIFO are 15


/* LRU PAGE REPLACEMENT ALGORITHM */

#include<stdio.h>

#include<conio.h>

void main()

int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;

clrscr();

printf("Enter the length of reference string -- ");

scanf("%d",&n);

printf("Enter the reference string -- ");

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

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

flag[i]=0;

printf("Enter the number of frames -- ");

scanf("%d",&f);

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

count[i]=0;

m[i]=-1;

printf("\nThe Page Replacement process is -- \n");

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

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

if(m[j]==rs[i])

{
flag[i]=1;

count[j]=next;

next++;

if(flag[i]==0)

if(i<f && m[i]!=-1)

m[i]=rs[i];

count[i]=next;

next++;

else

min=0;

for(j=1;j<f;j++)

if(count[min] > count[j])

min=j;

m[min]=rs[i];

count[min]=next;

next++;

pf++;

printf("Page %d:\t Frames:\t",rs[i]);

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

printf("%d\t", m[j]);

if(flag[i]==0)

printf("PF No. -- %d" , pf);


printf("\n");

printf("\nThe number of page faults using LRU are %d",pf);

getch();

INPUT
Enter the length of reference string -- 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3
0 3 2 1 2 0 1 7 0 1Enter the number of frames -
-3

OUTPUT
The Page Replacement process is --
7 -1 -1 PF No. -- 1
7 0 -1 PF No. -- 2
7 0 1 PF No. -- 3
2 0 1 PF No. -- 4
2 0 1
2 0 3 PF No. -- 5
2 0 3
4 0 3 PF No. -- 6
4 0 2 PF No. -- 7
4 3 2 PF No. -- 8
0 3 2 PF No. -- 9
0 3 2
0 3 2
1 3 2 PF No. -- 10
1 3 2
1 0 2 PF No. -- 11
1 0 2
1 0 7 PF No. -- 12
1 0 7
1 0 7
The number of page faults using LRU are 12
/* LFU PAGE REPLACEMENT ALGORITHM */

#include<stdio.h>
#include<conio.h>
void main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, flag=0, pf=0;
clrscr();
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
printf("\nThe Page Replacement Process is : \n");
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
flag=1;
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
}
printf("\n");
printf("Page: %d\tFrames---\t",rs[i]);
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f&&flag==1)
printf("\tPF No %d",pf);
}
printf("\n\n Total number of page faults -- %d",pf);
getch();
}
INPUT
Enter number of page references -- 10
Enter the reference string -- 123452525143
Enter the available no. of frames 3

OUTPUT
The Page Replacement Process is –

1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
5 2 3
5 2 3
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8

Total number of page faults -- 8


7. Simulate the Following File Allocation Strategies a) Sequenced b) Indexed c) Linked

/* SEQUENTIAL FILE ALLOCATION*/

#include <stdio.h>

#include<stdlib.h>

#include<conio.h>

void main()

int f[50], i, st, len, j, c, k, count = 0;

clrscr();

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

f[i]=0;

printf("Files Allocated are : \n");

x: count=0;

printf(“Enter starting block and length of files: ”);

scanf("%d%d", &st,&len);

for(k=st;k<(st+len);k++)

if(f[k]==0)

count++;

if(len==count)

for(j=st;j<(st+len);j++)

if(f[j]==0)
{
f[j]=1;

printf("%d\t%d\n",j,f[j]);

if(j!=(st+len-1))

printf(" The file is allocated to disk\n");

else

printf(" The file is not allocated \n");

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:

Files Allocated are :

Enter starting block and length of files: 15 4

15 1
16 1
17 1

18 1
The file is allocated to disk

Do you want to enter more file(Yes - 1/No - 0)1

Enter starting block and length of files: 15 2

The file is not allocated

Do you want to enter more file(Yes - 1/No - 0)1

Enter starting block and length of files: 20 3

20 1
21 1

22 1

The file is allocated to disk

Do you want to enter more file(Yes - 1/No - 0)


/* LINKED FILE ALLOCATION*/

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

int f[50], p,i, st, len, j, c, k, a;

clrscr();

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:

Enter how many blocks already allocated: 3

Enter blocks already allocated: 4 7 9


Enter index starting block and length: 2 10

2 >1

3 >1

4 Block is already allocated

5 >1

6 >1

7 Block is already allocated

8 >1

9 Block is already allocated

10 >1

11 >1

12 >1

13 >1

14 >1

Do you want to enter more file(Yes - 1/No – 0)0


INDEXED FILE ALLOCATION

#include<stdio.h>

//#include<conio.h>

#include<stdlib.h>

void main()

int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;

//clrscr();

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:

sacet@sacet-HP-280-Pro-G6-Microtower-PC:~$ gcc indexalloc.c

sacet@sacet-HP-280-Pro-G6-Microtower-PC:~$ ./a.out

Enter the index block: 4

Enter no of blocks needed and no of files for the index 4 on the disk :

12345

Allocated

File Indexed

4 ------->1 : 1

4-------->2 : 1

4-------->3 : 1

4-------->4 : 1

4-------->5 : 1

Do you want to enter more file(Yes - 1/No - 0)1

Enter the index block: 6

Enter no of blocks needed and no of files for the index 6 on the disk :

11

File in the index is already allocated

Enter another file indexed6

Allocated
File Indexed 6 >6 : 1

Do you want to enter more file(Yes - 1/No - 0)1Enter the

index block: 4

4 index is already allocatedEnter the

index block: 9

Enter no of blocks needed and no of files for the index 9 on the disk :2

78

Allocated File Indexed 9

--------->7 : 1

9-------->8 : 1

Do you want to enter more file(Yes - 1/No - 0)


LINUX
PROGRAMMING
Basic Linux Commands

1. ls – Displays information about files in the current directory.


2. pwd – Displays the current working directory.
3. mkdir – Creates a directory.
4. cd – To navigate between different folders.
5. rmdir – Removes empty directories from the directory lists.
6. cp – Moves files from one directory to another.
7. mv – Rename and Replace the files
8. rm – Delete files
9. uname – Command to get basic information about the OS
10. locate– Find a file in the database.
11. touch – Create empty files
12. ln – Create shortcuts to other files
13. cat – Display file contents on terminal
14. clear – Clear terminal
15. ps- Display the processes in terminal
16. man – Access manual for all Linux commands
17. grep- Search for a specific string in an output
18. echo- Display active processes on the terminal
19. wget – download files from the internet
20. whoami- Create or update passwords for existing users
21. sort- sort the file content
22. cal- View Calendar in terminal
23. whereis – View the exact location of any command types after this command
24. df – Check the details of the file system
25. wc – Check the lines, word count, and characters in a file using different options

1. Is command

The ls command is commonly used to identify the files and directories in the working directory. This
command is one of the many often-used Linux commands that you should know.

This command can be used by itself without any arguments and it will provide us the output with all the
details about the files and the directories in the current working directory. There is a lot of flexibility
offered by this command in terms of displaying data in the output. Check the below image for the output.

2. pwd command

The pwd command is mostly used to print the current working directory on your terminal. It is also one of
the most commonly used commands.

Now, your terminal prompt should usually include the entire directory. If it doesn’t, this is a quick
command to see which directory you’re in. Another purpose for this command is when creating scripts
because it can help us find the directory in which the script was saved. The below pictures are the output
with the command.

Command:

Output:

3. mkdir command

This mkdir command allows you to create fresh directories in the terminal itself. The default syntax is
mkdir <directory name> and the new directory will be created.

You can see we used ls first to see the directories present there and then mkdir to create another directory
followed by ls to view the created directories.

4. cd command

The cd command is used to navigate between directories. It requires either the full path or the directory
name, depending on your current working directory. If you run this command without any options, it will
take you to your home folder. Keep in mind that it can only be executed by users with sudo privileges.

5. rmdir command

The rmdir command is used to delete permanently an empty directory. To perform this command the user
running this command must be having sudo privileges in the parent directory.

6. cp command

The cp command of Linux is equivalent to copy-paste and cut-paste in Windows.

Command:

Output:
Here we used ls to view the files and then used cp to copy the files of first.txt to second.txt and again used
ls command to view the updated files.

7. mv command

The mv command is generally used for renaming the files in Linux.

Command:

Output:

Here we used the ls command to check the directories and then used mv <file name> <Renamed file
name> to rename the files, and then again we used the ls command to view the renamed file as you can see
in the output screenshot.

8. rm command

rm command in Linux is generally used to delete the files created in the directory.

Command:

Output:

You can see as we wrote the ls command to view the files in the terminal and then rm <file name> to
delete the files and again we had the ls command to check the update.
9. uname command

The uname command is used to check the complete OS information of the system. Check out the
command and the output below

Command:

Output:

10. locate command

The locate command is generally used to locate the files in the database. Use an asterisk (*) to search for
content that contains two or more words. As an example: locate first*file. This command will search the
database for the files that contain these two names first and file.

Command:

Output:

We first used the rm command to delete the file and then used locate command to find the file in the
database which in return has given the output with a -e as the file was removed.

11. touch command

The touch command creates an empty file when put in the terminal in this format as touch <file name>

Command:

We used the ls command to check the current directories in the terminal and then used the touch command
to create an empty file and then again we used ls to find out the created file in the terminal.
12. ln command

The ln command is used to create a shortcut link to another file. This is among the most important Linux
commands to know if you want to operate as a Linux administrator.

Command:

Output:

Here we used mkdir to create two directories and then we used ln with an -s to create a soft link in it.

13. cat command

The cat command is the simplest command to use when you want to see the contents of a particular
file. The only issue is that it simply unloads the entire file to your terminal. If you want to navigate around a
huge file, should use less command alternatively.

Command:

Output:

14. clear command

The clear command is a standard command to clear the terminal screen.


Command: *This was the terminal before the command.

Output:

15. ps command

ps command in Linux is used to check the active processes in the terminal.

Command:

Output:
16. man command

The man command displays a user manual for any commands or utilities available in the Terminal,
including their name, description, and options.

Command to view the full manual:

man <command name>

For example, suppose you want to look up the manual for the ls command: man ls

Command:

Output:

17. grep command

The grep command is used to find a specific string in a series of outputs. For example, if you want to find
a string in a file, you can use the syntax: <Any command with output> | grep “<string to find> “

For Example:

cat Files.txt | grep “new”

Command:

Output:

In this command, we first used cat <file name> to view the content of the file, and then we used cat <file
name> | grep “string” to check the string in it.

18. echo command

echo command in Linux is specially used to print something in the terminal


Command:

Output:

19. wget command

The wget command in the Linux command line allows you to download files from the internet. It runs in
the background and does not interfere with other processes.

Here is the basic syntax: wget [option] [url]

Command:

wget http://sample.com/sample-menu.php

Output:

20. whoami command

The whoami command provides basic information that is extremely useful when working on multiple
systems. In general, if you are working with a single computer, you will not require it as frequently as a
network administrator.

Command:
Output:

21. sort command

The sort command is used generally to sort the output of the file.

22. cal command

The cal command is not the most famous command in the terminal but it functions to view the calendar for
a particular month in the terminal. Let’s see how this works.

Command:

Output:

23. whereis command

whereis command in Linux is generally used to see the exact location of any command typed after this.
Let’s see how this performs.

Command:

Output:

24. df command

df command in Linux gets the details of the file system.

Command:
Output:

Here we have used df -h as simply typing df will return the output in bytes which is not readable, so we
add -h to make the outputs more readable and understandable.

25. wc command

wc command in Linux indicates the number of words, characters, lines, etc using a set of options.

 wc -w shows the number of words


 wc -l shows the number of lines
 wc -m shows the number of characters present in a file

Let’s see one example of these options

Command:

Output:

Here we used the touch command to create a text file and then used the echo command to input a sentence
that contains six words and we used the wc -w command to calculate the number of words in it.
Study of Vi Editor

Introduction:

vi is a Full screen editor.”vi” editor supports cryptic, mnemonic and internal commands. Linux
environment supports Vim (improved) editor, which is the improved flavor of vi editor. We can enter
into the vi editor by typing the following command at the prompt.

Syntax: vi filename

Example: vi testfile

Once the user hit the command , it opens the vi session for testfile

You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line.

If a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or
someother non viewable character present.

vi editor supports 3 modes of operations

1. Command mode

2. Input mode

3. Last line mode (or) ex mode


Command mode:-

This mode helps the user to supply the commands which acts on text. In this mode pressing a key
doesn’t show any characters on the screen, but it may perform an action like moving the cursor to
the next line or deleting a line. Command mode will not allow the user to enter or edit the text.
‘Spacebar’and ‘backspace’ are the examples of command mode operation.

Input Mode:-

This mode allows the user to enter the text or modify the text. To come out from this mode, the
userhas to enter the esc key.

Last line mode (or) ex Mode:-

Last line mode helps the user to enter the commands at the bottom of the editor, which is useful
tomake a global substitution in the file.

Relationship between three modes:


Last line Mode Commands:-

Commands for Inserting and Replacing Text:-


The following commands changes the default command mode to input mode.
Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system.

SHELL:

A shell acts as a centre person or interface between a user and a LINUX operating system.
Basically, a shell is considered as a preprocessor, its task is to execute the commands or instructions
given to the system.

There are two major parts of a shell: The interpreter and programming capability. The
interpreter reads user commands and works with the kernel to execute them. The programming
capability allows the user to write shell script. A shell script (or a shell program) is a file that
performsa useful function. Some standard UNIX shells are shown in the following figure:

A Shell is a program that plays a role of mediator between client and the LINUX operating
system. This mediator takes the command given by the user, analyses it, understand s it and finally
processes the commands to produce the desired result. To obtain desired results its upto the
knowledge of the user to give the accurate command to the system. The outcome is completely
dependent on the user’s command, the task of the shell is just to analyze the command and execute
it.

Advantages:-
1. Easy to use
2. Quick start and interactive debugging.
3. Time Saving.
4. System Admin task automation.
5. Shell scripts can execute without any additional effort on nearly any modern UNIX / Linux / BSD /
Mac OS X operating system as they are written an interpreted language.

Shell Types:

There are many types of shells .The mostly common UNIX shells are as follows.

1. Bourne shell (sh)


2. C shell (csh)
3. TC Shell (tcsh)
4. Korn Shell (ksh)
5. Bourne again Shell (bash)

Bourne Shell (sh):-

The Bourne shell is the original shell developed by Steve Bourne at the AT&T labs. This shell
does not have the interactive facilities provided by C shell and the korn shell. However, it provides an
easy to use language to write shell scripts.

C Shell (csh):-

The C shell is created by an undergraduate student William Joy at the University of California
at Berkeley. It provides two features over the Bourne shell. First, an aliasing of commands by this
feature we can rename the commands. This is useful when the commands are very lengthy and
required frequently. Second, a Command History Feature with this feature we can recall previously
executed commands and save the time. Since, the C shell keeps track of all commands typed at the
command line. In addition, it provides a C-like language to write the shell scripts. Due to this fact it is
names as C Shell.
Bourne again Shell (bash):-

The Bourne again (bash) shell is created by the Free software Foundation under their GNUinitiative.
It has all the interactive features of the C and the korn shells together with a shell programming
language similar to that of the original Bourne shell.

The following table shows the Differences among shells

Features Bourne shell C shell Bash

Shell Scripts YES YES YES

Command History NO YES YES

Command Alias NO YES YES

Job Control NO YES YES

Filename Completion NO Not Default YES


Command-line Editing NO NO YES

An entry “Not default” means the shell provides the feature but it is not the default setting for
thisshell.

Study of UNIX File System

File System:-
A file system is a logical collection of files on a partition or disk. A partition is a container for
information and can span an entire hard drive if desired. Your hard drive can have various partitions
which usually contains only one file system, such as one file system housing the / file system or
another containing the /home file system. One file system per partition allows for the logical
maintenance and management of differing file systems.

UNIX File System:-


UNIX file structure (or UNIX file system) plays a vital role in understanding most of the other
UNIX commands. UNIX treats all of its utilities in the form of a “file”. This file structure when
pictorially represented, resembles, an upside down tree, with root directory claiming the
peak positionand various other sub-directories spreaded below it. A snapshot of such
representation is as follows.

Directory:
A directory is nothing but a collection of related files hence it is no wrong in calling
a directoryas a file.

Fig: UNIX Directory (or File structure)


Directory Description
/ This is the root directory which should contain only the directories needed at the
toplevel of the file structure.
/bin This is where the executable files are located. They are available to all users.

/dev These are device drivers.dev is short notation of the word “devices”. Hence input/output
devices relative files are stored under this directory.
/etc Supervisor directory commands, configuration files, disk configuration files, valid user
lists, groups, Ethernet, hosts, where to send critical messages.
/home
Contains the home directory for users and other accounts.
/lib
Contains shared library files and sometimes other kernel-related files.
/mnt Used to mount other temporary file systems, such as cdrom and floppy for the CD-ROM
drive and floppy diskette drive, respectively.
/proc Contains all processes marked as a file by process number or other information that is
dynamic to the system.
/sbin Contains binary (executable) files, usually for system administration. For
example fdisk and ifconfig utlities.
/tmp Holds temporary files used between system boots
/usr Used for miscellaneous purposes, or can be used by many users. Includes administrative
commands, shared files, library files, and others
1. Write a Shell program to check whether given number is prime
or not.

#!/bin/bash
echo -e "Enter Number : \c"
read n
for((i=2; i<=$n/2; i++))
do
ans=$(( n%i ))
if [ $ans -eq 0 ]
then
echo "$n is not a prime number."
exit 0
fi
done
echo "$n is a prime number."

OUTPUT 1:

Enter Number : 25

25 is not a prime number.

OUTPUT 2:

Enter Number : 29

29 is a prime number.
2. Write a shell script which will display Fibonacci series up to the given range

#! /bin/bash
echo "Enter the value of n"
read n
a=0
b=1
count=2
echo "Fibonacci series:"
echo $a
echo $b
while [ $count -le $n ]
do
fib=`expr $a + $b`
a=$b
b=$fib
echo $fib
count=`expr $count + 1`
done

OUTPUT:

Enter the value of n

Fibonacci series:

8
3. Write a shell script to check whether the given number is Armstrong or not.

echo "Enter the number"


read n
function ams
{
t=$n
s=0
b=0
c=10
while [ $n -gt $b ]
do
r=$((n % c))
i=$((r * r * r))
s=$((s + i))
n=$((n / c))
done
echo $s
if [ $s == $t ]
then
echo "Amstrong number"
else
echo "Not an Armstrong number"
fi
}
result=`ams $n`
echo "$result"

OUTPUT:

Enter the number

153

153

Amstrong number
4. Write a shell script to the calculate the value of NCR

echo “ Enter values for n and r”


read n
read r
t=0
i=1
a=1
b=1
c=1
d=0
while [ $i –le $n ]
do
a=`expr $a \* $i`
i=`expr $i + 1`
done
i=1
while [ $i –le $r ]
do
b=`expr $b \* $i`
i=`expr $i + 1`
done
i=1
d=`expr $n - $r`
while [ $i –le $d ]
do
c=`expr $c \* $i`
i=`expr $i + 1`
done
i=`expr $b \* $c`
t=`expr $a / $i`
echo “ The Result Is = $t”
5.Write a shell script to accept student number, name, marks in 5 subjects.
6. Find total, average and grade

clear
echo -----------------------------------
echo '\tStudent Mark List'
echo -----------------------------------
echo Enter the Student name
read name
echo Enter the Register number
read rno
echo Enter the Mark1
read m1
echo Enter the Mark2
read m2
echo Enter the Mark3
read m3
echo Enter the Mark4
read m4
echo Enter the Mark5
read m5
tot=$(expr $m1 + $m2 + $m3 + $m4 + $m5)
avg=$(expr $tot / 5)
echo -----------------------------------
echo '\tStudent Mark List'
echo -----------------------------------
echo "Student Name : $name"
echo "Register Number : $rno"
echo "Mark1 : $m1"
echo "Mark2 : $m2"
echo "Mark3 : $m3"
echo "Mark4 : $m4"
echo "Mark5 : $m5"
echo "Total : $tot"
echo "Average : $avg"
if [ $m1 -ge 35 ] && [ $m2 -ge 35 ] && [ $m3 -ge 35 ] && [ $m4 -ge 35 ] && [ $m5 -ge 35 ]
then
echo "Result : Pass"
if [ $avg -ge 90 ]
then
echo "Grade : S"
elif [ $avg -ge 80 ]
then
echo "Grade : A"
elif [ $avg -ge 70 ]
then
echo "Grade : B"
elif [ $avg -ge 60 ]
then
echo "Grade : C"
elif [ $avg -ge 50 ]
then
echo "Grade : D"
elif [ $avg -ge 35 ]
then
echo "Grade : E"
fi
else
echo "Result : Fail"
fi
echo -----------------------------------

OUTPUT:
-----------------------------------
Student Mark List
-----------------------------------
Enter the Student name
Ram
Enter the Register number
21F01F0060
Enter the Mark1
76
Enter the Mark2
78
Enter the Mark3
67
Enter the Mark4
90
Enter the Mark5
80
-----------------------------------
Student Mark List
-----------------------------------
Student Name : Ram
Register Number : 21F01F0060
Mark1 : 76
Mark2 : 78
Mark3 : 67
Mark4 : 90
Mark5 : 80
Total : 391
Average : 78
Result : Pass
Grade :B
-----------------------------------
7.Write a shell script to find minimum and maximum elements in the given list of
elements.

#!/bin/bash
echo "enter size of an array"
read n
#taking input from user
for((i=0;i<n;i++))
do
echo " enter $((i+1)) number"
read nos[$i]
done
#printing the entered numbers
echo "number entered are"
for((i=0;i<n;i++))
do
echo ${nos[$i]}
done
#main loop
small=${nos[0]}
greatest=${nos[0]}
for((i=0;i<n;i++))
do
#logic for smallest number
if [ ${nos[$i]} -lt $small ]; then
small=${nos[$i]}
#logic for greatest number
elif [ ${nos[$i]} -gt $greatest ]; then
greatest=${nos[$i]}
fi
done
#printing smallest and greatest number
echo "smallest number in an array is $small"
echo "greatest number in an array is $greatest"
8.Write a shell program to check whether the given 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

Output 1

Enter a String
test
test is not palindrome

Output 2

Enter a String
malayalam
malayalam is palindrome
9. Write an awk program to print sum, avg of students marks list

#!/bin/awk

# Begin

BEGIN {

FS=" ";

# Dev

name[NR] = $1;

sum[NR] = $2 + $3 + $4 +$5 + $6;

average[NR] = sum[NR] / 5;

# End

END {

print "We saw " FNR " students, their marks, sum and average are:\n";

i = 1;

while (i <= FNR) {

printf("%-10s %d %d %d %d %d %d %.2f\n", name[i] , $2, $3, $4, $5, $6, sum[i], average[i++]);

}
10. Write a shell script to compute no. of characters and words in each line of given file
OUTPUT:
11. Write a shell script to check whether the given input is a number or a string

You might also like