2 years ago

#29235

test-img

Lexlein

How can i stop and continue an execvp() call?

I wrote a program which stops the time of a child process calling execvp(). The next task is to occasionally stop the child process for a given time and continue it.

pid_t child = fork();
if(child == 0){
    // In child process
    execvp(); // e.g. sleep 10
} else {
    // In parent process
    kill(child, SIGSTOP);
    // Do stuff
    kill(child, SIGCONT);
}

What i think is happening here is, that kill(child, SIGSTOP) is pausing the child process but not the execvp() process within it. Is there a way to get the pid of the execvp() process and stop it directly?

Edit example:

int main(int argsc, char *argv[]) {

int status;

// Fork for processB
pid_t processB = fork();

if (processB == 0) {
    system("~/loop.sh");     
} else {
    for (;;) {
        sleep(5);
        kill(processB, SIGSTOP);
        std::cout << "Should have stopped counting" << std::endl;
        sleep(3);
        kill(processB, SIGCONT);

        // If loop process ended, exit watchDog
        if (waitpid(processB, &status, WNOHANG) == processB)
            _exit(1);
        }
    }
    return 0;
}

Loop.sh

counter=0;
while :;
    counter=$((counter+1));
    do echo $counter;
    sleep 1;
done
return 0;

c++

signals

fork

exec

kill

0 Answers

Your Answer

Accepted video resources