Tuesday, July 26, 2022

Operating System Assignment 2

 Operating System Assignment 2 

Operations on Processes -Simulation of Operating System Shell and its working


Practical Assignments:

Set A

Write a C program that behaves like a 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 ‘count’ as

myshell$ count c filename: It will display the number of characters in given file

myshell$ count w filename: It will display the number of words in given file

myshell$ count l filename: It will display the number of lines in given file


Program:-

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.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)

{

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

char c;

fh = open(fn,O_RDONLY);

if(fh==-1)

{

printf("File %s not found.\n",fn);

return;

}

while(read(fh,&c,1)>0)

{

if(c==' ') wc++;

else if(c=='\n')

{

wc++;

lc++;

}

cc++;

}

close(fh);

switch(op)

{

case 'c':

printf("No.of characters:%d\n",cc);

break;

case 'w':

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

break;

case 'l':

printf("No.of lines:%d\n",lc);

break;

}

}

int main()

{

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

int pid;

while(1)

{

printf("myshell$");

fflush(stdin);

fgets(buff,80,stdin);

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

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.\n");

}

}

}

return 0;

}


Set B

Write a C program that behaves like a 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 ‘list’ as

myshell$ list f dirname: It will display filenames in a given directory.

myshell$ list n dirname: It will count the number of entries in a given directory.

myshell$ list i dirname: It will display filenames and their inode number for the files in a given directory.


Program :-

#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 *dn, char op)

{

DIR *dp;

struct dirent *entry;

int dc=0,fc=0;

dp = opendir(dn);

if(dp==NULL)

{

printf("Dir %s not found.\n",dn);

return;

}

switch(op)

{

case 'f':

while(entry=readdir(dp))

{

if(entry->d_type==DT_REG)

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

}

break;

case 'n':

while(entry=readdir(dp))

{

if(entry->d_type==DT_DIR) dc++;

if(entry->d_type==DT_REG) fc++;

}

printf("%d Dir(s)\t%d File(s)\n",dc,fc);

break;

case 'i':

while(entry=readdir(dp))

{

if(entry->d_type==DT_REG)

printf("%s\t%d\n",entry->d_name,entry->d_fileno);

}

}

closedir(dp);

}

int main()

{

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

int pid;

while(1)

{

printf("myshell$");

fflush(stdin);

fgets(buff,80,stdin);

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

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.\n");

}

}

}

return 0;

}


Set C

(1)Write a C program that behaves like a 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

myshell$ typeline n filename: It will display first n lines of the file.

myshell$ typeline -n filename: It will display last n lines of the file.

myshell$ typeline a filename: It will display all the lines of the file.

Program:-

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.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 typeline(char *fn, char *op)

{

int fh,i,j,n;

char c;

fh = open(fn,O_RDONLY);

if(fh==-1)

{

printf("File %s not found.\n",fn);

return;

}

if(strcmp(op,"a")==0)

{

while(read(fh,&c,1)>0)

printf("%c",c);

close(fh);

return;

}

n = atoi(op);

if(n>0)

{

i=0;

while(read(fh,&c,1)>0)

{

printf("%c",c);

if(c=='\n') i++;

if(i==n) break;

}

}

if(n<0)

{

i=0;

while(read(fh,&c,1)>0)

{

if(c=='\n') i++;

}

lseek(fh,0,SEEK_SET);

j=0;

while(read(fh,&c,1)>0)

{

if(c=='\n') j++;

if(j==i+n) break;

}

while(read(fh,&c,1)>0)

{

printf("%c",c);

}

}

close(fh);

}

int main()

{

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

int pid;

while(1)

{

printf("myshell$");

fflush(stdin);

fgets(buff,80,stdin);

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

make_toks(buff,args);

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

typeline(args[2],args[1]);

else

{

pid = fork();

if(pid>0)

wait();

else

{

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

printf("Bad command.\n");

}

}

}

return 0;

}


(2)Write a C program that behaves like a 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 ‘search’ as

myshell$ search f filename pattern : It will search the first occurrence of pattern in the given file

myshell$ search a filename pattern : It will search all the occurrence of pattern in the given file

myshell$ search c filename pattern : It will count the number of occurrence of pattern in the given file


Program:-
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.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)
{
int fh,count=0,i=0,j=0;
char buff[255],c,*p;

fh = open(fn,O_RDONLY);
if(fh==-1)
{
printf("File %s Not Found\n",fn);
return;
}

switch(op)
{
case 'f':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j=0;
i++;
if(strstr(buff,pattern))
{
printf("%d: %s",i,buff);
break;
}
}
}
break;
case 'c':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j=0;
p = buff;
while(p=strstr(p,pattern))
{
count++;
p++;
}
}
}
printf("Total No.of Occurrences = %d\n",count);
break;
case 'a':
while(read(fh,&c,1))
{
buff[j++]=c;
if(c=='\n')
{
buff[j]='\0';
j = 0;
i++;
if(strstr(buff,pattern))
printf("%d: %s",i,buff);
}
}
}//switch
close(fh);
}//search

int main()
{
char buff[80],*args[10];
int pid;

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

return 0;
}

No comments:

Post a Comment