0% found this document useful (0 votes)
9 views122 pages

Operating system

Operating system practical ans in text format

Uploaded by

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

Operating system

Operating system practical ans in text format

Uploaded by

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

SLIP 1

Q.1 Write the simulation program to implement demand paging and show the

page scheduling and total number of page faults according to the LFU page
replacement

algorithm. Assume the memory of n frames.

Reference String : 3,4,5,4,3,4,7,2,4,5,6,7,2,4,6


SOLUTION:

#include <stdio.h>
#define MAX 20

int frames[MAX], ref[MAX], mem[MAX][MAX], faults, sp, m, n, time[MAX];

void accept()
{
int i;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of references: ");
scanf("%d", &m);
printf("Enter the reference string:
");
for (i = 0; i < m; i++)
{
printf("[%d] = ", i);
scanf("%d", &ref[i]);
}
}

void disp()
{
int i, j;
for (i = 0; i < m; i++)
printf("%3d", ref[i]);
printf("

");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (mem[i][j])
printf("%3d", mem[i][j]);
else
printf(" ");
}

printf("
");
}
printf("Total Page Faults: %d
", faults);
}

int search(int pno)


{
int i;
for (i = 0; i < n; i++)
{
if (frames[i] == pno)
return i;
}
return -1;
}

void lfu()
{
int i, j, k, count[20];
for (i = 0; i < m && sp < n; i++)
{
k = search(ref[i]);
if (k == -1)
{
frames[sp] = ref[i];
time[sp] = 1;
count[sp] = 1;
faults++;
sp++;
for (j = 0; j < n; j++)
mem[j][i] = frames[j];
}
else
{
time[k] = i;
count[k]++;
}
}
for (; i < m; i++)
{
k = search(ref[i]);
if (k == -1)
{
int min_i = 0, min = 9999;
for (j = 0; j < n; j++)
{

if (count[j] < min)


{
min = count[j];
min_i = j;
}
}
sp = min_i;
frames[sp] = ref[i];
time[sp] = i;
count[sp] = 1;
faults++;
for (j = 0; j < n; j++)
mem[j][i] = frames[j];
}
else
{
time[k] = i;
count[k]++;
}
}
}

int main()
{
accept();
lfu();
disp();
return 0;
}

Q.2. Write a C program to implement the shell which displays the command prompt

“myshell$”. It accepts the command, tokenize the command line and execute it by
creating

the child process. Also implement the additional command ‘typeline’ as

typeline +n filename :- To print first n lines in the file.


typeline -a filename :- To print all lines in the file.
SOLUTION:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void make_toks(char *s, char *tok[])


{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);

if (strcmp(args[0], "typeline") == 0)
{
// Check if the '+n' option is specified
if (args[1][0] == '+')
{
// Read the line count
int n = atoi(args[1] + 1);
// Open the file
FILE *fp = fopen(args[2], "r");
if (fp == NULL)
{
printf("Error opening file %s.
", args[2]);
return 1;
}
// Read and print the first n lines
char line[80];
int count = 0;
while (count < n && fgets(line, 80, fp) != NULL)
{
printf("%s", line);

count++;
}
// Close the file
fclose(fp);
}
// If the '-a' option is specified
else if (strcmp(args[2], "-a") == 0)
{
// Open the file
FILE *fp = fopen(args[2], "r");
if (fp == NULL)
{
printf("Error opening file %s.
", args[2]);
return 1;
}
// Read and print the entire file
char line[80];
while (fgets(line, 80, fp) != NULL)
printf("%s", line);
// Close the file
fclose(fp);
}
else
{
printf("Invalid option.
");
}
}
}
}

SLIP 2

Q.1 Write the simulation program for demand paging and show the page scheduling and

total number of page faults according the FIFO page replacement algorithm. Assume
the memory of n frames.
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
SOLUTION:

#include<stdio.h>
#define MAX 20
int frames[MAX],ref[MAX],mem[MAX][MAX],faults,sp,m,n;
void accept()
{

int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:
");
for(i=0;i<m;i++)
{

printf("[%d]=",i);
scanf("%d",&ref[i]);

}
}

void disp()
{

int i,j;
for(i=0;i<m;i++)
printf("%3d",ref[i]);
printf("

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

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

if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");

}
printf("
");

}
printf("Total Page Faults: %d
",faults);

int search(int pno)


{
int i;
for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}
return -1;
}
void fifo()
{
int i,j;
for(i=0;i<m;i++)
{
if(search(ref[i])==-1)
{
frames[sp] = ref[i];
sp = (sp+1)%n;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
}
}
int main()
{
accept();
fifo();
disp();
return 0;
}

Q.2 Write a program to implement the shell. It should display the command prompt
“myshell$”. Tokenize the command line and execute the given command by creating
the child process. Additionally it should interpret the following ‘list’ commands
as
myshell$ list f dirname :- To print names of all the files in current

directory.
myshell$ list n dirname :- To print the number of all entries in the current

directory
SOLUTION:

#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void list(char *dirname, char op)
{
DIR *dir;
struct dirent *entry;
int count = 0;
// Open the directory
dir = opendir(dirname);
if (dir == NULL)
{
printf("Directory %s not found.
", dirname);
return;
}
// Iterate through the entries in the directory
while ((entry = readdir(dir)) != NULL)
{
switch (op)
{
case 'f':
// Display the filename
printf("%s
", entry->d_name);
break;
case 'n':
// Increment the count
count++;
break;
}

}
// Close the directory
closedir(dir);
// If the 'n' option was specified, print the count
if (op == 'n')
{
printf("Number of entries: %d
", count);
}
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "list") == 0)
{
list(args[2], args[1][0]);
}
else
{
pid = fork();
if (pid > 0)
wait();
else
{
if (execvp(args[0], args) == -1)
printf("Bad command.
");
}
}
}
return 0;
}
SLIP 3

Q.1 Write the simulation program to implement demand paging and show the page
scheduling and total number of page faults according to the LRU (using counter
method) page replacement algorithm. Assume the memory of n frames.
Reference String : 3,5,7,2,5,1,2,3,1,3,5,3,1,6,2
SOLUTION:

#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,time[MAX];
void accept()
{
int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:
");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;
for(i=0;i<m;i++)
printf("%3d",ref[i]);
printf("

");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("
");
}
printf("Total Page Faults: %d
",faults);
}
int search(int pno)
{
int i;
for(i=0;i<n;i++)
{

if(frames[i]==pno)
return i;
}
return -1;
}
int get_lru()
{
int i,min_i,min=9999;
for(i=0;i<n;i++)
{
if(time[i]<min)
{
min = time[i];
min_i = i;
}
}
return min_i;
}

void lru()
{
int i,j,k;
for(i=0;i<m && sp<n;i++)
{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
time[sp]=i;
faults++;
sp++;
for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
time[k]=i;
}
for(;i<m;i++)
{

k = search(ref[i]);
if(k==-1)
{
sp = get_lru();
frames[sp] = ref[i];
time[sp] = i;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
time[k]=i;
}
}

int main()
{
accept();
lru();
disp();
return 0;
}

Q.2 Write a program to implement the toy shell. It should display the command
prompt
“myshell$”. Tokenize the command line and execute the given command by creating
the child process. Additionally it should interpret the following commands.
count c filename: To print number of characters in the file.
count w filename: To print number of words in the file.

count l filename: To print number of lines in the file.


SOLUTION:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)

{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void count(char *fn, char op)
{
FILE *fh;
int cc=0,wc=0,lc=0;
char c;
fh = fopen(fn, "r");
if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

while ((c = fgetc(fh)) != EOF)


{
if (c == ' ')
wc++;
else if (c == '
')
{
wc++;
lc++;
}
cc++;
}
fclose(fh);
switch (op)
{
case 'c':
printf("No.of characters:%d
", cc - 1);
break;
case 'w':
printf("No.of words:%d
", wc);
break;
case 'l':
printf("No.of lines:%d
", lc + 1);
break;
}
}
int main()
{

char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if(strcmp(args[0],"count")==0)
count(args[2],args[1][0]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}

SLIP 4
Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the MFU page

replacement algorithm. Assume the memory of n frames.

Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
SOLUTION:

#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,count[MAX];

void accept()
{
int i;

printf("Enter no.of frames:");


scanf("%d", &n);

printf("Enter no.of references:");


scanf("%d", &m);

printf("Enter reference string:


");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}

void disp()
{
int i,j;

for(i=0;i<m;i++)
printf("%3d",ref[i]);

printf("

");

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{

if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("
");
}

printf("Total Page Faults: %d


",faults);
}

int search(int pno)


{
int i;

for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}

return -1;
}

int get_mfu(int sp)


{
int i,max_i,max=-9999;

i=sp;
do
{
if(count[i]>max)
{
max = count[i];
max_i = i;
}
i=(i+1)%n;
}while(i!=sp);

return max_i;
}

void mfu()
{
int i,j,k;

for(i=0;i<m && sp<n;i++)


{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
count[sp]++;
faults++;
sp++;

for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
count[k]++;

sp=0;
for(;i<m;i++)
{
k = search(ref[i]);
if(k==-1)
{
sp = get_mfu(sp);
frames[sp] = ref[i];
count[sp]=1;
faults++;
sp = (sp+1)%n;

for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
count[k]++;
}
}

int main()
{
accept();
mfu();
disp();

return 0; }

Q.2 Write a program to implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by creating

the child process. Additionally, it should interpret the following commands.

myshell$ search a filename pattern: To search all the occurrence of pattern in the
file.
myshell$ search c filename pattern: To count the number of occurrence of pattern in
the file.
SOLUTION:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void search(char *fn, char op, char *pattern)
{
FILE *fh;
int count = 0;
char line[80];

fh = fopen(fn, "r");
if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

while (fgets(line, 80, fh) != NULL)


{
if (strstr(line, pattern) != NULL)
{
count++;
if (op == 'a')
printf("%s", line);
}
}

fclose(fh);

if (op == 'c')
printf("Number of occurrences: %d
", count);
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "search") == 0)
search(args[2], args[1][0], args[3]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}

SLIP 5
Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the optimal page

replacement algorithm. Assume the memory of n frames.

Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2

SOLUTION:

#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2,
flag3, i, j,
k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);

printf("Enter page reference string: ");

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


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

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


frames[i] = -1;
}

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


flag1 = flag2 = 0;

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


if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){

faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
flag3 =0;

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


temp[j] = -1;

for(k = i + 1; k < no_of_pages; ++k){


if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}

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


if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}

if(flag3 ==0){
max = temp[0];
pos = 0;

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


if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}

printf("
");

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


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

printf("

Total Page Faults = %d", faults);

return 0;
}

Q.2 Write a program to implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by creating
the child process. Additionally it should interpret the

following commands.

myshell$ search f filename pattern :- To display first occurrence of

pattern in the file.

myshell$ search c filename pattern :- To count the number of occurrence

of pattern in the file.

SOLUTION:
#include <sys/types.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}

void search(char *fn, char op, char *pattern)


{
FILE *fh;
int count = 0;
char line[80];

fh = fopen(fn, "r");
if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

if (op == 'f')
{
// Search for the first occurrence of the pattern
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
// Print the line and exit the loop
printf("%s", line);
break;
}
}
}
else
{
// Continue with the existing search implementation
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
count++;
if (op == 'a')
printf("%s", line);
}
}
if (op == 'c')
printf("Number of occurrences: %d
", count);
}
fclose(fh);
}

int main()

{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "search") == 0)
search(args[2], args[1][0], args[3]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}
SLIP 6

Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the MRU page

replacement algorithm. Assume the memory of n frames.

Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
SOLUTION:

#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,count[MAX],time[MAX];

void accept()
{
int i;

printf("Enter no.of frames:");


scanf("%d", &n);

printf("Enter no.of references:");


scanf("%d", &m);

printf("Enter reference string:


");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}

void disp()
{
int i,j;

for(i=0;i<m;i++)
printf("%3d",ref[i]);

printf("

");

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

{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("
");
}

printf("Total Page Faults: %d


",faults);
}

int search(int pno)


{
int i;

for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}

return -1;
}
int get_mru()
{
int i,max_i,max=0;
for(i=0;i<n;i++)
{
if(time[i]>max)
{
max = time[i];
max_i = i;
}
}
return max_i;
}

void mru()
{
int i,j,k;
for(i=0;i<m && sp<n;i++)
{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
time[sp]=i;
faults++;
sp++;
for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
time[k]=i;
}
for(;i<m;i++)
{
k = search(ref[i]);
if(k==-1)
{
sp = get_mru();

frames[sp] = ref[i];
time[sp] = i;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
time[k]=i;
}
}
int main()
{
accept();
mru();
disp();

return 0;
}

Q.2 Write a programto implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by creating

the child process. Additionally it should interpret the following

commands.
myshell$ search f filename pattern :- To display first occurrence of

pattern in the file.

myshell$ search a filename pattern :- To search all the occurrence of


pattern in the file.

#include <sys/types.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void search(char *fn, char op, char *pattern)
{
FILE *fh;
int count = 0;
char line[80];

fh = fopen(fn, "r");
if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

if (op == 'f')
{

// Search for the first occurrence of the pattern


while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
// Print the line and exit the loop
printf("%s", line);
break;
}
}
}
else
{
// Continue with the existing search implementation
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
count++;
if (op == 'a')
printf("%s", line);
}
}
if (op == 'c')
printf("Number of occurrences: %d
", count);
}
fclose(fh);
}

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "search") == 0)
search(args[2], args[1][0], args[3]);
else
{
pid = fork();

if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}
SLIP 7

Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the Optimal page

replacement algorithm. Assume the memory of n frames.

Reference String : 7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6, 2 .
SOLUTION:

#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2,
flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);

printf("Enter page reference string: ");

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


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

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


frames[i] = -1;
}

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


flag1 = flag2 = 0;

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


if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){

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


if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
flag3 =0;

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


temp[j] = -1;

for(k = i + 1; k < no_of_pages; ++k){


if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}

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


if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}

if(flag3 ==0){
max = temp[0];
pos = 0;

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


if(temp[j] > max){
max = temp[j];
pos = j;

}
}
}
frames[pos] = pages[i];
faults++;
}
printf("
");

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


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

printf("

Total Page Faults = %d", faults);

return 0;
}

Q.2 Write a program to implement shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret the following

commands.

myshell$ search a filename pattern :- To search all the occurrence of

pattern in the file.

myshell$ search c filename pattern :- To count the number of occurrence


of pattern in the file.

SOLUTION:

#include <sys/types.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;

char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void search(char *fn, char op, char *pattern)
{
FILE *fh;
int count = 0;
char line[80];

fh = fopen(fn, "r");
if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

if (op == 'f')
{
// Search for the first occurrence of the pattern
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
// Print the line and exit the loop
printf("%s", line);
break;
}
}
}
else
{
// Continue with the existing search implementation
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
count++;
if (op == 'a')
printf("%s", line);

}
}
if (op == 'c')
printf("Number of occurrences: %d
", count);
}
fclose(fh);
}

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "search") == 0)
search(args[2], args[1][0], args[3]);
else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}

SLIP 8
Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the LRU page

replacement algorithm. Assume the memory of n frames.

Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
SOLUTION:

#include <stdio.h>
#define MAX 20

int frames[MAX], ref[MAX], mem[MAX][MAX], faults, sp, m, n, time[MAX];

void accept()
{
int i;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of references: ");
scanf("%d", &m);
printf("Enter the reference string:
");
for (i = 0; i < m; i++)
{
printf("[%d] = ", i);
scanf("%d", &ref[i]);
}
}

void disp()
{
int i, j;
for (i = 0; i < m; i++)
printf("%3d", ref[i]);
printf("

");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (mem[i][j])
printf("%3d", mem[i][j]);
else
printf(" ");
}
printf("
");

}
printf("Total Page Faults: %d
", faults);
}

int search(int pno)


{
int i;
for (i = 0; i < n; i++)
{
if (frames[i] == pno)
return i;
}
return -1;
}

void lfu()
{
int i, j, k, count[20];
for (i = 0; i < m && sp < n; i++)
{
k = search(ref[i]);
if (k == -1)
{
frames[sp] = ref[i];
time[sp] = 1;
count[sp] = 1;
faults++;
sp++;
for (j = 0; j < n; j++)
mem[j][i] = frames[j];
}
else
{
time[k] = i;
count[k]++;
}
}
for (; i < m; i++)
{
k = search(ref[i]);
if (k == -1)
{
int min_i = 0, min = 9999;
for (j = 0; j < n; j++)
{
if (count[j] < min)

{
min = count[j];
min_i = j;
}
}
sp = min_i;
frames[sp] = ref[i];
time[sp] = i;
count[sp] = 1;
faults++;
for (j = 0; j < n; j++)
mem[j][i] = frames[j];
}
else
{
time[k] = i;
count[k]++;
}
}
}

int main()
{
accept()
lfu();
disp();
return 0;
}
Q.2 Write a progra[Author]mto implement the shell. It should display the command
prompt

“myshell$”. Tokenize the command line and execute the given command by creating

the child process. Additionally it should interpret the following commands.

myshell$ search f filename pattern :- To display first occurrence of

pattern in the file.

myshell$ search c filename pattern :- To count the number of occurrence


of pattern in the file.

SOLUTION:
#include <sys/types.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void search(char *fn, char op, char *pattern)
{
FILE *fh;
int count = 0;
char line[80];

fh = fopen(fn, "r");
if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

if (op == 'f')
{
// Search for the first occurrence of the pattern
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
// Print the line and exit the loop
printf("%s", line);
break;
}
}
}
else
{
// Continue with the existing search implementation
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
count++;
if (op == 'a')
printf("%s", line);
}
}
if (op == 'c')
printf("Number of occurrences: %d
", count);
}
fclose(fh);
}

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "search") == 0)
search(args[2], args[1][0], args[3]);
else
{
pid = fork();
if(pid>0)

wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}

SLIP 9

Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the FIFO page

replacement algorithm. Assume the memory of n frames.

Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
SOLUTION:

#include<stdio.h>
#define MAX 20
int frames[MAX],ref[MAX],mem[MAX][MAX],faults,sp,m,n;
void accept()
{
int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:
");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;
for(i=0;i<m;i++)
printf("%3d",ref[i]);
printf("

");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");

}
printf("
");
}
printf("Total Page Faults: %d
",faults);

}
int search(int pno)
{
int i;
for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}
return -1;
}
void fifo()
{
int i,j;
for(i=0;i<m;i++)
{
if(search(ref[i])==-1)
{
frames[sp] = ref[i];
sp = (sp+1)%n;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
}
}
int main()
{
accept();
fifo();
disp();
return 0;
}

Q.2 Write a program to implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret

the following commands.

myshell$ search f filename pattern :- To display first occurrence of

pattern in the file.

myshell$ search a filename pattern :- To search all the occurrence of


pattern in the file.

SOLUTION:

#include <sys/types.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void search(char *fn, char op, char *pattern)
{
FILE *fh;
int count = 0;
char line[80];

fh = fopen(fn, "r");
if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

if (op == 'f')
{
// Search for the first occurrence of the pattern
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
// Print the line and exit the loop
printf("%s", line);
break;
}
}
}
else
{
// Continue with the existing search implementation
while (fgets(line, 80, fh) != NULL)
{
if (strstr(line, pattern) != NULL)
{
count++;
if (op == 'a')
printf("%s", line);
}
}
if (op == 'c')
printf("Number of occurrences: %d
", count);
}
fclose(fh);
}

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "search") == 0)
search(args[2], args[1][0], args[3]);

