首页 > 其他分享 >thread互斥测试

thread互斥测试

时间:2022-11-09 13:33:34浏览次数:35  
标签:count thread lock printf mailbox 互斥 测试 pthread NULL

一、实验截图



二、实验代码

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

struct arg_set {
		char *fname;
		int  count;
};

struct arg_set  *mailbox = NULL;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  flag = PTHREAD_COND_INITIALIZER;

void *count_words(void *);
int main(int argc, char *argv[])
{
	pthread_t t1, t2;
	struct arg_set args1, args2;	
	int reports_in = 0;
	int	total_words = 0;

	if ( argc != 3 ){
		printf("usage: %s file1 file2\n", argv[0]);
		exit(1);
	}

	args1.fname = argv[1];
	args1.count = 0;
	pthread_create(&t1, NULL, count_words, (void *) &args1);

	args2.fname = argv[2];
	args2.count = 0;
	pthread_create(&t2, NULL, count_words, (void *) &args2);

	pthread_mutex_lock(&lock);
	while( reports_in < 2 ){
		printf("MAIN: waiting for flag to go up\n");
		pthread_cond_wait(&flag, &lock); 
		printf("MAIN: Wow! flag was raised, I have the lock\n");
		printf("%7d: %s\n", mailbox->count, mailbox->fname);
		total_words += mailbox->count;
		if ( mailbox == &args1) 
			pthread_join(t1,NULL);
		if ( mailbox == &args2) 
			pthread_join(t2,NULL);
		mailbox = NULL;
		pthread_cond_signal(&flag);	
		reports_in++;
	}
	pthread_mutex_unlock(&lock);
	
	printf("%7d: total words\n", total_words);
}
void *count_words(void *a)
{
	struct arg_set *args = a;
	FILE *fp;
	int  c, prevc = '\0';
	
	if ( (fp = fopen(args->fname, "r")) != NULL ){
		while( ( c = getc(fp)) != EOF ){
			if ( !isalnum(c) && isalnum(prevc) )
				args->count++;
			prevc = c;
		}
		fclose(fp);
	} else 
		perror(args->fname);
	printf("COUNT: waiting to get lock\n");
	pthread_mutex_lock(&lock);	
	printf("COUNT: have lock, storing data\n");
	if ( mailbox != NULL ){
		printf("COUNT: oops..mailbox not empty. wait for signal\n");
		pthread_cond_wait(&flag,&lock);
	}
	mailbox = args;			
	printf("COUNT: raising flag\n");
	pthread_cond_signal(&flag);	
	printf("COUNT: unlocking box\n");
	pthread_mutex_unlock(&lock);	
	return NULL;
}

三、程序功能

通过thread互斥来查看两个文件中字符串的数量,一个空格分开算两个,第一个zx.txt文件先获得锁,第二个xx.txt文件则需要等待,当第一个完成之后再进行第二个文件的统计操作,最后输出总结果。

标签:count,thread,lock,printf,mailbox,互斥,测试,pthread,NULL
From: https://www.cnblogs.com/daijun123/p/16873320.html

相关文章

  • thread互斥测试
    截图编译结果说明互斥锁,是一种信号量,常用来防止两个进程或线程在同一时刻访问相同的共享资源。可以保证以下三点:原子性:把一个互斥量锁定为一个原子操作,这意味着操作......
  • thread互斥测试
    thread互斥测试编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片代码#include<stdio.h>#include<stdlib.h>#include<pthread.h>#incl......
  • 测试左移与右移
    大家熟悉的测试工作(也是传统的瀑布式),是接到项目后参与需求评审,然后根据需求文档写写用例和准备脚本,等开发提测之后正式开始测试、提bug、回归,测试通过后就结束了,项目交给......
  • thread同步测试
    thread同步测试1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后......
  • thread同步测试
    任务详情1编译运行附件中的代码,提交运行结果截图,并说明程序功能2修改代码,把同步资源个数减少为3个,把使用资源的线程增加到(你的学号%3+4)个,编译代码,提交修改后的代码......
  • thread互斥测试
    编译运行附件中的代码,并说明程序的功能根据自己的理解,提交不少于3张图片程序功能:通过thread互斥来查看两个文件中字符串的数量,一个空格分开算两个,第一个zx.txt文件先获得......
  • 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)个,编译代码,提交修改后的代码和运行结果......