开周边网站怎么做品牌企业内网

当前位置: 首页 > news >正文

开周边网站怎么做品牌,企业内网,河北网站建设流程,做游戏网站有钱赚吗1. 说明文档中原有程序实现的功能、实现方法。#xff08;用语言、程序流程图、为原有程序添加注释等方式均可#xff09; 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 -…1. 说明文档中原有程序实现的功能、实现方法。用语言、程序流程图、为原有程序添加注释等方式均可 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//队列中最多有20个元素 12.#define MAXLEN 100//字符串最大长度 13. #endif 1.//Typedefine.h 2.//定义结构体 3.#ifndef TYPEDEFINE_H 4.#define TYPEDEFINE_H 5. 6.#include const.h 7.//字符串结构体 8.typedef struct{

  1. char data[MAXLEN];//数据
  2. int len;//数据长度不算字符串结束字符”\0” 11.}SString; 12.//元素类型结构体 13.typedef struct{
  3. SString* strName;//数据
  4. long INumber;//号码 16.}Elemtype; 17.//结点结构体 18.typedef struct node{
  5. Elemtype data;//结点的数据域
  6. struct node *next;//结点的指针域指向下一个结点地址 21.}SNode; 22.//队列结构体 23.typedef struct{
  7. Elemtype q[MAXSIZE];//存放队列元素
  8. int front;//队列头指针
  9. int rear;//队列尾指针存放尾元素的下一个位置 27.}Queue; 28.//栈结构体 29.typedef struct st{
  10. Elemtype data[MAXSIZE];//存放栈元素
  11. int top;//栈顶指针 32.}Stack; 33. 34.typedef int BOOL; 35.typedef int Status; 36. #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);//判断队列是否为空非空返回0空返回1 13.int QueueCount(Queue *HQ);//统计队列元素个数 14.bool QueueFull(Queue Q);//判断队列是否满满返回 TRUE非满返回 FALSE 15. #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(){
  12. Queue Q (Queue)malloc(sizeof(Queue));//申请空间
  13. Q-front Q-rear 0;//头尾指针相等时说明队列为空
  14. return Q;//返回队列指针 13.} 14.//入队函数 15.int EnQueue(Queue *Q, Elemtype x){
  15. if((Q-rear 1) % MAXSIZE Q-front) return FALSE;//队满入队失败返回0
  16. else{
  17. Q-q[Q-rear] x;//存放元素至队尾
  18. Q-rear (Q-rear 1) % MAXSIZE;//队列尾指针指向下一位
  19. return TRUE;
  20. } 22.} 23.//出队函数 24.int DeQueue(Queue *Q, Elemtype *x){
  21. if(Q-rear Q-front) return FALSE;//队空出队失败返回0
  22. else{
  23. *x Q-q[Q-front];//从队头取出元素
  24. Q-front (Q-front 1) % MAXSIZE;//队头指针指向下一位
  25. return TRUE;
  26. } 31.} 32.//判断队是否为空非空返回0空返回1 33.int QueueEmpty(Queue Q){
  27. if(Q.rear Q.front) return TRUE;//队空
  28. else return FALSE; 36.} 37.//返回队列中的最后的一个元素 38.Elemtype Last(Queue *Q){
  29. Elemtype *prElem NULL;
  30. Queue *prTempQueue;
  31. prTempQueue InitQueue();//初始化一个新的临时队列用于暂存数据
  32. while(QueueEmpty(*Q) 1){//将Q中的元素依次取出放至prTempQueue中
  33. DeQueue(Q, prElem);
  34. EnQueue(prTempQueue, *prElem);
  35. }
  36. while(QueueEmpty(*prTempQueue) 1){//将prTempQueue中的元素依次取出放至Q中
  37. DeQueue(prTempQueue, prElem);
  38. EnQueue(Q, *prElem);
  39. }
  40. return *prElem;//返回队列中的最后的一个元素 51.} 52.//判断队是否为满满返回1非满返回0 53.bool QueueFull(Queue Q){
  41. if(((Q.rear 1) % MAXSIZE) Q.front) return TRUE;//队满
  42. else return FALSE; } 1.//exp1.c 2.//主程序实现三个生产者与两个消费者问题 3.//在30s内生产者把产品放入缓冲区消费者从缓冲区中拿走产品缓冲区空间定义为5因为队列中元素最大值设为了20所以无需考虑队列满情况。生产者在缓冲区满时必须等待直到缓冲区有空间才继续生产消费者在缓冲区空时必须等待直到缓冲区中有产品才能继续读取。共有三个生产者与两个消费者线程并将生产的商品放入队列中商品编号从1000开始最多放五个根据先生产先消费的原则进行消费。 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//定义可用资源数量 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(){
  43. pthread_t id1, id2, id3, id4, id5;//创建5个线程分别对应三个生产者与两个消费者
  44. pthread_t mon_th_id;
  45. int ret;
  46. end_time time(NULL) 30;//程序执行30s停止
  47. qt InitQueue();//初始化队列qt
  48. p.INumber 1000;//号码从1000开始
  49. ret sem_init(mutex, 0, 1);//初使化互斥信号量mutex为
  50. ret sem_init(empty, 0, N);//初使化empty信号量为N 35.
  51. ret sem_init(full, 0, 0);//初使化full信号量为
  52. if(ret ! 0) perror(sem_init); 38.
  53. ret pthread_create(id1, NULL, (void*)productor, NULL);//生产者线程1
  54. if(ret ! 0) perror(pthread cread1); 41.
  55. ret pthread_create(id3, NULL, (void*)productor, NULL);//生产者线程3
  56. if(ret ! 0) perror(pthread cread3); 44.
  57. ret pthread_create(id2, NULL, (void*)consumer, NULL);//消费者线程2
  58. if(ret ! 0) perror(pthread cread2); 47.
  59. ret pthread_create(id5, NULL, (void*)productor, NULL);//生产者线程5
  60. if(ret ! 0) perror(pthread cread5); 50.
  61. ret pthread_create(id4, NULL, (void*)consumer, NULL);//消费者线程4
  62. if(ret ! 0) perror(pthread cread4); 53.
  63. pthread_join(id1, NULL);//等待生产者线程1结束
  64. pthread_join(id2, NULL);//等待消费者线程2结束
  65. pthread_join(id3, NULL);//等待生产者线程3结束
  66. pthread_join(id4, NULL);//等待消费者线程4结束
  67. pthread_join(id5, NULL);//等待生产者线程5结束 59.
  68. exit(0); 61.} 62.//生产者线程函数 63.void productor(void *arg){
  69. int i, nwrite;
  70. while(time(NULL) end_time){//在规定时间内循环生产商品
  71. sem_wait(empty);//empty信号量P操作
  72. sem_wait(mutex);//互斥信号量P操作
  73. if(TRUE QueueFull(*qt)){//队满不操作
  74. printf(Procuctor: buffer is full, please try to write later.\n);
  75. }
  76. else{//队不满
  77. EnQueue(qt, p);//入队
  78. printf(Productor: write [%d] to buffer\n,p.INumber);
  79. p.INumber;//编号加一
  80. }
  81. sem_post(full);//full信号量V操作
  82. sem_post(mutex);//mutex信号量V操作
  83. sleep(1);
  84. } 80.} 81. 82.void consumer(void *arg){
  85. int nolock 0;
  86. int ret, nread;
  87. Elemtype p2;
  88. while((time(NULL) end_time) || (FALSE (QueueEmpty(*qt)))){//在规定时间内或队列非空时循环消费商品
  89. sem_wait(full);//full信号量P操作
  90. sem_wait(mutex);//互斥信号量P操作
  91. if(TRUE QueueEmpty(*qt)){//队列空不能消费
  92. printf(Consumer: the buffer is empty, please try to read later.\n);
  93. }
  94. else{//队列非空
  95. DeQueue(qt, p2);//出队
  96. printf(Consumer: read [%d] from buffer.\n, p2.INumber);
  97. }
  98. sem_post(empty);//empty信号量V操作
  99. sem_post(mutex);//互斥信号量V操作
  100. sleep(2);
  101. } }
  102. 列出可改进的功能、实现方法等 可改进的功能 将代码整理去除掉了冗余代码。显示缓冲区资源个数创建全局变量n统计生产者/消费者 生产/消费后缓冲区中剩余的资源个数并予以显示。提示生产者/消费者线程结束语句如Productor 1 is killed。利用gettid()函数获得生产者、消费者的线程id并显示是哪一个生产者/消费者 生产/消费 了哪一个商品。编写了display()函数根据生产者/消费者的动作可视化了缓冲区中的商品状况使商品变化情况更加简单直观。利用for循环创建线程实现可以自主输入想要创建的生产者与消费者个数数值在1到10之间、缓冲区大小数值在1到20之间与产品编号数值在1到1000之间。编写了print()函数优化了终端可视化界面可在界面中选择功能并自定义赋值使程序运行更灵活直观。 实现方法 首先打印出终端可视化界面让用户可以自主选择功能可以进行自定义赋值、运行程序与退出系统操作未进行自定义赋值的变量使用默认值。自定义赋值后运行程序根据用户的赋值给变量赋值并创建生产者、消费者线程运行生产者、消费者函数。在生产者、消费者函数中根据队列情况进行P、V操作与生产/消费操作并利用gettid()函数获得生产者的线程id显示是哪一个生产者/消费者 生产/消费了哪一件商品并可视化生产/消费操作前后缓冲区中的产品变化情况与缓冲区中剩余的资源个数。运行规定时间后生产者/消费者线程结束退出系统。
  103. 详细说明已完成的改进附上程序代码改进处加注释注意代码格式 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{
  104. char data[MAXLEN];
  105. int len; 10.}SString; 11. 12.typedef struct{
  106. SString* strName;
  107. long INumber; 15.}Elemtype; 16. 17.typedef struct{
  108. Elemtype q[MAXSIZE];
  109. int front;
  110. 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(){
  111. Queue Q (Queue)malloc(sizeof(Queue));
  112. Q-front Q-rear 0;
  113. return Q; 13.} 14. 15.int EnQueue(Queue *Q, Elemtype x){
  114. if((Q-rear 1) % MAXSIZE Q-front) return FALSE;
  115. else{
  116. Q-q[Q-rear] x;
  117. Q-rear (Q-rear 1) % MAXSIZE;
  118. return TRUE;
  119. } 22.} 23. 24.int DeQueue(Queue *Q, Elemtype *x){
  120. if(Q-rear Q-front) return FALSE;
  121. else{
  122. *x Q-q[Q-front];
  123. Q-front (Q-front 1) % MAXSIZE;
  124. return TRUE;
  125. } 31.} 32. 33.int QueueEmpty(Queue Q){
  126. if(Q.rear Q.front) return TRUE;
  127. else return FALSE; 36.} 37. 38.bool QueueFull(Queue Q){
  128. if(((Q.rear 1) % MAXSIZE) Q.front) return TRUE;
  129. 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()函数所需要的头文件 15.#define gettid() syscall(__NR_gettid)//使用gettid()函数所需要的头文件 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;//缓冲区中资源个数 23.int x 2, y 2;//生产者与消费者个数默认值 24.int ret; 25.int N 5;//缓冲区最大资源个数默认值 26. 27.void consumer(void *arg); 28.void productor(void *arg); 29.void display();//可视化缓冲区中商品状况 30.void print();//终端可视化界面 31. 32.int main(){
  130. p.INumber 1;//产品初始编号默认值
  131. while(1){
  132. print();//展示系统可视化选项
  133. //接收用户需要的功能号
  134. char number[100];
  135. ret scanf(%s, number[0]);
  136. if(strlen(number)!1 || ret ! 1 || number[0] 0 || number[0] 5){
  137. printf(\nplease input a number between 0 and 5!\n);
  138. continue;
  139. }
  140. int num number[0] - 0;
  141. //输入想要创建的生产者个数并确保数值在1到10之间
  142. if(num 1){
  143. do{
  144. printf(Please input the number of productor: );
  145. ret scanf(%d,x);
  146. if(ret ! 1 || x 1 || x 10){
  147. printf(Please ensure the input between 1 and 10\n);
  148. }
  149. }while(ret ! 1 || x 1 || x 10);
  150. }
  151. //输入想要创建的消费者个数并确保数值在1到10之间
  152. else if(num 2){
  153. do{
  154. printf(Please input the number of consumer: );
  155. ret scanf(%d,y);
  156. if(ret ! 1 || y 1 || y 10){
  157. printf(Please ensure the input between 1 and 10\n);
  158. }
  159. }while(ret ! 1 || y 1 || y 10);
  160. }
  161. //输入想要创建的缓冲区大小并确保数值在1到20之间
  162. else if(num 3){
  163. do{
  164. printf(Please input the size of buffer: );
  165. ret scanf(%d,N);
  166. if(ret ! 1 || N 1 || N 20){
  167. printf(Please ensure the input between 1 and 20\n);
  168. }
  169. }while(ret ! 1 || N 1 || N 20);
  170. }
  171. //输入想要生产的产品的初始编号并确保数值在1到1000之间
  172. else if(num 4){
  173. do{
  174. printf(Please input the key of product: );
  175. ret scanf(%d,p.INumber);
  176. if(ret ! 1 || p.INumber 1 || p.INumber 1000){
  177. printf(Please ensure the input between 1 and 1000\n);
  178. }
  179. }while(ret ! 1 || p.INumber 1 || p.INumber 1000);
  180. }
  181. //运行程序
  182. else if(num 5){
  183. pthread_t id;
  184. end_time time(NULL) 10;
  185. qt InitQueue();
  186. //p.INumber 1000;
  187. ret sem_init(mutex, 0, 1);
  188. ret sem_init(empty, 0, N);
  189. ret sem_init(full, 0, 0);
  190. if(ret ! 0) perror(sem_init);
  191. //创建线程
  192. for(int i 0; i x; i){
  193. ret pthread_create(id, NULL, (void*)productor, NULL);
  194. if(ret ! 0) perror(Productor pthread create);
  195. }
  196. for(int i x; i x y; i){
  197. ret pthread_create(id, NULL, (void*)consumer, NULL);
  198. if(ret ! 0) perror(Consumer pthread create);
  199. }
  200. //等待线程结束
  201. for(int i 0; i x; i){
  202. pthread_join(id, NULL);
  203. printf(Productor %d is killed\n, i);//提示程序结束语句
  204. }
  205. for(int i x; i x y; i){
  206. pthread_join(id, NULL);
  207. printf(Consumer %d is killed\n, i);//提示程序结束语句
  208. }
  209. break;
  210. }
  211. //退出系统
  212. else if(num 0){
  213. break;
  214. }
  215. }
  216. return 0; 122.} 123. 124.void productor(void *arg){
  217. while(time(NULL) end_time){
  218. sem_wait(empty);
  219. sem_wait(mutex);
  220. if(TRUE QueueFull(*qt)){
  221. printf(Productor pthread %u: buffer is full, please try to write later.\n, gettid());
  222. }
  223. else{
  224. EnQueue(qt, p);
  225. display();//可视化缓冲区
  226. n;//缓冲区资源数加一
  227. printf(——-);
  228. display();//可视化缓冲区
  229. printf(\n);
  230. printf(Productor pthread %u: write [%d] to buffer.\n, gettid(), p.INumber);//利用gettid()函数获得生产者的线程id并显示是哪一个生产者生产了哪一件商品
  231. printf(There are %d left in buffer\n,n);//显示缓冲区中剩余的资源个数
  232. printf(————————————————–\n);//生产者/消费者动作分隔符
  233. p.INumber;
  234. }
  235. sem_post(full);
  236. sem_post(mutex);
  237. sleep(1);
  238. } 147.} 148. 149.void consumer(void *arg){
  239. Elemtype p2;
  240. while((time(NULL) end_time) || (FALSE (QueueEmpty(*qt)))){
  241. sem_wait(full);
  242. sem_wait(mutex);
  243. if(TRUE QueueEmpty(*qt)){
  244. printf(Consumer pthread %u: the buffer is empty, please try to read later.\n, gettid());
  245. }
  246. else{
  247. DeQueue(qt, p2);
  248. display();//可视化缓冲区
  249. n–;//缓冲区资源数减一
  250. printf(——-);
  251. display();//可视化缓冲区
  252. printf(\n);
  253. printf(Consumer pthread %u: read [%d] from buffer.\n, gettid(), p2.INumber);//利用gettid()函数获得消费者的线程id并显示是哪一个消费者消费了哪一件商品
  254. printf(There are %d left in buffer\n,n);//显示缓冲区中剩余的资源个数
  255. printf(————————————————–\n);//生产者/消费者动作分隔符
  256. }
  257. sem_post(empty);
  258. sem_post(mutex);
  259. sleep(2);
  260. } 172.} 173.//可视化缓冲区中的商品状况 174.void display(){
  261. printf([);
  262. for(int i n; i 0; i–){
  263. printf( P );
  264. }
  265. printf(]); 180.} 181.//终端可视化界面 182.void print() 183.{
  266. printf(\n);
  267. printf(*********************************************************************************\n);
  268. printf(——————–[ Producer Consumer Program Function Bar ]——————-\n);
  269. printf(*********************************************************************************\n);
  270. printf(* 1.Enter the number of producers you want to create (default value is 2) *\n);
  271. printf(* 2.Enter the number of consumers you want to create (default value is 2) *\n);
  272. printf(* 3.Enter the buffer size you want to create (default value is 5) *\n);
  273. printf(* 4.Enter the initial product number you want to produce (default value is 1) *\n);
  274. printf(* 5.run the program *\n);
  275. printf(* 0.Exit the system *\n);
  276. printf(———————————————————————————\n);
  277. printf(Please enter 0-5 to select a function: ); }
  278. 运行结果截图 1示例一这次运行我们想将生产者改为2个消费者改为4个其余选项为默认值运行程序。 中间略 2示例二这次运行我们想将缓冲区大小改为7商品初始编号改为300其余选项为默认值运行程序。 中间略 5. 总结 实验中遇到的问题与解决方法 问题1想要改进程序执行语句使程序运行时能反映出具体是哪一位生产者/消费者 生产/消费 了哪一个商品。一开始我尝试在pthread_create()函数中给生产者/消费者函数传递生产者/消费者id变量但是由于线程共享变量无法在生产者-生产者、消费者-消费者之间作出区分。 解决方法最终我决定使用gettid()函数获得生产者、消费者的线程id作为每一位生产者/消费者的标志来反映出具体是哪一位生产者/消费者 生产/消费 了商品。 问题2在程序运行过程中缓冲区中的产品变化并不能被很好的反映出来。 解决方法我编写了display()函数分别在生产/消费前后调用它可视化了缓冲区中的商品状况。 问题3程序不够灵活并且界面对用户不够友好。想要实现用户能够简单自主地控制想要创建的生产者与消费者个数、缓冲区大小与产品编号。 解决方法我使用了全局变量在程序执行之初定义好默认值并提供了与用户交互的接口使用户能够自主改变程序运行的参数。编写了print()函数优化了终端可视化界面使用户能够自主选择想要改变何种参数使程序运行更灵活直观。