资源描述:
《linux代码分析.pptx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Linux代码阅读——进程管理部分Linux代码阅读——进程管理信号量进程切换进程执行信号量——P、V操作信号量相关数据结构structsemaphore{atomic_tcount;intsleepers;wait_queue_head_twait;};#define__SEMAPHORE_INITIALIZER(name,n){.count=ATOMIC_INIT(n),.sleepers=0,.wait=__WAIT_QUEUE_HEAD_INITIALIZER((name).wait)}D
2、own()——P操作staticinlinevoiddown(structsemaphore*sem){might_sleep();__asm____volatile__("#atomicdownoperationt"LOCK_PREFIX"decl%0t"/*--sem->count*/“jns2f“//小于0"tlea%0,%%eaxt""call__down_failed""2:":"+m"(sem->count)::"memory","ax");}__down_faile
3、dvoid__sched__down_failed(structsemaphore*sem){structtask_struct*tsk=current;DECLARE_WAITQUEUE(wait,tsk);#ifdefCONFIG_DEBUG_SEMAPHOREprintk("%s(%d):downfailed(%p)",tsk->comm,tsk->pid,sem);#endiftsk->state=TASK_UNINTERRUPTIBLE;wmb();add_wait_queue_exclusi
4、ve(&sem->wait,&wait);__down_failedwhile(__sem_update_count(sem,-1)<=0){schedule();set_task_state(tsk,TASK_UNINTERRUPTIBLE);}remove_wait_queue(&sem->wait,&wait);tsk->state=TASK_RUNNING;wake_up(&sem->wait);#ifdefCONFIG_DEBUG_SEMAPHOREprintk("%s(%d):downacqui
5、red(%p)",tsk->comm,tsk->pid,sem);#endif}int__sem_update_countstaticinlineint__sem_update_count(structsemaphore*sem,intincr){longold_count,tmp=0;__asm____volatile__("1:ldl_l%0,%2""cmovgt%0,%0,%1"//如果oldcount大于0,移动到tmp"addl%1,%3,%1"//tmp与incr相加"stl_c
6、%1,%2""beq%1,2f"//相等的话"mb"".subsection2""2:br1b"".previous":"=&r"(old_count),"=&r"(tmp),"=m"(sem->count):"Ir"(incr),"1"(tmp),"m"(sem->count));returnold_count;}Down()——P操作一些类似的操作staticinlineintdown_interruptible(structsemaphore*sem)staticinlineint
7、down_trylock(structsemaphore*sem)void__scheddown(structsemaphore*sem)int__scheddown_interruptible(structsemaphore*sem)intdown_trylock(structsemaphore*sem)Up()——V操作staticinlinevoidup(structsemaphore*sem){__asm____volatile__("#atomicupoperationt"LOCK_PREF
8、IX"incl%0t"/*++sem->count*/"jg1ft""lea%0,%%eaxt""call__up_wakeup""1:":"+m"(sem->count)::"memory","ax");}__up_wakeup__up_wakeup(structsemaphore*sem){/**Notethatweincrementedcountinup()beforewecamehe