Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Shell programming : Process •Create a simple shell program that repeats the foll

ID: 3779982 • Letter: S

Question

Shell programming : Process

•Create a simple shell program that repeats the following until the user hits ^D

–Show the prompt “$ “

–Read the command string (use scanf)

•scanf returns EOF if the user hits ^D, in which case you should “break” out of the loop

–Fork off a child process

•Child process executes the command using execlp

–Child process should check if execlp failed, in which case it should exit with a positive exit status code.

–Wait for the child process to terminate

I have this code so far

#include<stdio.h>
#include<unistd.h>
#include<sys/type.h>
#include<sys/wait.h>

int main(){

char buf[10];
pid_t pid;

while(1){

printf("$ ");
scanf("s",buf);
pid = fork();

if(pid==0){

execlp(buf,buf, (char *) NULL);
}

wait(NULL);

}


}

Explanation / Answer

#include<stdio.h>
#include<unistd.h>
#include<sys/type.h>
#include<sys/wait.h>
int main(){
char buf[10];
pid_t pid;
while(1){
printf("$ ");
scanf("s",buf);
// checking if condition
if(s == D){
   exit(0);
}

pid = fork();
if(pid==0){
   execlp(buf,buf, (char *) NULL);
   exit(errno); // exit safely
}
wait(NULL);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote