I?ve written a timer thread which has a linked list where are the threads, which are sleeping. So far no problem
But I have a problem when I want to sleep a thread. I use a trap gate for my kernel callings.
When a thread wants to go sleeping he calls my int routine and I add a new timer item to the linked list of the timer.
My problem is now that I have a ready and a sleeping queue for my threads. But I cannot remove a running thread from the ready list. 1st I thought I can solve the problem by using a msg system, but if there are to much msgs the msg for removing the thread wont be worked on.
So, how have you done a sleeping function for threads/processes? Or maybe anyone has an idea how I can solve this problem?!
Timer
Re:Timer
I gather that the timer list is implemented separately from the scheduler, then? The usual solution is to use the same task-record nodes for both the pending and sleeping queues, and that when a sleep() call occurs, it signals the scheduler, which makes all the changes itself. How do you call the scheduler for, say, time slices, or for spawning a new thread, and why can't you do the same here?
Just as a matter of clarification: are these processes or threads? Usually, a process refers to a task with it's own protected memory and which is scheduled by the system, whereas threads are usually subtasks of a process, running in shared memory and scheduled by a function within the process. There are some grey areas to this, esp. in multiprocessor systems, but these are the generally accepted definitions. It could make a considerable difference in this case.
BTW, do you also have a separate queue for task waiting on synch events or messages, or are you handling those in some other manner?
Just as a matter of clarification: are these processes or threads? Usually, a process refers to a task with it's own protected memory and which is scheduled by the system, whereas threads are usually subtasks of a process, running in shared memory and scheduled by a function within the process. There are some grey areas to this, esp. in multiprocessor systems, but these are the generally accepted definitions. It could make a considerable difference in this case.
BTW, do you also have a separate queue for task waiting on synch events or messages, or are you handling those in some other manner?
Re:Timer
I have 3 queues: wait, sleep and ready. The only thing my scheduler does, is saving the state of the actual thread and put it to the end of the ready queue and load the state of the next thread.
The problem is that when I remove the actual thread from the ready list, that the scheduler tries to save the state and there occures an error because the pointers that he uses are not right.
My system only uses threads, I have tasks, but they are only for knowing which threads together are a task!
The problem is that when I remove the actual thread from the ready list, that the scheduler tries to save the state and there occures an error because the pointers that he uses are not right.
My system only uses threads, I have tasks, but they are only for knowing which threads together are a task!
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Timer
So if we have a Thread Queue.head(void) and a Queue.enqueue(Thread) operations, and if q.head() actually extracts the queue's head, what you'd need to do is
By having a dedicated 'out-of-the-queue' pointer for the current thread, you should avoid pointers trashing during list manipulations.
If you're still doubting at what should be done, grab a pencil and a WhiteSheetOfPaper (tm) and draw the situation with Box-and-Arrows: it should make things way clearer.
Code: Select all
class Scheduler {
Thread current;
Queue ready;
// this one is called when the current thread remains ready
void switch() {
current.saveState();
ready.enqueue(current);
current=ready.head();
current.restoreState();
}
// this one is called when the current thread is no longer ready
void switch(Queue sleep) {
current.saveState();
sleep.enqueue(current);
current=ready.head();
current.restoreState();
}
}
If you're still doubting at what should be done, grab a pencil and a WhiteSheetOfPaper (tm) and draw the situation with Box-and-Arrows: it should make things way clearer.