fork download
  1. #include<pthread.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<asm/unistd.h>
  5. void *T_Routine(void *param);
  6. int main(int argc,char *argv[])
  7. {
  8. int i;
  9. int *ran_ret;
  10. pthread_t tid1, tid2, tid3;
  11. pthread_attr_t attr1, attr2;
  12. pthread_attr_init(&attr1);
  13. pthread_attr_init(&attr2);
  14. pthread_attr_setdetachstate(&attr2,PTHREAD_CREATE_DETACHED);
  15. pthread_create(&tid1,&attr1,T_Routine,"1");//attr1 can be NULL
  16. pthread_create(&tid2,&attr1,T_Routine,"2"); //attr1 can be NULL
  17. pthread_create(&tid3,&attr2,T_Routine,"3");
  18. pthread_join(tid1,(void **)(&ran_ret));
  19. pthread_join(tid2,NULL);
  20. printf("Random numbers\n");
  21. for(i=1;i<=ran_ret[0];i++)
  22. printf("%d\t",ran_ret[i]);
  23. printf("\n");
  24. free(ran_ret);
  25. return 0;
  26. }
  27.  
  28. // runner function
  29. void *T_Routine ( void *param )
  30. {
  31. int i,N,K;
  32. int *ran;
  33. N = atoi(param);
  34. printf("PID=%d,PID_TID=%d\n",getpid(),syscall(__NR_gettid));
  35. if(N==1)
  36. {
  37. K=5;
  38. ran=(int *)malloc((K+1)*sizeof(int));
  39. ran[0]=5;
  40. ran[1]=rand();
  41. ran[2]=rand();
  42. ran[3]=rand();
  43. ran[4]=rand();
  44. ran[5]=rand();
  45. printf("Random value from thread 1\n");
  46. for(i=1;i<=ran[0];i++)
  47. printf("%d\t",ran[i]);
  48. printf("\n");
  49. pthread_exit(ran);
  50. }
  51. else if(N==2)
  52. {
  53. pthread_detach(pthread_self());
  54. pthread_exit(0);
  55. }
  56. else if(N==3)
  57. {
  58. pthread_exit(0);
  59. }
  60. }
  61.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
PID=1542332,PID_TID=1542337
PID=1542332,PID_TID=1542336
PID=1542332,PID_TID=1542335
Random value from thread 1
1804289383	846930886	1681692777	1714636915	1957747793	
Random numbers
1804289383	846930886	1681692777	1714636915	1957747793