Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago