LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Linux Thread Questions and Problems (https://www.linuxquestions.org/questions/linux-software-2/linux-thread-questions-and-problems-210580/)

esi-eric 07-28-2004 07:58 AM

Linux Thread Questions and Problems
 
Hello everyone!!!

I have some questions and problems with Linux Threads that I would greatly appreciate some assistance with.

1. Can you prioritize each thread in your system, i.e. give it a different "thread priority"?

2. What is the time interval that a given thread gets pre-empted by a higher priority thread (assuming that you can have higher priority threads)?

3. If you are in one thread and you are executing some code that takes in the microseconds to execute, what is the chance that your current thread will be pre-empted by the higher priority thread? (assuming that once again you can have a higher priority thread scheduled to do this)

4. My scenario is as follows:

Thread 1: Thread 2:
1. Sets Flag for thread 2 to do work Reads flag to do some work
2. Does some work (us range) Clears flag received from thread 1
3. Sends status information back to Performs status calcs for thread #1
another computer over ethernet. Function in thread ends
4. Function in thread ends


As you can see, I need the status calculations in thread #2 performed before thread #1 sends the information via ethernet to the client computer.
How can this be coordinated? I have already thought of having another flag in thread one telling thread #2 to send the status back to the client comptuer, but I am thinking that this is a bit unstructured!!! Would any type of semaphore mechanisms be of assistance, or will thread 1 always complete before thread #2 gets to execute irrespective of the thread priority of thread #1 and thread #2?

Thank you very much for all of your time and consideration!

Eric.

bruce ford 07-28-2004 12:21 PM

hi eric

Quote:

1. Can you prioritize each thread in your system, i.e. give it a different "thread priority"?
Posix threads offers an interface to that, but if the implementation actually does something sensibe is left up to the implementation.
But I have made bad experiences experimenting with that (on Sun Solaris).

Quote:

2. What is the time interval that a given thread gets pre-empted by a higher priority thread (assuming that you can have higher priority threads)?
You won't find an OS (besides hard realtime OSes) that lets you make any assumption on this value. The same yields for your question 3.

If I understood your scenario right, the structure you need is a condition variable and a mutex (you always need a mutex for a condvar).

Your thread 2 should wait on the condition variable until thread 1 signals it. Thread 1 then can do its further processing while thread 2 performs the status calculations. When thread 1 needs the data it locks the mutex and checks whether the data is already available. If yes, it processes it and if no, it waits on the condvar. When thread 2 is finished calculating the status it locks the mutex, writes the status to the memory (variable) shared between the processes, unlocks the mutex and signals the condvar. Then the loop can begin from start.

Example 1 (Race-Condition: thread 1 needs the status data before thread 2 is finished calculating it)
1. T2 waits on the condvar (with mutex locked)
2. T1 signals the condvar and continues processing
3. T2 gets woken up, unlocks the mutex and starts calculating the status
4. T1 locks the mutex, checks the data and finds it unfinished yet
5. T1 waits on the condvar (with mutex locked)
6. T2 is finished calculating the status, locks the mutex and writes the status
7. T2 signals the condvar, then unlocks the mutex
8. T1 gets woken up, reads the data and transmits it over network, then unlocks the mutex

Example 2 (Race-Condition: thread 2 is finished calculating the data before thread 1 needs it)
1. T2 waits on the condvar (with mutex locked)
2. T1 signals the condvar and continues processing
3. T2 gets woken up, unlocks the mutex and starts calculating the status
4. T2 is finished calculating the status, locks the mutex and writes the status
5. T2 signals the condvar, then unlocks the mutex
6. T1 locks the mutex, checks the data and finds it finished
7. T1 reads the data and transmits it over network, then unlocks the mutex

As you can see, at the end of each example the mutex is unlocked.

Please do not hesitate to ask if something still is unclear.

So long...
bruce


All times are GMT -5. The time now is 04:13 PM.