Question: #include #include #include #include #include int main ( ) { pid _ t p; char input [ 1 0 0 ] ; while ( 1

#include
#include
#include
#include
#include
int main()
{
pid_t p;
char input[100];
while (1){
printf("Enter a command (or 'Exit' to quit): ");
fgets(input, sizeof(input), stdin);
// Remove the newline character from the input
input[strcspn(input,"
")]='\0';
if (strcmp(input, "Exit")==0){
printf("Exiting...
");
break;
}
p = fork();
if (p ==-1){
perror("Fork error");
exit(2);}
if (p ==0)// Child process
{ char *arg[20]; // Maximum of 20 arguments
int i =0;
char *token = strtok(input,"");
while (token != NULL){
arg[i++]= token;
token = strtok(NULL,"");
}
arg[i]= NULL; // Null-terminate the argument list
// Execute the command
if (execvp(arg[0], arg)==-1){
perror("Exec error");
exit(2);
}
} else {
// Parent process
int status;
wait(&status);
if (WIFEXITED(status)){
printf("Child process exited with status %d
", WEXITSTATUS(status));
} else {
perror("Wait error");
exit(2);
}
}
}
}
how can we use four exec functions in above program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!