Find the answer to your Linux question:
Results 1 to 8 of 8
Dear All, I have one function in my driver which will read some data from one buffer and write it to another buffer. In my current implementation both are combined ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Location
    Bangalore
    Posts
    7

    How to run threads independently


    Dear All,

    I have one function in my driver which will read some data from one buffer and write it to another buffer.

    In my current implementation both are combined in one function. Now i am planning to split the jobs to two threads. So i have divided the function in two small functions. One is reader and another one is writer.

    I thought of running these functions by using two independent threads. I am calling the kthread APIs as follows,


    /*********/
    void read_write() {
    struct task_struct *task1, *task2;

    task1 = kthread_run(data_read, common_data, "data_reader");
    some code to check error conditions......

    task2 = kthread_run(data_write, common_data, "data_writer");
    some code to check error conditions......

    kthread_stop(task1);
    kthread_stop(task2);
    }
    /********/

    The above code shows how the read_write() function is. Other functions are calling this read_write() function so many times. For example,

    static int open(.....) {
    read_write(arguments...);
    }

    Here when i am calling read_write() function in other functions, threads are created each time. When i am calling reader and writer as functions, i am able to get the results from read_write() function. But when i am calling two threads, i am not getting the results as same as calling two functions.

    Can anybody help me, how to run these two threads independently to do read and write in all the time.

    Regards,
    Sundar

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    threads are designed to run asynchronously (which is independently), so there is a flaw in your code or the threads aren't working the way you are expecting them to

  3. #3
    Just Joined!
    Join Date
    Sep 2005
    Location
    Bangalore
    Posts
    7
    My machine is single core.

    So when my function is running the two threads (reader and writer), reader is executing without any problem. After execution, it is coming to my function and returns the values. Then the writer thread is executing. So the problem is, writer thread is not getting the correct values from the reader thread.

    For example,
    static int read_write() {
    kthread_run(data_read, common_data, "data_reader");
    kthread_run(data_write, common_data, "data_writer");
    return 0
    }

    In the above example, data_read is running first and coming out of the function itself. Then data_write thread is running. How it is possible that without running write_thread it is coming out?

    I don't have any code in between these two threads. But if i call like functions then it is working properly as,

    static int read_write() {
    data_read(common_data);
    data_write(common_data);
    return 0
    }
    Now both operations are fine. But when i am running as threads, then only the problem arises. Can you please tell me, do i need to do any other operations in these two threads.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    threads run asynchronously, as soon as you create those threads and let them run kernel will decide which one gets CPU time and when, there is no way to assure that the read thread will execute and finish before the write thread

    you could create a thread that does the read and write inside it, but thats it, I'm not sure you are needing threads in your situation

  6. #5
    Just Joined!
    Join Date
    Sep 2005
    Location
    Bangalore
    Posts
    7
    I am trying to parallelize the code, that means read thread can run in one cpu and write thread can run in another cpu. Just like dividing the task to two cpus.

    My read_write function is doing the following things,

    1. Reading the contents from disk1 and doing memcopy to a temporary buffer (This is done by one cpu with read thread).
    2. This temporary buffer will be given to second thread which is doing write operation (This is done by another cpu with write thread) on disk2.

    During write thread operation, the read thread will read the next contents and store it in read buffer. By the time write thread finishes the job, it will take temporary buffer and write it on disk2. This logic follows, pipeline parallelization.

    When first cpu started reading the data with the help read thread, the second cpu is waiting for the temporary buffer to be filled. Once the temporary buffer is filled, then second cpu start writing with the help of write thread.

    The order of output as follows,

    Expected output:

    Started read_write function....
    read_thread is executed successfully, number of bytes read = 100
    write_thread is executed successfully, number of bytes written = 100
    Stopped read_write function.....

    Actual output:
    Started read_write function....
    read_thread is executed successfully, number of bytes read = 100
    Stopped read_write function.....
    write_thread is executed successfully, number of bytes written = 0

    My problem is, read thread is ran first and returning to my function. Then the write thread is running. In between i am not getting the number of bytes written to disk2 correctly.

  7. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    i'm not familiar with kernel threads, but I see what you are doing wrong based on this page Kernel threads made easy [LWN.net]

    Most likely you are killing off your threads before they have finished, you will have to wait for the write thread to actually write before killing it

  8. #7
    Just Joined!
    Join Date
    Sep 2005
    Location
    Bangalore
    Posts
    7
    In my code, i am not calling any functions to stop the thread like kthread_stop or kthread_should_stop.

    I am just callling kthread_run() function to create and run threads. Also inside the read thread i am returning zero on success -1 on failure and doing the same in write thread also.

    My doubt is, i am not calling any function to stop the threads. Then who else is doing it (kernel?). Do i need to call any scheduling functions?

  9. #8
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    well, you posted some code or pseudocode that contained the stop calls, which is why i pointed it out

    I've not worked with kernel threads, so i can't comment on how they work, but if they work like java/python threads, they terminate themselves when they have no more code left to run

    that being said, i think you seem to be making a basic mistake: if the write thread must execute after the read thread, you must put something into the write thread to make sure it does the write only after the read thread is terminated, that is the only way to ensure that the read thread is successfully executed before the write thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •