开张hoho,贴个小程序

原创 2005年06月01日 12:34:00

以下代码参考《现代操作系统》英文第2版P127哲学家就餐问题例子程序,在windowsXP下使用VC6.0进行了实现,希望大家多提改正意见:

#include "windows.h"

#include <stdio.h>
#include <stdlib.h>

#define PHILO_NUM 5     /* how many philosophers */
#define THINKING 0
#define HUNGRY  1
#define EATING  2
#define DELAY_THINK 500
#define DELAY_EAT 400
#define STRING_LEN 7
#define LEFT(i)  (i+PHILO_NUM-1)%PHILO_NUM
#define RIGHT(i) (i+1)%PHILO_NUM

typedef struct
{
 int i;
}INTEGER;

/* global variables */
int state[PHILO_NUM];
HANDLE h_mutex;       /* Semaphore for exclusion */
HANDLE h_s[PHILO_NUM];     /* Semaphore for philosophers */

/* definition */
void philosopher(void* id)
{

 DWORD wait_for_mutex;
 DWORD wait_for_self;
 int nid;

 nid = ((INTEGER*) id)->i;
 while(TRUE)
 {
  printf("Philosopher%d is thinking.../n", nid);
  Sleep(DELAY_THINK);
  /* He wants to eat. */
  wait_for_mutex = WaitForSingleObject(h_mutex, -1);
  state[nid] = HUNGRY;
  if (state[nid] == HUNGRY && state[LEFT(nid)] != EATING && state[RIGHT(nid)] != EATING)
  {
   state[nid] = EATING;
   ReleaseSemaphore(h_s[nid], 1, NULL);
  }
  ReleaseMutex(h_mutex);
  wait_for_self = WaitForSingleObject(h_s[nid], -1);
  
  printf("Philosopher%d is eating.../n", nid);
  Sleep(DELAY_EAT);
  /* He finishes eating. */
  wait_for_mutex = WaitForSingleObject(h_mutex, -1);
  state[nid] = THINKING;
  if (state[LEFT(nid)] == HUNGRY && state[LEFT(LEFT(nid))] != EATING && state[RIGHT(LEFT(nid))] != EATING)
  {
   state[LEFT(nid)] = EATING;
   ReleaseSemaphore(h_s[LEFT(nid)], 1, NULL);
  }
  if (state[RIGHT(nid)] == HUNGRY && state[LEFT(RIGHT(nid))] != EATING && state[RIGHT(RIGHT(nid))] != EATING)
  {
   state[RIGHT(nid)] = EATING;
   ReleaseSemaphore(h_s[RIGHT(nid)], 1, NULL);
  }
  ReleaseMutex(h_mutex);
 }
}


int main(void)
{
 DWORD thread_ID;
 DWORD wait_for_all;
 HANDLE h_thread[PHILO_NUM];
 INTEGER intNums[PHILO_NUM];

 int i;
 char str_name[STRING_LEN];

 /* initializing... */
 h_mutex = CreateMutex(NULL, FALSE, "mutex_for_state_array");
 h_mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, "mutex_for_state_array");

 memset(str_name, 0, STRING_LEN * sizeof(char));
 for (i = 0; i < PHILO_NUM; i++)
 {
  sprintf(str_name, "philo%d", i);
  h_s[i] = CreateSemaphore(NULL, 0, 1, str_name);
  h_s[i] = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, str_name);
 }

 for (i = 0; i < PHILO_NUM; i++)
 {
  intNums[i].i = i;
  h_thread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(philosopher), &intNums[i], 0, &thread_ID);
 }

 wait_for_all = WaitForMultipleObjects(PHILO_NUM, h_thread, TRUE, -1);
 return 0;
}

第一款小应用顺利通过app store审核 HOHO

研究苹果开发已经有三个月了idp申请下来也有一个月了每年99美元啊。。。不能浪费所以上星期花了两个晚上整了个小应用传到了app store 上周五晚上整的,看网上教程说审核大概要2-3周,所以定了发布...

某网友总结的grep、sed、awk、perl等对正则表达式的支持的差别,谁给贴个网址

grep 2.5.1 egrep 2.5.1 sed 3.02 sed 4.07 awk 3.1.1 perl 5.8.0 vim 6.1 JavaScript ...
  • morem
  • morem
  • 2012年09月21日 10:13
  • 424

创业街分店开张啦---原型模式

前情提要上集讲到, 小光请来堂哥大龙作为自己的代理与饮品供应商谈判, 最终大龙用自己丰富的商场经验帮小光拿到合适的价格.小光也是尝到了代理的甜头, 开始将店里的更多工作交给表妹来打理, 自己腾出功夫去...

我的CSDN博客开张了~

大家好! 本人于2015年7月告别了自己的学生生涯,硕士毕业,开始融入社会参加工作,就职于一家自动化软件公司从事软件研发工作。 工作中遇到种种技术问题,我不断地查缺补漏,踏踏实实地干活。知识点千千...

淘宝新店开张没人气没流量没生意怎么办

淘宝新店才开一定要多学习,现在网络骗子很多,最好找个师傅带带这样会少走很多弯路的,...

闲的蛋疼 贴个代码

//FileList.java package keywords; /* * fuction: returning the file list of the folder * * by: G...

贴个自个用的vimrc

"~/.vimrc (configuration file for vim only) " Encoding related set fileencodings=utf-8,gb2312,gbk,g...

POJ 2051 Argus 堆排序 贴个模板

#include #include #include #include #include #include #include #pragma comment(linker, "/STA...

带你快速玩转canvas(3)贴个图片,写点字

5. 将图片绘制到画布之上解释:1. 简单来说,就是将一张图的某个区域,画到画布上的某个区域; 2. 和ctx.fillStyle = pattern不同的是,pattern是将图片作为背景,该背景位...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:开张hoho,贴个小程序
举报原因:
原因补充:

(最多只允许输入30个字)