关键词:
实验六 线程同步实验
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
实验六进程基础
...-姓名18043224-向康宁作业学习目标掌握Linux系统环境C语言编程概念学习Linux系统进程概念请举例说明静态链接库的创建与使用。add.csub.cmain.c编程实现一个简单文件复制命令mycp.c使用fork创建一个子进程,进程创建成功后父子进程分... 查看详情
实验六进程基础
...-姓名18041524-饶文峰作业学习目标1掌握Linux系统环境C语言编程概念2学习Linux系统进程概念1.请举例说明静态链接库的创建与使用。2.请举例说明共享库的创建与使用。创建共享数据库3.编程实现一个简单文件复制命令.以前做了没截... 查看详情
实验六进程基础(代码片段)
...-姓名18043204-王紫嫣作业学习目标1.掌握Linux系统环境C语言编程概念2.学习Linux系统进程概念实验内容1.请举例说明静态链接库的创建与使用。2.请举例说明共享库的创建与使用。3.编程实现一个简单文件复制命令。test文件:4.使用fo... 查看详情
实验六分析linux内核创建一个新进程的过程
...OC课程http://mooc.study.163.com/course/USTC-10000290001,进程的描述操作系统三大功能:进程管理(核心),内存管理,文件系统1,进程控制块PCB——task_struct也叫进程描述符,为了管理进程,内核必须对每个进程进行清晰的描述,进程描... 查看详情
实验六进程基础
...号-姓名18041516-郭昊作业学习目标掌握Linux系统环境C语言编程概念,学习Linux系统进程概念。1.请举例说明静态链接库的创建与使用add.csub.cmain.c2.请举例说明共享库的创建与使用common.hadd.csub.cmain.c开始时:创建共享库:3.编程实现... 查看详情
实验六进程基础
...—姓名18041528-朱海作业学习目标1.掌握Linux系统环境C语言编程概念2.学习Linux系统进程概念 1.使用fork创建一个子进程,进程创建成功后父子进程分别输出不同的内容。manforkfork1.c删除fflush(NuLL):删除换行:2.使用fork创建多个子进... 查看详情
linux内核分析实验六
============================================================... 查看详情
实验六进程基础(代码片段)
...-姓名18043116-刘霆锋作业学习目标1.掌握Linux系统环境C语言编程概念2.学习Linux系统进程概念1.请举例说明静态链接库的创建与使用。ar:建立,修改档案或从档案中抽取成员ar-r:替换归档文件中已有的文件或加入新文件ar-t:显示归档... 查看详情
linux学习-linux系统及编程基础笔记(代码片段)
useraddzhangsanpasswdzhangsanvisudo往/etc/sudoers文件中添加zhangsan#visudo找到如下的行rootALL=(ALL)ALL往该行下面添加zhangsanzhangsanALL=(ALL)ALL2.2Linux的基本结构一些根文件系统中较为重要的二级目录:①/boot:存放系统引导时所需的文件... 查看详情
unix系统编程信号部分学习笔记(代码片段)
《Unix系统编程》Unix信号学习笔记一、基础概念二、信号的产生三、对信号掩码和信号集进行操作四、捕捉与忽略信号——sigaction五、等待信号——pause、sigsuspend和sigwait六、信号处理:错误和异步信号安全七、用siglongjmp和sig... 查看详情
多线程编程学习笔记——任务并行库
接上文多线程编程学习笔记——任务并行库(一)接上文 多线程编程学习笔记——任务并行库(二) 六、 实现取消选项 本示例学习如何实现基于Task的异步操... 查看详情
windows编程课程学习笔记
一.Windows程序内部运行机制--Windows编程课程学习笔记二.MFC框架程序分析--Windows编程课程学习笔记三.简单绘图--Windows编程课程学习笔记四.文本编程--Windows编程课程学习笔记五.菜单编程--Windows编程课程学习笔记六.对话框编程--Window... 查看详情
六lwip学习笔记之用户数据报协议(udp)
...1、报文首部结构2、控制块三、控制块操作函数1、使用UDP编程2、新建控制块3、绑定控制块4、连接控制块5、其他控制块操作函数四、报文处理函数1、报文的发送2、报文接收与递交五、测试程序 查看详情
rancherserverha的高可用部署实验-学习笔记
..._duomaomao/article/details/78771731RancherServerHA的高可用部署实验-学习笔记 一、机器规划二、数据库服务器的安装三、RancherServerHA1的安装四、RancherServerHA2的安装五、HAProxy 负载均衡器的安装六、注册主机(工作主机)七、参考链... 查看详情
操作系统是如何进行工作的
李亚健 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000一、实验过程:实验内容为完成一个简单的时间片轮转多道程序内核代码1.根据老师指导按照实验步骤,在实验楼环境下打开shell:cdLinuxKernel/linux-3.9.4r... 查看详情
windows编程课程学习笔记
一.Windows程序内部运行机制--Windows编程课程学习笔记二.MFC框架程序分析--Windows编程课程学习笔记三.简单绘图--Windows编程课程学习笔记四.文本编程--Windows编程课程学习笔记五.菜单编程--Windows编程课程学习笔记六.对话框编程--Window... 查看详情
os学习笔记六:文件系统
...件内容的意义:由文件建立者和使用者解释2、文件系统操作系统中统一管理信息资源的一种软件,管理文件的存储、检索、更新,提供安全可靠的共享和保护手段,并且方便用户使用统一管理磁盘空间,实施磁盘空 查看详情
第四周
...了OS内核态和用户态两个概念。我们这周的实验就是关于操作系统的用户态、内核态切换以及中断。 先谈几点理解:1)OS采用系统调用实现用户态进程与I/O 查看详情