Real Time System Lab

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

Implement the following programs on ONLINE GDB platform using C

language.

Link: https://www.onlinegdb.com/#

List of Experiments

Unit-1

1. Implementation of CPU scheduling. a) Round Robin b) SJF c) FCFS d)


Priority
2. Implement producer Consumer problem using semaphore.
3. Write a C program to simulate the following file allocation strategies. a)
Sequential b) Indexed c) Linked
4. Write a C program to simulate the concept of Dining-Philosopher’s
problem

Unit-2
5. Write a C program to simulate Banker’s algorithm for the purpose of
deadlock avoidance
6. Write a C program to simulate disk scheduling algorithms a) FCFS b)
SCAN
7. Write C programs to simulate UNIX commands like ls, grep, etc.

Unit-3
8. A program to simulate the MVT.
9. A Program to simulate the MFT
10. A program to simulate LRU Page Replacement Algorithm

2
EXPERIMENT 1

1.1 OBJECTIVE
Write a C program to simulate the following non-preemptive CPU scheduling algorithms to find turnaround time
and waiting time for the above problem.
a) FCFS b) SJF c) Round Robin d) Priority

1.2 DESCRIPTION
Assume all the processes arrive at the same time.

1.2.1 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.

1.2.2 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.

1.2.3 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.

1.2.4 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.

1.3 PROGRAM

1.3.1 FCFS CPU SCHEDULING ALGORITHM


#include<stdio.h>
#include<conio.h>
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");

3
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

1.3.2 SJF CPU SCHEDULING ALGORITHM


#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++)
4
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

1.3.3 ROUND ROBIN CPU SCHEDULING ALGORITHM


#include<stdio.h>
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];
5
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

1.3.4 PRIORITY CPU SCHEDULING ALGORITHM


#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];
6
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

7
EXPERIMENT 3

3.1 OBJECTIVE
Write a C program to simulate the following file allocation strategies.
a) Sequential b) Linked c) ) Indexed

3.2 DESCRIPTION
A file is a collection of data, usually stored on disk. As a logical entity, a file enables to divide data into
meaningful groups. As a physical entity, a file should be considered in terms of its organization. The term "file
organization" refers to the way in which data is stored in a file and, consequently, the method(s) by which it can
be accessed.

3.2.1 SEQUENTIAL FILE ALLOCATION


In this file organization, the records of the file are stored one after another both physically and logically. That is,
record with sequence number 16 is located just after the 15th record. A record of a sequential file can only be
accessed by reading all the previous records.

3.2.2 LINKED FILE ALLOCATION


With linked allocation, each file is a linked list of disk blocks; the disk blocks may be scattered anywhere on the
disk. The directory contains a pointer to the first and last blocks of the file. Each block contains a pointer to the
next block.

3.2.3 INDEXED FILE ALLOCATION


Indexed file allocation strategy brings all the pointers together into one location: an index block. Each file has its
own index block, which is an array of disk-block addresses. The ith entry in the index block points to the ith block
of the file. The directory contains the address of the index block. To find and read the i th block, the pointer in the
ith index-block entry is used.

3.3 PROGRAM

3.3.1 SEQUENTIAL FILE ALLOCATION


#include<stdio.h>
#include<conio.h>

struct fileTable
{
char name[20];
int sb, nob;
}ft[30];

void main()
{
int i, j, n;
char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
}
printf("\nEnter the file name to be searched -- ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
9
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED\n");
printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].sb+j);
}
getch();
}

INPUT:
Enter no of files :3

Enter file name 1 :A


Enter starting block of file 1 :85
Enter no of blocks in file 1 :6

Enter file name 2 :B


Enter starting block of file 2 :102
Enter no of blocks in file 2 :4

Enter file name 3 :C


Enter starting block of file 3 :60
Enter no of blocks in file 3 :4
Enter the file name to be searched -- B

OUTPUT:
FILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED
B 102 4 102, 103, 104, 105

3.3.2 LINKED FILE ALLOCATION


#include<stdio.h>
#include<conio.h>

struct fileTable
{
char name[20];
int nob;
struct block *sb;
}ft[30];

struct block
{
int bno;
struct block *next;
};