else
{
pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}

SLIP 10

Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the FIFO page

replacement algorithm. Assume the memory of n frames.

Reference String : 2, 4, 5, 6, 9, 4, 7, 3, 4, 5, 6, 7, 2, 4, 7, 1 [15]

SOLUTION:

#include<stdio.h>
#define MAX 20
int frames[MAX],ref[MAX],mem[MAX][MAX],faults,sp,m,n;
void accept()
{

int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:
");
for(i=0;i<m;i++)
{

printf("[%d]=",i);
scanf("%d",&ref[i]);

}
}

void disp()
{

int i,j;

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

printf("%3d",ref[i]);
printf("

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

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

if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");

}
printf("
");

}
printf("Total Page Faults: %d
",faults);

}
int search(int pno)
{
int i;
for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}
return -1;
}
void fifo()
{
int i,j;
for(i=0;i<m;i++)
{
if(search(ref[i])==-1)
{
frames[sp] = ref[i];
sp = (sp+1)%n;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
}
}
int main()
{
accept();
fifo();

disp();
return 0;
}

Q.2 Write a program to implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret the following ‘list’
commands as

myshell$ list f dirname :- To print names of all the files in current

directory.

myshell$ list i dirname :- To print names and inodes of the files in the

current directory. [15]

SOLUTION:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void list(char *dirname, char op)
{
DIR *dir;
struct dirent *entry;
int count = 0;
// Open the directory
dir = opendir(dirname);
if (dir == NULL)
{
printf("Directory %s not found.
", dirname);
return;
}
// Iterate through the entries in the directory
while ((entry = readdir(dir)) != NULL)
{
switch (op)
{
case 'f':
// Display the filename
printf("%s
", entry->d_name);
break;
case 'n':
// Increment the count
count++;
break;
case 'i':
// Display the filename and inode number
printf("%s: %lu
", entry->d_name, entry->d_ino);
break;
}
}
// Close the directory
closedir(dir);
// If the 'n' option was specified, print the count
if (op == 'n')
{
printf("Number of entries: %d
", count);
}
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "list") == 0)

{
list(args[2], args[1][0]);
}
else
{
pid = fork();
if (pid > 0)
wait();
else
{
if (execvp(args[0], args) == -1)
printf("Bad command.
");
}
}
}
return 0;
}

SLIP 11

Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the LFU page

replacement algorithm. Assume the memory of n frames. Reference

String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6 [15]

SOLUTION:

#include <stdio.h>
#define MAX 20

int frames[MAX], ref[MAX], mem[MAX][MAX], faults, sp, m, n, time[MAX];

void accept()
{
int i;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of references: ");
scanf("%d", &m);
printf("Enter the reference string:
");
for (i = 0; i < m; i++)
{
printf("[%d] = ", i);
scanf("%d", &ref[i]);
}
}

void disp()
{
int i, j;
for (i = 0; i < m; i++)
printf("%3d", ref[i]);
printf("

");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (mem[i][j])
printf("%3d", mem[i][j]);
else
printf(" ");

}
printf("
");
}
printf("Total Page Faults: %d
", faults);
}

int search(int pno)


{
int i;
for (i = 0; i < n; i++)
{
if (frames[i] == pno)
return i;
}
return -1;
}

void lfu()
{
int i, j, k, count[20];
for (i = 0; i < m && sp < n; i++)
{
k = search(ref[i]);
if (k == -1)
{
frames[sp] = ref[i];
time[sp] = 1;
count[sp] = 1;
faults++;
sp++;
for (j = 0; j < n; j++)
mem[j][i] = frames[j];
}
else
{
time[k] = i;
count[k]++;
}
}
for (; i < m; i++)
{
k = search(ref[i]);
if (k == -1)
{
int min_i = 0, min = 9999;
for (j = 0; j < n; j++)

{
if (count[j] < min)
{
min = count[j];
min_i = j;
}
}
sp = min_i;
frames[sp] = ref[i];
time[sp] = i;
count[sp] = 1;
faults++;
for (j = 0; j < n; j++)
mem[j][i] = frames[j];
}
else
{
time[k] = i;
count[k]++;
}
}
}

int main()
{
accept();
lfu();
disp();
return 0;
}

Q.2 Write a C program to implement the shell. It should display the command prompt
“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret the following ‘list’
commands as

myshell$ list f dirname :- To print names of all the files in current

directory.

myshell$ list n dirname :- To print the number of all entries in the current
directory
SOLUTION:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void list(char *dirname, char op)
{
DIR *dir;
struct dirent *entry;
int count = 0;
// Open the directory
dir = opendir(dirname);
if (dir == NULL)
{
printf("Directory %s not found.
", dirname);
return;
}
// Iterate through the entries in the directory
while ((entry = readdir(dir)) != NULL)
{
switch (op)
{
case 'f':
// Display the filename
printf("%s
", entry->d_name);
break;
case 'n':
// Increment the count
count++;

break;
case 'i':
// Display the filename and inode number
printf("%s: %lu
", entry->d_name, entry->d_ino);
break;
}
}
// Close the directory
closedir(dir);
// If the 'n' option was specified, print the count
if (op == 'n')
{
printf("Number of entries: %d
", count);
}
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "list") == 0)
{
list(args[2], args[1][0]);
}
else
{
pid = fork();
if (pid > 0)
wait();
else
{
if (execvp(args[0], args) == -1)
printf("Bad command.
");
}
}
}
return 0;
}
SLIP 12

Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the LRU page

replacement algorithm. Assume the memory of n frames.

Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
SOLUTION:

#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,time[MAX];
void accept()
{
int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:
");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;
for(i=0;i<m;i++)
printf("%3d",ref[i]);
printf("

");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("
");
}

printf("Total Page Faults: %d


",faults);
}
int search(int pno)
{
int i;
for(i=0;i<n;i++)
{

if(frames[i]==pno)
return i;
}
return -1;
}
int get_lru()
{
int i,min_i,min=9999;
for(i=0;i<n;i++)
{
if(time[i]<min)
{
min = time[i];
min_i = i;
}
}
return min_i;
}

void lru()
{
int i,j,k;
for(i=0;i<m && sp<n;i++)
{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
time[sp]=i;
faults++;
sp++;
for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
time[k]=i;
}
for(;i<m;i++)

{
k = search(ref[i]);
if(k==-1)
{
sp = get_lru();

frames[sp] = ref[i];
time[sp] = i;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
time[k]=i;
}
}

int main()
{
accept();
lru();
disp();
return 0;
}

Q.2 Write a program to implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret the following ‘list’
commands as

myshell$ list f dirname :- To print names of all the files in current

directory.

myshell$ list n dirname :- To print the number of all entries in the current
directory

SOLUTION:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dirent.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void list(char *dirname, char op)
{
DIR *dir;
struct dirent *entry;
int count = 0;
// Open the directory
dir = opendir(dirname);
if (dir == NULL)
{
printf("Directory %s not found.
", dirname);
return;
}
// Iterate through the entries in the directory
while ((entry = readdir(dir)) != NULL)
{
switch (op)
{
case 'f':
// Display the filename
printf("%s
", entry->d_name);
break;
case 'n':
// Increment the count
count++;
break;
case 'i':
// Display the filename and inode number
printf("%s: %lu
", entry->d_name, entry->d_ino);
break;
}
}
// Close the directory

closedir(dir);
// If the 'n' option was specified, print the count
if (op == 'n')
{
printf("Number of entries: %d
", count);
}
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "list") == 0)
{
list(args[2], args[1][0]);
}
else
{
pid = fork();
if (pid > 0)
wait();
else
{
if (execvp(args[0], args) == -1)
printf("Bad command.
");
}
}
}
return 0;
}

SLIP 13

Q.1 Write a C program to implement the shell which displays the command prompt

“myshell$”. It accepts the command, tokenize the command line and execute it by

creating the child process. Also implement the additional command ‘typeline’ as

typeline -a filename :- To print all lines in the file.


SOLUTION:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);

if (strcmp(args[0], "typeline") == 0)
{

// Check if the '+n' option is specified


if (args[1][0] == '+')
{
// Read the line count
int n = atoi(args[1] + 1);
// Open the file
FILE *fp = fopen(args[2], "r");
if (fp == NULL)
{
printf("Error opening file %s.
", args[2]);
return 1;
}
// Read and print the first n lines
char line[80];
int count = 0;
while (count < n && fgets(line, 80, fp) != NULL)
{
printf("%s", line);
count++;
}
// Close the file
fclose(fp);
}
// If the '-a' option is specified
else if (strcmp(args[2], "-a") == 0)
{
// Open the file
FILE *fp = fopen(args[2], "r");
if (fp == NULL)
{
printf("Error opening file %s.
", args[2]);
return 1;
}
// Read and print the entire file
char line[80];
while (fgets(line, 80, fp) != NULL)
printf("%s", line);
// Close the file
fclose(fp);
}
else
{
printf("Invalid option.
");
}
}

Q.2 Write the simulation program for Round Robin scheduling for given time quantum.

The arrival time and first CPU-burst of different jobs should be input to the
system.

Accept no. of Processes, arrival time and burst time. The output should give the
Gantt

chart, turnaround time and waiting time for each process. Also display the average

turnaround time and average waiting time.

SOLUTION:

404 Error not found


SLIP 14
Q.1 Write a C program to implement the shell which displays the command prompt

“myshell$”. It accepts the command, tokenize the command line and execute it by

creating the child process. Also implement the additional command ‘typeline’ as

typeline +n filename :- To print first n lines in the file.


SOLUTION:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);

if (strcmp(args[0], "typeline") == 0)
{
// Check if the '+n' option is specified
if (args[1][0] == '+')
{
// Read the line count
int n = atoi(args[1] + 1);
// Open the file
FILE *fp = fopen(args[2], "r");
if (fp == NULL)
{
printf("Error opening file %s.
", args[2]);
return 1;
}
// Read and print the first n lines
char line[80];
int count = 0;
while (count < n && fgets(line, 80, fp) != NULL)
{
printf("%s", line);
count++;
}
// Close the file
fclose(fp);
}
// If the '-a' option is specified
else if (strcmp(args[2], "-a") == 0)
{
// Open the file
FILE *fp = fopen(args[2], "r");
if (fp == NULL)
{
printf("Error opening file %s.
", args[2]);
return 1;
}
// Read and print the entire file
char line[80];
while (fgets(line, 80, fp) != NULL)
printf("%s", line);
// Close the file
fclose(fp);
}
else
{

printf("Invalid option.
");
}
}
}
}

Write a C program to simulate Non-preemptive Shortest Job First (SJF) – scheduling.


The
arrival time and first CPU-burst of different jobs should be input to the system.
Accept no. of
Processes, arrival time and burst time. The output should give Gantt chart,
turnaround time
and waiting time for each process. Also find the average waiting time and
turnaround time
SOLUTION:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1;
struct process_info *next;
}NODE;
int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

printf("Enter no.of process:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);

p->bt1 = p->bt;
p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname at bt ct tat wt
");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;

avg_tat+=tat;
avg_wt+=wt;
printf("%s %d %d %d %d %d
",
p->pname,p->at,p->bt,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f Avg WT=%f


",
avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname at bt
");
while(p!=NULL)
{
printf("%s %d %d
",
p->pname,p->at,p->bt1);
p = p->next;
}
}

void sort()
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;

t = p->bt;
p->bt = q->bt;
q->bt = t;
t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_sjf()
{
NODE *p,*min_p=NULL;
int min=9999;

p = first;
while(p!=NULL)
{
if(p->at<=time && p->bt1!=0 &&
p->bt1<min)
{
min = p->bt1;
min_p = p;
}
p=p->next;
}

return min_p;
}

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void sjfnp()
{
int prev=0,n1=0;
NODE *p;
while(n1!=n)
{
p = get_sjf();

if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time+=p->bt1;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

p->ct = time;
p->bt1 = 0;

n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)
{
if(strcmp(s[i].pname,s1[j].pname)==0)

s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);
for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);

for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
sjfnp();
print_output();
print_gantt_chart();

return 0;
}

SLIP 15

Q.1 Write a C program to implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret the following ‘list’
commands as

myshell$ list f dirname :- To print names of all the files in current directory.

SOLUTION:

#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <dirent.h>

void make_toks(char *s, char *tok[])

int i=0;

char *p;

p = strtok(s," ");

while(p!=NULL)

tok[i++]=p;

p=strtok(NULL," ");

tok[i]=NULL;

void list(char *dirname, char op)

DIR *dir;

struct dirent *entry;

int count = 0;

// Open the directory

dir = opendir(dirname);

if (dir == NULL)

printf("Directory %s not found.


", dirname);
return;

// Iterate through the entries in the directory

while ((entry = readdir(dir)) != NULL)

switch (op)

case 'f':

// Display the filename

printf("%s
", entry->d_name);

break;

case 'n':

// Increment the count

count++;

break;

case 'i':

// Display the filename and inode number

printf("%s: %lu
", entry->d_name, entry->d_ino);

break;

// Close the directory

closedir(dir);

// If the 'n' option was specified, print the count

if (op == 'n')

printf("Number of entries: %d
", count);
}

int main()

char buff[80],*args[10];

int pid;

while(1)

printf("myshell$ ");

fflush(stdin);

fgets(buff,80,stdin);

buff[strlen(buff)-1]='';

make_toks(buff,args);

if (strcmp(args[0], "list") == 0)

list(args[2], args[1][0]);

else

pid = fork();

if (pid > 0)

wait();

else

if (execvp(args[0], args) == -1)

printf("Bad command.
");

}
}

return 0;

Write the program to simulate preemptive Shortest Job First (SJF) – scheduling. The
arrival
time and first CPU-burst of different jobs should be input to the system. Accept
no. of
Processes, arrival time and burst time. The output should give Gantt chart,
turnaround time
and waiting time for each process. Also find the average waiting time and
turnaround time

SOLUTION:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1;
struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

printf("Enter no.of process:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");

scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);
p->bt1 = p->bt;

p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname at bt ct tat wt
");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;

avg_tat+=tat;
avg_wt+=wt;

printf("%s %d %d %d %d %d
",
p->pname,p->at,p->bt,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f Avg WT=%f


",
avg_tat/n,avg_wt/n);
}

void print_input()

{
NODE *p;

p = first;

printf("pname at bt
");
while(p!=NULL)
{
printf("%s %d %d
",
p->pname,p->at,p->bt1);
p = p->next;
}
}

void sort()
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;

t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;

p->bt1 = q->bt1;
q->bt1 = t;
}

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_sjf()
{
NODE *p,*min_p=NULL;
int min=9999;

p = first;
while(p!=NULL)
{
if(p->at<=time && p->bt1!=0 &&
p->bt1<min)
{
min = p->bt1;
min_p = p;
}
p=p->next;
}

return min_p;
}

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void sjfp()
{
int prev=0,n1=0;

NODE *p;

while(n1!=n)
{
p = get_sjf();

if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time++;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

p->ct = time;
p->bt1--;
if(p->bt1==0)
n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)

{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);

for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);

for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
sjfp();
print_output();
print_gantt_chart();

return 0;
}
SLIP 16

Q.1 Write a programto implement the toy shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret the following

commands.

count c filename :- To print number of characters in the file.

count w filename :- To print number of words in the file. [15]

SOLUTION:

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

void make_toks(char *s, char *tok[])

int i=0;

char *p;

p = strtok(s," ");

while(p!=NULL)

tok[i++]=p;

p=strtok(NULL," ");
}

tok[i]=NULL;

void count(char *fn, char op)

FILE *fh;

int cc=0,wc=0,lc=0;

char c;

fh = fopen(fn, "r");

if (fh == NULL)

printf("File %s not found.


", fn);

return;

while ((c = fgetc(fh)) != EOF)

if (c == ' ')

wc++;

else if (c == '
')

wc++;

lc++;

cc++;

fclose(fh);
switch (op)

case 'c':

printf("No.of characters:%d
", cc - 1);

break;

case 'w':

printf("No.of words:%d
", wc);

break;

case 'l':

printf("No.of lines:%d
", lc + 1);

break;

int main()

char buff[80],*args[10];

int pid;

while(1)

printf("myshell$ ");

fflush(stdin);

fgets(buff,80,stdin);

buff[strlen(buff)-1]='';

make_toks(buff,args);

if(strcmp(args[0],"count")==0)

count(args[2],args[1][0]);

else
{

pid = fork();

if(pid>0)

wait();

else

if(execvp(args[0],args)==-1)

printf("Bad command.
");

return 0;

Q.2 Write the program to simulate Non preemptive priority scheduling. The arrival

time and first CPU-burst of different jobs should be input to the system.

Accept no. of Processes, arrival time and burst time. The output

should give Gantt chart, turnaround time and waiting time for each process. Also
find the
average waiting time and turnaround time

SOLUTION:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1,p;
struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

printf("Enter no.of process:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);

printf("Enter priority:");

scanf("%d",&p->p);

p->bt1 = p->bt;

p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname at bt p tct tat wt


");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;

avg_tat+=tat;
avg_wt+=wt;

printf("%s %d %d %d %d %d %d
",
p->pname,p->at,p->bt,p->p,p->ct,tat,wt);
p=p->next;
}

printf("Avg TAT=%f Avg WT=%f


",
avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname at bt p
");
while(p!=NULL)
{
printf("%s %d %d %d
",
p->pname,p->at,p->bt1,p->p);
p = p->next;
}
}

void sort()
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;

t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;
t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;

t = p->p;
p->p = q->p;
q->p = t;
}

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_p()
{
NODE *p,*min_p=NULL;
int min=9999;

p = first;
while(p!=NULL)
{
if(p->at<=time && p->bt1!=0 &&
p->p<min)
{
min = p->p;
min_p = p;
}
p=p->next;
}

return min_p;
}

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void pnp()
{

int prev=0,n1=0;
NODE *p;
while(n1!=n)
{
p = get_p();

if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time+=p->bt1;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

p->ct = time;
p->bt1 = 0;

n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)

{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);
for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);

for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
pnp();
print_output();
print_gantt_chart();

return 0;
}

SLIP 17

Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the Optimal page

replacement algorithm. Assume the memory of n frames.

Reference String : 7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6,
SOLUTION:

#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2,
flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);

printf("Enter page reference string: ");

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


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

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


frames[i] = -1;
}

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


flag1 = flag2 = 0;

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


if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
flag3 =0;

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


temp[j] = -1;

for(k = i + 1; k < no_of_pages; ++k){


if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}

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


if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}

if(flag3 ==0){
max = temp[0];
pos = 0;

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


if(temp[j] > max){
max = temp[j];

pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}

printf("
");

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


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

printf("

Total Page Faults = %d", faults);

return 0;
}

Write the program to simulate FCFS CPU-scheduling. The arrival time and first CPU-
burst
of different jobs should be input to the system. Accept no. of Processes, arrival
time and burst
time. The output should give Gantt chart, turnaround time and waiting time for each
process.
Also find the average waiting time and turnaround time.

SOLUTION:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1;
struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()

{
NODE *p;
int i;

printf("Enter no.of process:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);

p->bt1 = p->bt;

p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname at bt ct tat wt
");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;
avg_tat+=tat;
avg_wt+=wt;

printf("%s %d %d %d %d %d
",
p->pname,p->at,p->bt,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f Avg WT=%f


",
avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname at bt
");
while(p!=NULL)
{
printf("%s %d %d
",
p->pname,p->at,p->bt1);
p = p->next;
}
}

void sort()
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);

strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;
t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;
}

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_fcfs()
{
NODE *p;

p = first;
while(p!=NULL)
{
if(p->at<=time && p->bt1!=0)
return p;

p=p->next;
}

return NULL;
}

struct gantt_chart

{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void fcfs()
{
int prev=0,n1=0;
NODE *p;

while(n1!=n)
{
p = get_fcfs();
if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time+=p->bt1;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

p->ct = time;
p->bt1 = 0;

n1++;
}

print_input();
sort();

}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)
{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);

for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);
for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
fcfs();
print_output();
print_gantt_chart();

return 0;
}

SLIP 18

Q.1 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the LRU page

replacement algorithm. Assume the memory of n frames.

Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
SOLUTION:

#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,
sp,m,n,time[MAX];
void accept()
{
int i;
printf("Enter no.of frames:");
scanf("%d", &n);
printf("Enter no.of references:");
scanf("%d", &m);
printf("Enter reference string:
");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}
void disp()
{
int i,j;
for(i=0;i<m;i++)
printf("%3d",ref[i]);
printf("

");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf(" ");
}
printf("
");

}
printf("Total Page Faults: %d
",faults);
}
int search(int pno)
{
int i;
for(i=0;i<n;i++)
{

if(frames[i]==pno)
return i;
}
return -1;
}
int get_lru()
{
int i,min_i,min=9999;
for(i=0;i<n;i++)
{
if(time[i]<min)
{
min = time[i];
min_i = i;
}
}
return min_i;
}

void lru()
{
int i,j,k;
for(i=0;i<m && sp<n;i++)
{
k=search(ref[i]);
if(k==-1)
{
frames[sp]=ref[i];
time[sp]=i;
faults++;
sp++;
for(j=0;j<n;j++)
mem[j][i]=frames[j];
}
else
time[k]=i;
}

for(;i<m;i++)
{
k = search(ref[i]);
if(k==-1)
{
sp = get_lru();

frames[sp] = ref[i];
time[sp] = i;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];
}
else
time[k]=i;
}
}

int main()
{
accept();
lru();
disp();
return 0;
}

Write a C program to simulate FCFS CPU-scheduling. The arrival time and first CPU-
burst
of different jobs should be input to the system. Accept no. of Processes, arrival
time and burst
time. The output should give Gantt chart, turnaround time and waiting time for each
process.
Also find the average waiting time and turnaround time.
SOLUTION:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1;
struct process_info *next;
}NODE;
int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

printf("Enter no.of process:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);

p->bt1 = p->bt;

p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname at bt ct tat wt
");

p = first;
while(p!=NULL)
{

int tat = p->ct-p->at;


int wt = tat-p->bt;
avg_tat+=tat;
avg_wt+=wt;

printf("%s %d %d %d %d %d
",
p->pname,p->at,p->bt,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f Avg WT=%f


",
avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname at bt
");
while(p!=NULL)
{
printf("%s %d %d
",
p->pname,p->at,p->bt1);
p = p->next;
}
}

void sort()
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{

strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;

t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;
}

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_fcfs()
{
NODE *p;

p = first;
while(p!=NULL)
{
if(p->at<=time && p->bt1!=0)
return p;

p=p->next;
}

return NULL;
}

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void fcfs()
{
int prev=0,n1=0;
NODE *p;

while(n1!=n)
{
p = get_fcfs();
if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time+=p->bt1;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

p->ct = time;
p->bt1 = 0;

n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)
{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);

for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);
for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
fcfs();
print_output();
print_gantt_chart();

return 0; }

SLIP 19

Q.1 Write a C program to implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret the following ‘list’
commands as

myshell$ list f dirname :- To print names of all the files in current

directory.

SOLUTION:

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <dirent.h>

void make_toks(char *s, char *tok[])

int i=0;
char *p;

p = strtok(s," ");

while(p!=NULL)

tok[i++]=p;

p=strtok(NULL," ");

tok[i]=NULL;

void list(char *dirname, char op)

DIR *dir;

struct dirent *entry;

int count = 0;

// Open the directory

dir = opendir(dirname);

if (dir == NULL)

printf("Directory %s not found.


", dirname);

return;

// Iterate through the entries in the directory

while ((entry = readdir(dir)) != NULL)

switch (op)

case 'f':

// Display the filename


printf("%s
", entry->d_name);

break;

case 'n':

// Increment the count

count++;

break;

case 'i':

// Display the filename and inode number

printf("%s: %lu
", entry->d_name, entry->d_ino);

break;

// Close the directory

closedir(dir);

// If the 'n' option was specified, print the count

if (op == 'n')

printf("Number of entries: %d
", count);

int main()

char buff[80],*args[10];

int pid;

while(1)

printf("myshell$ ");
fflush(stdin);

fgets(buff,80,stdin);

buff[strlen(buff)-1]='';

make_toks(buff,args);

if (strcmp(args[0], "list") == 0)

list(args[2], args[1][0]);

else

pid = fork();

if (pid > 0)

wait();

else

if (execvp(args[0], args) == -1)

printf("Bad command.
");

return 0;

Q.2 Write the simulation program for Round Robin scheduling for given time quantum.

The arrival time and first CPU-burst of different jobs should be input to the
system.

Accept no. of Processes, arrival time and burst time. The output should give the
Gantt

chart, turnaround time and waiting time for each process. Also display the average
turnaround time and average waiting time.

404 Error not found

SLIP 20

Q.1 Write a C program to implement the shell which displays the command prompt

“myshell$”. It accepts the command, tokenize the command line and execute it by

creating the child process. Also implement the additional command ‘typeline’ as

typeline -a filename :- To print all lines in the file.


SOLUTION:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}

int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);

if (strcmp(args[0], "typeline") == 0)
{

// Check if the '+n' option is specified


if (args[1][0] == '+')
{
// Read the line count
int n = atoi(args[1] + 1);
// Open the file
FILE *fp = fopen(args[2], "r");
if (fp == NULL)
{
printf("Error opening file %s.
", args[2]);
return 1;
}
// Read and print the first n lines
char line[80];
int count = 0;
while (count < n && fgets(line, 80, fp) != NULL)
{
printf("%s", line);
count++;
}
// Close the file
fclose(fp);
}
// If the '-a' option is specified
else if (strcmp(args[2], "-a") == 0)
{
// Open the file
FILE *fp = fopen(args[2], "r");
if (fp == NULL)
{
printf("Error opening file %s.
", args[2]);
return 1;
}
// Read and print the entire file
char line[80];
while (fgets(line, 80, fp) != NULL)
printf("%s", line);
// Close the file
fclose(fp);
}
else
{
printf("Invalid option.
");
}
}

}
}

Write the program to simulate Non-preemptive Shortest Job First (SJF) – scheduling.
The
arrival time and first CPU-burst of different jobs should be input to the system.
Accept no. of
Processes, arrival time and burst time. The output should give Gantt chart,
turnaround time
and waiting time for each process. Also find the average waiting time and
turnaround time

SOLUTION:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1;
struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

printf("Enter no.of process:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);
p->bt1 = p->bt;
p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname at bt ct tat wt
");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;

avg_tat+=tat;
avg_wt+=wt;

printf("%s %d %d %d %d %d
",
p->pname,p->at,p->bt,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f Avg WT=%f


",
avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname at bt
");
while(p!=NULL)
{
printf("%s %d %d
",
p->pname,p->at,p->bt1);
p = p->next;
}
}

void sort()
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;

t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_sjf()
{
NODE *p,*min_p=NULL;
int min=9999;

p = first;
while(p!=NULL)
{
if(p->at<=time && p->bt1!=0 &&
p->bt1<min)
{
min = p->bt1;
min_p = p;
}
p=p->next;
}

return min_p;
}

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void sjfnp()
{
int prev=0,n1=0;
NODE *p;

while(n1!=n)
{

p = get_sjf();

if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time+=p->bt1;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;
p->ct = time;
p->bt1 = 0;

n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)
{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);

for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);

for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
sjfnp();
print_output();
print_gantt_chart();

return 0;
}
SLIP 21

Q.1 Write a C Program to create a child process using fork (), display parent and
child

process id. Child process will display the message “I am Child Process�� and the
parent

process should display “I am Parent Process”.


SOLUTION:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

int pid=fork();

if(pid>0){

printf("I am parent process


");

printf("PID = %d

",getpid());

else if(pid==0){

printf("I am child process


");

printf("PID = %d
",getpid());

else{

printf("Failed to create child process");

return 0;

Q.2 Write a C program to simulate Preemptive Priority scheduling. The arrival time

and first CPU-burst and priority for different n number of processes should be

input to the algorithm. Assume the fixed IO

waiting time (2 units). The next CPU-burst should be generated randomly. The output
should
give Gantt chart, turnaround time and waiting time for each process. Also find the
average waiting time and turnaround time.

SOLUTION:

NEEDS IMPROVEMENT

SLIP 22

Q.1 Write a C program that demonstrates the use of nice() system call. After a
child

Process is started using fork (), assign higher priority to the child using nice
()

system call.

SOLUTION:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

int pid=fork();

int retnice;
if(pid==0){

retnice=nice(-15);

printf("
I am child process, PID = %d
",getpid());

printf("
Child gets higher priority %d
",retnice);

else if(pid>0){

retnice=nice(15);

printf("
I am parent process, PID = %d
",getpid());

printf("
Parent gets lower priority %d
",retnice);

return 0;

Q.2 Write a C program to simulate Non preemptive priority scheduling. The arrival

time and first CPU-burst of different jobs should be input to the system. Accept

no. of Processes, arrival time and burst time. The output should give Gantt

chart, turnaround time and waiting time for each process. Also find the average

waiting time and turnaround

time.

SOLUTION:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct process_info


{
char pname[20];
int at,bt,ct,bt1,p;
struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
NODE *p;
int i;

printf("Enter no.of process:");


scanf("%d",&n);

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

{
p = (NODE*)malloc(sizeof(NODE));

printf("Enter process name:");


scanf("%s",p->pname);

printf("Enter arrival time:");


scanf("%d",&p->at);

printf("Enter first CPU burst time:");


scanf("%d",&p->bt);

printf("Enter priority:");
scanf("%d",&p->p);

p->bt1 = p->bt;

p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last = p;
}
}

void print_output()
{
NODE *p;
float avg_tat=0,avg_wt=0;

printf("pname at bt p tct tat wt


");

p = first;
while(p!=NULL)
{
int tat = p->ct-p->at;
int wt = tat-p->bt;
avg_tat+=tat;
avg_wt+=wt;

printf("%s %d %d %d %d %d %d
",

p->pname,p->at,p->bt,p->p,p->ct,tat,wt);

p=p->next;
}

printf("Avg TAT=%f Avg WT=%f


",
avg_tat/n,avg_wt/n);
}

void print_input()
{
NODE *p;

p = first;

printf("pname at bt p
");
while(p!=NULL)
{
printf("%s %d %d %d
",
p->pname,p->at,p->bt1,p->p);
p = p->next;
}
}

void sort()
{
NODE *p,*q;
int t;
char name[20];

p = first;
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(p->at > q->at)
{
strcpy(name,p->pname);
strcpy(p->pname,q->pname);
strcpy(q->pname,name);

t = p->at;
p->at = q->at;
q->at = t;
t = p->bt;
p->bt = q->bt;
q->bt = t;

t = p->ct;
p->ct = q->ct;
q->ct = t;

t = p->bt1;
p->bt1 = q->bt1;
q->bt1 = t;

t = p->p;
p->p = q->p;
q->p = t;
}

q=q->next;
}

p=p->next;
}
}

int time;

NODE * get_p()
{
NODE *p,*min_p=NULL;
int min=9999;

p = first;
while(p!=NULL)
{
if(p->at<=time && p->bt1!=0 &&
p->p<min)
{
min = p->p;
min_p = p;
}
p=p->next;
}

return min_p;

struct gantt_chart
{
int start;
char pname[30];
int end;
}s[100],s1[100];

int k;

void pnp()
{
int prev=0,n1=0;
NODE *p;

while(n1!=n)
{
p = get_p();

if(p==NULL)
{
time++;
s[k].start = prev;
strcpy(s[k].pname,"*");
s[k].end = time;

prev = time;
k++;
}
else
{
time+=p->bt1;
s[k].start = prev;
strcpy(s[k].pname, p->pname);
s[k].end = time;

prev = time;
k++;

p->ct = time;
p->bt1 = 0;

n1++;
}

print_input();
sort();
}
}

void print_gantt_chart()
{
int i,j,m;

s1[0] = s[0];

for(i=1,j=0;i<k;i++)
{
if(strcmp(s[i].pname,s1[j].pname)==0)
s1[j].end = s[i].end;
else
s1[++j] = s[i];
}

printf("%d",s1[0].start);
for(i=0;i<=j;i++)
{
m = (s1[i].end - s1[i].start);

for(k=0;k<m/2;k++)
printf("-");

printf("%s",s1[i].pname);

for(k=0;k<(m+1)/2;k++)
printf("-");

printf("%d",s1[i].end);
}
}

int main()
{
accept_info();
sort();
pnp();
print_output();
print_gantt_chart();

return 0;
}
SLIP 23

Q.1 Write a C program to illustrate the concept of orphan process. Parent process
creates

a child and terminates before child has finished its task. So child process becomes

orphan process. (Use fork(), sleep(), getpid(), getppid())

SOLUTION:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

int pid;

pid=getpid();

printf("Current Process ID is : %d
",pid);

printf("
[Forking Child Process ... ]
");

pid=fork();

if(pid < 0)

printf("
Process can not be created ");

else

{
if(pid==0)

printf("
Child Process is Sleeping ...");

sleep(5);

printf("
Orphan Process ID : %d",getppid());

else

{ /* Parent Process */

printf("
Parent Process Completed ...");

return 0;

Q.2 Write the simulation program for demand paging and show the page

scheduling and total number of page faults according the Optimal page

replacement algorithm. Assume the memory of n frames.

Reference String : 7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6,

SOLUTION:

#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2,
flag3, i, j,
k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);

printf("Enter page reference string: ");


for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}

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


frames[i] = -1;
}

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


flag1 = flag2 = 0;

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


if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
flag3 =0;

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


temp[j] = -1;

for(k = i + 1; k < no_of_pages; ++k){


if(frames[j] == pages[k]){
temp[j] = k;
break;

}
}
}

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


if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}

if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}

printf("
");

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


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

printf("

Total Page Faults = %d", faults);

return 0;
}

SLIP 24

Q.1 Write a C program to accept n integers to be sorted. Main function

creates child process using fork system call. Parent process sorts the integers
using

bubble sort and waits for child process using wait system call. Child process sorts

the integers using insertion sort.

SOLUTION:

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
void bubblesort(int arr[30],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void insertionsort(int arr[30], int n)
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
j = i - 1;

while(j>=0 && arr[j] > temp)

{
arr[j+1] = arr[j];
j --;
}
arr[j+1] = temp;
}
}
void fork1()
{
int arr[25],arr1[25],n,i,status;
printf("
Enter the no of values in array :");
scanf("%d",&n);
printf("
Enter the array elements :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
int pid=fork();
if(pid==0)
{
sleep(10);
printf("Child process PID = %d
",getpid());
printf("
Elements Sorted Using insertionsort:");
insertionsort(arr,n);
printf("
");
for(i=0;i<n;i++)
printf("%d,",arr[i]);
printf("#");
printf("
parent process id=%d
",getppid());
system("ps -x");
}
else
{
printf("
Parent process PID = %d
",getppid());
printf("Elements Sorted Using bubblesort:");
bubblesort(arr,n);
printf("
");
for(i=0;i<n;i++)
printf("%d,",arr[i]);
printf("

");
}
}
int main()
{
fork1();
return 0;
}

Q.2 Write a C program to implement the toy shell. It should display the command
prompt

“myshell$”. Tokenize the command line and execute the given command by creating

the child process. Additionally it should interpret the following commands.

count c filename :- To print number of characters in the file.


count w filename :- To print number of words in the file.
count l filename:- To print number of lines in the file.

SOLUTION:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void count(char *fn, char op)
{
FILE *fh;
int cc=0,wc=0,lc=0;
char c;
fh = fopen(fn, "r");

if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

while ((c = fgetc(fh)) != EOF)


{
if (c == ' ')
wc++;
else if (c == '
')
{
wc++;
lc++;
}
cc++;
}
fclose(fh);
switch (op)
{
case 'c':
printf("No.of characters:%d
", cc - 1);
break;
case 'w':
printf("No.of words:%d
", wc);
break;
case 'l':
printf("No.of lines:%d
", lc + 1);
break;
}
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if(strcmp(args[0],"count")==0)
count(args[2],args[1][0]);
else
{

pid = fork();
if(pid>0)
wait();
else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}

SLIP 25

Q.1 Write a C program that accepts an integer array. Main function forks child
process.

Parent process sorts an integer array and passes the sorted array to child process
through the command line arguments of execve() system call. The child process uses

execve() system call to load new program that uses this sorted array for
performing

the binary search to search the particular item in the array.


SOLUTION:

Do it yourself

Q.2 Write a programto implement the shell. It should display the command prompt

“myshell$”. Tokenize the command line and execute the given command by

creating the child process. Additionally it should interpret the following

commands.

myshell$ search f filename pattern :- To display first occurrence of


pattern in the file.

SOLUTION:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
void search(char *fn, char op, char *pattern)

{
FILE *fh;
int count = 0;
char line[80];

fh = fopen(fn, "r");
if (fh == NULL)
{
printf("File %s not found.
", fn);
return;
}

while (fgets(line, 80, fh) != NULL)


{
if (strstr(line, pattern) != NULL)
{
count++;
if (op == 'a')
printf("%s", line);
}
}

fclose(fh);

if (op == 'c')
printf("Number of occurrences: %d
", count);
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
printf("myshell$ ");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='';
make_toks(buff,args);
if (strcmp(args[0], "search") == 0)
search(args[2], args[1][0], args[3]);
else
{
pid = fork();
if(pid>0)
wait();

else
{
if(execvp(args[0],args)==-1)
printf("Bad command.
");
}
}
}
return 0;
}

You might also like