exit_group.c 780 B

12345678910111213141516171819202122232425262728293031323334
  1. #define _XOPEN_SOURCE 700
  2. #include <linux/unistd.h>
  3. #include <pthread.h>
  4. #include <stdatomic.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #define THREAD_NUM 4
  9. atomic_int counter = 1;
  10. pthread_barrier_t barrier;
  11. /* Test the process exit logic in Graphene. Multiple threads race to execute exit()/exit_group().
  12. * Expected return code is 0 .. 4, depending on which thread wins. */
  13. void* inc(void* arg) {
  14. int a = counter++;
  15. pthread_barrier_wait(&barrier);
  16. exit(a);
  17. }
  18. int main(int argc, char** argv) {
  19. pthread_t thread[THREAD_NUM];
  20. pthread_barrier_init(&barrier, NULL, THREAD_NUM + 1);
  21. for (int j = 0; j < THREAD_NUM; j++) {
  22. pthread_create(&thread[j], NULL, inc, NULL);
  23. }
  24. pthread_barrier_wait(&barrier);
  25. return 0;
  26. }