Advertisement
Guest User

tu wbijaj jak cos to lab 7

a guest
Jun 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include<pthread.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5. #include<string.h>
  6. #include<sched.h>
  7. #define ELEMENTS_NUMBER 20
  8. int array[ELEMENTS_NUMBER];
  9. void *thread_function(void *data)
  10.  
  11. {
  12.  
  13. } {
  14.  
  15. int i, old, type; type = *(int *)data;int return_code =
  16.  
  17. pthread_setcanceltype(type==1?PTHREAD_CANCEL_ASYNCHRONOUS:PTHREAD_CANCEL_DEFERRED,&old);if(return_code!=0)
  18.  
  19. fprintf(stderr,"pthread_setcanceltype() error: %d\n",return_code);for(;;){
  20.  
  21. memset((void *)array,0,ELEMENTS_NUMBER*sizeof(int));for(i=0;i<ELEMENTS_NUMBER;i++) {
  22.  
  23. } }
  24.  
  25. if(i==ELEMENTS_NUMBER/2) pthread_testcancel();
  26.  
  27. array[i]=i;
  28.  
  29. return NULL;
  30. int main(int argc,char **argv)
  31.  
  32. int type, i, j;char output[20];if(argc==2) {
  33.  
  34. type = atoi(argv[1]);if(type!=1&&type!=2) {
  35.  
  36. puts("Niewłaściwa wartość argumentów. Uruchom program bez\
  37. argumentów, żeby dowiedzieć się więcej.");
  38. return -1;
  39.  
  40. }
  41. pthread_t thread_id;for(j=0;j<10;j++) {
  42.  
  43. } } else
  44.  
  45. sprintf(output,"Iteracja nr: %d\n",j+1);
  46. write(1,output,strlen(output));
  47. int return_code = pthread_create(&thread_id,NULL,thread_function,(void *)&type);if(return_code!=0)
  48.  
  49. fprintf(stderr,"pthread_create() error: %d\n",return_code);if(sched_yield()<0)
  50.  
  51. perror("sched_yield"); return_code = pthread_cancel(thread_id);if(return_code!=0)
  52.  
  53. fprintf(stderr,"pthread_cancel() error: %d\n",return_code); return_code=pthread_join(thread_id,NULL);
  54. if(return_code!=0)
  55.  
  56. fprintf(stderr,"pthread_join() error: %d\n",return_code);for(i=0;i<ELEMENTS_NUMBER;i++) {
  57.  
  58. sprintf(output,"%d ",array[i]);
  59. write(1,output,strlen(output));
  60. } write(1,"\n",strlen("\n"));
  61.  
  62. puts("Wymaga argumentu wywołania: \n\
  63. 1 - anulowanie asynchroniczne, \n 2 - anulowanie synchroniczne.");
  64.  
  65.  
  66. return 0;
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement