Thread同步测试
代码
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;
//信号量的数据类型为结构sem_t,本质上是长整型的数
void *producer ( void * arg )
{
static int p = 0;
for ( ;; ) {
sem_wait( &blank_number );
//sem_wait是一个函数,也是一个原子操作,它的作用是从信号量的值减去一个“1”,但它永远会先等待该信号量为一个非零值才开始做减法。
queue[p] = rand() % 1000;
printf("Product %d \n", queue[p]);
p = (p+1) % NUM;
sleep ( rand() % 5);
//执行挂起一段时间
sem_post( &product_number );
//sem_post是给信号量的值加上一个“1”,它是一个“原子操作”---即同时对同一个信号量做加“1”操作的两个线程是不会冲突的;而同时对同一个文件进行读和写操作的两个程序就有可能会引起冲突。
}
}
void *consumer ( void * arg )
{
static int c = 0;
for( ;; ) {
sem_wait( &product_number );
printf("Consume %d\n", queue[c]);
c = (c+1) % NUM;
sleep( rand() % 5 );
sem_post( &blank_number );
}
}
int main(int argc, char *argv[] )
{
pthread_t pid, cid;
//声明线程ID
sem_init( &blank_number, 0, NUM );
//初始化信号量
sem_init( &product_number, 0, 0);
//初始化信号量
pthread_create( &pid, NULL, producer, NULL);
//创建线程
pthread_create( &cid, NULL, consumer, NULL);
//创建线程
pthread_join( pid, NULL );
pthread_join( cid, NULL );
//获取某个线程执行结束后返回的数据,阻塞调用它的线程,直至目标线程执行结束。
sem_destroy( &blank_number );
sem_destroy( &product_number );
//销毁sem指向的匿名信号量
return 0;
}
运行截图
修改代码,把同步资源个数减少为3个,把使用资源的线程增加到 (你的学号%3 + 4)个,编译代码,提交修改后的代码和运行结果截图。
线程数:20201317%3+4=5,即消费者线程增加到个数为5,且需要互斥地去访问产品(最多为三个),1一个生产者线程,5个消费者线程。
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM 3
int queue[NUM];
sem_t blank_number, product_number,mutex;
int c = 0;
void *producer ( void * arg )//生产者
{
static int p = 0;
for ( ;; ) {
sem_wait( &blank_number );//空格子锁
queue[p] = rand() % 1000;
printf("Product %d \n", queue[p]);
p = (p+1) % NUM;
sleep ( rand() % 5);
sem_post( &product_number );
}
}
void *consumer ( void * arg )//消费者
{
for( ;; ) {
sem_wait(&mutex);
sem_wait( &product_number );
printf("Consume %d\n", queue[c]);
c = (c+1) % NUM;
sleep( rand() % 5 );
sem_post( &blank_number );
sem_post( &mutex);
}
}
int main(int argc, char *argv[] )
{
pthread_t pid, cid,cid1,cid2,cid3,cid4;
sem_init( &blank_number, 0, NUM );
sem_init( &product_number, 0, 0);
sem_init( &mutex, 1, 1);
pthread_create( &pid, NULL, producer, NULL);
pthread_create( &cid, NULL, consumer, NULL);
pthread_create( &cid1, NULL, consumer, NULL);
pthread_create( &cid2, NULL, consumer, NULL);
pthread_create( &cid3, NULL, consumer, NULL);
pthread_create( &cid4, NULL, consumer, NULL);
pthread_join( pid, NULL );
pthread_join( cid, NULL );
pthread_join( cid1, NULL );
pthread_join( cid2, NULL );
pthread_join( cid3, NULL );
pthread_join( cid4, NULL );
sem_destroy( &blank_number );
sem_destroy( &product_number );
return 0;
}
原文地址:http://www.cnblogs.com/lyxhhz/p/16880324.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性