0

I am implementing a thread library in C using makecontext(), getcontext() and swapcontext(). I need to implement a scheduler which is invoked every 5 ms in order to switch contexts with another thread (round robin). How could I implement this timer functionality? If I put the timer in the scheduler, then it will be impossible for time to be incremented while the scheduler is not running. Is there a way to associate a timer with a particular process that updates no matter what context is active?

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
  • The timer could just run in its own thread with a callback being invoked every 5ms. Even if it were running inside the scheduler thread, you could still invoke the callback. – Irelia Oct 11 '19 at 00:08
  • the issue is that even if the scheduler or timer thread is not running, I still need the time to be updating. For example, if a particular thread is running that is not the scheduler or timer thread, how could I keep track of the amount of time? @Nina – Cole Bisaccia Oct 11 '19 at 00:10
  • 1
    So I assume you have a deadlock loop in your timer thread that waits 5ms then does a context switch? If that's the case, you could use asynchronious timers instead in which you would not need to handle the time increment yourself. – Irelia Oct 11 '19 at 00:11
  • 2
    A hardware timer interrupt is traditionally used. – Martin James Oct 11 '19 at 04:22

1 Answers1

0

A solution based on setitimer() system call could do the job. This can be programmed to trigger a cyclic SIGALRM signal. It could then be possible to attach a signal handler which would trigger the scheduler.

Rachid K.
  • 4,490
  • 3
  • 11
  • 30