0

I try to understand how the synchronization works in linux kernel. I read that semaphores can be use for exceptions but I can not find an example for a situation , semaphore is needed. So why using a semaphore in uni-processor system?

Ohad Shamir
  • 37
  • 1
  • 6
  • `So why using a semaphore in uni-processor system?` - For the same purposes as in a SMP system. Semaphores synchronize **threads**, and even uni-processor system has many threads and may schedule them. – Tsyvarev Feb 14 '17 at 09:27

1 Answers1

0

I am assuming that you are interested in locking generally, rather than semaphores opposed to mutexes (see also "Difference between Counting and Binary Semaphores"[1]). I won't give a detailed explanation of locking, just point out a couple of things.

  1. It usually makes sense to assume that code you might write could be executed on a multi-processor system (uni-processor is increasingly rare these days). I assume because you explicitly mentioned uni-processor that you understand that case.

  2. The Linux kernel can be built to be fully preemptive while running kernel code[2][3]. In that case threads can be interrupted and resumed at almost any point, including for example in the middle of writing I/O to a device. If a thread writing I/O is interrupted and another accessing the same device switched to things will probably not work as intended.

[1] Differnce between Counting and Binary Semaphores

[2] https://kernelnewbies.org/FAQ/Preemption

[3] https://rt.wiki.kernel.org/index.php/CONFIG_PREEMPT_RT_Patch

Community
  • 1
  • 1
michaeljt
  • 1,106
  • 8
  • 17