Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<pthread.h>
  4.  
  5. int main(){
  6. int a,b,i;
  7. pthread_mutex_t lock;
  8.  
  9. int getLineCount(){
  10.  
  11. FILE *fp;
  12. char ch;
  13. int linesCount=0;
  14.  
  15. fp=fopen("data.txt","r");
  16. if(fp==NULL)
  17. {
  18. printf("File does not exist!!!n");
  19. return -1;
  20. }
  21. while((ch=fgetc(fp))!=EOF)
  22. {
  23. if(ch=='n')
  24. linesCount=linesCount+1;
  25. }
  26. fclose(fp);
  27. return linesCount;
  28. }
  29.  
  30. void *readLine(void *lineNumb){
  31. printf("%d",5);
  32. int lineN= (*(int *)lineNumb);
  33. pthread_mutex_lock(&lock);
  34.  
  35. FILE* fp = fopen("data.txt", "r");
  36. char line[256];
  37. int i = 0;
  38.  
  39. while (fgets(line, sizeof(line), fp)) {
  40. i++;
  41. if(i == lineN)
  42. {
  43. fscanf(fp,"%d%d",&a,&b);
  44. printf("%d n",a+b);
  45. }
  46.  
  47. }
  48. fclose(fp);
  49. pthread_mutex_unlock(&lock);
  50.  
  51. }
  52.  
  53.  
  54. int lineCount = getLineCount()+1;
  55.  
  56. pthread_t tid[lineCount];
  57.  
  58.  
  59. for (i = 1; i <= lineCount; i++) {
  60.  
  61. void *b =&i;
  62. pthread_create(&tid[i], NULL,readLine,(void*)b);
  63. pthread_mutex_destroy(&lock);
  64. }
  65. pthread_join(tid[1] ,NULL);
  66. pthread_join(tid[2] ,NULL);
  67. pthread_join(tid[3] ,NULL);
  68. pthread_join(tid[4] ,NULL);
  69. pthread_join(tid[5] , NULL);
  70.  
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement