0% found this document useful (1 vote)
545 views2 pages

Work Shop B

This document contains code for a simple shell program that forks processes to execute commands. It uses functions like fork(), execvp(), waitpid() and others to spawn child processes, execute commands, and wait for processes to finish. The main loop gets input, splits it into words, handles built-in commands like cd, otherwise forks a child process which executes the command and the parent waits for it to complete before looping.

Uploaded by

hisanju88
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
545 views2 pages

Work Shop B

This document contains code for a simple shell program that forks processes to execute commands. It uses functions like fork(), execvp(), waitpid() and others to spawn child processes, execute commands, and wait for processes to finish. The main loop gets input, splits it into words, handles built-in commands like cd, otherwise forks a child process which executes the command and the parent waits for it to complete before looping.

Uploaded by

hisanju88
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# include<stdlib.

h>
# include<stdio.h>
# include<string.h>
# include<sys/types.h>
# include<sys/wait.h>
# include<unistd.h>
# include<errno.h>

#define MAXLINE 4096


#define MAXWORDS MAXLINE /2 // A line can have at least at most MAX LINE/2 words

void tokenize (char *line, char **words, int *nwords);


/* Berak lines into words seprated by whitespace , placing them in the array words and setting
the count to nowords, dosent handle quoted strings so no spaces in names as "cd" builtin only uses words[1]!!! */

main()
{
char line [MAXLINE], *words[MAXWORDS], message[MAXLINE];
int stop=0, nwords=0;
int result, pid, status;

while(1)
{
printf ("WS B Shell $ ");
if (NULL == fgets(line , MAXLINE, stdin))
return 0;
// exit if fgets() returns NULL (NULL is returnd on error or when end of file occurs while no characters have been read)
tokenize (line,words,&nwords);

if ((strcmp(words[0],"exit") == 0) && (nwords == 1)) // exit if word exit appears in the input as the first word of a line
return 0;

if (strcmp(words[0],"cd") == 0)
{
result = chdir(words[1]); // chdir changes the current working directory to that specified in path

if (result < 0) // On success, zero is returned. On error, -1 is returned


printf ("No such directory\n");
}
else{
pid_t child;
switch(child = fork())
{
case -1: // error occurs creating a new process
perror("fork");
exit(1);
break;
case 0: // control is returned to the child
//printf("I am the child my pid is: %d\n", getpid());
execvp(*words, words); // find_and_execute
perror(*words); // report error
default: // control is returned to the parent ( >0 )
//printf("I am the parent, my pid is %d," , getpid());
//printf("and my childs is %d\n", child);
break;
}
wait(&status); = waitpid(-1, &status, 0); //pause until the created process is concluded
// new process has fnished and print out the exit status of that process.
if (WIFEXITED(status)) // returns ture if the child terminated normally
{
printf("exited, status=%d\n", WEXITSTATUS(status)); // returns the exit status of the child
}
else if (WIFSIGNALED(status)) // returns true if the child was terminated by a signal
{
printf("killed by signal %d\n", WTERMSIG(status)); // returns the number of the signal that caused the child
process to terminate
}
else if (WIFSTOPPED(status)) // returns true if the child process was stopped by delivery of a signal
{
printf("stopped by signal %d\n", WSTOPSIG(status)); // returns the number of the signal which caused the child
to stop
}
else if (WIFCONTINUED(status)) // returns ture if the child process was resumed by delivery of SIGCONT
{
printf("continued\n");
}
//=================================================================================================
void tokenize (char * line , char **words, int * nwords)
{ *nwords = 1;
for (words[0] = strtok(line, " \t\n");
(*nwords < MAXWORDS) && (words[*nwords] = strtok(NULL, " \t\n"));
*nwords = *nwords + 1);
return;
}

You might also like