⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 102.htm

📁 unix高级编程原吗
💻 HTM
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>123</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">               ● UNIX网络编程                       (BM: clown)                </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="54.htm">上一层</a>][<a href="103.htm">下一篇</a>]
<hr><p align="left"><small>发信人: clown (梧桐叶), 信区: UNP <br>

标  题: 信号量的例子--看医生 <br>

发信站: UNIX编程 (2001年10月20日21:59:12 星期六), 站内信件 <br>

  <br>

过程:医生初诊->化验血液->得到化验结果->医生确诊 <br>

这里只考虑一个病人的情况,创建了三个线程,分别用于表示医生 <br>

诊断,化验血液和得到血液结果。 <br>

关于信号量,第一个信号量可以省略。 <br>

  <br>

//gcc file.c -lpthread <br>

  <br>

#include <pthread.h> <br>

#include <semaphore.h> <br>

#include <errno.h> <br>

  <br>

sem_t prediag;/初诊 <br>

sem_t blood;//血液 <br>

sem_t bloodres;//血液结果 <br>

sem_t diag;//确诊 <br>

  <br>

void thread_blood(void); <br>

void thread_bloodres(void); <br>

  <br>

  <br>

int main() <br>

{ <br>

    pthread_t tid_blood, tid_bloodres; <br>

    errno = 0; <br>

  <br>

    if(sem_init(&prediag, 0, 1) == -1) { <br>

        perror("sem_init"); <br>

        exit(1); <br>

    } <br>

    if(sem_init(&blood, 0, 0) == -1) { <br>

        perror("sem_init error:"); <br>

        exit(1); <br>

    } <br>

    if(sem_init(&bloodres, 0, 0) == -1) { <br>

        perror("sem_init error:"); <br>

        exit(1); <br>

    } <br>

    if(sem_init(&diag, 0, 0) == -1) { <br>

        perror("sem_init error:"); <br>

        exit(1); <br>

    } <br>

  <br>

  <br>

    if(pthread_create(&tid_blood, NULL, (void *)&thread_blood, NULL) != 0) { <br>

        perror("create blood thread"); <br>

        exit(1); <br>

    } <br>

    if(pthread_create(&tid_bloodres, NULL, (void *)&thread_bloodres, NULL) != 0) <br>

 { <br>

        perror("create bloodres thread"); <br>

        exit(1); <br>

    } <br>

  <br>

    sem_wait(&prediag); <br>

    printf("pre diagnose, you need to check the blood\n"); <br>

    sem_post(&blood); <br>

    sem_wait(&diag); <br>

    printf("diagnosed, leaving ...\n"); <br>

    sem_post(&prediag); <br>

  <br>

    return 0; <br>

} <br>

  <br>

void thread_blood(void) <br>

{ <br>

{ <br>

    sem_wait(&blood); <br>

    printf("I'm blooding\n"); <br>

    sem_post(&bloodres); <br>

} <br>

  <br>

void thread_bloodres(void) <br>

{ <br>

    sem_wait(&bloodres); <br>

    printf("I've got the blood result\n"); <br>

    sem_post(&diag); <br>

} <br>

  <br>

  <br>

结果: <br>

pre diagnose, you need to check the blood <br>

I'm blooding <br>

I've got the blood result <br>

diagnosed, leaving ... <br>

  <br>

  <br>

  <br>

-- <br>



易朽的是生命,似那转瞬即谢的花朵;然而永存的,是对未来的渴望, <br>

是那生生世世传递下来的,不朽的,生的激情。每一朵勇敢开放的花, <br>

都是一个死亡唇边的微笑。 <br>

※ 修改:·clown 於 10月20日22:00:02 修改本文·[FROM: 202.114.1.61] <br>

※ 来源:·UNIX编程 apue.dhs.org·[FROM: 202.114.1.61] <br>

</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="54.htm">上一层</a>][<a href="103.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -