![]() The kernel semaphore is similar to a spinlock because it does not allow the kernel control path to continue when the lock is closed. Therefore, it also wakes up all tasks waiting for the semaphore. If the semaphore value is non positive, it indicates that there is a task waiting for the current semaphore. Releasing semaphore is realized by adding the semaphore value by 1. When a task accesses the shared resource protected by semaphore, it must release semaphore. If the current semaphore value is negative, it indicates that the semaphore cannot be obtained, and the task must be suspended in the The waiting queue of the semaphore waits for the semaphore to be available if the value of the current semaphore is non negative, it means that the semaphore can be obtained and the shared resources protected by the semaphore can be accessed immediately. The operation of obtaining semaphore will reduce the semaphore value by 1. If a task wants to access the shared resources, it must first get the semaphore. Semaphores, spinlocks, and deadlocks are discussed in detail later. Therefore, it is appropriate to use spinlock in this scenario. When waiting for a register to be set, the waiting time in this scenario is often very short, and the system cost is even much less than the cost of entering sleep. We just need to line up and brush the sound. If we need to find a place to sleep, then wait for the lift to lift up or the toilet can wake up again, it is obviously not necessary. Tiktok, the restroom restroom, we don't have to wait for a lot of time. It is appropriate to use semaphore in this scenario. When the waiting event occurs, other tasks wake up. It can suspend the current task and put it into sleep state. In this way, from the perspective of people (users), the experience is the best Compared with the process, when waiting for a time-consuming event, the program does not have to occupy the CPU all the time. The ideal situation is that we will go to bed directly when we get on the train and arrive at the station when we wake up (readers who have read "alien" will have a deep understanding). However, we don't have to keep our eyes open. We can only sit in the car and wait for the bus to arrive at the station. This' task 'is particularly time-consuming. It takes us two days to get from Nanjing to Xinjiang by train. Since semaphore contention processes sleep while waiting for a lock to become available again, semaphores are suitable for situations where locks will be held for a long time conversely, semaphores are not appropriate when locks are held for a short time, because the cost of sleeping, maintaining the waiting queue, and waking up may be longer than the total schedule occupied by the lock. Unlike spinlocks, the processor can execute other code. When the process holding the semaphore releases the semaphore, a task in the waiting queue will be awakened (because there may be more than one task in the queue) and let it get the semaphore. ![]() If a task wants to get a semaphore that has already been occupied, the semaphore puts it in a waiting queue (it does not stand outside waiting, but writes its name in the task queue) and lets it sleep. The semaphore of Linux kernel is the same as the IPC semaphore of System V in user mode in concept and principle, but it can never be used outside the kernel, so it has nothing to do with the IPC mechanism semaphore of system v.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |