-   Linux - Software (
-   -   semaphore usage in user and kernel level (

haribabu1836 10-30-2012 08:07 AM

semaphore usage in user and kernel level

I was going through the semaphore's usage in linux and not yet get clear understanding on below things.

1. Is user and kernel level programming in Linux had different semaphore function calls?

2. what is the difference between "#include <linux/semaphore.h>" and "#include <semaphore.h>" files.

3. Can you please let me know where the user(applications) and kernel(modules) concepts differ in terms of programming on Linux.

Thanks in advance.

neonsignal 11-09-2012 11:06 PM

  1. In general kernel code cannot make use of any user space code. So while implementations of semaphores in user space simply make use of the operating system API for their implementation (ie, only indirectly using the kernel), inside the kernel code there has to be more direct use of interprocess primitives. The C interfaces could be made to look the same. But the kind of locking you might want to do in the kernel is not quite the same as in user code (so there are different primitives available), there's little attempt made to make them look the same.
  2. You would normally use <semaphore.h> in your program. The directory /usr/include/linux contains header information that is specific to interfacing with the kernel; it is still designed for user space code, but is not portable. You might need it if you require more specific API support.
  3. Programming in C for user applications and for the kernel differs because the context is different. In user space, the operating system takes care of process spawning, supports the primitives that the standard C libraries use for all I/O, provides the memory handling and security to ensure the proper separation of user processes, and so on. In monolithic kernel code, you have to be more aware of the context, since the support code you are using is not standard C libraries, but other components of the kernel. In the kernel, you are one level closer to the hardware, and do not enjoy the abstraction of the standard libraries.

All times are GMT -5. The time now is 10:59 PM.