首页 > 其他分享 > thread同步测试

thread同步测试

时间:2022-11-13 01:00:17浏览次数:34  
标签:同步 thread create blank number 测试 pthread sem NULL

thread同步测试

任务详情
1 编译运行附件中的代码,提交运行结果截图,并说明程序功能
2 修改代码,把同步资源个数减少为3个,把使用资源的线程增加到 (你的学号%3 + 4)个,编译代码,提交修改后的代码和运行结果截图。

原代码:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>

#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;

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 )
{

	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;
    
	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 );
	return 0;
}

运行:

  • 程序功能:生产者消费者模型,假定有两个线程,一个消费者线程,一个生产者线程。一个模拟生产者行为,一个模拟消费者行为。两个线程同时操作一个共享资源(一般称之为汇聚),生产向其中添加产品,消费者从中消费掉产品。
    改代码,把同步资源个数减少为3个,把使用资源的线程增加到 (你的学号%3 + 4)个
    20201228%3+4=6
    之后运行:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>

#define NUM 3
int queue[NUM];
sem_t blank_number, product_number;

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() % 6);
		sem_post( &product_number );
	}
}
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,cid1,cid2,cid3,cid4,cid5;
    
    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_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_create( &cid5, NULL, consumer, NULL);
    pthread_join( pid, NULL );
    pthread_join( cid, NULL );
    pthread_join( cid, NULL );
    pthread_join( cid1, NULL );
    pthread_join( cid2, NULL );
    pthread_join( cid3, NULL );
    pthread_join( cid4, NULL );
    pthread_join( cid5, NULL );
    sem_destroy( &blank_number );
    sem_destroy( &product_number );
    return 0;
}

标签:同步,thread,create,blank,number,测试,pthread,sem,NULL
From: https://www.cnblogs.com/jiangcun/p/16885257.html

相关文章

  • 第5章Spring 事务(测试)-Spring的事务注解(小项目中),AspectJ的AOP配置管理事务(大项目中)
    第5章Spring事务(测试)-Spring的事务注解(小项目中),AspectJ的AOP配置管理事务(大项目中)spring框架中提供的事务处理方案适合中小项目使用的,注解方案。1.适合中小项目使用......
  • C# 文件同步工具(复制带进度显示)
    usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Text;namespaceSyncTool{internalc......
  • Spring 事务(测试)--在这个笔记中记录的是没有添加事务,数据库返回的效果。
    第5章Spring事务(测试)--在这个笔记中记录的是没有添加事务,数据库返回的效果。1.首先搞两张表,商品表和订单表举例:购买商品trans_sale项目本例要实现购买商品,模拟用......
  • datax同步数据java简单用法
    1.到github下载源码,自己编译。同步数据支持mysql8.0,如果直接用编译好的会遇到各种问题。https://github.com/alibaba/DataX/blob/master/userGuid.mdidea导入项目,需要先......
  • 04-测试领域实践
    测试环境管理文档完善的测试环境能让自动化任务的执行没有“后顾之忧”测试环境管理不单是某个部门、某个角色的事,每个人即是测试环境的使用者,更是测试环境的建设者基准......
  • 从软件工程角度看测试
    转载:https://www.cnblogs.com/imyalost/p/16866157.html这是软件工程系列的第六篇文章,我想从软件工程的角度来谈谈关于软件测试的一些话题。 软件工程的核心软件工程......
  • 测试架构需要具备哪些能力
    转载:https://www.cnblogs.com/imyalost/p/16834322.html这篇文章是软件工程系列知识总结的第五篇,同样我会以自己的理解来阐述软件工程中关于架构设计相关的知识。相比于......
  • Pytest接口测试框架实战项目搭建(三)—— 统一登录处理
    一、前言业务系统的登录均要经过统一登录系统S,本篇演示2个业务系统的登录,一个是内部业务系统C,一个是外部用户使用的系统W,因为账号密码以及headers信息都不一样,所以要......
  • Linux系统中线程同步方式中的条件变量操作方法
      大家好,今天主要和大家聊一聊,如何使用Linux中线程同步方式中的条件变量。    第一:条件变量基本简介   条件变量是线程可用的另一种同步机制,条件变量用于自动阻......
  • 软件产品测试之压力测试
    压力测试是一种性能测试方法,通过迫使软件处于极端情况发现性能瓶颈。在极端情况下软件更容易暴露出性能问题。因此采用压力测试分析实时软件的性能以保障它的质量。其实就......