您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > 操作系统linux版实验报告
1操作系统实验报告(Linux版)网络142潘豹1429992实验一观察Linux进程状态一、实验目的在本实验中学习Linux操作系统的进程状态,并通过编写一些简单代码来观察各种情况下,Linux进程的状态,进一步理解进程的状态及其转换机制。二、实验环境硬件环境:计算机一台,局域网环境;软件环境:LinuxUbuntu操作系统,gcc编译器。(四)查看“不可中断阻塞”状态(D)创建一个C程序,如uninter_status.c,让其睡眠30s代码:#includeunistd.h#includestdio.hintmain(){inti=0,j=0,k=0;for(i=0;i1000000;i++){for(j=0;j1000000;j++){k++;k--;}}}实验结果:(二)查看“暂停”状态(T)运行run_status进程,其进入R状态:代码同上:(三)查看“可中断阻塞”状态(S)创建一个C程序,如interruptiblie_status.c,让其睡眠30s编译链接,后台运行该程序(后接&符号),并使用ps命令查看运行状态代码:#includeunistd.h#includestdio.hintmain()3{sleep(30);return;}实验结果:(四)查看“不可中断阻塞”状态(D)创建一个C程序,如uninter_status.c,让其睡眠30s编译链接,后台运行该程序(后接&),并使用ps命令查看运行状态代码:#includeunistd.h#includestdio.hintmain(){if(vfork()==0){sleep(300);return;}}实验结果:(五)查看“僵尸”进程(Z)创建一个C程序,如zombie_status.c,在其中创建一个子进程,并让子进程迅速结束,而父进程陷入阻塞编译链接,后台运行该程序(后接&),并使用ps命令查看运行状态(30s内)代码:#includeunistd.h#inclduestdio.h4intmain(){if(fork()){sleep(300);}}实验结果:实验二观察Linux进程/线程的异步并发执行一、实验目的通过本实验学习如何创建Linux进程及线程,通过实验,观察Linux进程及线程的异步执行。理解进程及线程的区别及特性,进一步理解进程是资源分配单位,线程是独立调度单位。二、实验环境硬件环境:计算机一台,局域网环境;软件环境:LinuxUbuntu操作系统,gcc编译器。三、实验内容和步骤1、进程异步并发执行编写一个C语言程序,该程序首先初始化一个count变量为1,然后使用fork函数创建两个子进程,每个子进程对count加1后,显示“Iamson,count=x”或“Iamdaughter,count=x”,父进程对count加1之后,显示“Iamfather,count=x”,其中x使用count值代替。最后父进程使用waitpid等待两个子进程结束之后退出。编译连接后,多次运行该程序,观察屏幕上显示结果的顺序性,直到出现不一样的情况为止,并观察每行打印结果中count的值。代码:#includeunistd.h#includestdio.hintmain(){5pid_tson_pid,daughter_pid;intcount=1;son_pid=fork();if(son_pid==0){count++;printf(iamson,count=%d\n,count);}else{daughter_pid=fork();if(daughter_pid==0){count++;printf(iamdaughter,count=%d\n,count);}else{count++;printf(iamfather,count=%d\n,count);waitpid(son_pid,NULL,0);waitpid(daughter_pid,NULL,0);}}}2、线程异步并发执行编写一个C语言程序,该程序首先初始化一个count变量为1,然后使用pthread_create函数创建两个线程,每个线程对count加1后,显示“Iamson,count=x”或“Iamdaughter,count=x”,父进程对count加1之后,显示“Iamfather,count=x”,其中x使用count值代替。最后父进程使用pthread_join等待两个线程结束之后退出。6编译连接后,多次运行该程序,观察屏幕上显示结果的顺序性,直到出现不一样的情况为止,并观察每行打印结果中count的值。代码:#includeunistd.h#includestdio.h#includepthread.hvoid*daughter(void*num){int*a=(int*)num;*a+=1;printf(iamdaughter,count=%d\n,*a);}void*son(void*num){int*a=(int*)num;*a+=1;printf(iamson,count=%d\n,*a);}intmain(){pthread_tson_tid,daughter_tid;intcount=1;pthread_create(&son_tid,NULL,son,&count);pthread_create(&daughter_tid,NULL,daughter,&count);count++;printf(iamparent,count:=%d\n,count);pthread_join(son_tid,NULL);pthread_join(daughter_tid,NULL);return0;}实验结果:7实验三使用信号量进行互斥与同步一、实验目的本实验介绍在Linux中使用信号量进行进程同步、互斥的方法。读者可以通过实验进一步理解进程间同步与互斥、临界区与临界资源的概念与含义,并学会Linux信号量的基本使用方法。二、实验环境硬件环境:计算机一台,局域网环境;软件环境:LinuxUbuntu操作系统,gcc编译器。三、实验内容和步骤三、实验内容和步骤(一)参考:POSIX以及SystemVSystemV:Unix众多版本中的一支,最初由AT&T定义,目前为第四个版本,其中定义了较为复杂的API。POSIX:PortableOperatingSystemInterface,IEEE为了统一Unix接口而定义的标准,定义了统一的API接口。Linux即支持SystemAPI,又支持POSIXAPI(二)实验步骤step1:通过实例查看不使用互斥时的情况(假设文件命名为no_sem.c)编译链接,同时运行两个进程,显示结果代码:#includestdio.h#includestdlib.hintmain(intargc,char*argv[]){charmessage='x';inti=0;if(argc1){message=argv[1][0];8}for(i=0;i10;i++){printf(%c,message);fflush(stdout);sleep(rand()%3);printf(%c,message);fflush(stdout);sleep(rand()%2);}sleep(10);exit;}实验结果:step2:使用信号量来对临界资源进行互斥(假设文件命名为with_sem.c)编译链接,同时运行两个进程。观察X和O的出现规律,并分析原因。代码:#includestdio.h#includestdlib.h#includesys/types.h#includesys/ipc.h#includesemaphore.h#includefcntl.h#includesys/stat.hintmain(intargc,char*argv[]){charmessage='x';inti=0;if(argc1){message=argv[1][0];}sem_t*mutex=sem_open(mysem,O_CREAT,0666,1);for(i=0;i10;i++)9{sem_wait(mutex);printf(%c,message);fflush(stdout);sleep(rand()%3);printf(%c,message);fflush(stdout);sem_post(mutex);sleep(rand()%2);}sleep(10);sem_close(mutex);sem_unlink(mysem);exit(0);}实验结果:step3:使用信号量来模拟下象棋红黑轮流走子的情况编写两个C语言程序black_chess.c以及red_chess.c,分别模拟下象棋过程中红方走子和黑方走子过程。走子规则:红先黑后,红、黑双方轮流走子,到第10步,红方胜,黑方输。代码:红色棋#includestdio.h#includestdlib.h#includesys/types.h#includesys/ipc.h#includesemaphore.h#includefcntl.h10#includesys/stat.hintmain(intargc,char*argv[]){inti=0;sem_t*hei=sem_open(chess_black_sem,O_CREAT,0666,1);sem_t*hong=sem_open(chess_red_sem,O_CREAT,0666,0);for(i=0;i10;i++){sem_wait(hei);if(i!=9){printf(Redchesshadmoved,black,chessgo!\n);}else{printf(Redchesswin!\n);}fflush(stdout);sem_post(hong);}sleep(10);sem_close(hei);sem_close(hong);sem_unlink(chess_red_sem);sem_unlink(chess_black_sem);exit(0);}黑色棋:#includestdio.h#includestdlib.h#includesys/types.h#includesys/ipc.h#includesemaphore.h#includefcntl.h#includesys/stat.hintmain(intargc,char*argv[]){inti=0;sem_t*hei=sem_open(chess_black_sem,O_CREAT,0666,1);sem_t*hong=sem_open(chess_red_sem,O_CREAT,0666,0);for(i=0;i10;i++){sem_wait(hong);if(i!=9){printf(Blackchesshadmoved,redchessgo!\n);11}else{printf(Blackchesswin!\n);}fflush(stdout);sem_post(hei);}sleep(10);sem_close(hei);sem_close(hong);sem_unlink(chess_red_sem);sem_unlink(chess_black_sem);exit(0);}实验结果:实验四作业调度算法模拟一、实验目的(1)掌握周转时间、等待时间、平均周转时间等概念及其计算方法。(2)理解五种常用的进程调度算法(FCFS、SJF、HRRF、HPF、RR),区分算法之间的差异性,并用C语言模拟实现各算法。(3)了解操作系统中高级调度、中级调度和低级调度的区别和联系。12二、实验环境硬件环境:计算机一台,局域网环境;软件环境:LinuxUbuntu操作系统,gcc编译
本文标题:操作系统linux版实验报告
链接地址:https://www.777doc.com/doc-4185641 .html