Andrey Isakov
Andrey Isakov

Reputation: 1

does down(semaphore) and down_intterruptible(semaphore) must be called from system call?

i was asked if i can use the The following functions :

void down(struct semaphore* sem);
int down_intterruptible(struct semaphore* sem);

ONLY from system call ?

Upvotes: 0

Views: 772

Answers (2)

Pramod Gurav
Pramod Gurav

Reputation: 163

Not necessarily. You can try locking the semaphore anywhere from kernel except the interrupt context. Failing to acquire a semaphore will put your task to sleep and you cant afford to put your interrupt handler to sleep and cause a deadlock.

you can use

Upvotes: 0

Roland
Roland

Reputation: 6563

No, they are fine to use from any kernel context where you are allowed to sleep. For example, a kernel thread may do down(). Of course a timer function cannot, since down() may sleep. You definitely do not need to be directly handling a system call.

As an aside, in modern kernels, struct mutex and mutex_lock() are preferred to struct semaphore and down() except for cases where you truly need counting semaphores, or need to release the semaphore from a different context than where it is acquired.

Upvotes: 1

Related Questions