Alex
Alex

Reputation: 13116

How can I turn off interrupt that switching threads in OS, on short time, for my critical part of code?

As is known, if the executing threads is more than the number of processor cores, the threads are switched between themselves at certain time slots(quanta). This is true for Win/*nix. But, what mechanism is implemented for a threads switching, it is a hardware interrupt that are launching at specific time slots, isn't it?

What number this interrupt(IRQ), can I change/set the value of this time(time slot), and how can I turn off this interrupt, on short time, for my critical to performance(real-time) part of code (by using WINAPI/Posix)?

Upvotes: 3

Views: 3626

Answers (3)

If you need deterministic scheduling, you will need a Real-time operating system.

http://en.wikipedia.org/wiki/Real-time_operating_system

Upvotes: 1

James Kanze
James Kanze

Reputation: 153899

You can't turn off the timer interrupts for the OS; they're managed within the OS itself, and are necessary for its correct functionning. Under most Unix, you can use sched_setscheduler and/or pthread_setscheduler to set the scheduling policy to SCHED_FIFO, and sched_setparam/pthread_setschedprio to set the priority; you will then be guaranteed to not be interrupted by anything unless it has an even higher priority.

Under Linux, at least (and also under other Unix I've worked on), it is also possible to lock pages into real memory, so you can't get a page fault on them (which would cause you to wait for the disk access). The function in Linux is mlock.

Note that you'll probably need special privileges to change the global scheduler or to use mlock.

Upvotes: 6

Mats Petersson
Mats Petersson

Reputation: 129314

Short answer: You generally can't.

Longer answer: If you have a critical task, you could try to up the priority of that thread, but it's not going to help indefinitely, because modern (non-realtime) OS's do not allow a thread to "take all the CPU in the machine".

Further, ANY interrupt can cause your task to be scheduled. The timer is the one that does it based on time-quanta, but for example if another task is waiting for a network packet, that task will be placed in the runnable queue when such a packet comes in, so the scheduler will run at that point, and make a decision if it's time to run another thread (which technically could be NEITHER your thread, nor the network-packet-waiting thread). Likewise, if your thread touches some memory that has been swapped out to disk, it obviously can't continue until the data has been read back from disk, so it will be "parked" in the blocked queue and some other thread from the runnable queue. When the block has been read from disk, the OS will reschedule again.

There is really no (easy) way around this. And any way that isn't easy will involve messing with drivers and almost writing your own OS.

Upvotes: 4

Related Questions