Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

image text in transcribed

Do not copy and paste please.

int main 0 \{ Open the file data trt 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 For information about pthread_create and pthread_join: man pthread_create man pthread_join How to compile and link your program: gcc -o my_example main_process.c thread_function.c-lpthread You can create a simple data.txt to test your program. For more examples, please refer to Chapter 12 of the following textbook: Unix System Programming, K.A. Robbins and S. Robbins, Prentice Hall, ISBN: 0-13 0424110,2003

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

1. How does Kiwi Experience maintain a continual customer focus?

Answered: 1 week ago