Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2649036
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2008-12-29 16:10:06

一、实验任务
1、在WINDOWS 2000环境下,创建一个控制台进程,此进程包括2个线程:生产者线程和消费者线程。
2、用信号量机制解决进程(线程)的同步与互斥问题。
二、实验目的
1.掌握基本的同步互斥算法,理解生产者和消费者模型。
2.了解Windows 2000/XP中多线程的并发执行机制,线程间的同步和互斥。
3.学习使用Windows 2000/XP中基本的同步对象,掌握相应的API。
三、实验要求
1.生产者消费者对缓冲区进行互斥操作。
2.缓冲区大小为10,缓冲区满则不允许生产者生产数据,缓冲区空则不允许消费者消费数据。
3.生产者消费者各循环操作10次。
四、设计思路和采取的方案
1.利用windows提供的API函数CreateSemaphore()创建信号量对象;CreateThread()创建线程;WaitForSingleObject()执行P操作;ReleaseSemaphore()执行V操作;WaitForMultipleObjects()主进程等待线程的结束等函数进行设计。
2.在Windows中,常见的同步对象有:信号量(Semaphore)、互斥量(Mutex) 。使用这些对象都分为三个步骤,一是创建或者初始化;接着请求该同步对象,随即进入临界区,这一步对应于互斥量的上锁;最后释放该同步对象,这对应于互斥量的解锁。这些同步对象在主进程中创建,在其子线程中都可
五、程序源代码及注释
#include
#include
#include

HANDLE empty_Semaphore;//设置信号量用。empty_Semaphore表示空的缓冲池的数量
HANDLE full_Semaphore;//用full_Semaphore表示满的缓冲池的数量
HANDLE mutex_Semaphore;//用mutex_Semaphore表示互斥信号量

void Producer(void )//创建生产者进程
{
        for (int i=1;i<10;i++)
        {
      WaitForSingleObject(empty_Semaphore,-1);
//对empty_Semaphore进行P操作
      WaitForSingleObject(mutex_Semaphore,-1);
//对mutex_Semaphore进行P操作
          
          printf("生产者%d准备生产 \n",i);
   printf("生产者%d开始往缓冲区中写数据... \n",i);
//生产者生产产品
   printf("生产者%d开始退出缓冲区... \n\n",i);

      ReleaseSemaphore(mutex_Semaphore,1,NULL);
//对mutex_Semaphore进行V操作
      ReleaseSemaphore(full_Semaphore,1,NULL);
//对full_Semaphore进行V操作
        }       
   return ;
}

void Consumer(void )//创建消费者进程
{
        for (int j=1;j<10;j++)
        {
       WaitForSingleObject(full_Semaphore,-1);
//对full_Semaphore进行P操作
       WaitForSingleObject(mutex_Semaphore,-1);
//对mutex_Semaphore进行P操作

       printf("消费者%d准备消费 \n",j);
           printf("消费者%d开始消费缓冲区中数据\n",j);//消费者消费产
       printf("消费者%d开始开始退出缓冲区... \n\n",j);

       ReleaseSemaphore(mutex_Semaphore,1,NULL);
//对mutex_Semaphore进行V操作
       ReleaseSemaphore(empty_Semaphore,1,NULL);
//对empty_Semaphore进行V操作
        }
  return;
}

void main ()
{
        HANDLE Data[2];//创建HANDLE数组Data
        empty_Semaphore=CreateSemaphore(NULL,10,10,NULL);//创建信号量empty_Semaphore
        full_Semaphore=CreateSemaphore(NULL,0,10,NULL);//创建信号量full_Semaphore
        mutex_Semaphore=CreateSemaphore(NULL,1,1,NULL);//创建互斥信号量mutex_Semaphore

        HANDLE handle[2];//创建HANDLE数组handle
        //以下是创建生产着和消费者进程
        handle[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Producer),&Data[0],0,NULL);
        if (handle[0]==NULL)
        {
                printf("创建生产者线程失败 \n");
        }
       
        handle[1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Consumer),&Data[1],0,NULL);
        if (handle[0]==NULL)
        {
                printf("创建消费者线程失败 \n");
        }
       
     WaitForMultipleObjects(2,handle,TRUE,-1);//主进程等待线程的结束
        return;
}


/**************************************************************/
#include "stdafx.h"
#define MAXNUM 12
HANDLE empty_Semaphore;//设置信号量用。empty_Semaphore表示空的缓冲池的数量
HANDLE full_Semaphore;//用full_Semaphore表示满的缓冲池的数量
HANDLE mutex_Semaphore;//用mutex_Semaphore表示互斥信号量

/*mutex_Semaphore控制的是流程,当A完成之后才进行B的操作,而不是A、B操作的数据*/
void Producer(void )//创建生产者进程
{
 for (static int i=1;i {
  WaitForSingleObject(empty_Semaphore,-1);
  //对empty_Semaphore进行P操作
  WaitForSingleObject(mutex_Semaphore,-1);
  //对mutex_Semaphore进行P操作
  printf("生产者%d准备生产 \n",i);
  printf("生产者%d开始往缓冲区中写数据... \n",i);
  //生产者生产产品
  printf("生产者%d开始退出缓冲区... \n\n",i);
  ReleaseSemaphore(mutex_Semaphore,1,NULL);
  //对mutex_Semaphore进行V操作
  ReleaseSemaphore(full_Semaphore,1,NULL);
  //对full_Semaphore进行V操作
 }       
 return ;
}
void Consumer(void )//创建消费者进程
{
 //return;
 for (static int j=1;j {
  WaitForSingleObject(full_Semaphore,-1);
  //对full_Semaphore进行P操作
  WaitForSingleObject(mutex_Semaphore,-1);
  //对mutex_Semaphore进行P操作
  printf("消费者%d准备消费 \n",j);
  printf("消费者%d开始消费缓冲区中数据\n",j);//消费者消费产
  printf("消费者%d开始开始退出缓冲区... \n\n",j);
  ReleaseSemaphore(mutex_Semaphore,1,NULL);
  //对mutex_Semaphore进行V操作
  ReleaseSemaphore(empty_Semaphore,1,NULL);
  //对empty_Semaphore进行V操作
 }
 return;
}
void main ()
{
 HANDLE Data[4];//创建HANDLE数组Data
 empty_Semaphore=CreateSemaphore(NULL,5,5,NULL);//创建信号量empty_Semaphore
 full_Semaphore=CreateSemaphore(NULL,0,5,NULL);//创建信号量full_Semaphore
 mutex_Semaphore=CreateSemaphore(NULL,1,1,NULL);//创建互斥信号量mutex_Semaphore
 HANDLE handle[4];//创建HANDLE数组handle
 //以下是创建生产着和消费者进程
 handle[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Producer),&Data[0],0,NULL);
 handle[2]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Producer),&Data[2],0,NULL);
 if (handle[0]==NULL||handle[2]==NULL)
 {
  printf("创建生产者线程失败 \n");
 }
 handle[1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Consumer),&Data[1],0,NULL);
 handle[3]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(Consumer),&Data[3],0,NULL);
 if (handle[0]==NULL||handle[3]==NULL)
 {
  printf("创建消费者线程失败 \n");
 }
 WaitForMultipleObjects(4,handle,TRUE,-1);//主进程等待线程的结束
 return;
}

**********************************************************************
用于实现多生产者,单消费者的同步问题
//新建信号量
hSemaphore =  CreateSemaphore(NULL,1,1,"MUSEX");//控制信号量
hStored = CreateSemaphore(NULL,0,100,"Stored");//读控制信号量
hEmpty = CreateSemaphore(NULL,100,100,"Empty");//写控制信号量

//读数据
readRecord()
{
  WaitForSingleObject(hStored,INFINITE);
  WaitForSingleObject(hSemaphore,INFINITE);
  //读操作
  ReleaseSemaphore(hSemaphore,1,NULL);
  ReleaseSemaphore(hEmpty,1,NULL);
}

//写数据
writeRecord()
{
  WaitForSingleObject(hEmpty,INFINITE);
  WaitForSingleObject(hSemaphore,INFINITE);
  //写数据操作
  ReleaseSemaphore(hSemaphore,1,NULL);
  ReleaseSemaphore(hStored,1,NULL);
}
阅读(2706) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-06-14 11:08:26

有错误,生产者消费者进程里的循环本身已经限制了多个生产者不会同时进入临界区。

chinaunix网友2009-05-26 14:56:04

非常好