华工操作系统实验.docx
《华工操作系统实验.docx》由会员分享,可在线阅读,更多相关《华工操作系统实验.docx(15页珍藏版)》请在冰豆网上搜索。
华工操作系统实验
一、实验步骤:
1.在linux下编写一个应用程序,命名为an_ch2_1b。
这个程序不断地输出如下行:
Thoseoutputcomefromchild,[系统时间]
另外写一个应用程序,命名为an_ch2_1a。
这个程序创建一个子进程,执行an_ch2_1b。
观察程序运行的结果,并对你看到的现象进行解释。
2.在linux环境下编写一个控制台应用程序,程序中有一个共享的整型变量shared_var,初始值为0;创建一个线程并使其立即与主线程并发执行。
新创建的线程与主线程均不断地循环,并输出shared_var的值。
主线程在循环中不断地对shared_var进行加1操作,即每次循环shared_var被加1;而新创建的线程则不断地对shared_var进行减1操作,即每次循环shared_var被减1。
二、实验数据:
文件:
#include
#include<>
usingnamespacestd;
stringgetTime()an_ch2_1b");
return0;
}
文件:
intshared_var=0;
void*thread(void*arg)
{
while
(1)
printf("inthethreadshared_var:
%d\n",--shared_var);
intmain()
pthread_tpt;
intret=pthread_create(&pt,NULL,(void*)thread,NULL);
if(ret!
=0)printf("failtocreatethread\n");
printf("inthemainshared_var:
%d\n",++shared_var);
pthread_join(pt,NULL);
return0;
1.生产者消费者问题(信号量)
参考教材中的生产者消费者算法,创建5个进程,其中两个进程为生产者进程,3个进程为消费者进程。
一个生产者进程试图不断地在一个缓冲中写入大写字母,另一个生产者进程试图不断地在缓冲中写入小写字母。
3个消费者不断地从缓冲中读取一个字符并输出。
为了使得程序的输出易于看到结果,仿照的实例程序,分别在生产者和消费者进程的合适的位置加入一些随机睡眠时间。
可选的实验:
在上面实验的基础上实现部分消费者有选择地消费某些产品。
例如一个消费者只消费小写字符,一个消费者只消费大写字母,而另一个消费者则无选择地消费任何产品。
消费者要消费的产品没有时,消费者进程被阻塞。
注意缓冲的管理。
2.用线程实现睡觉的理发师问题,(同步互斥方式采用信号量或mutex方式均可)
理发师问题的描述:
一个理发店接待室有n张椅子,工作室有1张椅子;没有顾客时,理发师睡觉;第一个顾客来到时,必须将理发师唤醒;顾客来时如果还有空座的话,他就坐在一个座位上等待;如果顾客来时没有空座位了,他就离开,不理发了;当理发师处理完所有顾客,而又没有新顾客来时,他又开始睡觉。
3.读者写者问题
教材中对读者写者问题算法均有描述,但这个算法在不断地有读者流的情况下,写者会被阻塞。
编写一个写者优先解决读者写者问题的程序,其中读者和写者均是多个进程,用信号量作为同步互斥机制。
1.生产者消费者问题()
#defineN10...%d个顾客正在接待室等待。
\n",--waiting);
sleep
(2);
printf("一名顾客理发完成。
\n");
work=0;
sem_post(&barber);
if(waiting==0)printf("无顾客,理发师睡觉\n");
void*Customer(void*arg)
int*p=(int*)arg;
intx=*p;
pthread_mutex_lock(&mutex);
if(chairs>0)
chairs--;
sem_post(&customer);
if(waiting==0&&work==0)
printf("第%d个顾客进来,唤醒理发师...\n",++x);
waiting++;
elseprintf("第%d个顾客进来,%d个顾客正在接待室等待...\n",x+1,++waiting);
pthread_mutex_unlock(&mutex);
sem_wait(&barber);
else
printf("第%d个顾客进来,没有座位而离开!
\n",x+1);
sem_init(&customer,0,0);
sem_init(&barber,0,1);
chairs=N;
pthread_tbar;
pthread_tcus[N*100];
intcus_id[N*100];
pthread_create(&bar,NULL,Barber,NULL);
inti;
srand(time(0));
for(i=0;i{usleep(100000*(rand()%30));cus_id[i]=i;pthread_create(&cus[i],NULL,Customer,&cus_id[i]);}pthread_join(bar,NULL);for(i=0;i{pthread_join(cus_id[i],NULL);}}2.读者写者问题()#include<>#include<>#include<>#include#include<>#include<>#include<>#include<>sem_tRWMutex,mutex1,mutex2,mutex3,wrt;intwriteCount,readCount;structdata{intid;intlastTime;};void*Reader(void*param){intid=((structdata*)param)->id;intlastTime=((structdata*)param)->lastTime;printf("读进程%d等待读入\n",id);sem_wait(&mutex3);sem_wait(&RWMutex);sem_wait(&mutex2);readCount++;if(readCount==1)sem_wait(&wrt);sem_post(&mutex2);sem_post(&RWMutex);sem_post(&mutex3);printf("读进程%d开始读入,%d秒后完成\n",id,lastTime);sleep(lastTime);printf("读进程%d完成读入\n",id);sem_wait(&mutex2);readCount--;if(readCount==0)sem_post(&wrt);sem_post(&mutex2);pthread_exit(0);}void*Writer(void*param){intid=((structdata*)param)->id;intlastTime=((structdata*)param)->lastTime;printf("写进程%d等待写入\n",id);sem_wait(&mutex1);writeCount++;if(writeCount==1)sem_wait(&RWMutex);sem_post(&mutex1);sem_wait(&wrt);printf("写进程%d开始写入,%d秒后完成\n",id,lastTime);sleep(lastTime);printf("写进程%d完成写入\n",id);sem_post(&wrt);sem_wait(&mutex1);writeCount--;if(writeCount==0)sem_post(&RWMutex);sem_post(&mutex1);pthread_exit(0);}intmain(){pthread_ttid;pthread_attr_tattr;pthread_attr_init(&attr);sem_init(&mutex1,0,1);sem_init(&mutex2,0,1);sem_init(&mutex3,0,1);sem_init(&wrt,0,1);sem_init(&RWMutex,0,1);readCount=writeCount=0;intid=0;srand(time(0));while(1){introle=rand()%100;intlastTime=rand()%10;id++;structdata*d=(structdata*)malloc(sizeof(structdata));d->id=id;d->lastTime=lastTime;if(role<50)continue;inti;if(p_dirent->d_type==DT_DIR){intcurDirentNameLen=strlen(direntName2)+1;backupDirName=(char*)malloc(curDirentNameLen);memset(backupDirName,0,curDirentNameLen);memcpy(backupDirName,direntName2,curDirentNameLen);strcat(direntName2,"/");strcat(direntName2,p_dirent->d_name);findfile(fileName1,direntName1,direntName2);memcpy(direntName2,backupDirName,curDirentNameLen);free(backupDirName);backupDirName=NULL;}elseif(!strcmp(fileName1,p_dirent->d_name)){charFileDirName[256];strcpy(FileDirName,direntName2);intcurDirentNameLen=strlen(direntName2)+1;backupDirName=(char*)malloc(curDirentNameLen);memset(backupDirName,0,curDirentNameLen);memcpy(backupDirName,FileDirName,curDirentNameLen);strcat(FileDirName,"/");strcat(FileDirName,p_dirent->d_name);strcat(command,direntName1);strcat(command,"");strcat(command,FileDirName);printf("%s%s\n",p_dirent->d_name,"文件相同,比较:");system(command);}}closedir(p_dir);}voidcomparefile(chardirentName1[],chardirentName2[]){charcommand[512];DIR*p_dir=NULL;structdirent*p_dirent=NULL;p_dir=opendir(direntName1);if(p_dir==NULL){my_error("opendirerror");}while((p_dirent=readdir(p_dir))!=NULL){char*backupDirName=NULL;if(p_dirent->d_name[0]=='.'){continue;}inti;if(p_dirent->d_type==DT_DIR){intcurDirentNameLen=strlen(direntName1)+1;backupDirName=(char*)malloc(curDirentNameLen);memset(backupDirName,0,curDirentNameLen);memcpy(backupDirName,direntName1,curDirentNameLen);strcat(direntName1,"/");strcat(direntName1,p_dirent->d_name);comparefile(direntName1,direntName2);memcpy(direntName1,backupDirName,curDirentNameLen);free(backupDirName);backupDirName=NULL;}else{charFileDirName[256];strcpy(FileDirName,direntName1);intcurDirentNameLen=strlen(direntName1)+1;backupDirName=(char*)malloc(curDirentNameLen);memset(backupDirName,0,curDirentNameLen);memcpy(backupDirName,FileDirName,curDirentNameLen);strcat(FileDirName,"/");strcat(FileDirName,p_dirent->d_name);findfile(p_dirent->d_name,FileDirName,direntName2);}}closedir(p_dir);}voidPrintDirentStruct(chardirentName[],intlevel){DIR*p_dir=NULL;structdirent*p_dirent=NULL;p_dir=opendir(direntName);if(p_dir==NULL){my_error("opendirerror");}while((p_dirent=readdir(p_dir))!=NULL){char*backupDirName=NULL;if(p_dirent->d_name[0]=='.'){continue;}inti;for(i=0;i{printf("│");printf("");}printf("├───");printf("%s\n",p_dirent->d_name);_PC_PATH_MAX);if((buf=(char*)malloc((size_t)size))!=NULL)cwd=getcwd(buf,(size_t)size);printf("%s$",cwd);}voidgetcmd(char*cmd){while((cmd[0]=getchar())=='');inti=0;do{i++;cmd[i]=getchar();}while(cmd[i]!='\n'&&i<=MAX_CMD_LEN);if(i>MAX_CMD_LEN){perror("命令行的长度超限");Route();}cmd[i]='\0';}intmain(){charcmd[MAX_CMD_LEN+1];printf("entersehll....\n");while(1){Route();memset(cmd,0,sizeof(cmd));getcmd(cmd);if(strncmp(cmd,"exit",4)==0){printf("Successfulexit.....\n");break;}if(strncmp(cmd,"cd",2)==0){printf("nowdocd........\n");char*path=(char*)malloc(MAX_CMD_LEN+1);if(path==NULL){printf("memerysetfail\n");exit(0);}inti=2;while(cmd[i]=='')i++;intj=0;while(cmd[i]!='\0'){path[j++]=cmd[i++];}path[j]='\0';chdir(path);if(errno!=0){printf("error:%s\n",strerror(errno));exit(0);}printf("nowthepathis:%s\n",path);free(path);}elsesystem(cmd);if(errno!=0){printf("error:%s\n",strerror(errno));exit(0);}}return0;}
usleep(100000*(rand()%30));
cus_id[i]=i;
pthread_create(&cus[i],NULL,Customer,&cus_id[i]);
pthread_join(bar,NULL);
for(i=0;i{pthread_join(cus_id[i],NULL);}}2.读者写者问题()#include<>#include<>#include<>#include#include<>#include<>#include<>#include<>sem_tRWMutex,mutex1,mutex2,mutex3,wrt;intwriteCount,readCount;structdata{intid;intlastTime;};void*Reader(void*param){intid=((structdata*)param)->id;intlastTime=((structdata*)param)->lastTime;printf("读进程%d等待读入\n",id);sem_wait(&mutex3);sem_wait(&RWMutex);sem_wait(&mutex2);readCount++;if(readCount==1)sem_wait(&wrt);sem_post(&mutex2);sem_post(&RWMutex);sem_post(&mutex3);printf("读进程%d开始读入,%d秒后完成\n",id,lastTime);sleep(lastTime);printf("读进程%d完成读入\n",id);sem_wait(&mutex2);readCount--;if(readCount==0)sem_post(&wrt);sem_post(&mutex2);pthread_exit(0);}void*Writer(void*param){intid=((structdata*)param)->id;intlastTime=((structdata*)param)->lastTime;printf("写进程%d等待写入\n",id);sem_wait(&mutex1);writeCount++;if(writeCount==1)sem_wait(&RWMutex);sem_post(&mutex1);sem_wait(&wrt);printf("写进程%d开始写入,%d秒后完成\n",id,lastTime);sleep(lastTime);printf("写进程%d完成写入\n",id);sem_post(&wrt);sem_wait(&mutex1);writeCount--;if(writeCount==0)sem_post(&RWMutex);sem_post(&mutex1);pthread_exit(0);}intmain(){pthread_ttid;pthread_attr_tattr;pthread_attr_init(&attr);sem_init(&mutex1,0,1);sem_init(&mutex2,0,1);sem_init(&mutex3,0,1);sem_init(&wrt,0,1);sem_init(&RWMutex,0,1);readCount=writeCount=0;intid=0;srand(time(0));while(1){introle=rand()%100;intlastTime=rand()%10;id++;structdata*d=(structdata*)malloc(sizeof(structdata));d->id=id;d->lastTime=lastTime;if(role<50)continue;inti;if(p_dirent->d_type==DT_DIR){intcurDirentNameLen=strlen(direntName2)+1;backupDirName=(char*)malloc(curDirentNameLen);memset(backupDirName,0,curDirentNameLen);memcpy(backupDirName,direntName2,curDirentNameLen);strcat(direntName2,"/");strcat(direntName2,p_dirent->d_name);findfile(fileName1,direntName1,direntName2);memcpy(direntName2,backupDirName,curDirentNameLen);free(backupDirName);backupDirName=NULL;}elseif(!strcmp(fileName1,p_dirent->d_name)){charFileDirName[256];strcpy(FileDirName,direntName2);intcurDirentNameLen=strlen(direntName2)+1;backupDirName=(char*)malloc(curDirentNameLen);memset(backupDirName,0,curDirentNameLen);memcpy(backupDirName,FileDirName,curDirentNameLen);strcat(FileDirName,"/");strcat(FileDirName,p_dirent->d_name);strcat(command,direntName1);strcat(command,"");strcat(command,FileDirName);printf("%s%s\n",p_dirent->d_name,"文件相同,比较:");system(command);}}closedir(p_dir);}voidcomparefile(chardirentName1[],chardirentName2[]){charcommand[512];DIR*p_dir=NULL;structdirent*p_dirent=NULL;p_dir=opendir(direntName1);if(p_dir==NULL){my_error("opendirerror");}while((p_dirent=readdir(p_dir))!=NULL){char*backupDirName=NULL;if(p_dirent->d_name[0]=='.'){continue;}inti;if(p_dirent->d_type==DT_DIR){intcurDirentNameLen=strlen(direntName1)+1;backupDirName=(char*)malloc(curDirentNameLen);memset(backupDirName,0,curDirentNameLen);memcpy(backupDirName,direntName1,curDirentNameLen);strcat(direntName1,"/");strcat(direntName1,p_dirent->d_name);comparefile(direntName1,direntName2);memcpy(direntName1,backupDirName,curDirentNameLen);free(backupDirName);backupDirName=NULL;}else{charFileDirName[256];strcpy(FileDirName,direntName1);intcurDirentNameLen=strlen(direntName1)+1;backupDirName=(char*)malloc(curDirentNameLen);memset(backupDirName,0,curDirentNameLen);memcpy(backupDirName,FileDirName,curDirentNameLen);strcat(FileDirName,"/");strcat(FileDirName,p_dirent->d_name);findfile(p_dirent->d_name,FileDirName,direntName2);}}closedir(p_dir);}voidPrintDirentStruct(chardirentName[],intlevel){DIR*p_dir=NULL;structdirent*p_dirent=NULL;p_dir=opendir(direntName);if(p_dir==NULL){my_error("opendirerror");}while((p_dirent=readdir(p_dir))!=NULL){char*backupDirName=NULL;if(p_dirent->d_name[0]=='.'){continue;}inti;for(i=0;i{printf("│");printf("");}printf("├───");printf("%s\n",p_dirent->d_name);_PC_PATH_MAX);if((buf=(char*)malloc((size_t)size))!=NULL)cwd=getcwd(buf,(size_t)size);printf("%s$",cwd);}voidgetcmd(char*cmd){while((cmd[0]=getchar())=='');inti=0;do{i++;cmd[i]=getchar();}while(cmd[i]!='\n'&&i<=MAX_CMD_LEN);if(i>MAX_CMD_LEN){perror("命令行的长度超限");Route();}cmd[i]='\0';}intmain(){charcmd[MAX_CMD_LEN+1];printf("entersehll....\n");while(1){Route();memset(cmd,0,sizeof(cmd));getcmd(cmd);if(strncmp(cmd,"exit",4)==0){printf("Successfulexit.....\n");break;}if(strncmp(cmd,"cd",2)==0){printf("nowdocd........\n");char*path=(char*)malloc(MAX_CMD_LEN+1);if(path==NULL){printf("memerysetfail\n");exit(0);}inti=2;while(cmd[i]=='')i++;intj=0;while(cmd[i]!='\0'){path[j++]=cmd[i++];}path[j]='\0';chdir(path);if(errno!=0){printf("error:%s\n",strerror(errno));exit(0);}printf("nowthepathis:%s\n",path);free(path);}elsesystem(cmd);if(errno!=0){printf("error:%s\n",strerror(errno));exit(0);}}return0;}
pthread_join(cus_id[i],NULL);
2.读者写者问题()
sem_tRWMutex,mutex1,mutex2,mutex3,wrt;
intwriteCount,readCount;
structdata
intid;
intlastTime;
};
void*Reader(void*param)
intid=((structdata*)param)->id;
intlastTime=((structdata*)param)->lastTime;
printf("读进程%d等待读入\n",id);
sem_wait(&mutex3);
sem_wait(&RWMutex);
sem_wait(&mutex2);
readCount++;
if(readCount==1)sem_wait(&wrt);
sem_post(&mutex2);
sem_post(&RWMutex);
sem_post(&mutex3);
printf("读进程%d开始读入,%d秒后完成\n",id,lastTime);
sleep(lastTime);
printf("读进程%d完成读入\n",id);
readCount--;
if(readCount==0)sem_post(&wrt);
pthread_exit(0);
void*Writer(void*param){
printf("写进程%d等待写入\n",id);
sem_wait(&mutex1);
writeCount++;
if(writeCount==1)sem_wait(&RWMutex);
sem_post(&mutex1);
sem_wait(&wrt);
printf("写进程%d开始写入,%d秒后完成\n",id,lastTime);
printf("写进程%d完成写入\n",id);
sem_post(&wrt);
writeCount--;
if(writeCount==0)sem_post(&RWMutex);
pthread_ttid;
pthread_attr_tattr;
pthread_attr_init(&attr);
sem_init(&mutex1,0,1);
sem_init(&mutex2,0,1);
sem_init(&mutex3,0,1);
sem_init(&wrt,0,1);
sem_init(&RWMutex,0,1);
readCount=writeCount=0;
intid=0;
introle=rand()%100;
intlastTime=rand()%10;
id++;
structdata*d=(structdata*)malloc(sizeof(structdata));
d->id=id;
d->lastTime=lastTime;
if(role<50)continue;
if(p_dirent->d_type==DT_DIR)
intcurDirentNameLen=strlen(direntName2)+1;
backupDirName=(char*)malloc(curDirentNameLen);
memset(backupDirName,0,curDirentNameLen);
memcpy(backupDirName,direntName2,curDirentNameLen);
strcat(direntName2,"/");
strcat(direntName2,p_dirent->d_name);
findfile(fileName1,direntName1,direntName2);
memcpy(direntName2,backupDirName,curDirentNameLen);
free(backupDirName);
backupDirName=NULL;
elseif(!
strcmp(fileName1,p_dirent->d_name))
charFileDirName[256];
strcpy(FileDirName,direntName2);
memcpy(backupDirName,FileDirName,curDirentNameLen);
strcat(FileDirName,"/");
strcat(FileDirName,p_dirent->d_name);
strcat(command,direntName1);
strcat(command,"");
strcat(command,FileDirName);
printf("%s%s\n",p_dirent->d_name,"文件相同,比较:
");
system(command);
closedir(p_dir);
voidcomparefile(chardirentName1[],chardirentName2[])
charcommand[512];
DIR*p_dir=NULL;
structdirent*p_dirent=NULL;
p_dir=opendir(direntName1);
if(p_dir==NULL)
my_error("opendirerror");
while((p_dirent=readdir(p_dir))!
=NULL)
char*backupDirName=NULL;
if(p_dirent->d_name[0]=='.')
continue;
intcurDirentNameLen=strlen(direntName1)+1;
memcpy(backupDirName,direntName1,curDirentNameLen);
strcat(direntName1,"/");
strcat(direntName1,p_dirent->d_name);
comparefile(direntName1,direntName2);
memcpy(direntName1,backupDirName,curDirentNameLen);
strcpy(FileDirName,direntName1);
findfile(p_dirent->d_name,FileDirName,direntName2);
voidPrintDirentStruct(chardirentName[],intlevel)
p_dir=opendir(direntName);
for(i=0;i{printf("│");printf("");}printf("├───");printf("%s\n",p_dirent->d_name);_PC_PATH_MAX);if((buf=(char*)malloc((size_t)size))!=NULL)cwd=getcwd(buf,(size_t)size);printf("%s$",cwd);}voidgetcmd(char*cmd){while((cmd[0]=getchar())=='');inti=0;do{i++;cmd[i]=getchar();}while(cmd[i]!='\n'&&i<=MAX_CMD_LEN);if(i>MAX_CMD_LEN){perror("命令行的长度超限");Route();}cmd[i]='\0';}intmain(){charcmd[MAX_CMD_LEN+1];printf("entersehll....\n");while(1){Route();memset(cmd,0,sizeof(cmd));getcmd(cmd);if(strncmp(cmd,"exit",4)==0){printf("Successfulexit.....\n");break;}if(strncmp(cmd,"cd",2)==0){printf("nowdocd........\n");char*path=(char*)malloc(MAX_CMD_LEN+1);if(path==NULL){printf("memerysetfail\n");exit(0);}inti=2;while(cmd[i]=='')i++;intj=0;while(cmd[i]!='\0'){path[j++]=cmd[i++];}path[j]='\0';chdir(path);if(errno!=0){printf("error:%s\n",strerror(errno));exit(0);}printf("nowthepathis:%s\n",path);free(path);}elsesystem(cmd);if(errno!=0){printf("error:%s\n",strerror(errno));exit(0);}}return0;}
printf("│");
printf("");
printf("├───");
printf("%s\n",p_dirent->d_name);
_PC_PATH_MAX);
if((buf=(char*)malloc((size_t)size))!
cwd=getcwd(buf,(size_t)size);
printf("%s$",cwd);
voidgetcmd(char*cmd)
while((cmd[0]=getchar())=='');
inti=0;
do{
i++;
cmd[i]=getchar();
}while(cmd[i]!
='\n'&&i<=MAX_CMD_LEN);
if(i>MAX_CMD_LEN)
perror("命令行的长度超限");
Route();
cmd[i]='\0';
charcmd[MAX_CMD_LEN+1];
printf("entersehll....\n");
memset(cmd,0,sizeof(cmd));
getcmd(cmd);
if(strncmp(cmd,"exit",4)==0)
printf("Successfulexit.....\n");
break;
if(strncmp(cmd,"cd",2)==0)
printf("nowdocd........\n");
char*path=(char*)malloc(MAX_CMD_LEN+1);
if(path==NULL)
printf("memerysetfail\n");
exit(0);
inti=2;
while(cmd[i]=='')i++;
intj=0;
while(cmd[i]!
='\0')
path[j++]=cmd[i++];
path[j]='\0';
chdir(path);
if(errno!
=0)
printf("error:
%s\n",strerror(errno));
printf("nowthepathis:
%s\n",path);
free(path);
elsesystem(cmd);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1