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

thread同步测试

时间:2022-11-13 22:13:42浏览次数:43  
标签:同步 thread int number NUM 测试 pthread sem NULL

代码:

 

#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;
}

 

运行截图:

 

 

代码功能:生产者与消费者资源提供和消耗。

 

 

修改后代码:

 

#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;

void *producer ( void * arg )
{
    static int p = 0;

    for ( ;; ) {
        sem_wait( &blank_number );
        sem_wait( &mutex);
        queue[p] = rand() % 1000;
        printf("Product %d \n", queue[p]);
        p = (p+1) % NUM;
        sleep ( rand() % 5);
        sem_post(&mutex); 
        sem_post( &product_number );
    }
}
void *consumer ( void * arg )
{

    static int c = 0;
    for( ;; ) {
        sem_wait( &product_number );
        sem_wait(&mutex);
        printf("Consume %d\n", queue[c]);
        c = (c+1) % NUM;
        sleep( rand() % 5);
        sem_post(&mutex);
        sem_post( &blank_number );
    }
}

int main(int argc, char *argv[] )
{
    pthread_t pid, cid1,cid2,cid3,cid4,cid5;
    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( &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( 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,int,number,NUM,测试,pthread,sem,NULL
From: https://www.cnblogs.com/MRC-/p/16887135.html

相关文章

  • 非功能测试
    兼容性测试不同的操作系统、浏览器、分辨率下,软件的行为是否一致具体的兼容性测试由公司定义界面测试依据产品原型图或者UI设计图如果没有原型图,站在用户的角度来......
  • Pytest接口测试框架实战项目搭建(四)—— 业务系统接口请求演示
    一、前言前面相当于已经讲完整体框架搭建了,本篇主要讲述在实际业务系统的接口请求中,如何运用好该接口自动化测试框架。二、步骤演示1、在conf/api_path.py新增需要......
  • idea+maven+springboot如何配置Mybatis-plus并测试简单用例
    用例情况如上图,假设我们需要读取ywj数据库中的dept表中的记录 大致步骤1、首先,需要在对应maven工程的.pom文件中引入相应的依赖,包括mybatis-plus-boot-starter、mysq......
  • thread互斥测试
    thread互斥测试实践代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>//linux线程库#include<ctype.h>//测试和映射字符的库structarg_se......
  • iOS 启动优化测试
    APP的启动在iOS中,讨论的APP的启动可以分为2种:冷启动(ColdLaunch):从零开始启动APP热启动(WarmLaunch):APP已经在内存中,在后台存活着,再次点击图标启动APP主要是针对冷启......
  • 拿下阿里自动化测试岗23k*14薪offer的全程面试记录解析以及总结
    一、自我介绍面试官您好!我叫xx,来自深圳,毕业之后一直从事于软件测试的工作,有做过保险、金融、电商等项目;有做过做功能测试、接口测试,自动化测试,在工作中积极主动、可以独立的......
  • 【转行测试开发-HTML】(四)---对标签功能的一个重要补充
    之前写HTML基础知识时,不小心忽略掉了很多基础功能没有说,今天一并给大家补上。 1.HTML编辑工具   我现在使用的是sublimetext,如果未来会做前端开发,或者测试开发需......
  • thread同步测试
    任务详情1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后的代码......
  • (译)TDD(测试驱动开发)的5个步骤
    原文:5stepsoftest-drivendevelopmenthttps://developer.ibm.com/articles/5-steps-of-test-driven-development/作者GrantSteinfeld发表于2020年2月6日......
  • 【博学谷学习记录】超强总结,用心分享|Requests库以及集成UnitTest实现接口测试案例总
    一,介绍Requests库是一个基于python语言开发的一个开源的库,能够完全满足基于HTTP协议的接口测试。二,安装与验证Requests库的安装:在cmd窗口输入:pipinstallrequests......