gjp426
gjp426

Reputation: 1

How to create an uninterruptible sleep in C?

I'm looking to create a state of uninterruptible sleep for a program I'm writing. Any tips or ideas about how to create this state would be helpful.

So far I've looked into the wait_event() function defined in wait.h, but was having little luck implementing it. When trying to initialize my wait queue the compiler complained

warning: parameter names (without types) in function declaration static DECLARE_WAIT_QUEUE_HEAD(wq);

Has anyone had any experience with the wait_event() function or creating an uninterruptible sleep?

Upvotes: 0

Views: 1501

Answers (3)

Unavailable
Unavailable

Reputation: 681

Actually, you should use synchronization objects provided by the operating system you're working on or simply check the return value of sleep function. If it returns to a value bigger than zero, it means your procedure was interrupted. According to this return value, call sleep function again by passing the delta (T-returnVal) as argument (probably in a loop, in case of possible interrupts that might occur again in that time interval)

On the other hand, if you really want a real-uninterruptible custom sleep function, I may suggest something like the following:

void uninterruptible_sleep(long time, long factor)
{
    long i, j;

     __asm__("cli"); // close interrupts

     for(i=0; i<time; ++i)
        for(j=0; j<factor; ++j)
           ;                   // custom timer loop

     __asm__("sti"); // open interrupts
 }

cli and sti are x86 assembly instructions which allow us to set IF (interrupt flag) of the cpu. In this way, it is possible to clear (cli) or set (sti) all the interrupts. However, if you're working on a multi-processor system, there needs to be taken another synchronization precautions too, due to the fact that these instructions will only be valid for single microprocessor. Moreover, this type of function as I suggested above, will be very system (cpu) dependant. Because, the inner loop requires a clock-cycle count to measure an exact time interval (execution number of instructions per second) depending on the cpu frequency. Thus, if you really want to get rid of every possible interrupt, you may use a function as I suggested above. But be careful, if your program gets a deadlock situation while it's in cli state, you will need to restart your system.

(The inline assembly syntax I have written is for gcc compiler)

Upvotes: 0

Prabhu
Prabhu

Reputation: 3541

You can make sleep 'signal-aware`.

sleep can be interrupted by signal. In which case the pause would be stopped and sleep would return with amount of time still left. The application can choose to handle the signal notified and if needed resume sleep for the time left.

Upvotes: 1

user149341
user149341

Reputation:

The functions that you're looking at in include/linux/wait.h are internal to the Linux kernel. They are not available to userspace.

Generally speaking, uninterruptible sleep states are considered undesirable. Under normal circumstances, they cannot be triggered by user applications except by accident (e.g, by attempting to read from a storage device that is not responding correctly, or by causing the system to swap).

Upvotes: 4

Related Questions