fork download
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define NUM_THREADS 10
  6.  
  7. pthread_t tid[NUM_THREADS];
  8. void *runner(void *param);
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. long i;
  13. pthread_attr_t attr;
  14.  
  15. printf("I am the parent thread\n");
  16. printf("I am rayan : %s %d \n",argv[1], argc);
  17.  
  18. /* get the default attributes */
  19. pthread_attr_init(&attr);
  20.  
  21. /* set the scheduling algorithm to PROCESS(PCS) or SYSTEM(SCS) */
  22. pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  23.  
  24. /* set the scheduling policy - FIFO, RT, or OTHER */
  25. pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
  26.  
  27. /* create the threads */
  28. for (i = 0; i < NUM_THREADS; i++)
  29. pthread_create(&tid[i],&attr,runner,(void *) i);
  30.  
  31. /* now join on each thread */
  32. for (i = 0; i < NUM_THREADS; i++)
  33. pthread_join(tid[i], NULL);
  34.  
  35. printf("I am the parent thread again\n");
  36.  
  37. /* comment the following line if you compile for Linux */
  38. // system("PAUSE");
  39. return 0;
  40. }
  41.  
  42. /* Each thread will begin control in this function */
  43. void *runner(void *param)
  44. {
  45. long id;
  46. id = (long) param;
  47. printf("I am thread #%d, My ID #%lu\n", id, tid[id]);
  48. pthread_exit(0);
  49. }
Success #stdin #stdout 0s 97600KB
stdin
Standard input is empty
stdout
I am the parent thread
I am rayan : (null) 1 
I am thread #6, My ID #47616978077440
I am thread #7, My ID #47616981378816
I am thread #8, My ID #47616983480064
I am thread #9, My ID #47616985581312
I am thread #5, My ID #47616975976192
I am thread #4, My ID #47616973874944
I am thread #3, My ID #47616971773696
I am thread #2, My ID #47616969672448
I am thread #1, My ID #47616967571200
I am thread #0, My ID #47616965469952
I am the parent thread again