void main()
{
int i, j, n;
char s[20];
struct block *temp;
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
10
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
ft[i].sb=(struct block*)malloc(sizeof(struct block));
temp = ft[i].sb;
printf("Enter the blocks of the file :");
scanf("%d",&temp->bno);
temp->next=NULL;

for(j=1;j<ft[i].nob;j++)
{
temp->next = (struct block*)malloc(sizeof(struct block));
temp = temp->next;
scanf("%d",&temp->bno);
}
temp->next = NULL;
}
printf("\nEnter the file name to be searched -- ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME NO OF BLOCKS BLOCKS OCCUPIED");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
temp=ft[i].sb;
for(j=0;j<ft[i].nob;j++)
{
printf("%d → ",temp->bno);
temp = temp->next;
}
}
getch();
}

INPUT:
Enter no of files 2

Enter file 1 : A
Enter no of blocks in file 1 4
Enter the blocks of the file 1 : 12 23 9 4

Enter file 2 : G
Enter no of blocks in file 2 5
Enter the blocks of the file 2 88 77 66 55 44

Enter the file to be searched : G

OUTPUT:
FILE NAME NO OF BLOCKS BLOCKS OCCUPIED
G 5 88 → 77→ 66→ 55→ 44

3.3.3 INDEXED FILE ALLOCATION


#include<stdio.h>
#include<conio.h>

struct fileTable
{
char name[20];
int nob, blocks[30];
11
}ft[30];

void main()
{
int i, j, n;
char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
printf("Enter the blocks of the file :");
for(j=0;j<ft[i].nob;j++)
scanf("%d",&ft[i].blocks[j]);
}

printf("\nEnter the file name to be searched -- ");


scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME NO OF BLOCKS BLOCKS OCCUPIED");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].blocks[j]);
}
getch();
}

INPUT:
Enter no of files 2

Enter file 1 : A
Enter no of blocks in file 1 4
Enter the blocks of the file 1 : 12 23 9 4

Enter file 2 : G
Enter no of blocks in file 2 5
Enter the blocks of the file 2 88 77 66 55 44
Enter the file to be searched : G

OUTPUT:
FILE NAME NO OF BLOCKS BLOCKS OCCUPIED
G 5 88, 77, 66, 55, 44

12
EXPERIMENT 8 & 9
4.1 OBJECTIVE
Write a C program to simulate the MVT and MFT memory management techniques

4.2 DESCRIPTION
MFT (Multiprogramming with a Fixed number of Tasks) is one of the old memory management techniques in
which the memory is partitioned into fixed size partitions and each job is assigned to a partition. The memory
assigned to a partition does not change. MVT (Multiprogramming with a Variable number of Tasks) is the
memory management technique in which each job gets just the amount of memory it needs. That is, the
partitioning of memory is dynamic and changes as jobs enter and leave the system. MVT is a more ``efficient''
user of resources. MFT suffers with the problem of internal fragmentation and MVT suffers with external
fragmentation.

4.3 PROGRAM

4.3.1 MFT MEMORY MANAGEMENT TECHNIQUE


#include<stdio.h>
#include<conio.h>

main()
{
int ms, bs, nob, ef,n, mp[10],tif=0;
int i,p=0;

clrscr();
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes)-- ",i+1);
scanf("%d",&mp[i]);
}

printf("\nNo. of Blocks available in memory -- %d",nob);


printf("\n\nPROCESS\tMEMORY REQUIRED\t ALLOCATED\tINTERNAL FRAGMENTATION");
for(i=0;i<n && p<nob;i++)
{
printf("\n %d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\t\tNO\t\t---");
else
{
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemory is Full, Remaining Processes cannot be accomodated");

printf("\n\nTotal Internal Fragmentation is %d",tif);


printf("\nTotal External Fragmentation is %d",ef);
getch();

13
}

INPUT
Enter the total memory available (in Bytes) -- 1000
Enter the block size (in Bytes)-- 300
Enter the number of processes – 5
Enter memory required for process 1 (in Bytes) -- 275
Enter memory required for process 2 (in Bytes) -- 400
Enter memory required for process 3 (in Bytes) -- 290
Enter memory required for process 4 (in Bytes) -- 293
Enter memory required for process 5 (in Bytes) -- 100

No. of Blocks available in memory -- 3

OUTPUT
PROCESS MEMORY REQUIRED ALLOCATED INTERNAL FRAGMENTATION
1 275 YES 25
2 400 NO -----
3 290 YES 10
4 293 YES 7

Memory is Full, Remaining Processes cannot be accommodated


Total Internal Fragmentation is 42
Total External Fragmentation is 100

4.3.2 MVT MEMORY MANAGEMENT TECHNIQUE


#include<stdio.h>
#include<conio.h>

main()
{
int ms,mp[10],i, temp,n=0;
char ch = 'y';

clrscr();
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);

printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");


for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
14
getch();
}

