#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>
#define MAX_THREAD 10
#define SNL 8
typedef struct{
char tn[3]; //name of thread
unsigned int rm; //the moment when this thread request to access data.
unsigned int pt; //duration of operation
unsigned int i; //same to rm
unsigned int b; //instance at which this thread begin to operate data
unsigned int e; //ending instance
}TEST_INFO;
//TEST_INFO test_data[MAX_THREAD];
typedef struct {
char sn[SNL+1]; //student number
TEST_INFO ti[MAX_THREAD]; //test item
}TI; //test_item_for_student
//TI test_items[STUDENTS];
TI test_item={{"20141413"},
{{"w1",1,4},{"r1",7,10},{"w2",14,1},{"w3",4,8},{"r2",11,14},{"r3",2,5},
{"w4",8,11},{"w5",15,2},{"r4",5,8},{"r5",12,15}}
};
char r_seq[MAX_THREAD][3];
char o_seq[MAX_THREAD][3];
int sr=0;
int so=0;
int rc=0; //count how many readers are reading
pthread_mutex_t cs_d; //guarentee mutually access data
pthread_mutex_t cs_rc; //guarentee mutually access "rc"
pthread_mutex_t cs_sr; //guarentee mutually access "sr"
pthread_mutex_t cs_so; //guarentee mutually access "sr"
time_t base; //the moment when function main begin
void print_answer(){
int i;
printf("%s\n",test_item.sn);
printf("name r_m p_t i_t b_t e_t\n");
for(i=0;i<MAX_THREAD;i++){
printf("%4s%4d%4d%8d%8d%8d\n",(test_item.ti)[i].tn,(test_item.ti)[i].rm,(test_item).ti[i].pt,(test_item.ti)[i].i,(test_item.ti)[i].b,(test_item.ti)[i].e);
}
printf("r_seq:");
for(i=0;i<MAX_THREAD;i++){
printf("%4s",r_seq[i]);
}
printf("\n");
printf("o_seq:");
for(i=0;i<MAX_THREAD;i++){
printf("%4s",o_seq[i]);
}
printf("\n");
}
void save_answer(FILE *f){
int i;
fprintf(f,"\t%s_answer.txt\n\tr/w problem:read first\n\n",test_item.sn);
fprintf(f,"name r_m p_t i_t b_t e_t\n");
for(i=0;i<MAX_THREAD;i++){
fprintf(f,"%4s%4d%4d%8d%8d%8d\n",(test_item.ti)[i].tn,(test_item.ti)[i].rm,(test_item).ti[i].pt,(test_item.ti)[i].i,(test_item.ti)[i].b,(test_item.ti)[i].e);
}
fprintf(f,"\n");
fprintf(f,"r_seq:");
for(i=0;i<MAX_THREAD;i++){
fprintf(f,"%4s",r_seq[i]);
}
fprintf(f,"\n");
fprintf(f,"o_seq:");
for(i=0;i<MAX_THREAD;i++){
fprintf(f,"%4s",o_seq[i]);
}
fprintf(f,"\n");
}
void *r(void *td){
struct timeval t;
time_t rl=base;
sleep(((TEST_INFO *)td)->rm);
gettimeofday(&t,NULL);
((TEST_INFO *)td)->i=difftime(t.tv_sec,rl);
pthread_mutex_lock(&cs_sr);
strcpy(r_seq[sr++],((TEST_INFO *)td)->tn);
pthread_mutex_unlock(&cs_sr);
pthread_mutex_lock(&cs_rc);
rc++;
if(rc==1)pthread_mutex_lock(&cs_d);
pthread_mutex_unlock(&cs_rc);
gettimeofday(&t,NULL);
((TEST_INFO *)td)->b=difftime(t.tv_sec,rl);
pthread_mutex_lock(&cs_so);
strcpy(o_seq[so++],((TEST_INFO *)td)->tn);
pthread_mutex_unlock(&cs_so);
sleep(((TEST_INFO *)td)->pt);
gettimeofday(&t,NULL);
((TEST_INFO *)td)->e=difftime(t.tv_sec,rl);
pthread_mutex_lock(&cs_rc);
rc--;
if(rc==0)pthread_mutex_unlock(&cs_d);
pthread_mutex_unlock(&cs_rc);
return 0;
}
void *w(void *td){
struct timeval t;
time_t wl=base;
sleep(((TEST_INFO *)td)->rm);
gettimeofday(&t,NULL);
((TEST_INFO *)td)->i=difftime(t.tv_sec,wl);
pthread_mutex_lock(&cs_sr);
strcpy(r_seq[sr++],((TEST_INFO *)td)->tn);
pthread_mutex_unlock(&cs_sr);
pthread_mutex_lock(&cs_d);
gettimeofday(&t,NULL);
((TEST_INFO *)td)->b=difftime(t.tv_sec,wl);
pthread_mutex_lock(&cs_so);
strcpy(o_seq[so++],((TEST_INFO *)td)->tn);
pthread_mutex_unlock(&cs_so);
sleep(((TEST_INFO *)td)->pt);
gettimeofday(&t,NULL);
((TEST_INFO *)td)->e=difftime(t.tv_sec,wl);
pthread_mutex_unlock(&cs_d);
return 0;
}
void create_exam(){
int i=0;
pthread_t ht[MAX_THREAD];
pthread_mutex_init(&cs_d,NULL);
pthread_mutex_init(&cs_rc,NULL);
pthread_mutex_init(&cs_sr,NULL);
pthread_mutex_init(&cs_so,NULL);
struct timeval t;
gettimeofday(&t,NULL);
base=t.tv_sec;
for(i=0;i<MAX_THREAD;i++){
if((test_item.ti)[i].tn[0]=='r'){
pthread_create(&ht[i],NULL,r,&((test_item.ti)[i]));
}
else if((test_item.ti)[i].tn[0]=='w'){
pthread_create(&ht[i],NULL,w,&((test_item.ti)[i]));
}
}
for(i=0;i<MAX_THREAD;i++){
pthread_join(ht[i],NULL);
}
pthread_mutex_destroy(&cs_d);
pthread_mutex_destroy(&cs_rc);
pthread_mutex_destroy(&cs_sr);
pthread_mutex_destroy(&cs_so);
}
int main(int argc,char *argv[]){
int i=0;
int si,pos;
int fd;
FILE *fa;
char file_name[100];
create_exam();
sprintf(file_name,"%s_answer.txt",test_item.sn);
if((fa=fopen(file_name,"w"))==NULL){
printf("Error openning answer file:%s\n",file_name);
exit(3);
}
save_answer(fa);
exit(0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
test_get_save_answer.rar_读者优先
共1个文件
c:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 194 浏览量
2022-09-15
00:16:05
上传
评论
收藏 1KB RAR 举报
温馨提示
在linux环境下编写应用程序,该程序运行时能创建N个线程,其中既有读者线程又有写者线程,它们按照事先设计好的测试数据进行读写操作。
资源推荐
资源详情
资源评论
收起资源包目录
test_get_save_answer.rar (1个子文件)
test_get_save_answer.c 5KB
共 1 条
- 1
资源评论
朱moyimi
- 粉丝: 63
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功