(基于LINUX的操作系统实验教程)(最终版)
《基于LINUX的操作系统实验教程.doc》由会员分享,可免费在线阅读全文,更多与《(基于LINUX的操作系统实验教程)(最终版)》相关文档资源请在帮帮文库(www.woc88.com)数亿文档库存里搜索。
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无名信号量POSIX:SEM信号量是一个sem_t类型的变量。()声明一个信号量并对其进行初始化下面的代码声明了一个名为sem的信号量变量。includesem_tsem;必须在使用POSIX:SEM信号量之前对其进行初始化。初始化函数:intsem_init(sem_t*sem,intshared,unsignedvalue);参数shared=说明只能由初始化这个信号量的进程的线程才能使用该信号量,若shared为非,任何可以访问sem的进程都可以使用这个信号量。参数value设置信号量的初始值。()对信号量的操作sem_ost函数实现了经典的信号量signal操作。sem_wait函数实现了经典的信号量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;}.线程实现同步与互斥.互斥GNULinux提供了互斥锁mutex来实现线程之间的互斥。要创建一个互斥锁,首先定义一个thread_mutex_t类型的变量,然后调用thread_mutex_init函数。例如: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函数的第二个参数为mutex的属性,若为空,则使用缺省的属性。另一种建立使用缺省属性的互斥锁的方式是:thread_mutex_tmutex=PTHREAD_MUTEX_INITIALIZER;加锁操作使用函数thread_mutex_lock解锁操作使用函数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;}运行该程序,出现什么问题?分析出现的问题。例:等待线程结束上述问题的解决方法是在主线程中增加等待线程结束的函数调用thread_joinincludeintthread_join(thread_tthread,void**value_tr);该函数将调用线程挂起,直到第一个参数指定的线程退出为止。参数void**value_tr为指向目标线程的返回值的指针提供了一个位置,若为NULL,调用线程保存目标线程的返回值。程序_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。