dw怎么做自我展示網(wǎng)站類(lèi)似凡科建站的平臺(tái)
1. 說(shuō)明文檔中原有程序?qū)崿F(xiàn)的功能、實(shí)現(xiàn)方法。(用語(yǔ)言、程序流程圖、為原有程序添加注釋等方式均可)
1.//const.h
2.//定義宏變量
3.#ifndef CONST_H
4.#define CONST_H
5.
6.#define TRUE 1
7.#define FALSE 0
8.#define ERROR 0
9.#define OVERFLOW -2
10.#define OK 1
11.#define MAXSIZE 20//隊(duì)列中最多有20個(gè)元素
12.#define MAXLEN 100//字符串最大長(zhǎng)度
13.
#endif
1.//Typedefine.h
2.//定義結(jié)構(gòu)體
3.#ifndef TYPEDEFINE_H
4.#define TYPEDEFINE_H
5.
6.#include "const.h"
7.//字符串結(jié)構(gòu)體
8.typedef struct{
9. char data[MAXLEN];//數(shù)據(jù)
10. int len;//數(shù)據(jù)長(zhǎng)度,不算字符串結(jié)束字符”\0”
11.}SString;
12.//元素類(lèi)型結(jié)構(gòu)體
13.typedef struct{
14. SString* strName;//數(shù)據(jù)
15. long INumber;//號(hào)碼
16.}Elemtype;
17.//結(jié)點(diǎn)結(jié)構(gòu)體
18.typedef struct node{
19. Elemtype data;//結(jié)點(diǎn)的數(shù)據(jù)域
20. struct node *next;//結(jié)點(diǎn)的指針域,指向下一個(gè)結(jié)點(diǎn)地址
21.}SNode;
22.//隊(duì)列結(jié)構(gòu)體
23.typedef struct{
24. Elemtype q[MAXSIZE];//存放隊(duì)列元素
25. int front;//隊(duì)列頭指針
26. int rear;//隊(duì)列尾指針,存放尾元素的下一個(gè)位置
27.}Queue;
28.//棧結(jié)構(gòu)體
29.typedef struct st{
30. Elemtype data[MAXSIZE];//存放棧元素
31. int top;//棧頂指針
32.}Stack;
33.
34.typedef int BOOL;
35.typedef int Status;
36.
#endif
1.//Queue.h
2.//定義隊(duì)列的相關(guān)函數(shù)
3.#ifndef QUEUE_H_LY
4.#define QUEUE_H_LY
5.
6.#include "Typedefine.h"
7.#include <stdbool.h>
8.
9.Queue *InitQueue();//初始化隊(duì)列函數(shù)
10.int EnQueue(Queue *Q, Elemtype x);//入隊(duì)函數(shù)
11.int DeQueue(Queue *Q, Elemtype *x);//出隊(duì)函數(shù)
12.int QueueEmpty(Queue Q);//判斷隊(duì)列是否為空,非空返回0,空返回1
13.int QueueCount(Queue *HQ);//統(tǒng)計(jì)隊(duì)列元素個(gè)數(shù)
14.bool QueueFull(Queue Q);//判斷隊(duì)列是否滿,滿返回 TRUE,非滿返回 FALSE
15.
#endif
1.//Queue.c
2.//隊(duì)列的相關(guān)函數(shù)實(shí)現(xiàn)代碼
3.#include "stdio.h"
4.#include "malloc.h"
5.#include "const.h"
6.#include "Queue.h"
7.#include <stdbool.h>
8.//隊(duì)列初始化函數(shù)
9.Queue *InitQueue(){
10. Queue *Q = (Queue*)malloc(sizeof(Queue));//申請(qǐng)空間
11. Q->front = Q->rear = 0;//頭尾指針相等時(shí)說(shuō)明隊(duì)列為空
12. return Q;//返回隊(duì)列指針
13.}
14.//入隊(duì)函數(shù)
15.int EnQueue(Queue *Q, Elemtype x){
16. if((Q->rear + 1) % MAXSIZE == Q->front) return FALSE;//隊(duì)滿,入隊(duì)失敗,返回0
17. else{
18. Q->q[Q->rear] = x;//存放元素至隊(duì)尾
19. Q->rear = (Q->rear + 1) % MAXSIZE;//隊(duì)列尾指針指向下一位
20. return TRUE;
21. }
22.}
23.//出隊(duì)函數(shù)
24.int DeQueue(Queue *Q, Elemtype *x){
25. if(Q->rear == Q->front) return FALSE;//隊(duì)空,出隊(duì)失敗,返回0
26. else{
27. *x = Q->q[Q->front];//從隊(duì)頭取出元素
28. Q->front = (Q->front + 1) % MAXSIZE;//隊(duì)頭指針指向下一位
29. return TRUE;
30. }
31.}
32.//判斷隊(duì)是否為空,非空返回0,空返回1
33.int QueueEmpty(Queue Q){
34. if(Q.rear == Q.front) return TRUE;//隊(duì)空
35. else return FALSE;
36.}
37.//返回隊(duì)列中的最后的一個(gè)元素
38.Elemtype Last(Queue *Q){
39. Elemtype *prElem = NULL;
40. Queue *prTempQueue;
41. prTempQueue = InitQueue();//初始化一個(gè)新的臨時(shí)隊(duì)列,用于暫存數(shù)據(jù)
42. while(QueueEmpty(*Q) == 1){//將Q中的元素依次取出放至prTempQueue中
43. DeQueue(Q, prElem);
44. EnQueue(prTempQueue, *prElem);
45. }
46. while(QueueEmpty(*prTempQueue) == 1){//將prTempQueue中的元素依次取出放至Q中
47. DeQueue(prTempQueue, prElem);
48. EnQueue(Q, *prElem);
49. }
50. return *prElem;//返回隊(duì)列中的最后的一個(gè)元素
51.}
52.//判斷隊(duì)是否為滿,滿返回1,非滿返回0
53.bool QueueFull(Queue Q){
54. if(((Q.rear + 1) % MAXSIZE) == Q.front) return TRUE;//隊(duì)滿
55. else return FALSE;
}
1.//exp1.c
2.//主程序,實(shí)現(xiàn)三個(gè)生產(chǎn)者與兩個(gè)消費(fèi)者問(wèn)題
3.//在30s內(nèi),生產(chǎn)者把產(chǎn)品放入緩沖區(qū),消費(fèi)者從緩沖區(qū)中拿走產(chǎn)品,緩沖區(qū)空間定義為5,因?yàn)殛?duì)列中元素最大值設(shè)為了20,所以無(wú)需考慮隊(duì)列滿情況。生產(chǎn)者在緩沖區(qū)滿時(shí)必須等待,直到緩沖區(qū)有空間才繼續(xù)生產(chǎn);消費(fèi)者在緩沖區(qū)空時(shí)必須等待,直到緩沖區(qū)中有產(chǎn)品才能繼續(xù)讀取。共有三個(gè)生產(chǎn)者與兩個(gè)消費(fèi)者線程,并將生產(chǎn)的商品放入隊(duì)列中(商品編號(hào)從1000開(kāi)始,最多放五個(gè)),根據(jù)先生產(chǎn)先消費(fèi)的原則進(jìn)行消費(fèi)。
4.#include <stdio.h>
5.#include <stdlib.h>
6.#include <unistd.h>
7.#include <pthread.h>
8.#include <errno.h>
9.#include <sys/ipc.h>
10.#include <semaphore.h>
11.#include <fcntl.h>
12.#include "Queue.h"
13.#include "const.h"
14.#include "Queue.c"
15.
16.#define N 5//定義可用資源數(shù)量
17.
18.time_t end_time;
19.sem_t mutex, full, empty;
20.int fd;
21.Queue *qt;
22.Elemtype p;
23.void consumer(void *arg);
24.void productor(void *arg);
25.
26.int main(){
27. pthread_t id1, id2, id3, id4, id5;//創(chuàng)建5個(gè)線程,分別對(duì)應(yīng)三個(gè)生產(chǎn)者與兩個(gè)消費(fèi)者
28. pthread_t mon_th_id;
29. int ret;
30. end_time = time(NULL) + 30;//程序執(zhí)行30s停止
31. qt = InitQueue();//初始化隊(duì)列qt
32. p.INumber = 1000;//號(hào)碼從1000開(kāi)始
33. ret = sem_init(&mutex, 0, 1);//初使化互斥信號(hào)量mutex為1
34. ret = sem_init(&empty, 0, N);//初使化empty信號(hào)量為N
35.
36. ret = sem_init(&full, 0, 0);//初使化full信號(hào)量為0
37. if(ret != 0) perror("sem_init");
38.
39. ret = pthread_create(&id1, NULL, (void*)productor, NULL);//生產(chǎn)者線程1
40. if(ret != 0) perror("pthread cread1");
41.
42. ret = pthread_create(&id3, NULL, (void*)productor, NULL);//生產(chǎn)者線程3
43. if(ret != 0) perror("pthread cread3");
44.
45. ret = pthread_create(&id2, NULL, (void*)consumer, NULL);//消費(fèi)者線程2
46. if(ret != 0) perror("pthread cread2");
47.
48. ret = pthread_create(&id5, NULL, (void*)productor, NULL);//生產(chǎn)者線程5
49. if(ret != 0) perror("pthread cread5");
50.
51. ret = pthread_create(&id4, NULL, (void*)consumer, NULL);//消費(fèi)者線程4
52. if(ret != 0) perror("pthread cread4");
53.
54. pthread_join(id1, NULL);//等待生產(chǎn)者線程1結(jié)束
55. pthread_join(id2, NULL);//等待消費(fèi)者線程2結(jié)束
56. pthread_join(id3, NULL);//等待生產(chǎn)者線程3結(jié)束
57. pthread_join(id4, NULL);//等待消費(fèi)者線程4結(jié)束
58. pthread_join(id5, NULL);//等待生產(chǎn)者線程5結(jié)束
59.
60. exit(0);
61.}
62.//生產(chǎn)者線程函數(shù)
63.void productor(void *arg){
64. int i, nwrite;
65. while(time(NULL) < end_time){//在規(guī)定時(shí)間內(nèi)循環(huán)生產(chǎn)商品
66. sem_wait(&empty);//empty信號(hào)量P操作
67. sem_wait(&mutex);//互斥信號(hào)量P操作
68. if(TRUE == QueueFull(*qt)){//隊(duì)滿不操作
69. printf("Procuctor: buffer is full, please try to write later.\n");
70. }
71. else{//隊(duì)不滿
72. EnQueue(qt, p);//入隊(duì)
73. printf("Productor: write [%d] to buffer\n",p.INumber);
74. p.INumber++;//編號(hào)加一
75. }
76. sem_post(&full);//full信號(hào)量V操作
77. sem_post(&mutex);//mutex信號(hào)量V操作
78. sleep(1);
79. }
80.}
81.
82.void consumer(void *arg){
83. int nolock = 0;
84. int ret, nread;
85. Elemtype p2;
86. while((time(NULL) < end_time) || (FALSE == (QueueEmpty(*qt)))){//在規(guī)定時(shí)間內(nèi)或隊(duì)列非空時(shí),循環(huán)消費(fèi)商品
87. sem_wait(&full);//full信號(hào)量P操作
88. sem_wait(&mutex);//互斥信號(hào)量P操作
89. if(TRUE == QueueEmpty(*qt)){//隊(duì)列空,不能消費(fèi)
90. printf("Consumer: the buffer is empty, please try to read later.\n");
91. }
92. else{//隊(duì)列非空
93. DeQueue(qt, &p2);//出隊(duì)
94. printf("Consumer: read [%d] from buffer.\n", p2.INumber);
95. }
96. sem_post(&empty);//empty信號(hào)量V操作
97. sem_post(&mutex);//互斥信號(hào)量V操作
98. sleep(2);
99. }
}
2. 列出可改進(jìn)的功能、實(shí)現(xiàn)方法等
可改進(jìn)的功能:
- 將代碼整理,去除掉了冗余代碼。
- 顯示緩沖區(qū)資源個(gè)數(shù):創(chuàng)建全局變量n,統(tǒng)計(jì)生產(chǎn)者/消費(fèi)者 生產(chǎn)/消費(fèi)后緩沖區(qū)中剩余的資源個(gè)數(shù),并予以顯示。
- 提示生產(chǎn)者/消費(fèi)者線程結(jié)束語(yǔ)句,如:Productor 1 is killed。
- 利用gettid()函數(shù)獲得生產(chǎn)者、消費(fèi)者的線程id,并顯示是哪一個(gè)生產(chǎn)者/消費(fèi)者 生產(chǎn)/消費(fèi) 了哪一個(gè)商品。
- 編寫(xiě)了display()函數(shù),根據(jù)生產(chǎn)者/消費(fèi)者的動(dòng)作,可視化了緩沖區(qū)中的商品狀況,使商品變化情況更加簡(jiǎn)單直觀。
- 利用for循環(huán)創(chuàng)建線程,實(shí)現(xiàn)可以自主輸入想要?jiǎng)?chuàng)建的生產(chǎn)者與消費(fèi)者個(gè)數(shù)(數(shù)值在1到10之間)、緩沖區(qū)大小(數(shù)值在1到20之間)與產(chǎn)品編號(hào)(數(shù)值在1到1000之間)。
- 編寫(xiě)了print()函數(shù),優(yōu)化了終端可視化界面,可在界面中選擇功能并自定義賦值,使程序運(yùn)行更靈活直觀。
實(shí)現(xiàn)方法:
- 首先打印出終端可視化界面,讓用戶可以自主選擇功能,可以進(jìn)行自定義賦值、運(yùn)行程序與退出系統(tǒng)操作,未進(jìn)行自定義賦值的變量使用默認(rèn)值。
- 自定義賦值后運(yùn)行程序,根據(jù)用戶的賦值給變量賦值,并創(chuàng)建生產(chǎn)者、消費(fèi)者線程,運(yùn)行生產(chǎn)者、消費(fèi)者函數(shù)。
- 在生產(chǎn)者、消費(fèi)者函數(shù)中,根據(jù)隊(duì)列情況進(jìn)行P、V操作與生產(chǎn)/消費(fèi)操作,并利用gettid()函數(shù)獲得生產(chǎn)者的線程id,顯示是哪一個(gè)生產(chǎn)者/消費(fèi)者 生產(chǎn)/消費(fèi)了哪一件商品,并可視化生產(chǎn)/消費(fèi)操作前后緩沖區(qū)中的產(chǎn)品變化情況與緩沖區(qū)中剩余的資源個(gè)數(shù)。
- 運(yùn)行規(guī)定時(shí)間后生產(chǎn)者/消費(fèi)者線程結(jié)束,退出系統(tǒng)。
3. 詳細(xì)說(shuō)明已完成的改進(jìn),附上程序代碼,改進(jìn)處加注釋(注意代碼格式)
1.//const.h
2.
3.#ifndef CONST_H
4.#define CONST_H
5.#define TRUE 1
6.#define FALSE 0
7.#define MAXSIZE 20
8.#define MAXLEN 100
#endif
1.//Typedefine.h
2.
3.#ifndef TYPEDEFINE_H
4.#define TYPEDEFINE_H
5.#include "const.h"
6.
7.typedef struct{
8. char data[MAXLEN];
9. int len;
10.}SString;
11.
12.typedef struct{
13. SString* strName;
14. long INumber;
15.}Elemtype;
16.
17.typedef struct{
18. Elemtype q[MAXSIZE];
19. int front;
20. int rear;
21.}Queue;
22.
#endif
1.//Queue.h
2.
3.#ifndef QUEUE_H_LY
4.#define QUEUE_H_LY
5.
6.#include "Typedefine.h"
7.#include <stdbool.h>
8.
9.Queue *InitQueue();
10.int EnQueue(Queue *Q, Elemtype x);
11.int DeQueue(Queue *Q, Elemtype *x);
12.int QueueEmpty(Queue Q);
13.bool QueueFull(Queue Q);
14.
#endif
1.//Queue.c
2.
3.#include "stdio.h"
4.#include "malloc.h"
5.#include "const.h"
6.#include "Queue.h"
7.#include <stdbool.h>
8.
9.Queue *InitQueue(){
10. Queue *Q = (Queue*)malloc(sizeof(Queue));
11. Q->front = Q->rear = 0;
12. return Q;
13.}
14.
15.int EnQueue(Queue *Q, Elemtype x){
16. if((Q->rear + 1) % MAXSIZE == Q->front) return FALSE;
17. else{
18. Q->q[Q->rear] = x;
19. Q->rear = (Q->rear + 1) % MAXSIZE;
20. return TRUE;
21. }
22.}
23.
24.int DeQueue(Queue *Q, Elemtype *x){
25. if(Q->rear == Q->front) return FALSE;
26. else{
27. *x = Q->q[Q->front];
28. Q->front = (Q->front + 1) % MAXSIZE;
29. return TRUE;
30. }
31.}
32.
33.int QueueEmpty(Queue Q){
34. if(Q.rear == Q.front) return TRUE;
35. else return FALSE;
36.}
37.
38.bool QueueFull(Queue Q){
39. if(((Q.rear + 1) % MAXSIZE) == Q.front) return TRUE;
40. else return FALSE;
}
1.//exp1.c
2.
3.#include <stdio.h>
4.#include <stdlib.h>
5.#include <unistd.h>
6.#include <pthread.h>
7.#include <errno.h>
8.#include <sys/ipc.h>
9.#include <semaphore.h>
10.#include <fcntl.h>
11.#include "Queue.h"
12.#include "const.h"
13.#include "Queue.c"
14.#include <sys/syscall.h>//使用gettid()函數(shù)所需要的頭文件
15.#define gettid() syscall(__NR_gettid)//使用gettid()函數(shù)所需要的頭文件
16.#include <string.h>
17.
18.time_t end_time;
19.sem_t mutex, full, empty;
20.Queue *qt;
21.Elemtype p;
22.int n = 0;//緩沖區(qū)中資源個(gè)數(shù)
23.int x = 2, y = 2;//生產(chǎn)者與消費(fèi)者個(gè)數(shù)(默認(rèn)值)
24.int ret;
25.int N = 5;//緩沖區(qū)最大資源個(gè)數(shù)(默認(rèn)值)
26.
27.void consumer(void *arg);
28.void productor(void *arg);
29.void display();//可視化緩沖區(qū)中商品狀況
30.void print();//終端可視化界面
31.
32.int main(){
33. p.INumber = 1;//產(chǎn)品初始編號(hào)(默認(rèn)值)
34. while(1){
35. print();//展示系統(tǒng)可視化選項(xiàng)
36. //接收用戶需要的功能號(hào)
37. char number[100];
38. ret = scanf("%s", &number[0]);
39. if(strlen(number)!=1 || ret != 1 || number[0] < '0' || number[0] > '5'){
40. printf("\nplease input a number between 0 and 5!\n");
41. continue;
42. }
43. int num = number[0] - '0';
44. //輸入想要?jiǎng)?chuàng)建的生產(chǎn)者個(gè)數(shù),并確保數(shù)值在1到10之間
45. if(num == 1){
46. do{
47. printf("Please input the number of productor: ");
48. ret = scanf("%d",&x);
49. if(ret != 1 || x < 1 || x > 10){
50. printf("Please ensure the input between 1 and 10\n");
51. }
52. }while(ret != 1 || x < 1 || x > 10);
53. }
54. //輸入想要?jiǎng)?chuàng)建的消費(fèi)者個(gè)數(shù),并確保數(shù)值在1到10之間
55. else if(num == 2){
56. do{
57. printf("Please input the number of consumer: ");
58. ret = scanf("%d",&y);
59. if(ret != 1 || y < 1 || y > 10){
60. printf("Please ensure the input between 1 and 10\n");
61. }
62. }while(ret != 1 || y < 1 || y > 10);
63. }
64. //輸入想要?jiǎng)?chuàng)建的緩沖區(qū)大小,并確保數(shù)值在1到20之間
65. else if(num == 3){
66. do{
67. printf("Please input the size of buffer: ");
68. ret = scanf("%d",&N);
69. if(ret != 1 || N < 1 || N > 20){
70. printf("Please ensure the input between 1 and 20\n");
71. }
72. }while(ret != 1 || N < 1 || N > 20);
73. }
74. //輸入想要生產(chǎn)的產(chǎn)品的初始編號(hào),并確保數(shù)值在1到1000之間
75. else if(num == 4){
76. do{
77. printf("Please input the key of product: ");
78. ret = scanf("%d",&p.INumber);
79. if(ret != 1 || p.INumber < 1 || p.INumber > 1000){
80. printf("Please ensure the input between 1 and 1000\n");
81. }
82. }while(ret != 1 || p.INumber < 1 || p.INumber > 1000);
83. }
84. //運(yùn)行程序
85. else if(num == 5){
86. pthread_t id;
87. end_time = time(NULL) + 10;
88. qt = InitQueue();
89. //p.INumber = 1000;
90. ret = sem_init(&mutex, 0, 1);
91. ret = sem_init(&empty, 0, N);
92.
93. ret = sem_init(&full, 0, 0);
94. if(ret != 0) perror("sem_init");
95. //創(chuàng)建線程
96. for(int i = 0; i < x; i++){
97. ret = pthread_create(&id, NULL, (void*)productor, NULL);
98. if(ret != 0) perror("Productor pthread create");
99. }
100.
101. for(int i = x; i < x + y; i++){
102. ret = pthread_create(&id, NULL, (void*)consumer, NULL);
103. if(ret != 0) perror("Consumer pthread create");
104. }
105. //等待線程結(jié)束
106. for(int i = 0; i < x; i++){
107. pthread_join(id, NULL);
108. printf("Productor %d is killed\n", i);//提示程序結(jié)束語(yǔ)句
109. }
110. for(int i = x; i < x + y; i++){
111. pthread_join(id, NULL);
112. printf("Consumer %d is killed\n", i);//提示程序結(jié)束語(yǔ)句
113. }
114. break;
115. }
116. //退出系統(tǒng)
117. else if(num == 0){
118. break;
119. }
120. }
121. return 0;
122.}
123.
124.void productor(void *arg){
125. while(time(NULL) < end_time){
126. sem_wait(&empty);
127. sem_wait(&mutex);
128. if(TRUE == QueueFull(*qt)){
129. printf("Productor pthread %u: buffer is full, please try to write later.\n", gettid());
130. }
131. else{
132. EnQueue(qt, p);
133. display();//可視化緩沖區(qū)
134. n++;//緩沖區(qū)資源數(shù)加一
135. printf("------->");
136. display();//可視化緩沖區(qū)
137. printf("\n");
138. printf("Productor pthread %u: write [%d] to buffer.\n", gettid(), p.INumber);//利用gettid()函數(shù)獲得生產(chǎn)者的線程id,并顯示是哪一個(gè)生產(chǎn)者生產(chǎn)了哪一件商品
139. printf("There are %d left in buffer\n",n);//顯示緩沖區(qū)中剩余的資源個(gè)數(shù)
140. printf("--------------------------------------------------\n");//生產(chǎn)者/消費(fèi)者動(dòng)作分隔符
141. p.INumber++;
142. }
143. sem_post(&full);
144. sem_post(&mutex);
145. sleep(1);
146. }
147.}
148.
149.void consumer(void *arg){
150. Elemtype p2;
151. while((time(NULL) < end_time) || (FALSE == (QueueEmpty(*qt)))){
152. sem_wait(&full);
153. sem_wait(&mutex);
154. if(TRUE == QueueEmpty(*qt)){
155. printf("Consumer pthread %u: the buffer is empty, please try to read later.\n", gettid());
156. }
157. else{
158. DeQueue(qt, &p2);
159. display();//可視化緩沖區(qū)
160. n--;//緩沖區(qū)資源數(shù)減一
161. printf("------->");
162. display();//可視化緩沖區(qū)
163. printf("\n");
164. printf("Consumer pthread %u: read [%d] from buffer.\n", gettid(), p2.INumber);//利用gettid()函數(shù)獲得消費(fèi)者的線程id,并顯示是哪一個(gè)消費(fèi)者消費(fèi)了哪一件商品
165. printf("There are %d left in buffer\n",n);//顯示緩沖區(qū)中剩余的資源個(gè)數(shù)
166. printf("--------------------------------------------------\n");//生產(chǎn)者/消費(fèi)者動(dòng)作分隔符
167. }
168. sem_post(&empty);
169. sem_post(&mutex);
170. sleep(2);
171. }
172.}
173.//可視化緩沖區(qū)中的商品狀況
174.void display(){
175. printf("[");
176. for(int i = n; i > 0; i--){
177. printf(" P ");
178. }
179. printf("]");
180.}
181.//終端可視化界面
182.void print()
183.{
184. printf("\n");
185. printf("*********************************************************************************\n");
186. printf("--------------------[ Producer Consumer Program Function Bar ]-------------------\n");
187. printf("*********************************************************************************\n");
188. printf("* 1.Enter the number of producers you want to create (default value is 2) *\n");
189. printf("* 2.Enter the number of consumers you want to create (default value is 2) *\n");
190. printf("* 3.Enter the buffer size you want to create (default value is 5) *\n");
191. printf("* 4.Enter the initial product number you want to produce (default value is 1) *\n");
192. printf("* 5.run the program *\n");
193. printf("* 0.Exit the system *\n");
194. printf("---------------------------------------------------------------------------------\n");
195. printf("Please enter 0-5 to select a function: ");
}
4. 運(yùn)行結(jié)果截圖
(1)示例一,這次運(yùn)行我們想將生產(chǎn)者改為2個(gè),消費(fèi)者改為4個(gè),其余選項(xiàng)為默認(rèn)值,運(yùn)行程序。
中間略
(2)示例二,這次運(yùn)行我們想將緩沖區(qū)大小改為7,商品初始編號(hào)改為300,其余選項(xiàng)為默認(rèn)值,運(yùn)行程序。
中間略
5. 總結(jié)
實(shí)驗(yàn)中遇到的問(wèn)題與解決方法:
問(wèn)題1:想要改進(jìn)程序執(zhí)行語(yǔ)句,使程序運(yùn)行時(shí)能反映出具體是哪一位生產(chǎn)者/消費(fèi)者 生產(chǎn)/消費(fèi) 了哪一個(gè)商品。一開(kāi)始我嘗試在pthread_create()函數(shù)中給生產(chǎn)者/消費(fèi)者函數(shù)傳遞生產(chǎn)者/消費(fèi)者id變量,但是由于線程共享變量,無(wú)法在生產(chǎn)者-生產(chǎn)者、消費(fèi)者-消費(fèi)者之間作出區(qū)分。
解決方法:最終我決定使用gettid()函數(shù)獲得生產(chǎn)者、消費(fèi)者的線程id,作為每一位生產(chǎn)者/消費(fèi)者的標(biāo)志,來(lái)反映出具體是哪一位生產(chǎn)者/消費(fèi)者 生產(chǎn)/消費(fèi) 了商品。
問(wèn)題2:在程序運(yùn)行過(guò)程中,緩沖區(qū)中的產(chǎn)品變化并不能被很好的反映出來(lái)。
解決方法:我編寫(xiě)了display()函數(shù),分別在生產(chǎn)/消費(fèi)前后調(diào)用它,可視化了緩沖區(qū)中的商品狀況。
問(wèn)題3:程序不夠靈活,并且界面對(duì)用戶不夠友好。想要實(shí)現(xiàn)用戶能夠簡(jiǎn)單自主地控制想要?jiǎng)?chuàng)建的生產(chǎn)者與消費(fèi)者個(gè)數(shù)、緩沖區(qū)大小與產(chǎn)品編號(hào)。
解決方法:我使用了全局變量,在程序執(zhí)行之初定義好默認(rèn)值,并提供了與用戶交互的接口,使用戶能夠自主改變程序運(yùn)行的參數(shù)。編寫(xiě)了print()函數(shù),優(yōu)化了終端可視化界面,使用戶能夠自主選擇想要改變何種參數(shù),使程序運(yùn)行更靈活直觀。