#include <STDIO.H>
#include <windows.h>
//#include "stdafx.h"
#include <process.h> // _beginthread && _endthread
//#include "BaseOperation.h"
#define N 10 typedef int semaphore; /* 信号量是一种特殊的整型变量 */ semaphore mutex=; /* 互斥访问 */
semaphore empty=N; /* 记录缓冲池中空的缓冲区数 */
semaphore full=; /* 记录缓冲池中满的缓冲区数*/ semaphore buf[N]; /* 有N个缓冲区数的缓冲池buf[N],并实现循环缓冲队列 */
semaphore in=, out=; void p(semaphore *x) /* p操作 */
{
*x=(*x)-;
} void v(semaphore *y) /* v操作 */
{
*y=(*y)+;
} void produce_item(int *item_ptr)
{
/*printf("produce an item\n");*/
*item_ptr='F'; /* 'F' is "Full满" */
} void enter_item(int x)
{
in=(out+)%N;
buf[in]=x;
printf("enter_item %c to buf[%d]\n", buf[in], in);
} void remove_item(int *yy)
{
out=(out+)%N;
printf("remove_item %c from buf[%d]", buf[out], out);
*yy=buf[out];
buf[out]='E'; /* 'E' is "Empty空" */
printf(" so the buf[%d] changed to empty--%c\n", out, buf[out]);
}
void consume_item(int y)
{
printf("cosume the item :the screem print %c\n", y);
} void producer(void);
void consumer(void);
int item;
DWORD WINAPI ThreadProc1( LPVOID lpParam )
{ int i=,j=;
while()
{
produce_item(&item); //延时
for(i=;i<;i++)
{
;
}
}
}
/* 生产者 */ DWORD WINAPI ThreadProc2( LPVOID lpParam )
{ int i=,j=;
while()
{
consumer(); //延时
for(i=;i<;i++)
{
;
}
}
} void producer(void)
{ while(){
//_beginthread(produce_item(&item),0,NULL);
CreateThread(
NULL, // default security attributes
, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
, // use default creation flags
NULL); // returns the thread identifier
p(&empty); /* 递减空缓冲区数 */
p(&mutex); /* 进入临界区 */
enter_item(item); /* 将一个新的数据项放入缓冲池 */
v(&mutex); /* 离开临界区 */
v(&full); /* 递增满缓冲区数 */
if(full==N) /* 若缓冲池满的话,唤醒消费者进程 */
CreateThread(
NULL, // default security attributes
, // use default stack size
ThreadProc2, // thread function
NULL, // argument to thread function
, // use default creation flags
NULL); // returns the thread identifier
}
} /* 消费者 */
void consumer(void)
{
int get_item; while(){
p(&full); /* 递减满缓冲区数 */
p(&mutex); /* 进入临界区 */
remove_item(&get_item); /* 从缓冲池中取走一个数据项 */
v(&mutex); /* 离开临界区 */
v(&empty); /* 递增空缓冲区数 */
consume_item(get_item); /* 对数据项进行操作(消费)*/
if(empty==N) /* 若缓冲池全空的话,唤生产者进程 */
producer();
}
} /* 调用生产者-消费者进程实现进程间同步 */
main()
{
producer(); return ;
}
 #include <STDIO.H>
#include <windows.h>
//#include "stdafx.h"
#include <process.h> // _beginthread && _endthread
//#include "BaseOperation.h"
#define N 10 typedef int semaphore; /* 信号量是一种特殊的整型变量 */ semaphore mutex=; /* 互斥访问 */
semaphore empty=N; /* 记录缓冲池中空的缓冲区数 */
semaphore full=; /* 记录缓冲池中满的缓冲区数*/ semaphore buf[N]; /* 有N个缓冲区数的缓冲池buf[N],并实现循环缓冲队列 */
semaphore in=, out=; void p(semaphore *x) /* p操作 */
{
*x=(*x)-;
} void v(semaphore *y) /* v操作 */
{
*y=(*y)+;
} void produce_item(int *item_ptr)
{
/*printf("produce an item\n");*/
*item_ptr='F'; /* 'F' is "Full满" */
} void enter_item(int x)
{
in=(out+)%N;
buf[in]=x;
printf("enter_item %c to buf[%d]\n", buf[in], in);
} void remove_item(int *yy)
{
out=(out+)%N;
printf("remove_item %c from buf[%d]", buf[out], out);
*yy=buf[out];
buf[out]='E'; /* 'E' is "Empty空" */
printf(" so the buf[%d] changed to empty--%c\n", out, buf[out]);
}
void consume_item(int y)
{
printf("cosume the item :the screem print %c\n", y);
} void producer(void);
void consumer(void);
int item;
DWORD WINAPI ThreadProc1( LPVOID lpParam )
{ int i=,j=;
while()
{
produce_item(&item); //延时
Sleep();
}
}
/* 生产者 */ DWORD WINAPI ThreadProc2( LPVOID lpParam )
{ int i=,j=;
while()
{
consumer(); //延时
Sleep();
}
} void producer(void)
{ while(){
//_beginthread(produce_item(&item),0,NULL);
CreateThread(
NULL, // default security attributes
, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
, // use default creation flags
NULL); // returns the thread identifier
p(&empty); /* 递减空缓冲区数 */
p(&mutex); /* 进入临界区 */
enter_item(item); /* 将一个新的数据项放入缓冲池 */
v(&mutex); /* 离开临界区 */
v(&full); /* 递增满缓冲区数 */
if(full==N) /* 若缓冲池满的话,唤醒消费者进程 */
CreateThread(
NULL, // default security attributes
, // use default stack size
ThreadProc2, // thread function
NULL, // argument to thread function
, // use default creation flags
NULL); // returns the thread identifier
}
} /* 消费者 */
void consumer(void)
{
int get_item; while(){
p(&full); /* 递减满缓冲区数 */
p(&mutex); /* 进入临界区 */
remove_item(&get_item); /* 从缓冲池中取走一个数据项 */
v(&mutex); /* 离开临界区 */
v(&empty); /* 递增空缓冲区数 */
consume_item(get_item); /* 对数据项进行操作(消费)*/
if(empty==N) /* 若缓冲池全空的话,唤生产者进程 */
producer();
}
} /* 调用生产者-消费者进程实现进程间同步 */
main()
{
producer(); return ;
}

多线程PV的更多相关文章

  1. 多线程同步内功心法——PV操作上(未完待续。。。)

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  2. Java入门到精通——基础篇之多线程实现简单的PV操作的进程同步

    Java入门到精通——基础篇之多线程实现简单的PV操作的进程同步 一.概述     PV操作是对信号量进行的操作.     进程同步是指在并发进程之间存在一种制约关系,一个进程的执行依赖另一个进程的消 ...

  3. 多线程面试题系列(12):多线程同步内功心法——PV操作上

    上面的文章讲解了在Windows系统下实现多线程同步互斥的方法,为了提高在实际问题中分析和思考多个线程之间同步互斥问题的能力,接下来将讲解PV操作,这也是操作系统中的重点和难点.本文将会先简要介绍下P ...

  4. Java—多线程实现PV效果

    前言 还记得今年参加自学操作系统考试,最难分析的就是PV这部分,然而伟大的米老师却用一个放东西吃东西的小例子,把PV讲的栩栩如生,言简意赅.学J2SE时学到了线程部分,里面提到了线程同步,死锁问题等等 ...

  5. windows多线程(九) PV原语分析同步问题

    一.PV原语介绍 PV原语通过操作信号量来处理进程间的同步与互斥的问题.其核心就是一段不可分割不可中断的程序. 信号量的概念1965年由著名的荷兰计算机科学家Dijkstra提出,其基本思路是用一种新 ...

  6. 转---秒杀多线程第十二篇 多线程同步内功心法——PV操作上 (续)

    PV操作的核心就是 PV操作可以同时起到同步与互斥的作用. 1.同步就是通过P操作获取信号量,V操作释放信号量来进行. 2.互斥其实就是,同时操作P操作,结束后进行V操作即可做到. Java上实现PV ...

  7. [多线程同步练习]PV操作

    看一个较为复杂的生产者-消费者问题: 问题描述 桌子上有一只盘子,每次只能向其中放入一个水果.爸爸专向盘子中放苹果,妈妈专向盘子中放橘子,儿子专等吃盘子中的橘子,女儿专等吃盘子中的苹果.只有盘子为空时 ...

  8. iOS GCD NSOperation NSThread等多线程各种举例详解(拷贝)

    2年多的iOS之路匆匆而过,期间也拜读来不少大神的博客,近来突然为自己一直做伸手党感到羞耻,是时候回馈社会.回想当年自己还是小白的时候,照着一些iOS多线程教程学,也只是照抄,只知其然.不知其所以然. ...

  9. 【五子棋AI循序渐进】——多线程搜索

    关于多线程搜索,有很多方法来实现,很多文章推荐基于MTD(F)的方式.好处不言而喻,不过我的程序中采用的是基于PVS的多线程搜索.实现起来主要是这几个方面问题需要解决: 1.置换表的互斥访问. 2.局 ...

随机推荐

  1. Intelx86数据手册读书笔记---1

    1. 第一章 a. 符号约定 a1. 字节顺序 a2. 保留的比特位和软件兼容性 a3. 指令操作数 a4. 十六进制和二进制数 a5. 分段地址 a. 符号约定 a1. 字节顺序 intel的32和 ...

  2. 20155307 2016-2017第三次《Java程序设计》课堂实践项目

    码云链接 我使用电脑端登录的云班课,截图已经放上去了,却忘了点提交.

  3. [arc062E]Building Cubes with AtCoDeer

    Description 传送门 Solution 这道题直接暴力就好..毕竟只要枚举了前后两个瓷砖的方向和编号,其他瓷砖的颜色就是确定的了. 然而场上我的去重除了问题qaq. 我们钦定在立方体最前面的 ...

  4. 4361: isn

    4361: isn https://lydsy.com/JudgeOnline/problem.php?id=4361 分析: dp+容斥. 首先计算出每个长度有多少种子序列是非降的.这一步可以$n^ ...

  5. spark读取mongodb数据写入hive表中

    一 环境: spark-: hive-; scala-; hadoop--cdh-; jdk-1.8; mongodb-2.4.10; 二.数据情况: MongoDB数据格式{    "_i ...

  6. mongdb基础

    查看数据库 show dbs; 切换到需要使用的数据库:use local; 显示集合中的数据库表:show collections; 切换到数据库test,如果数据库不存在,则自动创建 插入数据 d ...

  7. 监听Entity Framework生成的Sql语句

               Entity Framework为我们提供了很大的方便,但有时候,我们想看看EF生成的Sql语句到底是怎样的,一种方式是我们可以启用Sql Server Profer工具.今天介 ...

  8. C#是数据类型

    C#又开始了 开始数据类型 用的软件是VS2017 E short 短整型 int  中等整型 long  长整形 string  字符串类型 bool  布尔类型(true/flase)  相当于数 ...

  9. Android Studio 3.1.2 Device File Explorer nothing to show

    Android Studio 3.1.2 Device File Explorer nothing to  show 不显示 目录  ,空白 手持终端设备: Android  4.2.2  ,API1 ...

  10. ffmpeg 压缩H265 Windows 硬件编码

    硬件NVIDIA:ffmpeg.exe -i input.avi -c:v hevc_nvenc -preset:v fast output.mp4 软件          :ffmpeg.exe - ...