Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<pthread.h>
- int main(){
- int a,b,i;
- pthread_mutex_t lock;
- int getLineCount(){
- FILE *fp;
- char ch;
- int linesCount=0;
- fp=fopen("data.txt","r");
- if(fp==NULL)
- {
- printf("File does not exist!!!n");
- return -1;
- }
- while((ch=fgetc(fp))!=EOF)
- {
- if(ch=='n')
- linesCount=linesCount+1;
- }
- fclose(fp);
- return linesCount;
- }
- void *readLine(void *lineNumb){
- printf("%d",5);
- int lineN= (*(int *)lineNumb);
- pthread_mutex_lock(&lock);
- FILE* fp = fopen("data.txt", "r");
- char line[256];
- int i = 0;
- while (fgets(line, sizeof(line), fp)) {
- i++;
- if(i == lineN)
- {
- fscanf(fp,"%d%d",&a,&b);
- printf("%d n",a+b);
- }
- }
- fclose(fp);
- pthread_mutex_unlock(&lock);
- }
- int lineCount = getLineCount()+1;
- pthread_t tid[lineCount];
- for (i = 1; i <= lineCount; i++) {
- void *b =&i;
- pthread_create(&tid[i], NULL,readLine,(void*)b);
- pthread_mutex_destroy(&lock);
- }
- pthread_join(tid[1] ,NULL);
- pthread_join(tid[2] ,NULL);
- pthread_join(tid[3] ,NULL);
- pthread_join(tid[4] ,NULL);
- pthread_join(tid[5] , NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement