luatest icon indicating copy to clipboard operation
luatest copied to clipboard

Get rid of sleeps after process:start()

Open rosik opened this issue 5 years ago • 1 comments

Sleeps are nasty. Better use fair system waits instead.

Here is an example of parent process waiting for his child to call exec. Parent can even detect if exec failed, but with C macro (which are hard to adopt in ffi):

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ptrace.h>

int main(int argc, char **argv)
{
    printf("PTRACE_TRACEME = %d\n", PTRACE_TRACEME);
    printf("PTRACE_DETACH = %d\n", PTRACE_DETACH);

    setlinebuf(stdout);
    pid_t i = fork();
    printf("Forked %d\n", i);
    if (i == 0)
    {
        // child
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);

        sleep(1);

        // printf("Child: executing bad-command\n");
        // execlp("bad-command", "", NULL);

        printf("Child: executing sleep 3\n");
        execlp("bash", "bash", "-c", "echo; echo sleeping...; sleep 3; echo done;", NULL);

        perror("exec failed");
        _exit(1);
    }
    else if (i > 0)
    {
        int status;
        waitpid(i, &status, 0);
        ptrace(PTRACE_DETACH, i, NULL, NULL);

        if (WIFEXITED(status)) {
            printf("Parent: child exit status %d\n", WEXITSTATUS(status));
        } else if (WIFSIGNALED(status)) {
            printf("Parent: child killed by signal %d\n", WTERMSIG(status));
        } else {
            printf("Parent: exec succeeded, child keeps running. Detaching\n");
        }

        _exit(0);
    }
    else
    {
        perror("fork failed");
        _exit(3);
    }
}


rosik avatar May 30 '19 22:05 rosik

Does 264130acc568a166dbbbeb221244e316fa61f8a3 fix it in #12?

printercu avatar Jun 14 '19 19:06 printercu