华工操作系统实验_第1页
华工操作系统实验_第2页
华工操作系统实验_第3页
华工操作系统实验_第4页
华工操作系统实验_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一、实验步骤:1. 在linux下编写一个应用程序,命名为an_ch2_1b。这个程序不断地输出如下行:Those output come from child,系统时间另外写一个应用程序,命名为an_ch2_1a。这个程序创建一个子进程,执行an_ch2_1b。这个程序不断地输出如下行:Those output come from child,系统时间观察程序运行的结果,并对你看到的现象进行解释。2. 在linux环境下编写一个控制台应用程序,程序中有一个共享的整型变量shared_var,初始值为0;创建一个线程并使其立即与主线程并发执行。新创建的线程与主线程 均不断地循环,并输出shar

2、ed_var 的值。主线程在循环中不断地对shared_var 进行加1操作,即每次循环shared_var 被加1;而新创建的线程则不断地对shared_var 进行减1 操作,即每次循环shared_var 被减1。观察程序运行的结果,并对你看到的现象进行解释。二、实验数据: an_ch2_1b.cpp文件:#include #include #include #include #include using namespace std;string getTime() /获取系统时间time_t timep;time(&timep);char tmp64;strftime(tmp,size

3、of(tmp),%Y-%m-%d%H:%M:%S,localtime(&timep);return tmp;int main()while (true)string tmn = getTime();cout Those output come from child, tmn endl;sleep(1); /为了便于截屏使用sleep()函数延迟输出return 0;an_ch2_1a.cpp文件:#include #include #include #include #include using namespace std;int main()pid_t pid;pid = fork();if

4、 (pid = -1) cout fail to create endl;else if (pid = 0) system(./an_ch2_1b);return 0;Consoleapp.c文件:#include #include #include #include int shared_var = 0;void * thread(void * arg)while (1)printf(in the thread shared_var:%dn, -shared_var);int main()pthread_t pt;int ret = pthread_create(&pt, NULL, (vo

5、id*)thread, NULL);if (ret != 0) printf(fail to create threadn);while (1)printf(in the main shared_var:%dn, +shared_var);pthread_join(pt, NULL);return 0;1. 生产者消费者问题(信号量)参考教材中的生产者消费者算法,创建5个进程,其中两个进程为生产者进程,3个进程为消费者进程。一个生产者进程试图不断地在一个缓冲中写入大写字母,另一个生产者进程试图不断地在缓冲中写入小写字母。3个消费者不断地从缓冲中读取一个字符并输出。为了使得程序的输出易于看到结果

6、,仿照的实例程序,分别在生产者和消费者进程的合适的位置加入一些随机睡眠时间。可选的实验:在上面实验的基础上实现部分消费者有选择地消费某些产品。例如一个消费者只消费小写字符,一个消费者只消费大写字母,而另一个消费者则无选择地消费任何产品。消费者要消费的产品没有时,消费者进程被阻塞。注意缓冲的管理。2. 用线程实现睡觉的理发师问题,(同步互斥方式采用信号量或mutex方式均可)理发师问题的描述:一个理发店接待室有n张椅子,工作室有1张椅子;没有顾客时,理发师睡觉;第一个顾客来到时,必须将理发师唤醒;顾客来时如果还有空座的话,他就坐在一个座位上等待;如果顾客来时没有空座位了,他就离开,不理发了;当理

7、发师处理完所有顾客,而又没有新顾客来时,他又开始睡觉。3. 读者写者问题教材中对读者写者问题算法均有描述,但这个算法在不断地有读者流的情况下,写者会被阻塞。编写一个写者优先解决读者写者问题的程序,其中读者和写者均是多个进程,用信号量作为同步互斥机制。1. 生产者消费者问题(pro_con.c)#include #include #include #include #include #include #include #define N 10 /缓冲区大小为100char *buffer;int capacity = N;sem_t mutex, empty, full;void * produ

8、ce_1()while (1)sem_wait(&empty);sem_wait(&mutex);int r1 = rand() % 26; int ju = 0; for(int i = 0; i N; i+) if (bufferi = 0)bufferi = A + r1;printf(生产者1号生产一个产品 : %c 剩余容量为:%dn, bufferi, -capacity); ju = 1; break; if(ju = 0)printf(没有足够容量!n);sem_post(&mutex);sem_post(&full);usleep(r1 * );void * produce_

