semaphore信号量

semaphore也就是信号量,是一种进程见同步机制,我们可以作为互斥量来保护临界区资源,但是作为一种同步机制,还能怎么用呢?当然我们可以做进程间的同步使用,比如进程A和进程B,如果进程A要等待进程B完成某项工作后才能继续运行,那么可以使用信号量来操作,进程A先对一个信号量进行P操作(减少),然后运行到特定的地方再次尝试去P操作(减少),如果是普通的锁,此时就已经死锁了,因为锁的获取与释放只能由同一个进程来做,但是信号量并不是,它可以由另一个进程去进行V操作(增加信号量),从而达到释放的目的。另外一个关键的特点是,在semaphore保护的临界区中是允许睡眠的。一般我们会初始化信号量为1。

P 操作

 /**
  * down - acquire the semaphore
  * @sem: the semaphore to be acquired
  *
  * Acquires the semaphore.  If no more tasks are allowed to acquire the
  * semaphore, calling this function will put the task to sleep until the
  * semaphore is released.
  *
  * Use of this function is deprecated, please use down_interruptible() or
  * down_killable() instead.
  */
 void down(struct semaphore *sem)
 {
     unsigned long flags;
 
     raw_spin_lock_irqsave(&sem->lock, flags);
     if (likely(sem->count > 0))
         sem->count--;
     else
         __down(sem);
     raw_spin_unlock_irqrestore(&sem->lock, flags);
 }

Linux内核采用down作为P操作,count–其实也就是减少操作。这个API已经逐渐被弃用了,推荐使用后面的两种:

int down_interruptible(struct semaphore *sem)
{
    unsigned long flags;
    int result = 0;

    raw_spin_lock_irqsave(&sem->lock, flags);
    if (likely(sem->count > 0))
        sem->count--;
    else
        result = __down_interruptible(sem);
    raw_spin_unlock_irqrestore(&sem->lock, flags);

    return result;
}

int down_killable(struct semaphore *sem)
{
    unsigned long flags;
    int result = 0;

    raw_spin_lock_irqsave(&sem->lock, flags);
    if (likely(sem->count > 0))
        sem->count--;
    else
        result = __down_killable(sem);
    raw_spin_unlock_irqrestore(&sem->lock, flags);

    return result;
}

static noinline void __sched __down(struct semaphore *sem)
{   
    __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
}

static noinline int __sched __down_interruptible(struct semaphore *sem)
{
    return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
}

static noinline int __sched __down_killable(struct semaphore *sem)
{
    return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
}

static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)
{   
    return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
}

最后都是通过__down_common这个函数来进行的操作:

static inline int __sched __down_common(struct semaphore *sem, long state,
                                long timeout)
{
    struct semaphore_waiter waiter;

    list_add_tail(&waiter.list, &sem->wait_list);
    waiter.task = current;
    waiter.up = false;

    for (;;) {
        if (signal_pending_state(state, current))
            goto interrupted;
        if (unlikely(timeout <= 0))
            goto timed_out;
        __set_current_state(state);
        raw_spin_unlock_irq(&sem->lock);
        timeout = schedule_timeout(timeout);
        raw_spin_lock_irq(&sem->lock);
        if (waiter.up)
            return 0;
    }

 timed_out:
    list_del(&waiter.list);
    return -ETIME;

 interrupted:
    list_del(&waiter.list);
    return -EINTR;
}

最后的函数是设置current进程状态,然后调用schedule_timeout进行调度。

V 操作

对应这个Linux内核中的up操作,如果没有人在等待这个信号量,那么就对计数器加1操作来增加count值,如果有人在等待就唤醒一个等待的进程。

/**
 * up - release the semaphore
 * @sem: the semaphore to release
 *
 * Release the semaphore.  Unlike mutexes, up() may be called from any
 * context and even by tasks which have never called down().
 */
void up(struct semaphore *sem)
{
    unsigned long flags;

    raw_spin_lock_irqsave(&sem->lock, flags);
    if (likely(list_empty(&sem->wait_list))) 
        sem->count++;
    else
        __up(sem);
    raw_spin_unlock_irqrestore(&sem->lock, flags);
}
EXPORT_SYMBOL(up);

static noinline void __sched __up(struct semaphore *sem)
{
    struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
                        struct semaphore_waiter, list);
    list_del(&waiter->list);
    waiter->up = true;
    wake_up_process(waiter->task);
}

唤醒操作是优先唤醒第一个等待的进程,从list head的第一个成员开始唤醒。

你可能感兴趣的:(内核锁与同步)