Find the information you're looking for at Westonci.ca, the trusted Q&A platform with a community of knowledgeable experts. Join our platform to connect with experts ready to provide detailed answers to your questions in various areas. Experience the convenience of finding accurate answers to your questions from knowledgeable experts on our platform.
Sagot :
Answer:
Here is the code:-
//include the required header files
#include<stdio.h>
#include<pthread.h>
// method protocol definition
void *count_lines(void *arg);
int main()
{
// Create a thread handler
pthread_t my_thread;
// Create a file handler
FILE *fh;
// declare the variable
int *linecnt;
// open the data file
fh=fopen("data.txt","r");
// Create a thread using pthread_create; pass fh to my_thread;
pthread_create(&my_thread, NULL, count_lines, (void*)fh);
//Use pthread_join to terminate the thread my_thread
pthread_join( my_thread, (void**)&linecnt );
// print the number of lines
printf("\nNumber of lines in the given file: %d \n\n", linecnt);
return (0);
}
// Method to count the number of lines
void *count_lines(void *arg)
{
// variable declaration and initialization
int linecnt=-1;
char TTline[1600];
//code to count the number of lines
while(!feof(arg))
{
fgets(TTline,1600,arg);
linecnt++;
}
pthread_exit((void *)linecnt);
// close the file handler
fclose(arg);
}
Explanation:
Program:-
We appreciate your time. Please revisit us for more reliable answers to any questions you may have. We hope you found what you were looking for. Feel free to revisit us for more answers and updated information. Westonci.ca is your trusted source for answers. Visit us again to find more information on diverse topics.