#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. Hadoop HA 高可用集群搭建

    一.首先配置集群信息 vi /etc/hosts 二.安装zookeeper 1.解压至/usr/hadoop/下 .tar.gz -C /usr/hadoop/ 2.进入/usr/hadoop/zo ...

  2. linux-2.6.22.6内核启动分析之配置

    配置过程最终结果是生成.config文件,我们想要对配置的目的有很清楚的了解,必须先对.config文件进行分析.通过cd命令切换到linux-2.6.22.6内核目录,输入vi .config 可以 ...

  3. 树莓3B+_root密码开启

    开启root用户的方法:1.设置密码:sudo passwd2.sudo passwd --unlock root3.root用户登录:su

  4. Linux下C语言编译的问题

    在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于undefi ...

  5. 用GO写一个区块链

    总结下最近用GO实现区块链实现下面的模块 基本原型 工作量证明,这里用的POW 持久化和命令行,这里用的BoltDB存储区块 地址,这里用的比特币的地址方案 交易 P2P网络,这里为方便本地调试,采用 ...

  6. Asp.Net Core跨域配置

    在没有设置跨域配置的时候,Ajax请求时会报以下错误 已拦截跨源请求:同源策略禁止读取位于 http://localhost:5000/Home/gettime 的远程资源.(原因:CORS 头缺少 ...

  7. 2017-2018-1 20155338《信息安全技术》实验二——Windows口令破解

    2017-2018-1 20155338<信息安全技术>实验二--Windows口令破解 一.试验环境 系统环境:Windows 实验工具: LC5 SuperDic 二.实验内容及要求 ...

  8. virsh常用维护命令

    virsh常用命令 一些常用命令参数 [root@kvm-server ~]# virsh --help                                     #查看命令帮忙 [ro ...

  9. linux安装PHP-memcache-redis扩展

    1.php memcache 扩展 http://pecl.php.net/package/memcache/3.0.8 下载文件源码 #tar zxvf memcache-3.0.8.tar#/us ...

  10. ES6中的promise

    Promise 对象用于一个异步操作的最终完成(或失败)及其结果值的表示.简单点说,它就是用于处理异步操作的,异步处理成功了就执行成功的操作,异步处理失败了就捕获错误或者停止后续操作. 它的一般表示形 ...