LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-09-2004, 02:12 AM   #1
sunang
LQ Newbie
 
Registered: Nov 2004
Posts: 5

Rep: Reputation: 0
Question schedule() Do i need to update current state of a process?


hi i 'm a newbie in linux kernel programming.
I would like to implement a task switch by calling schedule().
Do i need to change the current state of the prcess to Interrupt or non-interrupt state before calling schedule().
If i do change the current->state = TASK_INTERRUPTIBLE, and then call schedule();. The process will be blocked forever. How do you wake up the process.

Please help me.

Thanks
Regards,
Tien Sung
 
Old 11-09-2004, 04:12 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
If you just call schedule() on its own, it will perform a context-switch, i.e. another process gets to run for a while. When and if your process runs again depends on its state (if it's in a queue for a hardware interrupt, for example, it will wait for that interrupt).

Setting the state to TASK_INTERRUPTIBLE will make the task be interruptable; you can then send software signals to it (eg. using the kill command) to wake it up. You may need to daemonize() to get your process a PID for the signals to be sent to; alternativly, put your process in a queue for a hardware interrupt (you need to be the user of a bit of hardware to do this).

One way to wake the process is to call schedule_timeout(HZ) instead of schedule(). (The value of HZ is defined in asm/param.h and depends on the proecssor type) This will sleep for a short timeout, and then switch back again.

You shouldn't really call schedule() if the process is uninterruptable; I don't even know if schedule_timeout() will work. Uninterruptible mode is only usually used (as far as I know) for timing-critical handling of IRQs, DMAs and the like, where you don't want a signal to block you.
 
Old 11-09-2004, 08:45 PM   #3
sunang
LQ Newbie
 
Registered: Nov 2004
Posts: 5

Original Poster
Rep: Reputation: 0
hi rjlee,

The problem is if i call shcedule() on it's own, the current process will not guaranteed abort or in other words, context switch is not quaranteed 100%. I did a test on it. .

In order to make the current process switch to another one, a definite solution is to set the current state to TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE. You r right about this. Problem is once the process is change from TASK_RUNNING -> TASK_INTERRUPTIBLE, how can we wake up the process unless, a signal (from kill) or a hard interrupt happens. So, that means, this method of context switching is not workable.

For schedule_timeout , it works perfectly with TASK_INTERRUPTIBLE set. Because, this function actually intialzie a timer IRQ and an interrupt is triggered when time out thus, waking up the task.

I actually want to implement a time condition wait while checking the semaphore try lock. I do not want to use the down semaphore function which is blocking. I want to use a try lock (non -blocking semaphore) and then, check the timestamp. If the time stamp > time out , i will exit the loop. ( Implementing semaphoreWait with time out in linux).

Does anyone has the answer to this?


Thanks
 
Old 11-11-2004, 04:13 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
Edit: I think you mean that you want to try and acquire a semaphore lock, but allow an abore timeout in order to avoid deadlock?

You can set up an IRQ timeout in a similar way to how schedule_timeout() works (use the source) and use down_interruptible() instead of down().

down_interruptible() returns -EINTR if it was interrupted, instead of acquiring the lock.

I haven't found a single macro to do this nicely. There may be one; if not, why not submit one?

Last edited by rjlee; 11-11-2004 at 04:14 PM.
 
Old 11-17-2004, 12:17 AM   #5
sunang
LQ Newbie
 
Registered: Nov 2004
Posts: 5

Original Poster
Rep: Reputation: 0
hi rjlee,

yeah, you could also re-phrase it that way. Yup, your suggestion might work too. However, i was more to looking for a small code change kinda of method.

From further studies, it's easier then to use schedule_timeout(1) . Here we use "1" tick. I think it's reasonable and, it involves smaller code size.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Current state of Dual-Layer Burning Support in Linux linux-rulz Linux - Hardware 3 07-25-2005 05:27 PM
BenQ DC 1300 and gphoto current state v2lk Linux - Hardware 1 07-02-2004 09:56 AM
kill process in D state michal017 Linux - Software 1 04-28-2004 02:53 AM
Process in D state - how to handle them? arobic Linux - Newbie 3 12-11-2003 11:43 AM
use of TASK_UNTERRUPTIBLE process state ankushrb Linux - Software 0 06-09-2003 12:01 AM


All times are GMT -5. The time now is 10:28 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration