How do I create a daemon in uClinux using vfork?

That would be easy with fork (), but I don't have MMU. I heard that vfork () blocks the parent process until the child exits or execs exec (). How could I do something like this?

pid_t pid = vfork();

if (pid == -1) {
    // fail
    exit(-1);
}

if (pid == 0) {
    // child
    while(1) {
        // Do my daemon stuff
    }

    // Let pretend it exits sometime
    exit();
} 

// Continue execution in parent without blocking.....
+3
source share
3 answers

There seems to be no way to do this exactly as you have here. execor _exitmust be called to continue execution by the parent. Either enter the daemon code in another executable file and exec, or use the child to create the original task. The second approach is a sneaky approach and is described here.

+3
source

daemon() uClinux MMU fork() Jamie Lokier

daemon() vfork(). - MMU, vfork(), ( ), ( ).

, Linux (). , , daemon() ! MMU. Jamie Lokier , ARM i386, .

: Jamie Lokier() ! MMU Linux .

+3

, , , -, " ".

, ( , ) clone, :

pid_t new_vfork(void) {
    return clone(child_func,        /* child function */
                 child_stack,          /* child stack    */
                 SIGCHLD | CLONE_VM,   /* flags */
                 NULL,                 /* argument to child */
                 NULL,                 /* pid of the child */
                 NULL,                 /* thread local storage for child */
                 NULL);                /* thread id of child in child mem */
}

, child_stack child_func , vfork, , child_func clone, child_stack (sys_clone) .

, sys_clone

pid_t new_vfork(void) {
    return sys_clone( SIGCHLD | CLONE_VM, NULL);
}

, , , . NULL , child_stack, ​​ , vfork fork, , .

I have never used sys_clonedirectly or tested this, but I think it should work. I think that:

  sys_clone( SIGCHLD | CLONE_VM | CLONE_VFORK, NULL);

equivalently vfork.

If this does not work (and you can not figure out how to make something like that), you can use a normal call clone with the challenges setjumpand longjmpto emulate it, or you can bypass the need for semantics "to return twice" forkand vfork.

+1
source

Source: https://habr.com/ru/post/1768674/


All Articles