MOOC《Linux操作系统编程》学习笔记-实验六

实验六 线程同步实验

https://www.icourse163.org/learn/UESTC-1003040002?tid=1455108444#/learn/content?type=detail&id=1228729539&cid=1245454470

 

需求描述

MOOC《Linux操作系统编程》学习笔记-实验六

 

 

程序流程图

MOOC《Linux操作系统编程》学习笔记-实验六

 

 

MOOC《Linux操作系统编程》学习笔记-实验六

 

知识点记录:

MOOC《Linux操作系统编程》学习笔记-实验六

 

 

实验的一种实现方式:

 1 #include "stdio.h"
 2 #include "stdint.h"
 3 #include "stdlib.h"
 4 #include "pthread.h"
 5 #include "sys/types.h"
 6 #include "sys/wait.h"
 7 //#include <stdlib.h>
 8 #include "unistd.h"
 9 
10 #define PH_NUM (5u)         /* 哲学家数量 */
11 #define THINKING_TIME (2u)  /* 思考时间 */
12 #define EATING_TIME   (2u)  /* 进餐时间 */
13 
14 static uint8_t table[PH_NUM] = {0};
15 pthread_mutex_t tableMutex;
16 
17 //尝试拿起筷子,1-成功拿起 0-筷子已被其他人拿起
18 static uint8_t takechopstick(int number)
19 {
20     uint8_t ret = 0;
21 
22     if(0 == pthread_mutex_trylock(&tableMutex))
23     {
24         if(number >= sizeof(table)) number = 0;
25         if(table[number]) ret = 0;
26         else 
27         {
28             table[number] = 1;
29             ret = 1;
30         }
31 
32         if(0 == pthread_mutex_unlock(&tableMutex))
33             /*printf("taking %d chopstick is successful\n",number)*/; 
34     }else /*printf("taking %d chopstick is failure\n",number)*/; 
35 
36     return ret;
37 }
38 //放下筷子
39 static void putchopstick(int number)
40 {
41     if(0 == pthread_mutex_lock(&tableMutex))
42     {
43         if(number >= sizeof(table)) number = 0;
44         if(table[number])
45             table[number] = 0;
46 
47         if(0 == pthread_mutex_unlock(&tableMutex))
48             /*printf("putting %d chopstick is successful\n",number)*/; 
49     }
50 }
51 
52 //单哲学家任务
53 void *philosopher(int *Ptr)
54 {
55     int number = *Ptr;
56     while(1)
57     {
58         printf("philosopher %d is thinking\n",number);
59         sleep(THINKING_TIME);
60         if(takechopstick(number))
61         {
62             if(takechopstick((int)((number + 1)%(sizeof table))))
63             {
64                 printf("philosopher %d is eating\n",number);
65                 sleep(EATING_TIME);
66                 putchopstick(number);
67                 putchopstick((int)((number + 1)%(sizeof table)));
68             }
69             else 
70             {
71                 //printf("philosopher %d taking %d chopstick failure\n",(int)((number + 1)%(sizeof table)),(int)((number + 1)%(sizeof table)));
72                 putchopstick(number);
73             }
74         }
75         else /*printf("philosopher %d taking %d chopstick failure\n",number,number)*/;
76     }
77     return 0;
78 }
79 
80 int main(int argc ,char ** argv)
81 {
82     const int PHList[PH_NUM] = {0,1,2,3,4};
83     pthread_t tid[PH_NUM];
84     printf("create mainThread\n");
85     if(0 == pthread_mutex_init(&tableMutex,NULL))
86         printf("mutex init is successful\n"); 
87     else 
88         printf("mutex init is failure\n");   
89     //sprintf(str,"str from parent");
90     for(int i = 0; i < PH_NUM; i++)
91         pthread_create(&(tid[i]),NULL,(void *)philosopher,(void *)&(PHList[i]));
92     
93     pthread_join(tid[4],NULL);
94     pthread_mutex_destroy(&tableMutex);
95     printf("mainThread exit \n");
96 
97     return 0;
98 }

执行结果:

./test6
create mainThread
mutex init is successful
philosopher 0 is thinking
philosopher 1 is thinking
philosopher 2 is thinking
philosopher 3 is thinking
philosopher 4 is thinking
philosopher 1 is eating
philosopher 0 is thinking
philosopher 3 is eating
philosopher 4 is thinking
philosopher 2 is thinking
philosopher 1 is thinking
philosopher 0 is thinking
philosopher 3 is thinking
philosopher 4 is eating
philosopher 2 is eating
philosopher 1 is thinking
philosopher 0 is thinking
philosopher 3 is thinking
philosopher 4 is thinking
philosopher 2 is thinking
philosopher 1 is eating
philosopher 0 is thinking
philosopher 3 is eating
philosopher 4 is thinking
philosopher 2 is thinking
philosopher 1 is thinking
philosopher 0 is eating
philosopher 3 is thinking
philosopher 4 is thinking
philosopher 2 is eating
philosopher 1 is thinking
philosopher 0 is thinking
philosopher 3 is thinking
philosopher 4 is eating
philosopher 2 is thinking
philosopher 1 is eating
philosopher 0 is thinking
philosopher 4 is thinking
philosopher 3 is thinking
philosopher 2 is thinking
philosopher 1 is thinking
philosopher 3 is thinking
philosopher 0 is thinking
philosopher 4 is eating
philosopher 2 is eating
philosopher 1 is thinking
philosopher 3 is thinking
philosopher 2 is thinking
philosopher 4 is thinking
philosopher 0 is thinking
philosopher 1 is eating
philosopher 2 is thinking
philosopher 3 is eating
philosopher 4 is thinking
philosopher 0 is thinking
philosopher 1 is thinking
philosopher 4 is eating
philosopher 3 is thinking
philosopher 0 is thinking
philosopher 2 is thinking
philosopher 1 is eating
philosopher 0 is thinking
philosopher 3 is thinking
philosopher 2 is thinking
philosopher 4 is thinking
philosopher 1 is thinking
philosopher 3 is eating
philosopher 0 is eating
philosopher 2 is thinking
philosopher 4 is thinking
philosopher 1 is thinking
philosopher 3 is thinking
philosopher 0 is thinking
philosopher 4 is eating
philosopher 2 is eating
philosopher 3 is thinking
philosopher 0 is thinking
philosopher 1 is thinking
philosopher 2 is thinking
philosopher 4 is thinking
philosopher 3 is thinking
philosopher 2 is eating
philosopher 1 is thinking
philosopher 0 is eating
philosopher 4 is thinking
philosopher 3 is thinking
philosopher 1 is thinking
philosopher 0 is thinking
philosopher 4 is eating
philosopher 2 is thinking
philosopher 1 is thinking
philosopher 4 is thinking
philosopher 3 is thinking
philosopher 2 is eating
philosopher 0 is thinking
philosopher 4 is thinking
philosopher 1 is thinking
philosopher 2 is thinking
philosopher 0 is eating
philosopher 3 is eating
philosopher 4 is thinking
philosopher 1 is thinking
philosopher 3 is thinking
philosopher 2 is thinking
philosopher 0 is thinking
philosopher 4 is eating
philosopher 3 is thinking
philosopher 2 is thinking
philosopher 0 is thinking
philosopher 1 is eating
philosopher 4 is thinking
philosopher 2 is thinking
philosopher 0 is thinking
philosopher 1 is thinking
philosopher 3 is eating
philosopher 4 is thinking
philosopher 2 is thinking
philosopher 3 is thinking
philosopher 1 is thinking
philosopher 0 is eating
philosopher 4 is thinking
philosopher 2 is eating
philosopher 3 is thinking
philosopher 1 is thinking
philosopher 0 is thinking

 

上一篇:【Android Developers Training】 72. 缩放一个视图


下一篇:Thinking In Java -- Chapter 8 -- 多态