INPUT
Enter the total memory available (in Bytes) -- 1000

Enter memory required for process 1 (in Bytes) -- 400


Memory is allocated for Process 1
Do you want to continue(y/n) -- y

Enter memory required for process 2 (in Bytes) -- 275


Memory is allocated for Process 2
Do you want to continue(y/n) -- y

Enter memory required for process 3 (in Bytes) -- 550

OUTPUT
Memory is Full
Total Memory Available -- 1000

PROCESS MEMORY ALLOCATED


1 400
2 275

Total Memory Allocated is 675


Total External Fragmentation is 325

15
EXPERIMENT 5

8.1 OBJECTIVE
Write a C program to simulate Banker’s algorithm for the purpose of deadlock avoidance.

8.2 DESCRIPTION
In a multiprogramming environment, several processes may compete for a finite number of resources. A process
requests resources; if the resources are not available at that time, the process enters a waiting state.
Sometimes, a waiting process is never again able to change state, because the resources it has requested are
held by other waiting processes. This situation is called a deadlock. Deadlock avoidance is one of the techniques
for handling deadlocks. This approach requires that the operating system be given in advance additional
information concerning which resources a process will request and use during its lifetime. With this additional
knowledge, it can decide for each request whether or not the process should wait. To decide whether the
current request can be satisfied or must be delayed, the system must consider the resources currently available,
the resources currently allocated to each process, and the future requests and releases of each process.
Banker’s algorithm is a deadlock avoidance algorithm that is applicable to a system with multiple instances of
each resource type.

8.3 PROGRAM
#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
clrscr();
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);

printf("\nEnter New Request Details -- ");


printf("\nEnter pid \t -- \t");
scanf("%d",&id);
printf("Enter Request for Resources \t -- \t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
29
avail[i]=avail[i] - newr;
}

for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}
if(g==0)
{
printf("\n REQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
30
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
getch();
}

INPUT
Enter number of processes – 5
Enter number of resources -- 3
Enter details for P0
Enter allocation -- 0 1 0
Enter Max -- 7 5 3

Enter details for P1


Enter allocation -- 2 0 0
Enter Max -- 3 2 2

Enter details for P2


Enter allocation -- 3 0 2
Enter Max -- 9 0 2

Enter details for P3


Enter allocation -- 2 1 1
Enter Max -- 2 2 2

Enter details for P4


Enter allocation -- 0 0 2
Enter Max -- 4 3 3

Enter Available Resources -- 3 3 2


Enter New Request Details --
Enter pid -- 1
Enter Request for Resources -- 1 0 2

OUTPUT
P1 is visited( 5 3 2)
P3 is visited( 7 4 3)
P4 is visited( 7 4 5)
P0 is visited( 7 5 5)
P2 is visited( 10 5 7)
SYSTEM IS IN SAFE STATE
The Safe Sequence is -- (P1 P3 P4 P0 P2 )

Process Allocation Max Need


P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

31
EXPERIMENT 6

9.1 OBJECTIVE
*Write a C program to simulate disk scheduling algorithms
a) FCFS b) SCAN c) C-SCAN

9.2 DESCRIPTION
One of the responsibilities of the operating system is to use the hardware efficiently. For the disk drives, meeting
this responsibility entails having fast access time and large disk bandwidth. Both the access time and the
bandwidth can be improved by managing the order in which disk I/O requests are serviced which is called as disk
scheduling. The simplest form of disk scheduling is, of course, the first-come, first-served (FCFS) algorithm. This
algorithm is intrinsically fair, but it generally does not provide the fastest service. In the SCAN algorithm, the disk
arm starts at one end, and moves towards the other end, servicing requests as it reaches each cylinder, until it
gets to the other end of the disk. At the other end, the direction of head movement is reversed, and servicing
continues. The head continuously scans back and forth across the disk. C-SCAN is a variant of SCAN designed to
provide a more uniform wait time. Like SCAN, C-SCAN moves the head from one end of the disk to the other,
servicing requests along the way. When the head reaches the other end, however, it immediately returns to the
beginning of the disk without servicing any requests on the return trip

