qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 0/4] net-bridge: rootless bridge support for qem


From: Anthony Liguori
Subject: Re: [Qemu-devel] [PATCH 0/4] net-bridge: rootless bridge support for qemu
Date: Fri, 06 Nov 2009 06:42:44 -0600
User-agent: Thunderbird 2.0.0.23 (X11/20090825)

Jamie Lokier wrote:
Avi Kivity wrote:
I know this has been discussed before, but isn't this why there are things like vfork()?
vfork() doesn't work with threads - it requires that the calling process be halted until exec() is called.

On Linux (NPTL), vfork() is good for this.  It only halts the calling
thread.  Other threads continue to run, and when the vfork'd thread
called exec*(), it doesn't affect the other threads.  Of course this
behaviour isn't portable, but then again, neither is KVM.
Yup, see attached program. Here's the output which confirms this is the case.

tick
sleeping
tick
tick
tick
tick
tick
tick
tick
tick
tick
done sleeping
vfork() returned
hello world

Regards,

Anthony Liguori
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

static void *tick_thread(void *unused)
{
    while (1) {
        pthread_testcancel();
        printf("tick\n");
        sleep(1);
    }
    return NULL;
}

int main(int argc, char **argv)
{
    pid_t pid;
    pthread_t tid;
    void *ret;

    if (pthread_create(&tid, NULL, tick_thread, NULL) < 0) {
        printf("pthread_create failed\n");
        return 1;
    }

    pid = vfork();
    if (pid == 0) {
        printf("sleeping\n");
        sleep(10);
        printf("done sleeping\n");
        execl("/bin/echo", "echo", "hello world", NULL);
        exit(1);
    } else if (pid > 0) {
        printf("vfork() returned\n");
        waitpid(pid, NULL, 0);
    } else {
        printf("vfork failed\n");
        return 1;
    }

    pthread_cancel(tid);
    pthread_join(tid, &ret);

    return 0;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]