Skip to content

Instantly share code, notes, and snippets.

@x42
Created February 25, 2019 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x42/60dfae7e5f2ec8cdb239087c324d10d0 to your computer and use it in GitHub Desktop.
Save x42/60dfae7e5f2ec8cdb239087c324d10d0 to your computer and use it in GitHub Desktop.
// gcc -Wall -pthread -o vforktest vforktest.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
static void* run (void* arg)
{
for (int i = 0; i < 6; ++i) {
printf ("Test %d\n", i);
sleep (1);
}
return 0;
}
int main (int argc, char** argv)
{
pthread_t tid;
pthread_create (&tid, NULL, &run, NULL);
int pid = vfork ();
sleep (3);
if (pid == 0) {
execlp ("/bin/ls", "ls", (char*)NULL);
}
sleep (3);
pthread_join (tid, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment