华工操作系统实验.docx

上传人:b****5 文档编号:12090585 上传时间:2023-04-17 格式:DOCX 页数:17 大小:18.18KB
下载 相关 举报
华工操作系统实验.docx_第1页
第1页 / 共17页
华工操作系统实验.docx_第2页
第2页 / 共17页
华工操作系统实验.docx_第3页
第3页 / 共17页
华工操作系统实验.docx_第4页
第4页 / 共17页
华工操作系统实验.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

华工操作系统实验.docx

《华工操作系统实验.docx》由会员分享,可在线阅读,更多相关《华工操作系统实验.docx(17页珍藏版)》请在冰豆网上搜索。

华工操作系统实验.docx

华工操作系统实验

 

一、实验步骤:

在linux下编写一个应用程序,命名为an_ch2_1b。

这个程序不停地输出以下行:

Thoseoutputcomefromchild,[系统时间]

别的写一个应用程序,命名为an_ch2_1a。

这个程序创立一个子进度,履行an_ch2_1b。

这个程序不停地输出以下行:

Thoseoutputcomefromchild,[系统时间]

观察程序运转的结果,并对你看到的现象进行解说。

在linux环境下编写一个控制台应用程序,程序中有一个共享的整型变量shared_var,初始值为0;创立一个线程并使其马上与主线程并发履行。

新创立的线程与主线程均不停地循环,并输出

shared_var的值。

主线程在循环中不停地对shared_var进行加1操作,即每次循环shared_var被加1;而新创立的线程则不停地对shared_var进行减1操作,即每次循环shared_var被减1。

观察程序运转的结果,并对你看到的现象进行解说。

二、实验数据:

文件:

#include

#include

#include<>

#include<>

#include<>

usingnamespacestd;

stringgetTime()an_ch2_1b");

return0;

}

 

文件:

#include

#include<>

#include<>

#include<>

intshared_var=0;

void*thread(void*arg)

{

while

(1)

{

printf("inthethreadshared_var:

%d\n",--shared_var);

}

}

intmain()

{

pthread_tpt;

1

 

intret=pthread_create(&pt,NULL,(void*)thread,NULL);

if(ret!

=0)printf("failtocreatethread\n");

while

(1)

{

printf("inthemainshared_var:

%d\n",++shared_var);

}

pthread_join(pt,NULL);

return0;

}

 

生产者花费者问题(信号量)

参照教材中的生产者花费者算法,创立5个进度,此中两个进度为

生产者进度,3个进度为花费者进度。

一个生产者进度试图不停地在一个缓冲中写入大写字母,另一个生产者进度试图不停地在缓冲中写入小写字母。

3个花费者不停地从缓冲中读取一个字符并输出。

为了使得程序的输出易于看到结果,模拟的实例程序,分别在生产者和花费者进度的适合的地点加入一些随机睡眠时间。

可选的实验:

在上边实验的基础上实现部分花费者有选择地花费某

些产品。

比方一个花费者只花费小写字符,一个花费者只花费大写字母,

而另一个花费者则无选择地花费任何产品。

花费者要花费的产品没有时,

花费者进度被堵塞。

注意缓冲的管理。

用线程实现睡觉的剪发师问题,(同步互斥方式采纳信号量或mutex方式均可)

剪发师问题的描述:

一个剪发店招待室有n张椅子,工作室有1张椅子;没有顾客时,剪发师睡觉;第一个顾客抵达时,一定将剪发师唤

醒;顾客来时假如还有空座的话,他就坐在一个座位上等候;假如顾客来时没有空座位了,他就走开,不剪发了;当剪发师办理完全部顾客,而又没有新顾客来时,他又开始睡觉。

读者写者问题

教材中对读者写者问题算法均有描述,但这个算法在不停地有读者流的状况下,写者会被堵塞。

编写一个写者优先解决读者写者问题的程序,此中读者和写者均是多个进度,用信号量作为同步互斥体系。

生产者花费者问题()

#include<>

#include<>

#include<>

#include<>

#include<>

#include<>

#include<>

 

2

 

#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

{

pthread_mutex_unlock(&mutex);

printf("第%d个顾客进来,没有座位而走开!

\n",x+1);

}

}

intmain()

{

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

 

3

 

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

}

}

读者写者问题()

include<>

include<>

include<>

include

include<>

include<>

include<>

include<>

sem_tRWMutex,mutex1,mutex2,mutex3,wrt;

intwriteCount,readCount;

structdata

{

intid;

intlastTime;

};

 

void*Reader(void*

param)

{

int

id=((struct

data*)param)->id;

int

lastTime=((

structdata*)param)->lastTime;

printf("读进度%d

等候读入\n",id);

sem_wait(&mutex3);

sem_wait(&RWMutex);

sem_wait(&mutex2);

readCount++;

if

(readCount==1)sem_wait(&wrt);

4

 

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){

int

id=((

struct

data*)param)->id;

int

lastTime=((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;

 

5

 

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)

{

int

curDirentNameLen=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);

}

 

6

 

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

}

}

 

7

 

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("命令行的长度超限");

 

8

 

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

 

9

 

}

elsesystem(cmd);

if(errno!

=0)

{

printf("error:

%s\n",strerror(errno));

exit(0);

}

}

return0;

}

10

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 育儿理论经验

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1