哲学家进餐问题的c语言代码
来源:互联网 发布:无间道3电影解析知乎 编辑:程序博客网 时间:2024/04/30 15:36
#include <pthread.h>#include <stdio.h>#define N 5#define LEFT (i+N-1)%N#define RIGHT (i+1)%N#define THINK_TIME 3#define EAT_TIME 2enum { THINKING, HUNGRY, EATING } state[N];pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER, s[N];void test(int i){ if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) { state[i] = EATING; pthread_mutex_unlock(&s[i]); }}void take_forks(int i){ pthread_mutex_lock(&mutex); state[i] = HUNGRY; test(i); pthread_mutex_unlock(&mutex); pthread_mutex_lock(&s[i]);}void put_forks(int i){ pthread_mutex_lock(&mutex); state[i] = THINKING; test(LEFT); test(RIGHT); pthread_mutex_unlock(&mutex);}void think(int i){ printf("philosopher %d is thinking...\n", i); sleep(THINK_TIME);}void eat(int i){ printf("philosopher %d is eating...\n", i); sleep(EAT_TIME);}void* phi(void* vargp){ int i = *(int*)vargp; while (1) { think(i); take_forks(i); eat(i); put_forks(i); } return NULL;}int main(){ int i; pthread_t tid[N]; for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, phi, (void*)(&i)); for (i = 0; i < N; i++) pthread_join(tid[i], NULL); return 0;}
0 0
- 哲学家进餐问题的c语言代码
- 哲学家进餐问题的C语言实现
- 哲学家进餐问题C语言实现
- 哲学家进餐的问题
- C-多线程-哲学家进餐问题
- 哲学家进餐问题的算法与实现
- 哲学家进餐问题
- 哲学家进餐问题
- 哲学家进餐问题
- 关于哲学家进餐问题
- 哲学家进餐问题
- 哲学家进餐问题
- 哲学家进餐问题
- 关于哲学家进餐问题
- 哲学家进餐问题:
- 哲学家进餐问题
- 哲学家进餐问题
- 哲学家进餐问题
- mybatis中的#和$的区别
- Random Sample Consensus(RANSAC)算法介绍
- 信息系统项目人员转移流程
- 64bit CentOS搭建基于LAMP架构的discuz 3.2论坛
- DB 查询分析器 方便地创建DB2自定义函数
- 哲学家进餐问题的c语言代码
- Aerospike C客户端手册———数据扫描—记录扫描
- JDBC(三)数据库连接池,动态代理
- 数据仓库专题(8)-维度属性选择之维护历史是否应该保留
- Tips to Optimize Your SQL Statements - Part 1
- 数学电子书下载
- 线程局部存储
- kpush源码解析---android端
- Tips to Optimize Your SQL Statements - Part 2