Os - I Slips Solution Done
Os - I Slips Solution Done
CODE:-
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
int i = 0;
char *p;
while(p != NULL) {
tok[i++] = p;
tok[i] = NULL;
return i;
int fh,
i,
j,
n;
char c;
fh = open(fn, O_RDONLY);
if(fh == -1) {
return;
if(strcmp(op, "a") == 0) {
printf("%c", c);
close(fh);
return;
n = atoi(op);
if(n > 0) {
i = 0;
if(i == n) break;
if(n < 0) {
i = 0;
lseek(fh, 0, SEEK_SET);
j = 0;
printf("%c", c);
close(fh);
int main() {
char buff[80],
*args[10];
while(1) {
printf ("\n");
printf("\nmyshell$ ");
buff[strlen(buff)-1] = '\0';
switch (n) {
case 1:
if(strcmp(args[0], "exit") == 0)
exit(1);
if (!fork())
break;
case 2:
if (!fork ())
break;
case 3:
if (strcmp(args[0], "typeline") == 0)
else {
if (!fork ())
break;
case 4:
if (!fork ())
execlp (args [0], args [0], args [1], args [2], args [3], NULL);
break;
return 0;
Output :
pune
kolkata
doremon
mumbai
vadapav
chandigarh
pune
prisonbreak
pogo
misalpav
gogo
pune
\0
pune
kolkata
doremon
pogo
misalpav
gogo
pune
\0
<---text.txt-->
pune
kolkata
doremon
mumbai
vadapav
chandigarh
pune
prisonbreak
pogo
misalpav
gogo
pune
\0
· SLIP NO 2B, SLIP 10B, SLIP 11B SLIP 12B, SLIP15A, SLIP19A
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 .
CODE:-
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
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;
}
Q.2 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.
count l filename :- To print number of lines in the file.
CODE : -
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
tok[i]=NULL;
int fh,cc=0,wc=0,lc=0;
char c;
fh = open(fn,O_RDONLY);
if(fh==-1)
return;
while(read(fh,&c,1)>0)
else if(c=='\n')
wc++;
lc++;
}
cc++;
close(fh);
switch(op)
case 'c':
printf("No.of characters:%d\n",cc-1);
break;
case 'w':
printf("No.of words:%d\n",wc);
break;
case 'l':
printf("No.of lines:%d\n",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]='\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;
Output :
myshell$ count c info.txt
No.of characters:45
No.of words:3
No.of lines:3
myshell$
<---info.txt--->
Hello world
Ramayan-Valmiki
Bhagwatgeeta-Vyasa
· SLIP NO 4B ,SLIP 5B, SLIP6B, SLIP7B, SLIP 8B, SLIP9B, SLIP 25B
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.
CODE:-
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
p = strtok(s," ");
while(p!=NULL)
{
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}
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;
}
· SLIP21 A
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”.
CODE:-
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// fork() Create a child process
}
else {
printf("Failed to create child process");
}
return 0;
}
Output :
@kali-linux:~/Desktop/Ty$ ./a.out
I am Parent process
ID : 3698
I am Child process
ID: 3699
· SLIP22 A
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.
CODE:-
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
{
printf("\nI am child process, id=%d\n",getpid());
printf("\nPriority :%d,id=%d\n",nice (-7),getpid());
}
else
{
printf("\nI am parent process, id=%d\n",getpid());
nice(1);
printf("\nPriority :%d,id=%d\n",nice (15),getpid());
}
return 0;
}
Output:
· SLIP 23A
else
{ /* Parent Process */
printf("\nParent Process Completed ...");
}
}
return 0;
}
Output :
@kali-linux:~/Desktop/Ty$ cc qq.c
@kali-linux:~/Desktop/Ty$ ./a.out
Current Process ID is : 5546
· SLIP 25A
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.
Here is a C program that accomplishes the task:
CODE :-
sorting_binary_search.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main() {
int arr[] = {5, 2, 8, 12, 3};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 8; // Element to search
if (pid < 0) {
perror("fork");
exit(1);
}
// Parent process
if (pid > 0) {
// Sort the array
sort_array(arr, size);
// Child process
else {
// Wait for parent to finish execution
sleep(1);
return 0;
}
binary_search.c
#include <stdio.h>
#include <stdlib.h>