惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
NISL@THU
NISL@THU
S
Securelist
O
OpenAI News
S
Security Affairs
Cyberwarzone
Cyberwarzone
T
Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
SecWiki News
SecWiki News
S
Secure Thoughts
GbyAI
GbyAI
I
Intezer
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
Google Online Security Blog
Google Online Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
A
About on SuperTechFans
S
Schneier on Security
P
Proofpoint News Feed
雷峰网
雷峰网
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
H
Heimdal Security Blog
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
V
V2EX
L
Lohrmann on Cybersecurity
Security Latest
Security Latest
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
H
Hacker News: Front Page
Cisco Talos Blog
Cisco Talos Blog
Webroot Blog
Webroot Blog
T
Tenable Blog
MyScale Blog
MyScale Blog
博客园 - 司徒正美
S
SegmentFault 最新的问题
Y
Y Combinator Blog
腾讯CDC
Hacker News: Ask HN
Hacker News: Ask HN
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY

博客园 - 飘啊飘

一个精简的vi源码(2000行) dtruss 粗糙的翻译 gdb常用命令[转] gdb中信号的处理[转] 在linux中通过进程名获得进程id busybox0.60.3源码学习开始 pygame做的贪吃蛇 python中使用struct模块处理二进制数据 使用PYGAME开发的坦克游戏[代码][思路] 一篇很好的讲/etc/inittab的文章[转] 《代码整洁之道》笔记之函数 使用面向对象概念优化条件判断语句的一个小应用 python生成文件树的代码 深入理解软件包的配置、编译与安装[转] pygame学习之对象移动 pywin32重启电脑 - 飘啊飘 - 博客园 解决LINUX和WINDOWS时间不一置 通过状态机实现的一个配置读取函数 UNIX基础知识--《APUE》第一章笔记
哲学家吃空心粉问题
飘啊飘 · 2011-04-29 · via 博客园 - 飘啊飘

//      pe.c
//      哲学家吃空心粉的问题
//      Copyright 2011 shiweifu <shiweifu@126.com>
//      

#define _REENTRANT
#include 
<stdio.h>
#include 
<unistd.h>
#include 
<stdlib.h>
#include 
<string.h>

#include 

<semaphore.h>
#include 
<pthread.h>#define STR_MAX 256

typedef 

struct philosophy {
    
char name[STR_MAX];
    sem_t 
*left;
    sem_t 
*right;
    
char is_stop;
} philosophy;
void *thread_function(void *arg);int main(int argc, char **argv) {    
    
    
int i = 0;
    
//五只悲剧的筷子
    sem_t sems[5];
    
//五个NC哲学家
    philosophy philers[5];
    
    pthread_t threads[
5];
    
    
int res = 0;
    
    
char tmp[255= {0};
    
    
for(; i < 5; i++) {
        philers[i].left 
= &sems[i];
        philers[i].right 
= &sems[i];
        
        
if (i == 4) {
            philers[i].left 
= &sems[0];
        }
        philers[i].is_stop 
= 0;
        sprintf(tmp,
"NC %d",i);
        strncpy(philers[i].name,tmp,
255);
    }
    
for(i = 0; i < 5; i++) {
        sem_init(
&sems[i], 010);

        res 

= pthread_create(&threads[i],NULL,thread_function,&philers[i]);
        
if (res != 0) {
            perror(
"创建线程出错啦...");
        }
    }
    
    
//给这群吃货吃20秒
    sleep(20);
    
    
for(i = 0; i < 5; i++) {
        philers[i].is_stop 
= 1;
        pthread_join(threads[i],NULL);
        
    }
    
for(i = 0; i < 5; i++) {
        sem_destroy(
&sems[i]);
    }

    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
    philosophy 
*= arg;
    
    
while (1) {
        
if (sem_trywait(p->left) == 0) {
            
if (sem_trywait(p->right) == 0) {
                printf(
"红烧翅膀真好吃,我是%s\n",p->name);
                sem_post(p
->left);
                sem_post(p
->right);
                sleep(
1);

            } 

else {
                sem_post(p
->left);
            }
        }
        
if (p->is_stop == 1) {
            
break;
        }
    }
    pthread_exit(NULL);
}