For assignment, I am working on creating a time-based shell. A shell wags and executes commands and kills them if they work more than a certain amount of time. For instance.
input# /bin/ls
a.out code.c
input# /bin/cat
Error - Expired After 10 Seconds.
input#
Now, my question is: is there a way to prevent the alarm from starting if there is an error while processing the program, that is, when exevce returns -1?
Since the child process is performed separately, and after several hours of experimentation and research, I still have to find something that discusses or even hints at this type of task, I have a feeling that this may not be possible. If this is really impossible, how can I prevent something like the following ...
input
Error executing program
input
, , , . !
while(1){
write(1, prompt, sizeof(prompt)); //Prompt user
byteCount = read(0, cmd, 1024); //Retrieve command from user, and count bytes
cmd[byteCount-1] = '\0'; //Prepare command for execution
//Create Thread
child = fork();
if(child == -1){
write(2, error_fork, sizeof(error_fork));
}
if(child == 0){ //Working in child
if(-1 == execve(cmd,arg,env)){ //Execute program or error
write(2, error_exe, sizeof(error_exe));
}
}else if(child != 0){ //Working in the parent
signal(SIGALRM, handler); //Handle the alarm when it goes off
alarm(time);
wait();
alarm(0);
}
}