Use sema_trywait(3C) to atomically decrement the count in the semaphore pointed to by sp when the count is greater than zero. This function is a nonblocking version of sema_wait().
#include <thread.h> int sema_trywait(sema_t *sp);
sema_trywait() returns 0 if successful. When any of the following conditions is detected, sema_trywait() fails and returns the corresponding value.
EINVAL
Description:sp refers to an invalid semaphore.
EBUSY
Description:The semaphore pointed to by sp has a zero count.