易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Linux网络编程:生产者消费者问题
Linux网络编程:生产者消费者问题
/*************************************************
* File name :
* Description :
* Author : sg131971@qq.com
* Version : V1.0
* Date :
* Compiler : arm-linux-gcc-4.4.3
* Target : mini2440(Linux-2.6.32)
* History :
* <author> <time> <version > <desc>
*************************************************/
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
void
*producter_f(
void
*arg);
void
*consumer_f(
void
*arg);
int
buffer_has_item = 0;
sem_t sem;
int
running = 1;
/*************************************************
* Function :
* Description :
* Calls :
* Called By :
* Input :
* Output :
* Return :
*************************************************/
int
main(
void
)
{
pthread_t consumer_t;
pthread_t producter_t;
sem_init(&sem, 0, 16);
pthread_create(&producter_t, NULL, (
void
*)producter_f, NULL);
pthread_create(&consumer_t, NULL, (
void
*)consumer_f, NULL);
sleep(1);
running = 0;
pthread_join(consumer_t, NULL);
pthread_join(producter_t, NULL);
sem_destroy(&sem);
return
0;
}
/*************************************************
* Function :
* Description :
* Calls :
* Called By : main
* Input :
* Output :
* Return :
*************************************************/
void
*producter_f(
void
*arg)
{
int
semval = 0;
while
(running)
{
usleep(10);
sem_post(&sem);
sem_getvalue(&sem, &semval);
printf(
"生产,总数量:%d "
, semval);
}
}
/*************************************************
* Function :
* Description :
* Calls :
* Called By : main
* Input :
* Output :
* Return :
*************************************************/
void
*consumer_f(
void
*arg)
{
int
semval = 0;
while
(running)
{
usleep(1);
sem_wait(&sem);
sem_getvalue(&sem, &semval);
printf(
"消费,总数量:%d "
, semval);
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图