in Operating System edited by
7,806 views
22 votes
22 votes
Semaphore operations are atomic because they are implemented within the OS _________.
in Operating System edited by
7.8k views

2 Comments

Semaphores are lock variables with sleeping mutex hence they are atomic in nature
1
1
can somebody explain the question
0
0

1 Answer

28 votes
28 votes
Best answer
The concept of semaphores is used for synchronization.
Semaphore is an integer with a difference. Well, actually a few differences.
You set the value of the integer when you create it, but can never access the value directly after that; you must use one of the semaphore functions to adjust it, and you cannot ask for the current value.
There are semaphore functions to increment or decrement the value of the integer by one.
Decrementing is a (possibly) blocking function. If the resulting semaphore value is negative, the calling thread or process is blocked, and cannot continue until some other thread or process increments it.
Incrementing the semaphore when it is negative causes one (and only one) of the threads blocked by this semaphore to become unblocked and runnable.

Therefore, all semaphore operations are atomic. Implemented in kernel,
selected by

5 Comments

The fact that Semaphore operations needs to be atomic is fine but I didn't get that because of atomic operations, they must be implemented in kernel. Does that mean operations in kernel are atomic i.e. once a system call is made, it cannot be pre-empted?
1
1
Semaphores are OS solution to critical section problem,so to get any service from OS,they must be implemented in kernel. It doesn't mean anything that they must be atomic
0
0

@

Yes, processes running in user mode can be preempted but processes running in kernel mode can not be preempted.

 

 

1
1
Semaphore operations are atomic because they are implemented with atomic hardware instructions or synchronized using operating system mechanisms like locks or atomic operations. These mechanisms ensure that semaphore operations, such as wait (P) and signal (V), are indivisible and cannot be interrupted by other processes or threads, thereby maintaining consistency and preventing race conditions. So, while the operating system plays a role in managing concurrency and providing synchronization primitives, the atomicity of semaphore operations stems from low-level hardware support or operating system synchronization mechanisms.
0
0
No, not all operations in the kernel are atomic. While certain operations in the kernel may be implemented atomically to prevent race conditions and ensure consistency, not all operations are guaranteed to be atomic.

Regarding system calls, they can be preempted, but the kernel often takes measures to ensure that critical sections of code, such as those involving shared resources or synchronization primitives like semaphores, are executed atomically. This is typically achieved through disabling interrupts or using locking mechanisms to prevent preemption while critical operations are being performed. However, this doesn't mean that all system calls are atomic or cannot be preempted. It depends on the specific implementation and requirements of the kernel and the operations being performed.
0
0

Related questions