vfork-thread.c revision bf6b270bf2b6268153efc88cc5808ffd9bcb596e
1#include <pthread.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <stdio.h>
5
6
7void *
8routine (void *data)
9{
10  int i;
11  for (i = 0; i < 6; ++i)
12    {
13      puts ("bleble");
14      sleep (1);
15    }
16}
17
18
19void *
20routine2 (void *data)
21{
22  pid_t child = vfork ();
23  if (child == 0)
24    {
25      int i, j;
26      puts ("vforked");
27      for (i = 0; i < 100000; ++i)
28	for (j = 0; j < 10000; ++j)
29	  ;
30      puts ("vforked child exiting");
31      _exit (0);
32    }
33  puts ("parent continuing");
34  return NULL;
35}
36
37int
38main(int argc, char *argv[])
39{
40  pthread_t thread;
41  pthread_create (&thread, NULL, &routine, NULL);
42
43  sleep (1);
44
45  pthread_t thread2;
46  pthread_create (&thread2, NULL, &routine2, NULL);
47  pthread_join (thread2, NULL);
48  pthread_join (thread, NULL);
49  return 0;
50}
51