哲学家问题观察死锁

摘要:
1.1非死锁代码#include #include #include #include #include<errno。h˃ #包含 //chopstickasmutexthread_ mutex_tchopstick[6];void*eat_think(void*arg){charphi=*(char*)

1.1 不死锁代码

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <pthread.h>
#include <errno.h>
#include <math.h>

// chopstick as mutex
pthread_mutex_t chopstick[6];
void *eat_think(void *arg)
{
	char phi = *(char *)arg;
	int left, right;
	switch(phi) {
	case 'A':
		left = 5;
		right = 1;
		break;
	case 'B':
		left = 1;
		right = 2;
		break;
	case 'C':
		left = 2;
		right = 3;
		break;
	case 'D':
		left = 3;
		right = 4;
		break;
	case 'E':
		left = 4;
		right = 5;
		break;
	}
	int j;
	for (j = 0; j < 5; j++)
	{
		usleep(3);
		pthread_mutex_lock(&chopstick[left]);
		printf("Philosopher %c fetches chopstick %d\n", phi, left);

		if (pthread_mutex_trylock(&chopstick[right]) == EBUSY)
		{
			pthread_mutex_unlock(&chopstick[left]);
			continue;
		}

		// pthread_mutex_lock(&chopstick[right]);
		printf("Philosopher %c fetches chopstick %d\n", phi, right);
		printf("philosopher %c is eating.  %d\n", phi, j+1);
		usleep(3);
		pthread_mutex_unlock(&chopstick[left]);
		printf("philosopher %c release chopstick %d\n", phi, left);
		pthread_mutex_unlock(&chopstick[right]);
		printf("Philosopher %c release chopstick %d\n", phi, right);
	}
}

int main()
{
	pthread_t A, B, C, D, E;
	int i;
	for(i = 0; i < 5; i++)
	{
		pthread_mutex_init(&chopstick[i], NULL);
	}
	pthread_create(&A, NULL, eat_think, "A");
	pthread_create(&B, NULL, eat_think, "B");
	pthread_create(&C, NULL, eat_think, "C");
	pthread_create(&D, NULL, eat_think, "D");
	pthread_create(&E, NULL, eat_think, "E");

	pthread_join(A, NULL);
	pthread_join(B, NULL);
	pthread_join(C, NULL);
	pthread_join(D, NULL);
	pthread_join(E, NULL);
	return 0;
}

1.2 不死锁截图

哲学家问题观察死锁第1张

2.1 死锁代码

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <pthread.h>
#include <errno.h>
#include <math.h>

// chopstick as mutex
pthread_mutex_t chopstick[6];
void *eat_think(void *arg)
{
	char phi = *(char *)arg;
	int left, right;
	switch(phi) {
	case 'A':
		left = 5;
		right = 1;
		break;
	case 'B':
		left = 1;
		right = 2;
		break;
	case 'C':
		left = 2;
		right = 3;
		break;
	case 'D':
		left = 3;
		right = 4;
		break;
	case 'E':
		left = 4;
		right = 5;
		break;
	}
	int j;
	for (j = 0; j < 500; j++)
	{
		usleep(3);
		pthread_mutex_lock(&chopstick[left]);
		printf("Philosopher %c fetches chopstick %d\n", phi, left);

		// if (pthread_mutex_trylock(&chopstick[right] == EBUSY))
		// {
		//	pthread_mutex_unlock(&chopstick[left]);
		//	continue;
		// }

		pthread_mutex_lock(&chopstick[right]);
		printf("Philosopher %c fetches chopstick %d\n", phi, right);
		printf("philosopher %c is eating.  %d\n", phi, j+1);
		usleep(3);
		pthread_mutex_unlock(&chopstick[left]);
		printf("philosopher %c release chopstick %d\n", phi, left);
		pthread_mutex_unlock(&chopstick[right]);
		printf("Philosopher %c release chopstick %d\n", phi, right);
	}
}

int main()
{
	pthread_t A, B, C, D, E;
	int i;
	for(i = 0; i < 5; i++)
	{
		pthread_mutex_init(&chopstick[i], NULL);
	}
	pthread_create(&A, NULL, eat_think, "A");
	pthread_create(&B, NULL, eat_think, "B");
	pthread_create(&C, NULL, eat_think, "C");
	pthread_create(&D, NULL, eat_think, "D");
	pthread_create(&E, NULL, eat_think, "E");

	pthread_join(A, NULL);
	pthread_join(B, NULL);
	pthread_join(C, NULL);
	pthread_join(D, NULL);
	pthread_join(E, NULL);
	return 0;
}

2.2 死锁截图

哲学家问题观察死锁第2张

免责声明:文章转载自《哲学家问题观察死锁》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇jQuery 操作Cookie(Windows)VS2017下编译FLANN1.8.4下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

传奇衣服、翅膀、武器、怪物、NPC等外观代码计算方法与公式

例如:MON50.WIL,要添加的怪物图像是361-720,因此怪物在文件中的位置是第二位,因为怪物位置代码从0开始,即第一个怪物是0,第二个怪物是1,第三个怪物是2……因此,当前文件中怪物的位置代码是1。因此,怪物的APPR值是{*10}+1=491。...

sikuli简介

Sikuli脚本自动化,你在屏幕上看到的任何东西。Sikuli是一个开放源码的最初的用户界面设计组织在麻省理工学院的研究项目。现在是保持并进一步协调与开源社区开发的Sikuli实验室在美国科罗拉多州博尔德大学。Sikuli的MIT许可证下发布的。当然,你也可以使用sikuli的javaAPI使其在java环境下运行。小例子大体上了解sikuli的界面,下面来...

"SQLserver 事务日志已满"解决方法

如果不够,备份后换个地方存[注:tempdb你数据库名称。...

《学习opencv》笔记——矩阵和图像操作——cvAnd、cvAndS、cvAvg and cvAvgSdv

矩阵和图像的操作cvAnd函数其结构voidcvAnd;程序实例#include#include#includeintmain{IplImage*src1,*src2,*src3;src1=cvLoadImage;src2=cvLoadImage;src3=cvLoadImage;cvAnd;cvShowImage;cvShowImage;cvShowIma...

建行手机银行4.0版本转账怎么不要求输入支付密码?

建行手机银行单笔限额50万,日限额100万,这个6位数的验证码价值50万元!输入6个数字的支付密码只需要几秒钟而已,转账操作频率不是很高,手机银行转账速度比人工柜台、ATM、电脑网银转账速度不知道快了多少倍,输入6个数字的支付密码这几秒钟相对安全性算什么呢?另外建行还有帐号支付的方式,对电子商户日限额10000元,只需要帐号+手机验证码就可以支付,密码都不用...

AVUE 下拉 select 获取选中项的文本

底层应该不支持,其它方式应该可以,到时候看看黎大神给的方案。...