OS & Inux Lab Manual for Print (1)
OS & Inux Lab Manual for Print (1)
OS & Inux Lab Manual for Print (1)
List of Experiments
1. Simulate the following CPU scheduling algorithms: a. Round Robin (b) SJF (c)
FCFS (d) Priority
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
OUTPUT
The Average Turnaround time is – 15.666667
The Average Waiting time is -- 5.666667
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]);
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
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
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];
}
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
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;
pid = fork();
if (pid == 0) {
execv("./hello", args);
if (pid > 0) {
pid = wait(&status);
if (WIFEXITED(status)) {
if (WIFSIGNALED(status)) {
if (pid < 0) {
perror("In fork():");
exit(0);
hello.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int i;
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:~$ ./example
We are in Example..c
I am the child.
We are in Hello.c
Argument 0 = Hello
Argument 1 = C
Argument 2 = Programming
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
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
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
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;
scanf("%d",&n);
scanf("%d",&r);
scanf("%d",&arrmax[i][j]);
{
for (j = 0; j < r; j++)
scanf("%d",&alloc[i][j]);
scanf("%d",&avail[j]);
void show()
int i, j;
printf("%d ",alloc[i][j]);
printf("\t\t");
printf("%d ",arrmax[i][j]);
}
printf("\t ");
if (i == 0)
printf("%d ",avail[j]);
void cal()
int dead[100];
int safe[100];
int i, j;
finish[i] = 0;
}
}
while (flag)
flag = 0;
int c = 0;
c++;
if (c == r)
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
//cout<<"\nP%d",i;
if (finish[i] == 1)
i = n;
}
}
j = 0;
flag = 0;
if (finish[i] == 0)
dead[j] = i;
j++;
flag = 1;
if (flag == 1)
printf("P%d \t",dead[i]);
else
int main()
int i, j;
input();
show();
cal();
return 0;
OUTPUT 1:
368
433
344
333
203
124
Enter the available Resources
120
P2 203 433
P3 124 344
P0 P1 P2
OUTPUT 2:
753
322
902
222
433
010
302
302
211
002
Enter the available Resources
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();
scanf("%d",&pno);
scanf("%d",&rno);
for(i=1;i<=pno;i++)
flag[i]=0;
for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
for(i=1;i<= pno;i++)
for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}
for(i=1;i<= pno;i++)
for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);
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];
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)
count++;
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)
else
getch();
OUTPUT:
available resources:
2 3 0
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
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
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
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
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
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.
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
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++;
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\n");
if(count==f)
count=0;
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
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
flag[i]=0;
scanf("%d",&f);
for(i=0;i<f;i++)
count[i]=0;
m[i]=-1;
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)
m[i]=rs[i];
count[i]=next;
next++;
else
min=0;
for(j=1;j<f;j++)
min=j;
m[min]=rs[i];
count[min]=next;
next++;
pf++;
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
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
#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x: count=0;
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))
else
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
OUTPUT:
15 1
16 1
17 1
18 1
The file is allocated to disk
20 1
21 1
22 1
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
clrscr();
for(i=0;i<50;i++)
f[i]=0;
scanf("%d",&p);
for(i=0;i<p;i++)
scanf("%d",&a);
f[a]=1;
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;
else
k++;
else
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
OUTPUT:
2 >1
3 >1
5 >1
6 >1
8 >1
10 >1
11 >1
12 >1
13 >1
14 >1
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
void main()
//clrscr();
for(i=0;i<50;i++)
f[i]=0;
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
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++)
else
goto y;
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
//getch();
OUTPUT:
sacet@sacet-HP-280-Pro-G6-Microtower-PC:~$ ./a.out
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
Enter no of blocks needed and no of files for the index 6 on the disk :
11
Allocated
File Indexed 6 >6 : 1
index block: 4
index block: 9
Enter no of blocks needed and no of files for the index 9 on the disk :2
78
--------->7 : 1
9-------->8 : 1
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
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
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:
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.
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.
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:
Output:
15. ps command
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.
For example, suppose you want to look up the manual for the ls command: man ls
Command:
Output:
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:
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.
Output:
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.
Command:
wget http://sample.com/sample-menu.php
Output:
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:
The sort command is used generally to sort the output of the file.
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:
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
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.
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.
1. Command mode
2. Input 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 helps the user to enter the commands at the bottom of the editor, which is useful
tomake a global substitution in the file.
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.
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.
An entry “Not default” means the shell provides the feature but it is not the default setting for
thisshell.
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.
Directory:
A directory is nothing but a collection of related files hence it is no wrong in calling
a directoryas a file.
/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
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:
Fibonacci series:
8
3. Write a shell script to check whether the given number is Armstrong or not.
OUTPUT:
153
153
Amstrong number
4. Write a shell script to the calculate the value of NCR
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;
average[NR] = sum[NR] / 5;
# End
END {
print "We saw " FNR " students, their marks, sum and average are:\n";
i = 1;
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