1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
cupoosta [38]
3 years ago
6

Please use thread to complete the following program: one process opens a file data.txt, then creates a thread my_thread. The job

of the thread my_thread is to count how many lines exist in the file data.txt, and return the number of lines to the calling process. The process then prints this number to the screen.
Basically, you need to implement main_process.c and thread_function.c.
Basic structure of main_process.c:
int main ()
{
Open the file data.txt and obtain the file handler fh;
Create a thread my_thread using pthread_create; pass fh to my_thread;
Wait until my_thread terminates, using pthread_join;
Print out how many lines exist in data.txt.}
Basic structure of thread_function.c:
void *count_lines(void *arg)
{
Obtain fh from arg;
Count how many lines num_lines exist in fh;
Close fh;
Return num_lines
}
Computers and Technology
1 answer:
Firlakuza [10]3 years ago
8 0

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:-

You might be interested in
What is lasso tool write the name of any modelling and animation software<br>​
VladimirAG [237]

Explanation:

6579689993 and password is 3250 I RISHAVjoin please I join yar

7 0
3 years ago
What is a ribbon in word
nika2105 [10]
A ribbon is somehing ou earn
7 0
3 years ago
Read 2 more answers
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outpu
Bond [772]

Answer:

user_string = input("Input a string : ")

output = user_string.isnumeric()

if output == True:

   print("yes")

else:

   print("no")

Explanation:

  • First of all check whether user input have all numeric value or not .
  • Display yes if string contain all numeric values .
  • Display no if string contain at least one non - integer character .

7 0
4 years ago
Read 2 more answers
In a five-choice multiple-choice test, which letter is most often the correct
tatiyna

I believe the answer is C.

5 0
3 years ago
Read 2 more answers
Which bus slot provides the highest video performance​
Sphinxa [80]

Answer: PCI, The PCI provides the highest performance

4 0
3 years ago
Other questions:
  • Why is television reactive, requiring no skills or thinking
    11·1 answer
  • PLEASE HELP!!!!! MUCH OBLIGED!!!!
    15·1 answer
  • In which type of land contract does the seller earn interest on the difference between what the seller owes on an existing loan
    14·1 answer
  • A virus is a self-replicating program that produces its own code by attaching copies of it into other executable codes.
    7·1 answer
  • To reload a picture taken with a digital camera means to copy the digital picture from the camera to your computer.
    5·2 answers
  • The Linux operating system was created by which software engineer?
    15·1 answer
  • Now tell me how be rich like Bill Gates
    6·1 answer
  • Is amazon a e-commerce website <br> o true <br> o false
    8·2 answers
  • A monopoly is a market that has few competing businesses. many sellers of the same item. many sellers of a variety of products.
    8·2 answers
  • Write the simplest statement that prints the following: 3 2 1 Go! python
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!