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

thread同步测试

时间:2022-11-09 11:28:04浏览次数:42  
标签:同步 thread create number 线程 测试 pthread sem NULL

任务详情

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

代码截图:

 

 


代码运行截图:

 

 

程序功能:生产者消费者模型,假定有两个线程,一个消费者线程,一个生产者线程。一个模拟生产者行为,一个模拟消费者行为。
两个线程同时操作一个共享资源(一般称之为汇聚),生产向其中添加产品,消费者从中消费掉产品。

 

修改代码
代码如下:
#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() % 4);
        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;
}

 


修改代码截图:

20201208%3+4=4


 

 代码运行截图:

 

 

 

标签:同步,thread,create,number,线程,测试,pthread,sem,NULL
From: https://www.cnblogs.com/syf0105/p/16872980.html

相关文章

  • thread互斥测试
    编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片程序功能:通过thread互斥来查看两个文件中字符串的数量,一个空格分开算两个,第一个zx.txt文件先获得......
  • 线程同步-读者写者问题(多线程)
    任务描述:0推荐在openEuer上实现1描述操作系统中“读者-写者”问题,理解问题的本质,提交你理解或查找到的文本资料2利用多线程完成reader和writer3在main中测试若干个......
  • report 鼠标键盘报告描述符测试
     0x05,0x01,//USAGE_PAGE(GenericDesktop)0x09,0x06,//USAGE(Keyboard)0xa1,0x01,//COLLECTION(Application)0x85,0x01,//ReportID(1)......
  • 输入n组测试数据(1<=n<=10),每组数据中第一行输入一个整数m,表示下行有m个数据输入,判断每
    输入:一行输入一个整数n接下去每组数据中,第一个输入整数m下一行输入m个整数输出:输出每组大于6000的数的个数#include<stdio.h>main(){intn,m,i,j,a,s=0;scanf("......
  • 【MySQL】测试题01
    ✅作者简介:热爱国学的Java后端开发者,修心和技术同步精进。......
  • thread同步测试
    1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后的代码和运行结果......
  • thread互斥测试
    编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片一、代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<ctype.h......
  • 34_音视频播放器_音视频同步
    目录简介实现音视频同步处理没有音频的播放更新界面播放的进度条处理切换音视频时还保留上一个视频的最后一帧画面简介音视频同步的实现可以有两种方式视频同步到音频......
  • day22- 同步方法
    同步方法死锁的概念互斥条件:一个资源每次只能被一个进程使用请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放不剥夺条件:进程已获得的资源,在未......
  • 数据测试总结
    问题:mysql 语法转hql语法过程遇到的问题:int类型和字符串类型比较大小,比如表中是int类型 10< '1'  或者  '10'>1 ,常见与where 后写法类......