exit_group.c 754 B

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