linux 用mutex定义一个linkedlist,【基于LINUX的操作系统实验教程最终版材料】
(基于LINUX的操作系統(tǒng)實(shí)驗(yàn)教程)(最終版)
《基于LINUX的操作系統(tǒng)實(shí)驗(yàn)教程.doc》由會(huì)員分享,可免費(fèi)在線閱讀全文,更多與《(基于LINUX的操作系統(tǒng)實(shí)驗(yàn)教程)(最終版)》相關(guān)文檔資源請(qǐng)?jiān)趲蛶臀膸?kù)(www.woc88.com)數(shù)億文檔庫(kù)存里搜索。
1、b;*WaitonthejobqueuesemahoreIfitsvalueisositive,indicatingthatthequeueisnotemty,decrementthecountbyIfthequeueisemty,blockuntilanewjobisenqueued*sem_wait(amjob_queue_count);*Lockthemutexonthejobqueue*thread_mutex_lock(amjob_queue_mutex);*Becauseofthesemahore,weknowthequeueisnotemtyGetthenextavailablejob*next_job=job_queue;*Removethisjobfromthelist*job_queue=job_queuegtnext;*Unlockthemutexonthejobqueuebecausewe’redonewiththequeuefornow*thread_mutex_unlock(amjob_queue_mutex);*Carryoutthework*rocess_job(next_job);*Cleanu*free(next_job);}returnNULL;}*Addanewjobtothefrontofthejobqueue*voidenqueue_job(*Passjobsecificdatahere*){structjob*new_job;*Allocateane。
2、else{*Getthenextavailablejob*next_job=job_queue;*Removethisjobfromthelist*job_queue=job_queuegtnext;}*Unlockthemutexonthejobqueuebecausewe’redonewiththequeuefornow*thread_mutex_unlock(amjob_queue_mutex);*Wasthequeueemty?Ifso,endthethread*if(next_job==NULL)eak;*Carryoutthework*rocess_job(next_job);*Cleanu*free(next_job);}returnNULL;}.POSIX:SEM無(wú)名信號(hào)量POSIX:SEM信號(hào)量是一個(gè)sem_t類型的變量。()聲明一個(gè)信號(hào)量并對(duì)其進(jìn)行初始化下面的代碼聲明了一個(gè)名為sem的信號(hào)量變量。includesem_tsem;必須在使用POSIX:SEM信號(hào)量之前對(duì)其進(jìn)行初始化。初始化函數(shù):intsem_init(sem_t*sem,intshared,unsignedvalue);參數(shù)shared=說(shuō)明只能由初始化這個(gè)信號(hào)量的進(jìn)程的線程才能使用該信號(hào)量,若shared為非,任何可以訪問(wèn)sem的進(jìn)程都可以使用這個(gè)信號(hào)量。參數(shù)value設(shè)置信號(hào)量的初始值。()對(duì)信號(hào)量的操作sem_ost函數(shù)實(shí)現(xiàn)了經(jīng)典的信號(hào)量signal操作。sem_wait函數(shù)實(shí)現(xiàn)了經(jīng)典的信號(hào)量wait操作。
3、ar_rint_arms*)arameters;inti;for(i=;icount;++i)futc(gtcharacter,stderr);returnNULL;}*Themainrogram*intmain(){thread_tthread_id;thread_tthread_id;structchar_rint_armsthread_args;structchar_rint_armsthread_args;*Createanewthreadtorint,'x's*thread_argscharacter='x';thread_argscount=;thread_create(amthread_id,NULL,amchar_rint,amthread_args);*Createanewthreadtorint,o's*thread_argscharacter='o';thread_argscount=;thread_create(amthread_id,NULL,amchar_rint,amthread_args);thread_join(thread_id,NULL);thread_join(thread_id,NULL);return;}.線程實(shí)現(xiàn)同步與互斥.互斥GNULinux提供了互斥鎖mutex來(lái)實(shí)現(xiàn)線程之間的互斥。要?jiǎng)?chuàng)建一個(gè)互斥鎖,首先定義一個(gè)thread_mutex_t類型的變量,然后調(diào)用thread_mutex_init函數(shù)。例如:thread_mutex_tmutex;t。
4、。intsem_ost(sem_tsem);intsem_wait(sem_tsem);舉例:_cincludeincludeincludestructjob{*Linkfieldforlinkedlist*structjob*next;*Otherfieldsdescribingworktobedone*};*Alinkedlistofendingjobs*structjob*job_queue;*Amutexrotectingjob_queue*thread_mutex_tjob_queue_mutex=PTHREAD_MUTEX_INITIALIZER;*Asemahorecountingthenumberofjobsinthequeue*sem_tjob_queue_count;*Performonetimeinitializationofthejobqueue*voidinitialize_job_queue(){*Thequeueisinitiallyemty*job_queue=NULL;*InitializethesemahorewhichcountsjobsinthequeueItsinitialvalueshouldbezero*sem_init(amjob_queue_count,,);}*Processqueuedjobsuntilthequeueisemty*void*thread_function(void*arg){while(){structjob*next_jo。
5、hread_mutex_init(ammutex,NULL);thread_mutex_init函數(shù)的第二個(gè)參數(shù)為mutex的屬性,若為空,則使用缺省的屬性。另一種建立使用缺省屬性的互斥鎖的方式是:thread_mutex_tmutex=PTHREAD_MUTEX_INITIALIZER;加鎖操作使用函數(shù)thread_mutex_lock解鎖操作使用函數(shù)thread_mutex_unlock例如:_cincludeincludestructjob{*Linkfieldforlinkedlist*structjob*next;*Otherfieldsdescribingworktobedone*};*Alinkedlistofendingjobs*structjob*job_queue;*Amutexrotectingjob_queue*thread_mutex_tjob_queue_mutex=PTHREAD_MUTEX_INITIALIZER;*Processqueuedjobsuntilthequeueisemty*void*thread_function(void*arg){while(){structjob*next_job;*Lockthemutexonthejobqueue*thread_mutex_lock(amjob_queue_mutex);*Nowit’ssafetocheckifthequeueisemty*if(job_queue==NULL)next_job=NULL;。
6、eadtorint,o's*thread_argscharacter='o';thread_argscount=;thread_create(amthread_id,NULL,amchar_rint,amthread_args);return;}運(yùn)行該程序,出現(xiàn)什么問(wèn)題?分析出現(xiàn)的問(wèn)題。例:等待線程結(jié)束上述問(wèn)題的解決方法是在主線程中增加等待線程結(jié)束的函數(shù)調(diào)用thread_joinincludeintthread_join(thread_tthread,void**value_tr);該函數(shù)將調(diào)用線程掛起,直到第一個(gè)參數(shù)指定的線程退出為止。參數(shù)void**value_tr為指向目標(biāo)線程的返回值的指針提供了一個(gè)位置,若為NULL,調(diào)用線程保存目標(biāo)線程的返回值。程序_cincludeinclude*Parameterstorint_function*structchar_rint_arms{*Thecharactertorint*charcharacter;*Thenumberoftimestorintit*intcount;};*Printsanumberofcharacterstostderr,asgivenbyPARAMETERS,whichisaointertoastructchar_rint_arms*void*char_rint(void*arameters){*Castthecookieointertotherighttye*structchar_rint_arms*=(structch。
總結(jié)
以上是生活随笔為你收集整理的linux 用mutex定义一个linkedlist,【基于LINUX的操作系统实验教程最终版材料】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 查看linux电脑总内存,如何查看Lin
- 下一篇: linux下的五种io模型,Linux下