how to use a kernel semaphore

The following code snippet explains how to use a kernel semaphore.
 avatar
unknown
c_cpp
3 years ago
636 B
7
Indexable
#include <linux/semaphore.h>

// Semaphore Definition
// define a semaphore named ‘empty’
struct semaphore empty; 

// init the semaphore as 5
sema_init(&empty, 5); 

// if the thread works in an infinite loop, this is how it knows
// when to stop. Check d. module_exit for more information.
while (!kthread_should_stop())  {


    // down_interruptible()
    // allows the calling thread to be interrupted 
    // even if it has not acquired the
    // semaphore that it has been waiting for
    if (down_interruptible(&empty)) 
        break; 

    // Critical section
    
    
    
    // signal the semaphore
    up(empty) 
}

Editor is loading...