1、Linux程序设计课程设计 Linux程序设计课程组长春工业大学-12-24课程设计任务书课程设计题目:读者写者问题设计、实现及性能分析起止日期:.12.25-.12.29设计地点:计算机科学和工程学院机房任务:1. Linux环境下分别设计并实现读者写者问题读者优先算法、写者优先算法和无优先算法,针对测试数据得出每种算法下读者线程和写者线程开始操作时刻和结束操作时刻;2. 计算每个线程等候时间、周转时间、总等候时间、总周转时间、读者总等候时间、读者总周转时间、写者总等候时间、写者总周转时间、平均等候时间、平均周转时间;3. 使用合适可视化方法(比如甘特图)对计算结果进行可视化;4. 比较三种
2、算法性能。日程:此次设计共一周时间,参考日程安排以下:第1天:读者优先算法设计、实现和测试;第2天:写者优先算法设计、实现和测试;第3天:无优先算法设计、实现和测试;第4天:测试结果可视化及算法性能比较;第5天:答辩。汇报:仅写自己完成设计结果,包含:1 充足注释源代码(未写代码者略去)2 测试结果3 结果可视化4 性能比较5 参考文件6 心得指导老师:150408 焦素云 150409 王俊华目录第1章 设计要求12.1设计目标12.2设计要求1第2章 测试数据设计2第3章 算法实现3第4章 算法结果3第5章 结果可视化4第6章 性能分析4参考文件4心得4第1章 设计要求2.1设计目标了解临
3、界区和进程互斥概念,掌握用信号量和PV操作实现进程互斥方法。2.2设计要求在linux环境下编写应用程序,该程序运行时能创建N个线程,其中现有读者线程又有写者线程,它们根据事先设计好测试数据进行读写操作。读者/写者问题描述以下:有一个被很多进程共享数据区,这个数据区能够是一个文件,或主存一块空间,甚至能够是一组处理器寄存器。有部分只读取这个数据区线程(reader)和部分只往数据区中写数据线程(writer)。以下假设共享数据区是文件。这些读者和写者对数据区操作必需满足以下条件:读读许可;读写互斥;写写互斥。这些条件具体来说就是:(1)任意多读线程能够同时读这个文件;(2)一次只许可一个写线程
4、往文件中写;(3)假如一个写线程正在往文件中写,严禁任何读线程或写线程访问文件;(4)写线程实施写操作前,应让已经有写者或读者全部退出。这说明当有读者在读文件时不许可写者写文件。对于读者-写者问题,有三种处理方法:1、读者优先除了上述四个规则外,还增加读者优先要求,当有读者在读文件时,对随即抵达读者和写者,要首先满足读者,阻塞写者。这说明只要有一个读者活跃,那么随即而来读者全部将被许可访问文件,从而造成写者长时间等候,甚至有可能出现写者被饿死情况。2、写者优先除了上述四个规则外,还增加写者优先要求,即当有读者和写者同时等候时,首先满足写者。当一个写者申明想写文件时,不许可新读者再访问文件。3、
5、无优先除了上述四个规则外,不再要求读写优先权,谁先等候谁就先使用文件。第2章 测试数据设计表1 测试数据线程名称申请时刻连续使用时间r1015r2115w133r342w256w3610r478r592w41018w5122为了验证算法正确性,需要设计测试数据,并对测试数据进行分析,总结出在该组测试数据下,程序应该得到什么结果,然后运行程序,将程序运行结果和分析结果相比较,假如二者一致,则可认为程序是正确。作者设计测试数据如表1所表示,包含10个线程,其中有5个读者线程r1r5,另外5个是写者线程w1w5。读者线程r1在时刻0提出读请求,假如请求得到许可,r1将用15秒时间读文件;写者线程w3
6、在时刻6提出写请求,假如请求得到许可,w3将用10秒时间写文件。从表中能够看出,10个线程提出请求次序是:r1,r2,w1,r3,w2,w3,r4,r5,w4,w5。(1)读者优先算法线程实际读写文件次序为:r1,r2,r3,r4,r5,w1,w2,w3,w4,w5。实施情况见表2。表2 线程运行情况线程名称申请时刻连续时间开始操作时刻结束操作时刻r1015015r2115116r34246r478715r592911w1331619w2561925w36102535w410183553w51225355(2)写者优先算法线程实际读写文件次序为:r1,r2,w1,w2,w3,w4,w5,r3,
7、r4,r5。实施情况见表3。表3 线程运行情况线程名称申请时刻连续时间开始操作时刻结束操作时刻r1015015r2115116w1331619w2561925w36102535w410183553w51225355r3425557r4785563r5925557(3)无优先算法线程实际读写文件次序为:r1,r2,w1,r3,w2,w3,r4,r5,w4,w5。实施情况见表4。表4 线程运行情况线程名称申请时刻连续时间开始操作时刻结束操作时刻r1015015r2115116w1331619r3421921w2562127w36102737r4783745r5923739w410184563w51
8、226365第3章 算法实现读者优先算法:#include #include #include #include #include #include #include #define MAX_THREAD 10#define SNL 8typedef structchar tn3; /name of threadunsigned int rm; /the moment when this thread request to access data.unsigned int pt; /duration of operationunsigned int i; /same to rmunsigned
9、 int b; /instance at which this thread begin to operate dataunsigned int e; /ending instanceTEST_INFO;/TEST_INFO test_dataMAX_THREAD;typedef struct char snSNL+1; /student numberTEST_INFO tiMAX_THREAD; /test itemTI; /test_item_for_student/TI test_itemsSTUDENTS;TI test_item=2566,w1, 4,7,r1,11,14,r2,1,
10、5,w2,8,11,w3,15,2,r3,5,8,r4,12,15,w4,5,2,r5,9,12,r6,15,3;char r_seqMAX_THREAD3;char o_seqMAX_THREAD3;int sr=0;int so=0;int rc=0; /count how many readers are readingpthread_mutex_t cs_d; /guarentee mutually access datapthread_mutex_t cs_rc; /guarentee mutually access rcpthread_mutex_t cs_sr; /guarent
11、ee mutually access srpthread_mutex_t cs_so; /guarentee mutually access srtime_t base; /the moment when function main begin/*void print_answer()int i;printf(%sn,test_item.sn);printf(name r_m p_t i_t b_t e_tn);for(i=0;iMAX_THREAD;i+)printf(%4s%4d%4d%8d%8d%8dn,(test_item.ti)i.tn,(test_item.ti)i.rm,(tes
12、t_item).tii.pt,(test_item.ti)i.i,(test_item.ti)i.b,(test_item.ti)i.e);printf(r_seq:);for(i=0;iMAX_THREAD;i+)printf(%4s,r_seqi);printf(n);printf(o_seq:);for(i=0;iMAX_THREAD;i+)printf(%4s,o_seqi);printf(n);*/void save_answer(FILE *f)int i;fprintf(f,t%s_answer.txtntr/w problem:read firstnn,test_item.sn
13、);fprintf(f,name r_m p_t i_t b_t e_tn);for(i=0;iMAX_THREAD;i+)fprintf(f,%4s%4d%4d%8d%8d%8dn,(test_item.ti)i.tn,(test_item.ti)i.rm,(test_item).tii.pt,(test_item.ti)i.i,(test_item.ti)i.b,(test_item.ti)i.e);fprintf(f,n);fprintf(f,r_seq:);for(i=0;iMAX_THREAD;i+)fprintf(f,%4s,r_seqi);fprintf(f,n);fprintf
14、(f,o_seq:);for(i=0;irm); gettimeofday(&t,NULL); (TEST_INFO *)td)-i=difftime(t.tv_sec,rl); pthread_mutex_lock(&cs_sr); strcpy(r_seqsr+,(TEST_INFO *)td)-tn); pthread_mutex_unlock(&cs_sr); pthread_mutex_lock(&cs_rc); rc+; if(rc=1)pthread_mutex_lock(&cs_d); pthread_mutex_unlock(&cs_rc); gettimeofday(&t,
15、NULL); (TEST_INFO *)td)-b=difftime(t.tv_sec,rl); pthread_mutex_lock(&cs_so); strcpy(o_seqso+,(TEST_INFO *)td)-tn); pthread_mutex_unlock(&cs_so); sleep(TEST_INFO *)td)-pt); gettimeofday(&t,NULL); (TEST_INFO *)td)-e=difftime(t.tv_sec,rl); pthread_mutex_lock(&cs_rc); rc-; if(rc=0)pthread_mutex_unlock(&
16、cs_d); pthread_mutex_unlock(&cs_rc); return 0;void *w(void *td) struct timeval t; time_t wl=base; sleep(TEST_INFO *)td)-rm); gettimeofday(&t,NULL); (TEST_INFO *)td)-i=difftime(t.tv_sec,wl); pthread_mutex_lock(&cs_sr); strcpy(r_seqsr+,(TEST_INFO *)td)-tn); pthread_mutex_unlock(&cs_sr); pthread_mutex_
17、lock(&cs_d); gettimeofday(&t,NULL); (TEST_INFO *)td)-b=difftime(t.tv_sec,wl); pthread_mutex_lock(&cs_so); strcpy(o_seqso+,(TEST_INFO *)td)-tn); pthread_mutex_unlock(&cs_so); sleep(TEST_INFO *)td)-pt); gettimeofday(&t,NULL); (TEST_INFO *)td)-e=difftime(t.tv_sec,wl); pthread_mutex_unlock(&cs_d); retur
18、n 0;void create_exam()int i=0;pthread_t htMAX_THREAD;pthread_mutex_init(&cs_d,NULL);pthread_mutex_init(&cs_rc,NULL);pthread_mutex_init(&cs_sr,NULL);pthread_mutex_init(&cs_so,NULL);struct timeval t;gettimeofday(&t,NULL);base=t.tv_sec;for(i=0;iMAX_THREAD;i+)if(test_item.ti)i.tn0=r)pthread_create(&hti,
19、NULL,r,&(test_item.ti)i);else if(test_item.ti)i.tn0=w)pthread_create(&hti,NULL,w,&(test_item.ti)i);for(i=0;iMAX_THREAD;i+)pthread_join(hti,NULL);pthread_mutex_destroy(&cs_d);pthread_mutex_destroy(&cs_rc);pthread_mutex_destroy(&cs_sr);pthread_mutex_destroy(&cs_so);int main(int argc,char *argv)int i=0
20、;int si,pos;int fd;FILE *fa;char file_name100;create_exam();sprintf(file_name,%s_answer.txt,test_item.sn);if(fa=fopen(file_name,w)=NULL)printf(Error openning answer file:%sn,file_name);exit(3);save_answer(fa);exit(0);1. 写优先算法#include #include #include #include #include #include #include #define MAX_
21、THREAD 10#define SNL 8typedef structchar tn3; /name of threadunsigned int rm; /the moment when this thread request to access data.unsigned int pt; /duration of operationunsigned int i; /same to rmunsigned int b; /instance at which this thread begin to operate dataunsigned int e; /ending instanceTEST
22、_INFO;/TEST_INFO test_dataMAX_THREAD;typedef struct char sn100; /student numberTEST_INFO tiMAX_THREAD; /test itemTI; /test_item_for_student/TI test_itemsSTUDENTS;TI test_item=2566,w1, 4,7,r1,11,14,r2,1,5,w2,8,11,w3,15,2,r3,5,8,r4,12,15,w4,5,2,r5,9,12,r6,15,3;char r_seqMAX_THREAD3;char o_seqMAX_THREA
23、D3;int sr=0;int so=0;int rc=0; /count how many readers are readingpthread_mutex_t cs_d; /guarentee mutually access datapthread_mutex_t cs_rc; /guarentee mutually access rcpthread_mutex_t cs_sr; /guarentee mutually access srpthread_mutex_t cs_so; /guarentee mutually access srtime_t base; /the moment
24、when function main begin/*void print_answer()int i;printf(%sn,test_item.sn);printf(name r_m p_t i_t b_t e_tn);for(i=0;iMAX_THREAD;i+)printf(%4s%4d%4d%8d%8d%8dn,(test_item.ti)i.tn,(test_item.ti)i.rm,(test_item).tii.pt,(test_item.ti)i.i,(test_item.ti)i.b,(test_item.ti)i.e);printf(r_seq:);for(i=0;iMAX_
25、THREAD;i+)printf(%4s,r_seqi);printf(n);printf(o_seq:);for(i=0;iMAX_THREAD;i+)printf(%4s,o_seqi);printf(n);*/void save_answer(FILE *f) /save the result int i;fprintf(f,t%s_answer.txtntr/w problem:read firstnn,test_item.sn);fprintf(f,name r_m p_t i_t b_t e_tn);for(i=0;iMAX_THREAD;i+)fprintf(f,%4s%4d%4
26、d%8d%8d%8dn,(test_item.ti)i.tn,(test_item.ti)i.rm,(test_item).tii.pt,(test_item.ti)i.i,(test_item.ti)i.b,(test_item.ti)i.e);fprintf(f,n);fprintf(f,r_seq:);for(i=0;iMAX_THREAD;i+)fprintf(f,%4s,r_seqi);fprintf(f,n);fprintf(f,o_seq:);for(i=0;irm);/休眠读线程进入时间长 gettimeofday(&t,NULL);/取得目前时间 (TEST_INFO *)t
27、d)-i=difftime(t.tv_sec,rl);/将系统时间和main主函数实施时间差赋值给i变量(读线程进入时间) pthread_mutex_lock(&cs_sr);/建立互斥锁 strcpy(r_seqsr+,(TEST_INFO *)td)-tn); pthread_mutex_unlock(&cs_sr);/解除互斥锁 pthread_mutex_lock(&cs_rc); rc+; if(rc=1)pthread_mutex_lock(&cs_d); pthread_mutex_unlock(&cs_rc); gettimeofday(&t,NULL);/取得目前时间 (T
28、EST_INFO *)td)-b=difftime(t.tv_sec,rl);/将系统时间和main主函数实施时间差赋值给b变量(读线程开始时间) pthread_mutex_lock(&cs_so); strcpy(o_seqso+,(TEST_INFO *)td)-tn);/将读线程名复制给0_seq队列 pthread_mutex_unlock(&cs_so); sleep(TEST_INFO *)td)-pt);/休眠写进程 连续时间长 gettimeofday(&t,NULL);/取得目前时间 (TEST_INFO *)td)-e=difftime(t.tv_sec,rl);/ pt
29、hread_mutex_lock(&cs_rc); rc-; if(rc=0)pthread_mutex_unlock(&cs_d); pthread_mutex_unlock(&cs_rc); return 0;void *r(void *td) struct timeval t; time_t wl=base; sleep(TEST_INFO *)td)-rm);/休眠写进程进入时间长 gettimeofday(&t,NULL); (TEST_INFO *)td)-i=difftime(t.tv_sec,wl);/将系统时间和main主函数实施时间差赋值给i变量(写线程进入时间) pthr
30、ead_mutex_lock(&cs_sr);/建立互斥锁 strcpy(r_seqsr+,(TEST_INFO *)td)-tn);/ 将写线程名复制给r_seq队列 pthread_mutex_unlock(&cs_sr);/解除互斥锁 pthread_mutex_lock(&cs_d); gettimeofday(&t,NULL);/取得目前时间 (TEST_INFO *)td)-b=difftime(t.tv_sec,wl);/将系统时间和main主函数实施时间差赋值给b变量(写线程开始时间) pthread_mutex_lock(&cs_so); strcpy(o_seqso+,(T
31、EST_INFO *)td)-tn);/ 将写线程名复制给o_seq队列 pthread_mutex_unlock(&cs_so); sleep(TEST_INFO *)td)-pt);/休眠写进程连续时间长 gettimeofday(&t,NULL);/获取目前时间 (TEST_INFO *)td)-e=difftime(t.tv_sec,wl);/将系统时间和main主函数实施时间差赋值给e变量(写线程结束时间) pthread_mutex_unlock(&cs_d); return 0;void create_exam() int i=0;pthread_t htMAX_THREAD;p
32、thread_mutex_init(&cs_d,NULL);/初始化互斥锁 pthread_mutex_init(&cs_rc,NULL);pthread_mutex_init(&cs_sr,NULL);pthread_mutex_init(&cs_so,NULL);struct timeval t;gettimeofday(&t,NULL);base=t.tv_sec;for(i=0;iMAX_THREAD;i+)/分别创建读写线程初始化 if(test_item.ti)i.tn0=r)pthread_create(&hti,NULL,r,&(test_item.ti)i);else if(
33、test_item.ti)i.tn0=w)pthread_create(&hti,NULL,w,&(test_item.ti)i);for(i=0;iMAX_THREAD;i+)pthread_join(hti,NULL);pthread_mutex_destroy(&cs_d);/销毁互斥锁 pthread_mutex_destroy(&cs_rc);pthread_mutex_destroy(&cs_sr);pthread_mutex_destroy(&cs_so);int main(int argc,char *argv)int i=0;int si,pos;int fd;FILE *f
34、a;char file_name100;create_exam();sprintf(file_name,%s_answer.txt,test_item.sn);/从文件中读取 if(fa=fopen(file_name,w)=NULL)printf(Error openning answer file:%sn,file_name);exit(3);save_answer(fa);exit(0);2. 无优先算法#include #include #include #include #include #include #include #define MAX_THREAD 10#define S
35、NL 8typedef structchar tn3; /name of threadunsigned int rm; /the moment when this thread request to access data.unsigned int pt; /duration of operationunsigned int i; /same to rmunsigned int b; /instance at which this thread begin to operate dataunsigned int e; /ending instanceTEST_INFO;/TEST_INFO t
36、est_dataMAX_THREAD;typedef struct char sn100; /student numberTEST_INFO tiMAX_THREAD; /test itemTI; /test_item_for_student/TI test_itemsSTUDENTS;TI test_item=2566,w1, 4,7,r1,11,14,r2,1,5,w2,8,11,w3,15,2,r3,5,8,r4,12,15,w4,5,2,r5,9,12,r6,15,3;char r_seqMAX_THREAD3;char o_seqMAX_THREAD3;int sr=0;int so
37、=0;int rc=0; /count how many readers are readingpthread_mutex_t cs_d; /guarentee mutually access datapthread_mutex_t cs_rc; /guarentee mutually access rcpthread_mutex_t cs_sr; /guarentee mutually access srpthread_mutex_t cs_so; /guarentee mutually access srtime_t base; /the moment when function main begin/*void print_answer()int i;printf(%sn,test_item.sn);printf(name r_m p_t i_t b_t e_tn);for(i=0;iMAX_THREAD;i+)printf(%4s%4d%4d%8d%8d%8dn,(test_item.ti)i.tn,(test_item.ti)i.rm,(test_item).tii.pt,(test_item.ti)i.i,(test_item.ti)i.b,(test_item.ti)i.e);printf(r_seq:);for(i=0;iMAX_THREAD;i+)printf(