0

Basic structure of main.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;


    Return num_lines
}
--------------------------------------------------------------

How can i pass file handler from main.c to thread_function.c

Edited by Dani: Formatting fixed

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Arcaiz
0

Well you have

void *count_lines(void *arg)

I would use the void *arg to pass the file handle

like

count_lines(&fh)

Edited by gerard4143: n/a

0

one .c file will contain the above main.c decleration other file will contain only the thread.c function .....when i wll compile them using gcc ....in main.c file it will show number of lines ...which it will take from thread.c ......main.c file send file handler to thread.c file

0

You could declare some global variable so you could exchange handle in it but you must do it carefully.

IMHO I think you don't need to make a thread for a line-counting program.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.