LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-28-2004, 07:58 AM   #1
esi-eric
Member
 
Registered: May 2004
Location: Florida
Distribution: 7.2
Posts: 32

Rep: Reputation: 15
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.
 
Old 07-28-2004, 12:21 PM   #2
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A thread of my various problems (I'm changing from Windows) binarybob0001 Linux - General 4 12-02-2005 01:43 AM
FC4 Problems thread: Post your problems here hkl8324 Fedora 73 09-19-2005 04:24 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM
Compiling Problems - New Thread ryanstrayer Linux - General 12 02-14-2002 02:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration