1. 程式人生 > 其它 >MOOC《Linux作業系統程式設計》學習筆記-實驗六

MOOC《Linux作業系統程式設計》學習筆記-實驗六

實驗六執行緒同步實驗

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

需求描述

程式流程圖

知識點記錄:

實驗的一種實現方式:

 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