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

thread同步测试

时间:2022-11-13 17:01:54浏览次数:43  
标签:同步 thread number 信号量 include 测试 pthread sem NULL

任务详情

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


#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.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;
}

运行结果

程序实现的是生产者生成资源,消费者取走资源。


修改代码后

20201229%3+4=4,我们需要创建4个消费者线程。故我们需要添加互斥信号量mutex,加上头文件#include<unistd.h>,另外PV操作都要相应地加上对mutex互斥信号量的操作。创建线程是pthread_create( &cid1, NULL, consumer, NULL)。


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

#define NUM 3//资源的数量为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);//占用互斥信号量,互斥信号量-1
	    queue[p] = rand() % 1000;//等待队列
	    printf("Product %d \n", queue[p]);
	    p = (p+1) % NUM;//因为资源只有3个,计数后还需要mod 3
	    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;//同理,资源数只有3个
	    sleep( rand() % 5 );
	    sem_post(&mutex);//互斥信号量
	    sem_post( &blank_number );//是否唤醒生产者
    }
}

int main(int argc, char *argv[] )
{
    pthread_t pid, cid1,cid2,cid3,cid4;//创建4个消费者线程
    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);//这里创建了4个消费者线程
    pthread_join( pid, NULL );
    pthread_join( cid1, NULL );//等待消费者线程执行完毕
    pthread_join( cid2, NULL );
    pthread_join( cid3, NULL );
    pthread_join( cid4, NULL );//4个消费者线程执行完毕
    sem_destroy( &blank_number );//销毁信号量
    sem_destroy( &product_number );
    return 0;
  }

运行结果

标签:同步,thread,number,信号量,include,测试,pthread,sem,NULL
From: https://www.cnblogs.com/qwer6653/p/16886293.html

相关文章

  • (译)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......
  • BLYNK 之http api 测试
    搭了个BLYNK服务器想了解BLYNK之httpapi,由于所搭建的BLYNK服务器版本和当前blynk官网的版本不一致,没有找到相应资料。最后在网上找到了一篇https://blog.csdn.net/u0136......
  • C#多线程之同步基础篇
    目录一、基本概念二、锁构造MonitorMutex死锁三、信号构造SemaphoreManualResetEventAutoResetEventCountdownEvent四、等待句柄等待句柄和线程池WaitHandle一、基本概念......
  • thread同步
    一、代码(修改前)#include<stdio.h>#include<pthread.h>#include<stdlib.h>#include<semaphore.h>#defineNUM5intqueue[NUM];sem_tblank_number,product_nu......
  • thread 互斥测试
    编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片一、代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<ctype.h......
  • 抖音"凶猛"的幕后英雄,火山引擎 DataTester 累计做过 150 万次 A/B 测试
    在国内互联网领域,字节跳动是最为推崇A/B测试的公司,旗下“抖音”、“今日头条”两大最著名产品,连APP的名字都是来源于A/B测试。A/B测试(也叫AB实验)也被称为对照实......
  • 抖音"凶猛"的幕后英雄,火山引擎 DataTester 累计做过 150 万次 A/B 测试
    在国内互联网领域,字节跳动是最为推崇A/B测试的公司,旗下“抖音”、“今日头条”两大最著名产品,连APP的名字都是来源于A/B测试。A/B测试(也叫AB实验)也被称为对照实验,......
  • 关于上周五下午的测试
    上周五算是比较正式的用了三个半小时做了一套题,反映出自身有很大的问题,首先就是速度太慢了,导致做的非常慢,最后只完成了一小部分功能,还是非常不熟练。其次,部分功能不太会,例......
  • JetBrains新产品Aqua——自动化测试开发工具(抢鲜体验)
    转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.top/9a093c88.html你好,我是测试蔡坨坨。随着行业内卷越来越严重,自动化测试已成为测试工程师的必备技能,谈及自动化......