How to cancel an alarm () through a child process?

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# /bin/fgdsfgs
 Error executing program
 input# Error - Expired After 10 Seconds.

, , , . !

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);
    }
}
+3
2

:

alarm() SIGALRM , , , . .

0, , , .

; SIGALRM. SIGALRM , , SIGALRM.

alarm() setitimer(), ualarm() usleep() .

, : alarm(0). .

, :

if(child == 0){ //Working in child
    if(-1 == execve(cmd,arg,env)){  //Execute program or error
        write(2, error_exe, sizeof(error_exe));
        _exit(EXIT_FAILURE);  // EXIT OR A FORKED SHELL WILL KEEP GOING
    }   
}else if(child != 0){   //Working in the parent
+6

wait() ; ( ) ? , , , StackOverflow.

execve() ( exec*()); , .

. , , while (1), .

if (child == 0)
{
    execve(cmd, arg, env);
    write(2, error_exe, sizeof(error_exe));
    exit((errno == ENOEXEC) ? 126 : 127);
}

, ; , , . POSIX shell.

+1

All Articles