9.3 PROGRAM

9.3.1 FCFS DISK SCHEDULING ALGORITHM


#include<stdio.h>
main()
{
int t[20], n, I, j, tohm[20], tot=0;
float avhm;
clrscr();
printf(“enter the no.of tracks”);
scanf(“%d”,&n);
printf(“enter the tracks to be traversed”);
for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”);
for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+);
printf("\nAverage header movements:%f",avhm);
getch();
}

INPUT
Enter no.of tracks:9
Enter track position:55 58 60 70 18 90 150 160 184

OUTPUT
Tracks traversed Difference between tracks
55 45
58 3
60 2
70 10
18 52
90 72
32
150 60
160 10
184 24

Average header movements:30.888889

9.3.2 SCAN DISK SCHEDULING ALGORITHM


#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;

clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter the tracks");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{ if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
} } }
for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;k=i;
p=0;
while(t[j]!=0)
{
atr[p]=t[j];
j--;
p++;
}
atr[p]=t[j];
for(p=k+1;p<n+2;p++,k++)
atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("\nAverage header movements:%f",(float)sum/n);
getch();
}

INPUT
Enter no.of tracks:9
Enter track position:55 58 60 70 18 90 150 160 184

OUTPUT
Tracks traversed Difference between tracks
150 50
33
160 10
184 24
90 94
70 20
60 10
58 2
55 3
18 37

Average header movements: 27.77

9.3.3 C-SCAN DISK SCHEDULING ALGORITHM


#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter total tracks");
scanf("%d",&tot);
t[2]=tot-1;
printf("enter the tracks");
for(i=3;i<=n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<=n+2;i++)
for(j=0;j<=(n+2)-i-1;j++)
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
for(i=0;i<=n+2;i++)
if(t[i]==h)
j=i;break;
p=0;
while(t[j]!=tot-1)
{
atr[p]=t[j];
j++;
p++;
}
atr[p]=t[j];
p++;
i=0;
while(p!=(n+3) && t[i]!=t[h])
{
atr[p]=t[i];
i++;
p++;
}
for(j=0;j<n+2;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
34
}
printf("total header movements%d",sum);
printf("avg is %f",(float)sum/n);
getch();
}

INPUT
Enter the track position : 55 58 60 70 18 90 150 160 184
Enter starting position : 100

OUTPUT
Tracks traversed Difference Between tracks
150 50
160 10
184 24
18 240
55 37
58 3
60 2
70 10
90 20
Average seek time : 35.7777779

35
EXPERIMENT 11

11.1 OBJECTIVE
*Write a C program to simulate page replacement algorithms

11.2 DESCRIPTION
Optimal page replacement algorithm has the lowest page-fault rate of all algorithms and will never suffer from
Belady's anomaly. The basic idea is to replace the page that will not be used for the longest period of time. Use
of this page-replacement algorithm guarantees the lowest possible page fault rate for a fixed number of frames.
Unfortunately, the optimal page-replacement algorithm is difficult to implement, because it requires future
knowledge of the reference string.

11.3 PROGRAM
#include<stdio.h>
int n;
main()
{
int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s;
int count=1,pf=0,p=0;
float pfr;
clrscr();
printf("Enter maximum limit of the sequence: ");
scanf("%d",&max);
printf("\nEnter the sequence: ");
for(i=0;i<max;i++)
scanf("%d",&seq[i]);
printf("\nEnter no. of frames: ");
scanf("%d",&n);
fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j])
flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%d\t",fr[count]);
count++;
pf++;
}
i++;
}
printf("\n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
{
if(seq[i]==fr[j])
flag=0;
}
if(flag!=0)

41
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
pos[j]=1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==1)
{
s=k;
break;
}
}
}
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
pf++;
printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\nThe no. of page faults are %d",pf);
printf("\nPage fault rate %f",pfr);
getch();
}
int findmax(int a[])
{
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
k=i;
}
}
return k;
}
INPUT
Enter number of page references -- 10
Enter the reference string -- 123452525143
42
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

43
EXPERIMENT 2

12.1 OBJECTIVE
*Write a C program to simulate producer-consumer problem using semaphores.

12.2 DESCRIPTION
Producer-consumer problem, is a common paradigm for cooperating processes. A producer process produces
information that is consumed by a consumer process. One solution to the producer-consumer problem uses
shared memory. To allow producer and consumer processes to run concurrently, there must be available a
buffer of items that can be filled by the producer and emptied by the consumer. This buffer will reside in a
region of memory that is shared by the producer and consumer processes. A producer can produce one item
while the consumer is consuming another item. The producer and consumer must be synchronized, so that the
consumer does not try to consume an item that has not yet been produced.

12.3 PROGRAM
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”);
printf(“\nEnter your choice: ”);
scanf(“%d”, &choice);
switch(choice) {
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
Break;
case 2: if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
}
break;
} } }

OUTPUT
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit
Enter your choice: 3
44
EXPERIMENT 4

13.1 OBJECTIVE
*Write a C program to simulate the concept of Dining-Philosophers problem.

13.2 DESCRIPTION
The dining-philosophers problem is considered a classic synchronization problem because it is an example of a
large class of concurrency-control problems. It is a simple representation of the need to allocate several
resources among several processes in a deadlock-free and starvation-free manner. Consider five philosophers
who spend their lives thinking and eating. The philosophers share a circular table surrounded by five chairs, each
belonging to one philosopher. In the center of the table is a bowl of rice, and the table is laid with five single
chopsticks. When a philosopher thinks, she does not interact with her colleagues. From time to time, a
philosopher gets hungry and tries to pick up the two chopsticks that are closest to her (the chopsticks that are
between her and her left and right neighbors). A philosopher may pick up only one chopstick at a time.
Obviously, she cam1ot pick up a chopstick that is already in the hand of a neighbor. When a hungry philosopher
has both her chopsticks at the same time, she eats without releasing her chopsticks. When she is finished eating,
she puts down both of her chopsticks and starts thinking again. The dining-philosophers problem may lead to a
deadlock situation and hence some rules have to be framed to avoid the occurrence of deadlock.

13.3 PROGRAM
int tph, philname[20], status[20], howhung, hu[20], cho;
main()
{
int i;
clrscr();
printf("\n\nDINING PHILOSOPHER PROBLEM");
printf("\nEnter the total no. of philosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i] = (i+1);
status[i]=1;
}
printf("How many are hungry : ");
scanf("%d", &howhung);
if(howhung==tph)
{
printf("\nAll are hungry..\nDead lock stage will occur");
printf("\nExiting..");
}
else
{
for(i=0;i<howhung;i++)
{
printf("Enter philosopher %d position: ",(i+1));
scanf("%d", &hu[i]);
status[hu[i]]=2;
}
do
{
printf("1.One can eat at a time\t2.Two can eat at a time\t3.Exit\nEnter your choice:");
scanf("%d", &cho);
switch(cho)
{
case 1: one();
break;
case 2: two();
break;
case 3: exit(0);
default: printf("\nInvalid option..");
}
45
}while(1);
}
}
one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}
}
two()
{
int i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same time\n");
for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];
r=hu[j];
s++;
printf("\nP %d and P %d are granted to eat", philname[hu[i]],
philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}
}
}

INPUT
DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: 5
How many are hungry : 3
Enter philosopher 1 position: 2
Enter philosopher 2 position: 4
Enter philosopher 3 position: 5

OUTPUT
1. One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 1

Allow one philosopher to eat at any time


P 3 is granted to eat
P 3 is waiting
P 5 is waiting
P 0 is waiting
P 5 is granted to eat
P 5 is waiting
P 0 is waiting
P 0 is granted to eat
P 0 is waiting
46
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 2

Allow two philosophers to eat at same time


combination 1
P 3 and P 5 are granted to eat
P 0 is waiting

combination 2
P 3 and P 0 are granted to eat
P 5 is waiting

combination 3
P 5 and P 0 are granted to eat
P 3 is waiting

1.One can eat at a time 2.Two can eat at a time 3.Exit


Enter your choice: 3

47
}

INPUT
Enter the length of reference string – 20
Enter the reference string -- 70120304230321201701
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

10.3.2 LRU PAGE REPLACEMENT ALGORITHM


#include<stdio.h>
#include<conio.h>
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;
37
count[j]=next;
next++;
}

}
if(flag[i]==0)
{
if(i<f)
{
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++;
}
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 1
Enter 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
38
1 0 7
The number of page faults using LRU are 12

10.3.3 LFU PAGE REPLACEMENT ALGORITHM


#include<stdio.h>
#include<conio.h>

main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, 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++)
{

for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
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");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
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

39
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

40
EXPERIMENT 7
AIM:

To write a C program to simulate basic Unix commands like ls, grep.

DESCRIPTION:

UNIX commands can be simulated in the high-level language using Unix system calls and APIs available in the
language. In addition, programming language construct can also be used to avail the UNIX commands.

EXERCISES:

Simulation of ls command
ALGORITHM:

Step 1: Include necessary header files for manipulating directory.

Step 2: Declare and initialize required objects.

Step 3: Read the directory name form the user.

Step 4: Open the directory using opendir() system call and report error if the directory is not

available.

Step 5: Read the entry available in the directory.

Step 6: Display the directory entry ie., name of the file or sub directory.

Step 7: Repeat the step 6 and 7 until all the entries were read.

/* 1. Simulation of ls and CP command */


#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#define DATA_SIZE 1000
void createf()
{ char data[DATA_SIZE];
char n[100];
FILE * fPtr;
int i;
printf("create 2 files \nfile1: with data \nfile2: without data for copying\n");
for ( i=0;i<2;i++){
printf("enter a file name:");
gets(n);
fPtr = fopen(n,"w");
if(fPtr == NULL)
{ printf("Unable to create file.\n");
exit(EXIT_FAILURE);
}
printf("Enter contents to store in file : \n");
fgets(data, DATA_SIZE, stdin);
fputs(data, fPtr);
fclose(fPtr);
printf("File created and saved successfully. ?? \n");
}
}
void copyfun(){
char ch, source_file[20], target_file[20];
FILE *source, *target;

41
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
if (source == NULL)
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if (target == NULL)
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF)
fputc(ch, target);

printf("File copied successfully.\n");


fclose(source);
fclose(target);
}
void lsandgrep(){
char fn[10],pat[10],temp[200];
FILE *fp;
char dirname[10];
DIR*p;
struct dirent *d;
printf("Enter directory name\n");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find directory");
exit(0);
}
while(d=readdir(p))
printf("%s\n",d->d_name);

int main(){
createf();
copyfun();
lsandgrep();
}

2. Simulation of grep command.

ALGORITHM:

Step 1: Include necessary header files.

Step 2: Make necessary declarations.

Step 3: Read the file name from the user and open the file in the read only mode.
42
Step 4: Read the pattern from the user.

Step 5: Read a line of string from the file and search the pattern in that line.

Step 6: If pattern is available, print the line.

Step 7: Repeat the step 4 to 6 till the end of the file.

/* 2. Simulation of grep command */

#include <stdio.h>
#include <stdlib.h>
#include<dirent.h>
#include<conio.h>
#define DATA_SIZE 1000
#define BUFFER_SIZE 1000
void countch(){
FILE *fptr;
char path[100];

char word[50];

int wCount;

printf("Enter file name: ");


scanf("%s", path);

printf("Enter word to search in file: ");


scanf("%s", word);
fptr = fopen(path, "r");

if (fptr == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write previleges.\n");

exit(EXIT_FAILURE);
}

Search_in_File(fptr, word);
printf("%d",wCount);

fclose(fptr);

int Search_in_File(char *fname, char *str)


{
FILE *fp;
int line_num = 1;
int find_result = 0;
char temp[512];
if((fopen(fname, "r")) != NULL) {
43
return(-1);
}

while(fgets(temp, 512, fp) != NULL) {


if((strstr(temp,str)) != NULL) {

line_num=line_num+0;
printf("\n%s\n", temp);
find_result++;
}
line_num++;
}

if(find_result == 0) {
printf("\nSorry, couldn't find a match.\n");
}

if(fp) {
fclose(fp);
}
return(0);
}

int main(){
countch();
getch();
return 0;
}

Program:

Below code is included code of Grep command c - pattern

#include<stdio.h>
#define MAX_FILE_NAME 100
#include<conio.h>

int main()
{
FILE *fp;
int count = 0;
char filename[MAX_FILE_NAME];
char c;

printf("Enter file name: ");


scanf("%s", filename);

fp = fopen(filename, "r");

if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}

for (c = getc(fp); c != EOF; c = getc(fp))


if (c == '\n')
count = count + 1;

44
fclose(fp);
printf("The file %s has %d lines\n ", filename, count);
getch();
return 0;}

45

You might also like