|
Home > Archive > Unix Programming > August 2006 > how to put current processes into wait.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to put current processes into wait.
|
|
| Subhash 2006-08-29, 7:28 am |
| Hi all,
Iam learning pthreads i want to keep the current excuting thread
into ready queue
i can use sleep function, but i don't want to put it in sleep state,
is there any systemcall is defined to suspend the current process and
put it to ready queue??
Regards/-
Subhash M
| |
| David Schwartz 2006-08-29, 7:28 am |
|
Subhash wrote:
> Hi all,
> Iam learning pthreads i want to keep the current excuting thread
> into ready queue
> i can use sleep function, but i don't want to put it in sleep state,
> is there any systemcall is defined to suspend the current process and
> put it to ready queue??
>
> Regards/-
> Subhash M
You are being vague and contradictory. Do you want suspend the current
thread or the current process? For how long?
If the current thread, what do you think should happen if this is the
only ready-to-run thread? What if it's the only ready-to-run thread
that's not already running?
What are you actually trying to do? What problem are you trying to
solve?
DS
| |
| Thomas Maier-Komor 2006-08-29, 1:35 pm |
| Subhash wrote:
> Hi all,
> Iam learning pthreads i want to keep the current excuting thread
> into ready queue
> i can use sleep function, but i don't want to put it in sleep state,
> is there any systemcall is defined to suspend the current process and
> put it to ready queue??
>
> Regards/-
> Subhash M
>
Are you looking for sched_yield?
http://www.opengroup.org/onlinepubs...ched_yield.html
HTH,
Tom
| |
| Subhash 2006-08-30, 1:23 am |
|
David Schwartz wrote:
> Subhash wrote:
>
> You are being vague and contradictory. Do you want suspend the current
> thread or the current process? For how long?
>
> If the current thread, what do you think should happen if this is the
> only ready-to-run thread? What if it's the only ready-to-run thread
> that's not already running?
>
> What are you actually trying to do? What problem are you trying to
> solve?
>
> DS
I want to suspend the current thread and give execution to other
thread,
i don't want to sleep the current thread (for that i can use sleep( )
function).
if i use sleep(1sec) funtion the current thread(say first thread) gives
the execution to
other thread(say 2nd thread), if the 2nd thread completes its
execution,
then the first thread will not get executed untill sleep time
comlete....
i don't want to waste that time.
For that, I got an idea, i.e why can i put a thread into sleep state,
why don't i directly put it into ready state so that the first thread
will get CPU time after comletion of 2nd thread....
is there any function to put the current thread into ready state(i.e
running to ready)...
Subhash
| |
| Nils O. Selåsdal 2006-08-30, 7:33 am |
| Subhash wrote:
> David Schwartz wrote:
>
> I want to suspend the current thread and give execution to other
> thread,
But why !?
pthreads are preemptive, the OS will switch to another thread using
various rules and heuristics.
If you're attempting to synchronize access, or indicate finished work or
similar, this is not the way to do it. Use condition variables in that
case.
> i don't want to sleep the current thread (for that i can use sleep( )
> function).
> if i use sleep(1sec) funtion the current thread(say first thread) gives
> the execution to
> other thread(say 2nd thread), if the 2nd thread completes its
> execution,
> then the first thread will not get executed untill sleep time
> comlete....
> i don't want to waste that time.
> For that, I got an idea, i.e why can i put a thread into sleep state,
> why don't i directly put it into ready state so that the first thread
You can, but it's not generally useful unless you're doing cooperative
threading, which pthreads is not.
> will get CPU time after comletion of 2nd thread....
sched_yield();
There is no guarantee it will put the thread at the /back/ of
a ready queue, as the ready queue might not be a fifo queue (unless
when run with SCHED_FIFO).
> is there any function to put the current thread into ready state(i.e
> running to ready)...
>
>
> Subhash
>
| |
| Subhash 2006-08-30, 7:33 am |
|
Nils O. Sel=E5sdal wrote:
> Subhash wrote:
> But why !?
> pthreads are preemptive, the OS will switch to another thread using
> various rules and heuristics.
> If you're attempting to synchronize access, or indicate finished work or
> similar, this is not the way to do it. Use condition variables in that
> case.
what actually my problm is iam using a pthread it is created from main
thread,
the main aim of program is to print(printing at bottom left corner) the
current
charector position (it's just like vi editor).
To count no of lines, charectors i used a pthread.
To take input from user i used getchar( ) in main thread.
the main thread will be in waitqueue(i think) untill we enter a
charector from keybord.
In this time pthread will get CPU time(i mean running).
I used a while loop for counting no of lines and charectors in that
line
after that i want to put the pthread(this is excuting currently) into
ready state
here is the problm, if i use sleep( ) function here,
user can enter morethan one charector with in 1 sec.
some conflicts in displaying the line no..
my thought is if i directly change the state of the pthread from
running to ready
then CPU contole goes to main thread, if the main thread goes to wait
queue
the Pthread gets executed here iam using
mutex(mutex lock to read input buffer) to avoid conflects in input
can u suggest better way for my problm..
>
> You can, but it's not generally useful unless you're doing cooperative
> threading, which pthreads is not.
>
>
> sched_yield();
>
> There is no guarantee it will put the thread at the /back/ of
> a ready queue, as the ready queue might not be a fifo queue (unless
> when run with SCHED_FIFO).
>
thanks
actually iam not familier with SystemCalls[vbcol=seagreen]
>
| |
| Nils O. Selåsdal 2006-08-30, 7:33 am |
| Subhash wrote:
> Nils O. Selåsdal wrote:
>
> what actually my problm is iam using a pthread it is created from main
> thread,
> the main aim of program is to print(printing at bottom left corner) the
> current
> charector position (it's just like vi editor).
> To count no of lines, charectors i used a pthread.
> To take input from user i used getchar( ) in main thread.
> the main thread will be in waitqueue(i think) untill we enter a
> charector from keybord.
> In this time pthread will get CPU time(i mean running).
> I used a while loop for counting no of lines and charectors in that
> line
> after that i want to put the pthread(this is excuting currently) into
> ready state
Aha.
So your problem is actually notifying the other thread that there is
data available. Then you should use e.g. condition variables which
servers that purpose.
One thread waits for a condition (character is available), another
thread signals the condition so the thread can wake up and start
processing. Ensure locking of all data.
The easiest is likely to create a workqueue between the threads.
thread A reads characters, puts them on the queue, thread B grabs
itmes off the queue and processe them.
You can use http://asgaard.homelinux.org/code/threadqueue/ which
is specifically made for that purpose.
> here is the problm, if i use sleep( ) function here,
> user can enter morethan one charector with in 1 sec.
> some conflicts in displaying the line no..
> my thought is if i directly change the state of the pthread from
> running to ready
> then CPU contole goes to main thread, if the main thread goes to wait
> queue
Well, that is wrong. There is no guarantee it is the other thread that
will be run next, and if there was you would need some way to make sure
that thread runs until completion and not switch to the other one
inbetween.
> the Pthread gets executed here iam using
> mutex(mutex lock to read input buffer) to avoid conflects in input
| |
| David Schwartz 2006-08-31, 1:38 am |
|
Subhash wrote:
> I want to suspend the current thread and give execution to other
> thread,
Until what happens?
> i don't want to sleep the current thread (for that i can use sleep( )
> function).
> if i use sleep(1sec) funtion the current thread(say first thread) gives
> the execution to
> other thread(say 2nd thread), if the 2nd thread completes its
> execution,
> then the first thread will not get executed untill sleep time
> comlete....
> i don't want to waste that time.
When do you want the thread the recover? After some event happens? Then
just block on that event.
> For that, I got an idea, i.e why can i put a thread into sleep state,
> why don't i directly put it into ready state so that the first thread
> will get CPU time after comletion of 2nd thread....
>
> is there any function to put the current thread into ready state(i.e
> running to ready)...
Yeah, just unblock the event that thread is blocked on. That is, make
the thread wait for something, then just do that something. Most likely
what you want is pthread_cond_* functions.
DS
| |
| Subhash 2006-08-31, 7:35 am |
|
Nils O. Sel=E5sdal wrote:
> Subhash wrote:
te,[vbcol=seagreen]
nd[vbcol=seagreen]
nt[vbcol=seagreen]
or[vbcol=seagreen]
> Aha.
> So your problem is actually notifying the other thread that there is
> data available. Then you should use e.g. condition variables which
> servers that purpose.
>
> One thread waits for a condition (character is available), another
> thread signals the condition so the thread can wake up and start
> processing. Ensure locking of all data.
>
> The easiest is likely to create a workqueue between the threads.
> thread A reads characters, puts them on the queue, thread B grabs
> itmes off the queue and processe them.
> You can use http://asgaard.homelinux.org/code/threadqueue/ which
> is specifically made for that purpose.
>
>
> Well, that is wrong. There is no guarantee it is the other thread that
> will be run next, and if there was you would need some way to make sure
> that thread runs until completion and not switch to the other one
> inbetween.
Thats great ...
Thank you....
|
|
|
|
|