fork(1) download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. int tab[2000];
  5. pthread_t TH-begin,TH_end;
  6. void search_From_begin(int val){
  7. int i;
  8. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
  9. for(i=0;i<2000;i++){
  10.  
  11. pthread_testcancel();
  12. usleep(500);
  13.  
  14. if(tab[i]==val){
  15. pthread_cancel(TH_end);
  16. printf("search begin find it first\n");
  17. return;
  18. }
  19. }
  20. printf("search begin not find it first\n");
  21. }
  22. void search_From_end(int val){
  23. int i;
  24. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
  25. for(i=2000;i>-1;i--){
  26. pthread_testcancel();
  27. usleep(500);
  28.  
  29. if(tab[i]==val){
  30. pthread_cancel(TH_begin);
  31. printf("search end find it first\n");
  32. return;
  33. }
  34. }
  35. printf("search begin not find it first\n");
  36. }
  37. void main(){
  38.  
  39. int i,j;
  40. for(i=0;i<2000;i++)
  41. tab[i]=1;
  42. do{
  43. printf("Entrer val:");
  44. scanf("%d",&j);
  45. pthread_create(&TH_begin,NULL,&search_From_begin,j); // creation du thread t1
  46. pthread_create(&t2,NULL,&search_From_end,j); // creation du thread t2
  47. pthread_join(TH_begin,NULL); // attendre le thread t1
  48. pthread_join(TH_end,NULL); // attendre le thread t2
  49.  
  50. printf("V= %d\n",variable_globale); // afficher la valeur de var
  51. return 0;
  52. }while(1);
  53. }
  54.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:5:13: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘-’ token
 pthread_t TH-begin,TH_end;
             ^
prog.c: In function ‘search_From_begin’:
prog.c:12:4: warning: implicit declaration of function ‘usleep’ [-Wimplicit-function-declaration]
    usleep(500);
    ^~~~~~
prog.c:15:20: error: ‘TH_end’ undeclared (first use in this function)
     pthread_cancel(TH_end);
                    ^~~~~~
prog.c:15:20: note: each undeclared identifier is reported only once for each function it appears in
prog.c: In function ‘search_From_end’:
prog.c:30:20: error: ‘TH_begin’ undeclared (first use in this function)
     pthread_cancel(TH_begin);
                    ^~~~~~~~
prog.c: At top level:
prog.c:37:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(){
      ^~~~
prog.c: In function ‘main’:
prog.c:45:19: error: ‘TH_begin’ undeclared (first use in this function)
   pthread_create(&TH_begin,NULL,&search_From_begin,j); // creation du thread t1
                   ^~~~~~~~
prog.c:45:33: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
   pthread_create(&TH_begin,NULL,&search_From_begin,j); // creation du thread t1
                                 ^
In file included from prog.c:2:0:
/usr/include/pthread.h:233:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(int)’
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^~~~~~~~~~~~~~
prog.c:45:52: warning: passing argument 4 of ‘pthread_create’ makes pointer from integer without a cast [-Wint-conversion]
   pthread_create(&TH_begin,NULL,&search_From_begin,j); // creation du thread t1
                                                    ^
In file included from prog.c:2:0:
/usr/include/pthread.h:233:12: note: expected ‘void * restrict’ but argument is of type ‘int’
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^~~~~~~~~~~~~~
prog.c:46:19: error: ‘t2’ undeclared (first use in this function)
   pthread_create(&t2,NULL,&search_From_end,j); // creation du thread t2
                   ^~
prog.c:46:27: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
   pthread_create(&t2,NULL,&search_From_end,j); // creation du thread t2
                           ^
In file included from prog.c:2:0:
/usr/include/pthread.h:233:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(int)’
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^~~~~~~~~~~~~~
prog.c:46:44: warning: passing argument 4 of ‘pthread_create’ makes pointer from integer without a cast [-Wint-conversion]
   pthread_create(&t2,NULL,&search_From_end,j); // creation du thread t2
                                            ^
In file included from prog.c:2:0:
/usr/include/pthread.h:233:12: note: expected ‘void * restrict’ but argument is of type ‘int’
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^~~~~~~~~~~~~~
prog.c:48:16: error: ‘TH_end’ undeclared (first use in this function)
   pthread_join(TH_end,NULL); // attendre le thread t2
                ^~~~~~
prog.c:50:18: error: ‘variable_globale’ undeclared (first use in this function)
 printf("V= %d\n",variable_globale); // afficher la valeur de var
                  ^~~~~~~~~~~~~~~~
prog.c:51:9: warning: ‘return’ with a value, in function returning void
  return 0;
         ^
prog.c:37:6: note: declared here
 void main(){
      ^~~~
stdout
Standard output is empty