9、2()while (1)sem_wait(&empty);sem_wait(&mutex);int r2 = rand() % 26; int ju = 0; for(int i = 0; i N; i+) if (bufferi = 0)bufferi = a + r2;printf(生产者2号生产一个产品 : %c 剩余容量为:%dn, bufferi, -capacity);ju = 1; break; if(ju = 0)printf(没有足够容量!n);sem_post(&mutex);sem_post(&full);usleep(r2 * );void * consume_1()w

10、hile (1)sem_wait(&full);sem_wait(&mutex);int ju = 0; for (int i = 0; i = A&bufferi = Z)printf(消费者1号消费一个产品 : %c 剩余容量为:%dn, bufferi, +capacity); bufferi = 0;ju = 1; break;if (ju = 0)printf(没有消费者1号所需的产品!n);sem_post(&mutex);sem_post(&empty);int r3 = rand() % 26;usleep(r3 * );void * consume_2()while (1)s

11、em_wait(&full);sem_wait(&mutex);int ju = 0; for (int i = 0; i = a&bufferi = z)printf(消费者2号消费一个产品 : %c 剩余容量为:%dn, bufferi, +capacity); bufferi = 0; ju = 1;break;if (ju = 0)printf(没有消费者2号所需的产品!n);sem_post(&mutex);sem_post(&empty);int r4 = rand() % 26;usleep(r4 * );void * consume_3()int ju = 0;while (1

12、)sem_wait(&full);sem_wait(&mutex);int ju = 0; for (int i = 0; i = A & bufferi = a & bufferi = z)printf(消费者3号消费一个产品 : %c 剩余容量为:%dn, bufferi, +capacity); bufferi = 0;ju = 1; break;if (ju = 0)printf(没有产品可以消费!n);sem_post(&mutex);sem_post(&empty);int r5 = rand() % 26;usleep(r5 * );int main()buffer = (cha

13、r*)malloc(N * sizeof(char*);for (int i = 0; i N; i+)bufferi = 0;sem_init(&mutex, 1, 1);sem_init(&empty, 0, N);sem_init(&full, 0, 0); srand(time(0);pthread_t tid5;pthread_attr_t attr;pthread_attr_init(&attr);pthread_create(&tid0,&attr,produce_1,NULL);pthread_create(&tid1,&attr,produce_2,NULL);pthread

14、_create(&tid2,&attr,consume_1,NULL);pthread_create(&tid3,&attr,consume_2,NULL); pthread_create(&tid4,&attr,consume_3,NULL); for(int i=0;i5;i+)pthread_join(tidi,NULL);return 0;2. 用线程实现睡觉的理发师问题(barber.c)#include #include #include #include #include #include #define N 5sem_t customer,barber;int chairs,w

15、aiting = 0,work = 0;pthread_mutex_t mutex;void * Barber ( ) printf(无顾客,理发师睡觉n); while (1) sem_wait(&customer); pthread_mutex_lock(&mutex); chairs+; pthread_mutex_unlock(&mutex); work = 1; printf ( 理发师正在给一名顾客理发. %d 个顾客正在接待室等待。n, -waiting); sleep(2); printf ( 一名顾客理发完成。n ); work = 0; sem_post(&barber);

16、 if(waiting = 0)printf(无顾客,理发师睡觉n); void * Customer ( void* arg ) int *p = (int*) arg; int x = *p; pthread_mutex_lock(&mutex); if ( chairs 0 ) chairs-; sem_post(&customer); if(waiting = 0 & work = 0) printf( 第 %d 个顾客进来 , 唤醒理发师.n , +x); waiting+; else printf ( 第 %d 个顾客进来 , %d 个顾客正在接待室等待.n , x+1 , +wa

17、iting ); pthread_mutex_unlock(&mutex); sem_wait(&barber); else pthread_mutex_unlock(&mutex); printf ( 第 %d 个顾客进来,没有座位而离开!n , x+1 ); int main ( ) sem_init (&customer , 0 , 0 ); sem_init (&barber , 0 , 1 ); chairs = N; pthread_t bar; pthread_t cusN*100; int cus_idN*100; pthread_create ( &bar , NULL ,

18、Barber , NULL ); int i; srand(time(0); for ( i = 0 ; i N*100 ; i + ) usleep(*(rand() % 30); cus_idi = i; pthread_create ( &cusi , NULL , Customer , &cus_idi ); pthread_join ( bar , NULL ); for ( i = 0 ; i N*100 ; i+ ) pthread_join ( cus_idi , NULL ); 3. 读者写者问题(reader_writer.c)# include # include # i

19、nclude # include # include # include # include # include sem_t RWMutex, mutex1, mutex2, mutex3, wrt;int writeCount, readCount;struct data int id;int lastTime;void* Reader(void* param)int id = (struct data*)param)-id;int lastTime = (struct data*)param)-lastTime;printf(读进程 %d 等待读入n, id);sem_wait(&mute

20、x3);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(&mute

21、x2);pthread_exit(0);void* Writer(void* param) int id = (struct data*)param)-id;int lastTime = (struct data*)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(l

22、astTime);printf(写进程 %d 完成写入n, id);sem_post(&wrt);sem_wait(&mutex1);writeCount-;if(writeCount = 0) sem_post(&RWMutex);sem_post(&mutex1);pthread_exit(0);int main() pthread_t tid;pthread_attr_t attr;pthread_attr_init(&attr); sem_init(&mutex1, 0, 1); sem_init(&mutex2, 0, 1); sem_init(&mutex3, 0, 1); sem

23、_init(&wrt, 0, 1); sem_init(&RWMutex, 0, 1); readCount = writeCount = 0;int id = 0; srand(time(0);while(1) int role = rand() % 100; int lastTime = rand() % 10; id+;struct data* d = (struct data*)malloc(sizeof(struct data);d-id = id;d-lastTime = lastTime;if(role = 50) /写 printf(创建写进程,PID : %dn, id);p

24、thread_create(&tid, &attr, Writer, d); sleep(rand() % 8);return 0; 1. 实现一个“difftree”命令,其功能是比较两个目录下的文件结构和文件信息。当在命令行方式下执行“difftree ”命令时,能够比较目录dir1和 目录dir2是否具有相同的结构,对相同的部分,进一步比较相同文件名的文件内容。列出比较的文件系统结构图。本实验是对单个文件比较的扩展,设计中需要考虑目录操作。difftree.c#include #include #include #include #include #include #include in

25、t filelevel1 = 0, filelevel2 = 0;char DIRNAME1256, DIRNAME2256;void my_error(const char *strerr) perror(strerr); exit(1);void findfile(char fileName1, char direntName1, char direntName2) char command512 = diff ; DIR *p_dir = NULL; struct dirent *p_dirent = NULL; p_dir = opendir(direntName2); if(p_di

26、r = NULL) my_error(opendir error); while(p_dirent = readdir(p_dir) != NULL) char *backupDirName = NULL; if(p_dirent-d_name0 = .)continue; int i; if(p_dirent-d_type = DT_DIR) int curDirentNameLen = strlen(direntName2) + 1; backupDirName = (char *)malloc(curDirentNameLen); memset(backupDirName, 0, cur

27、DirentNameLen); memcpy(backupDirName, direntName2, curDirentNameLen); strcat(direntName2, /); strcat(direntName2, p_dirent-d_name); findfile(fileName1, direntName1, direntName2); memcpy(direntName2, backupDirName, curDirentNameLen); free(backupDirName); backupDirName = NULL; else if(!strcmp(fileName

28、1, p_dirent-d_name) char FileDirName256; strcpy(FileDirName, direntName2); int curDirentNameLen = strlen(direntName2) + 1; backupDirName = (char *)malloc(curDirentNameLen); memset(backupDirName, 0, curDirentNameLen); memcpy(backupDirName, FileDirName, curDirentNameLen); strcat(FileDirName, /); strca

29、t(FileDirName, p_dirent-d_name); strcat(command, direntName1); strcat(command, ); strcat(command, FileDirName); printf(%s%sn, p_dirent-d_name,文件相同,比较:); system(command); closedir(p_dir); void comparefile(char direntName1, char direntName2) char command512; DIR *p_dir = NULL; struct dirent *p_dirent

30、= NULL; p_dir = opendir(direntName1); if(p_dir = NULL) my_error(opendir error); while(p_dirent = readdir(p_dir) != NULL) char *backupDirName = NULL; if(p_dirent-d_name0 = .) continue; int i; if(p_dirent-d_type = DT_DIR) int curDirentNameLen = strlen(direntName1) + 1; backupDirName = (char *)malloc(c

31、urDirentNameLen); 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); backupDi

32、rName = NULL; else char FileDirName256; strcpy(FileDirName, direntName1); int curDirentNameLen = strlen(direntName1) + 1; backupDirName = (char *)malloc(curDirentNameLen); memset(backupDirName, 0, curDirentNameLen); memcpy(backupDirName, FileDirName, curDirentNameLen); strcat(FileDirName, /); strcat

33、(FileDirName, p_dirent-d_name); findfile(p_dirent-d_name, FileDirName, direntName2); closedir(p_dir); void PrintDirentStruct(char direntName, int level) DIR *p_dir = NULL; struct dirent *p_dirent = NULL; p_dir = opendir(direntName); if(p_dir = NULL) my_error(opendir error); while(p_dirent = readdir(

34、p_dir) != NULL) char *backupDirName = NULL; if(p_dirent-d_name0 = .) continue; int i; for(i = 0; i d_name); /如果目录项仍是一个目录的话,进入目录 if(p_dirent-d_type = DT_DIR) int curDirentNameLen = strlen(direntName) + 1; backupDirName = (char *)malloc(curDirentNameLen); memset(backupDirName, 0, curDirentNameLen); me

35、mcpy(backupDirName, direntName, curDirentNameLen); strcat(direntName, /); strcat(direntName, p_dirent-d_name); level += 1; PrintDirentStruct(direntName, level); memcpy(direntName, backupDirName, curDirentNameLen); free(backupDirName); backupDirName = NULL; closedir(p_dir); int main() memset(DIRNAME1

36、, 0, sizeof(DIRNAME1); memset(DIRNAME2, 0, sizeof(DIRNAME2); scanf(%s%s, DIRNAME1, DIRNAME2); printf(%sn, 第一个目录:); PrintDirentStruct(DIRNAME1, filelevel1); printf(n); printf(%sn, 第二个目录:); PrintDirentStruct(DIRNAME2, filelevel2); if(filelevel1 = filelevel2)printf(n两个目录文件结构相同n); comparefile(DIRNAME1,

37、DIRNAME2); return 0;1. 在linux中实现一个命令执行程序doit,它执行命令行参数中的命令之后统计1) 命令执行占用的CPU时间(包括用户态和系统态时间,以毫秒为单位),2) 命令执行的时间,3) 进程被抢占的次数,4) 进程主动放弃CPU的次数,5) 进程执行过程中发生缺页的次数2. 在linux中实现一个简单的命令解释程序,功能要求:1) 同时支持内部命令和外部命令,内部命令支持两个(cd、exit)2) 支持后台命令提示:实验中可能用到的系统调用如下: fork() 创建一个新进程 getrusage() 取得进程的资源使用情况 gettimeofday() 取当

38、前的时间 execve() 装入一个程序并执行 wait() 等待子进程结束 chdir() 改变进程的工作目录 strtok() 字符串解析1. doit.c #include#include#include#include#include#include#includeextern int errno;int main(int argc, char*argv)char command200;struct timeval start, end;strcpy(command, argv1);for (int i = 2; i argc; i+)strcat(command, );strcat(

39、command, argvi);gettimeofday(&start, NULL);system(command);gettimeofday(&end, NULL);if (errno != 0)printf(error:%sn, strerror(errno);exit(0);elsestruct rusage result;memset(&result, 0, sizeof(struct rusage);getrusage(RUSAGE_CHILDREN, &result);double time = (double)end.tv_sec - (double)start.tv_sec) * 1000 + (double)end.tv_usec - (double)start.tv_usec) / 1000;printf(n命令执行占用的CPU时间: 用户态:%f毫秒t系统态:%f毫秒n,(double)result.ru_utime.tv_usec / 1000 + (double)result.ru_utime.tv_sec *

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论