OPERATING SYSTEM - I SLIPS SOLUTION
· SLIP NO 1b, SLIP NO 13A, SLIP14A, SLIP 20A
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.
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 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;
return i;
void typeline(char *op, char *fn) {
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+1) break;
while(read(fh, &c, 1) > 0) {
printf("%c", c);
close(fh);
int main() {
char buff[80],
*args[10];
while(1) {
printf ("\n");
printf("\nmyshell$ ");
fgets(buff, 80, stdin);
buff[strlen(buff)-1] = '\0';
int n = make_toks(buff, args);
switch (n) {
case 1:
if(strcmp(args[0], "exit") == 0)
exit(1);
if (!fork())
execlp (args [0], args[0], NULL);
break;
case 2:
if (!fork ())
execlp (args [0], args[0], args[1], NULL);
break;
case 3:
if (strcmp(args[0], "typeline") == 0)
typeline (args[1], args[2]);
else {
if (!fork ())
execlp (args [0], args[0], args[1], args[2], NULL);
break;
case 4:
if (!fork ())
execlp (args [0], args [0], args [1], args [2], args [3], NULL);
break;
return 0;
Output :
myshell$ typeline a text.txt
pune
kolkata
doremon
mumbai
vadapav
chandigarh
pune
prisonbreak
pogo
misalpav
gogo
pune
\0
myshell$ typeline 3 text.txt
pune
kolkata
doremon
myshell$ typeline -5 text.txt
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>
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;
}
· SLIP NO 3B , SLIP 16A , SLIP 24B
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>
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-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
myshell$ count w info.txt
No.of words:3
myshell$ count l info.txt
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>
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;
}
· 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
int pid = fork();
if (pid > 0) {
printf("I am Parent process\n");
printf("ID : %d\n\n", getpid());
}
else if (pid == 0) {
printf("I am Child process\n");
// getpid() will return process id of child process
printf("ID: %d\n", getpid());
}
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:
I am parent process, id=6555
Priority :6,id=6555
I am child process, id=6556
Priority :-17,id=6556
· SLIP 23A
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()).
CODE:-
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid;
pid=getpid();
printf("Current Process ID is : %d\n",pid);
printf("\n[Forking Child Process ... ] \n");
pid=fork();
if(pid < 0)
{
printf("\nProcess can not be created ");
}
else
{
if(pid==0)
{
printf("\nChild Process is Sleeping ...");
sleep(5);
printf("\nOrphan Child's Parent ID : %d",getppid());
}
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
[Forking Child Process ... ]
Parent Process Completed ...
@kali-linux:~/Desktop/Ty$ Child Process is Sleeping ...
@kali-linux:~/Desktop/Ty$
· 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 :-
Program: Sorting and Binary Search using fork() and execve()
sorting_binary_search.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
// Function to swap two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to sort an array in ascending order
void sort_array(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
// Child process function to perform binary search
void binary_search(int arr[], int size, int target) {
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
printf("Element %d found at index %d\n", target, mid);
return;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
printf("Element %d not found in the array\n", target);
}
int main() {
int arr[] = {5, 2, 8, 12, 3};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 8; // Element to search
// Create a child process
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
// Parent process
if (pid > 0) {
// Sort the array
sort_array(arr, size);
// Convert array to string for command line arguments
char args[size * 2 + 1];
args[0] = '\0';
for (int i = 0; i < size; i++) {
char num_str[12];
sprintf(num_str, "%d ", arr[i]);
strcat(args, num_str);
}
// Pass sorted array to child process through command line arguments
char *argv[] = {"binary_search", args, NULL};
char *envp[] = {NULL};
// Execute binary_search program
execve("./binary_search", argv, envp);
perror("execve");
exit(1);
}
// Child process
else {
// Wait for parent to finish execution
sleep(1);
// Get command line arguments
char *argv[2];
argv[0] = "binary_search";
argv[1] = NULL;
char *args = argv[1];
// Get sorted array from command line arguments
int sorted_arr[size];
char *token = strtok(args, " ");
int i = 0;
while (token != NULL) {
sorted_arr[i++] = atoi(token);
token = strtok(NULL, " ");
}
// Perform binary search
binary_search(sorted_arr, size, target);
return 0;
}
return 0;
}
binary_search.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// Get command line arguments
char *args = argv[1];
// Get sorted array from command line arguments
int size = argc - 2;
int sorted_arr[size];
char *token = strtok(args, " ");
int i = 0;
while (token != NULL) {
sorted_arr[i++] = atoi(token);
token = strtok(NULL, " ");
}
// Perform binary search
int target = 8; // Element to search
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (sorted_arr[mid] == target) {
printf("Element %d found at index %d\n", target, mid);
return 0;
} else if (sorted_arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
printf("Element %d not found in the array\n", target);
return 0;
}