fork(1) download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. int tab[2000];
  5. pthread_t TH_begin;
  6. pthread_t TH_end;
  7. void search_From_begin(int val){
  8. int i;
  9. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
  10. for(i=0;i<2000;i++){
  11.  
  12. pthread_testcancel();
  13. usleep(500);
  14.  
  15. if(tab[i]==val){
  16. pthread_cancel(TH_end);
  17. printf("search begin find it first\n");
  18.  
  19. }
  20. }
  21. printf("search begin not find it first\n");
  22. }
  23. void search_From_end(int val){
  24. int i;
  25. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
  26. for(i=2000;i>-1;i--){
  27. pthread_testcancel();
  28. usleep(500);
  29.  
  30. if(tab[i]==val){
  31. pthread_cancel(TH_begin);
  32. printf("search end find it first\n");
  33.  
  34. }
  35. }
  36. printf("search begin not find it first\n");
  37. }
  38. void main(){
  39.  
  40. int i,j;
  41. for(i=0;i<2000;i++)
  42. tab[i]=i;
  43. do{
  44. printf("Entrer val:");
  45. scanf("%d",&j);
  46. pthread_create(&TH_begin,NULL,&search_From_begin,j);
  47. pthread_create(&TH_end,NULL,&search_From_end,j);
  48. pthread_join(TH_begin,NULL);
  49. pthread_join(TH_end,NULL);
  50. }while(1);
  51. }
  52.  
Time limit exceeded #stdin #stdout 5s 4532KB
stdin
Standard input is empty
stdout
Standard output is empty