Semaphore parsing in Linux kernel

Source: Internet
Author: User
Semaphore in the Linux kernel is a common method to protect critical zones using the kernel. the kernel semaphores are the same in terms of concept and principle, and are different from user-state semaphores, but it cannot be used outside the kernel. the kernel semaphore is actually a sleep lock prototype:/* Pleasedo...
Semaphore in the Linux kernel is a common method to protect critical zones using the kernel. the kernel semaphores are the same in terms of concept and principle, and are different from user-state semaphores, but it cannot be used outside the kernel. the kernel semaphore is actually a prototype of a sleep lock:/* Please don't access any members of this structure directly */struct semaphore {spinlock_t lock; unsigned int count; struct list_head wait_list;}; www.2cto.com instance: Ibmphp_hpc.c (\ linux-2.6.30.4 \ drivers \ pci \ hotplug) 1 declare static struct semaphore semOperations; // lock all operations and 2 is initialized in the void function _ Init ibmphp_hpc_initvars (void) init_MUTEX (& semOperations); other initialization methods initialize semaphores sema_init (struct semaphore * sem, int val); used to initialize semaphores, semaphores, set the semaphores sem value to val init_MUTEX (struct semaphore * sem). it is used to initialize the semaphores and set the semaphores sem value to 1 init_MUTEX_LOCKED (struct semaphore * sem ); this parameter is used to initialize the number and set the semaphores sem value to 0, that is, DECLARE_MUTEX (name). It defines a semaphores named name, and initialize the semaphores to 1 3. use the function void ibmphp_lock_operations (void) in www.2cto.com down (& semOper By using the down operation semaphores may cause the calling process to sleep until the semaphores are released by other callers, waiting is in the D state (which can be viewed in top). If you do not want to call the process to sleep, you should use down_trylock (& semOperations). how many threads can this number be obtained? ㄐ PA? Em; if yes? P gets, and it gets the Semaphore and? Return 0; otherwise ,? A non-zero value is returned. it does not cause sleep of the caller. you can use int down_interruptible (struct semaphore * sem) in the interrupt context if you want the caller to be interrupted ); this function is used to obtain semaphores sem. if semaphores are unavailable, the process is set to TASK_INTERRUPTIBLE. the number of sleep tasks is determined by the return value to determine whether the returned results are normal or interrupted. if 0 is returned, it indicates that the semaphore is returned normally. if the return value is not 0, it indicates that the semaphore is interrupted. 4. release void ibmphp_unlock_operations (void) {debug ("% s-Entry \ n ", _ func _); up (& semOperations); to_debug = 0; debug ("% s-Exit \ n", _ func __);} this number releases the semaphores sem. in essence, the sem value is increased by 1. if the sem value is not a positive number, it indicates that a task is waiting for the semaphores. Therefore, you need to wake up the waiting person www.2cto.com. note: the down Series function will reduce the semaphore value by 1 up, and add the semaphore value to the author tsuibin.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.