20201317-Thread同步测试

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

运行截图

image-20221111111636122

修改代码,把同步资源个数减少为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;
}

image-20221111140742464

posted @ 2022-11-11 14:10  B1smarck  阅读(14)  评论(0编辑  收藏  举报