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
In the early days of photography, cameras were limited to professional photographers because of the knowledge needed to work the
FinnZ [79.3K]
True they had to be developed in a special way in a dark room with several chemicals. 
3 0
4 years ago
Read 2 more answers
Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method
weeeeeb [17]

Using the computational language in python we have to use it to write to a file and read with the code.

<h3>Writing this code in python we have:</h3>

<em>filename = input()</em>

<em>file = open(filename)</em>

<em>lines = file.readlines()</em>

<em>data = {}</em>

<em>for i in range(0, len(lines), 2):</em>

<em>    num_seasons = int(lines[i].strip())</em>

<em>    show = lines[i + 1].strip()</em>

<em>    if num_seasons not in data:</em>

<em>        data[num_seasons] = []</em>

<em>    data[num_seasons].append(show)</em>

<em>file.close()</em>

<em>file_writer = open('output_keys.txt', 'w')</em>

<em>titles = []</em>

<em>for num_seasons in sorted(data):</em>

<em>    shows = []</em>

<em>    for show in sorted(data[num_seasons]):</em>

<em>        titles.append(show)</em>

<em>    file_writer.write(str(num_seasons) + ': ' + '; '.join(data[num_seasons]) + '\n')</em>

<em>file_writer.close()</em>

<em>file_writer = open('output_titles.txt', 'w')</em>

<em>for title in sorted(titles):</em>

<em>    file_writer.write(title + '\n')</em>

<em>file_writer.close()</em>

See more about python at brainly.com/question/18502436

#SPJ1

3 0
3 years ago
I need someone to explain gor me python coding!
forsale [732]

Answer:

Explanation:

Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general purpose language, meaning it can be used to create variety of different programs and isn't specialised for any specific problems

8 0
3 years ago
The faster alcohol is consumed, the faster it reaches the __________.
Anni [7]
The faster it reaches the bloodstream

6 0
3 years ago
Read 2 more answers
As you paste a chart from Excel to Word use the Paste Options button labeled ____ if you want the theme of the destination docum
AURORKA [14]

As you paste a chart from Excel to Word use the Paste Options button labelled <u>paste special</u> if you want the theme of the destination document applied to the chart, and the chart is linked to the object in the source document.

<u>Explanation:</u>

While end-user planning to copy the data from ms-excel to ms-word he or she has the option to select paster or paste special. The paste will just copy data from a destination to source for example just copy the data from ms-excel to ms-word. Whereas by selecting paste special use can select end-user can select the value or image or picture.  

By pasting special end-user can change the copying type or pasting type from just copy the data he or she can copy with formula, value, picture or formatting picture.

5 0
4 years ago
Other questions:
  • Write a function (named n_pointed_star) to make the turtle draw an n-pointed star. The function should return nothing, and accep
    10·1 answer
  • My programming lab 9.2 C++ Write a full class definition for a class named Counter, and containing the following members:_______
    6·1 answer
  • What is Napoleon's friend's full name? From the Napoleon Dynamite movie.
    9·2 answers
  • Which transmission media is faster? Fibre Optic or Radio-waves? I want full explanation!
    10·1 answer
  • Which is the most likely reason film companies expanded so quickly?
    8·1 answer
  • POINT AND BRAINLIEIST GIVE AWAY!!!
    11·2 answers
  • Which keyboard shortcut pastes information from the clipboard?
    6·2 answers
  • Irma bought a gaming computer at Bestbuy 3 years ago and it’s been on ever since. Her computer is staring to feel unstable and a
    10·1 answer
  • Describe ONE similarity and ONE difference between the use of lighting in candid and posed photography.
    15·1 answer
  • Relational operators compare values and determine whether a condition of an if statement is true or false.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!