Wake_up_interruptible () does not wake up processes sleeping on condition

I am writing a sleepy driver. Here, any process that tries to write to the device file should fall asleep in "n" the number of seconds provided by the user. The reading process should awaken all waiting processes.

Writer Code:

printk("Invoking interruptible_timeout for device flag[%d] = %d\n", idn, flag[idn]);
long ret = wait_event_interruptible_timeout(wq[idn],flag[idn]==1,sec*HZ)/HZ;
//flag[idn]=0;
printk("timeout returned : %d   idn = %d\n", ret, idn)
printk("writer : flag[%d] = %d\n", idn, flag[idn]);
retval=ret;

Reader Code:

printk("waking up process with idn = %d\n", idn);
flag[idn]=1;
printk("reader : flag[%d] = %d \n", idn, flag[idn]);
wake_up_interruptible(&wq[idn]);
while(waitqueue_active(&wq[idn]));
flag[idn] = 0;
printk("returned from wake_up_interruptible \n");
printk("reader : flag[%d] = %d \n", idn, flag[idn]);

Initially, the flag [0] = 0; Thus, all writing processes sleep until the condition flag [idn] == 1 becomes true. This works as expected.

But in the reader code, I set the [idn] = 1 flag and call wake_up_interruptible () to wake up all processes sleeping on condition. But this does not awaken sleeping processes. But if I put the [idn] = 0 flag a little lower (waitqueue_active (& wq [idn])), it works, that is, the function wakes up sleeping processes.

Why is this so?

+1
1

flag[idn] = 0, waitqueue , , awoken writer (flag[idn] == 1). , , 0 .

, wait_event- autoremove_wake_function, wait waitqueue. wake_up_interruptible(), , awoken, .

, wake_up() - : , , .

awoken :

:

int cflag = flag[idn];
wait_event_interruptible_timeout(&wq[idn], flag[idn] != cflag, sec*HZ);

:

flag[idn]++;
wake_up_interruptible_all(&wq[idn]);

, , . , flag[idn]++ reset flag[idn] != cflag.

+2

All Articles