linux操作系统加锁解锁,UNIX操作系统的加锁解锁:等待事件及唤醒

jerry520 于 2008-09-27 12:24:10发表:

而在schedule()返回到这段代码之后,事情就不一样了。因为在step 3之后,current进程已经进入睡眠,tmp指向的睡眠进程的描述符也被保存下来。从schedule()返回之后,执行的代码仍然是current,而tmp指向的仍然是wait process,此时将其状态置为就绪,等待下一次调度。

与前两个函数相比,wake_up相当简单:

//被唤醒的进程并不是马上投入运行,而是让其适合运行

void wake_up(struct task_struct **p)

{

if (p && *p) {

(**p).state=0; //将要唤醒的进程状态置为就绪

*p=NULL; //将进程移出等待的进程

}

}

有了sleep_on()和wake_up()之后,就可以对资源加锁了,如(硬盘缓冲加锁、等待缓冲可用、唤醒等待进程):

//锁住bh

static inline void lock_buffer(struct buffer_head * bh)

{

if (bh->b_lock)

printk("hd.c: buffer multiply locked\n");

bh->b_lock=1;

}

static inline void unlock_buffer(struct buffer_head * bh)

{

if (!bh->b_lock)

printk("hd.c: free buffer being unlocked\n");

bh->b_lock=0;

wake_up(&bh->b_wait);

}

static inline void wait_on_buffer(struct buffer_head * bh)

{

cli(); //禁止中断

while (bh->b_lock)

sleep_on(&bh->b_wait);

sti(); //恢复中断

}

//Linux 0.99.15的sleep和wake_up的实现(支持等待队列):

static inline void __sleep_on(struct wait_queue **p, int state)

{

unsigned long flags;

struct wait_queue wait = { current, NULL };

if (!p)

return;

if (current == task[0])

panic("task[0] trying to sleep");

current->state = state;

add_wait_queue(p, &wait); //将当前进程加入等待队列

save_flags(flags); //保存中断掩码

sti(); //屏蔽中断

schedule(); //上下文切换

remove_wait_queue(p, &wait); //从等待队列中移除当前进程

restore_flags(flags); //恢复中断掩码

}

void wake_up(struct wait_queue **q)

{

struct wait_queue *tmp;

struct task_struct * p;

if (!q || !(tmp = *q))

return;

do {//将等待队列中唤醒队首进程

if ((p = tmp->task) != NULL) {

if ((p->state == TASK_UNINTERRUPTIBLE) ||

(p->state == TASK_INTERRUPTIBLE)) {

p->state = TASK_RUNNING;

if (p->counter > current->counter)

need_resched = 1;

}

}

if (!tmp->next) {

printk("wait_queue is bad (eip = %08lx)\n",((unsigned long *) q)[-1]);

printk(" q = %p\n",q);

printk(" *q = %p\n",*q);

printk(" tmp = %p\n",tmp);

break;

}

tmp = tmp->next;

} while (tmp != *q);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值