操作系统实验报告

操作系统上机

             实 验 报 告

班    级:

学    号:

姓    名:

实验地点:  E-Ⅲ区203

 实验时间:  2012.9.26--2012.12.5

                           实验一进程的建立

 【实验目的】

创建进程及子进程

在父子进程间实现进程通信

【实验软硬件环境】

Linux

【实验内容】

创建进程并显示标识等进程控制块的属性信息;

显示父子进程的通信信息和相应的应答信息。

(进程间通信机制任选)

【实验程序及分析】

在linux操作系统桌面建立test1.c程序文件如下:

#include<stdio.h>

#include<string.h>

char father[]={"My dear son,you must be braver!!\n"};

char child[]={"I’m growing,please trust me!!!!!\n"};

main()

{

      int door1[2],door2[2];

      char buff[40];

      pipe(door1);//定义管道1

      pipe(door2);//定义管道2

     

      if(fork())//创建子进程成功,且fork为1,则处理机继续运行父进程

      {

       close(door1[0]);//关闭管道1的读

       close(door2[1]);//关闭管道2的写

       write(door1[1],father,strlen(father));//从管道1的写入口将字符串father的内容写入

       close(door1[1]);//关闭管道1的写

       read(door2[0],buff,40);//从管道2的读入口读入内容到buff中

       printf("Father's process,he get the child’s message:%s\n",buff);

       printf("It is father's process,whose PID=%d\n",getpid());//显示父进程的PID

       }

       else //创建子进程成功,且fork为0,则处理机转入子进程运行

       {

        close(door1[1]);//关闭管道1的写

        close(door2[0]);//关闭管道2的读

        read(door1[0],buff,40);//从管道2的读入口将内容读入到buff中

        printf("Child's process,he get the father’s message:%s\n",buff);

        write(door2[1],child,strlen(child));//从管道2写入口将字符串child的内容写入

        close(door2[1]);//关闭管道2写入口

        printf("It is child's process,whose PID=%d\n",getpid());//显示子进程PID

        }

        }

            

保存后,调用中断依次输入以下内容:

cd 桌面↙

gcc test1.c↙

./a.out↙

产生结果如下:

如图,父进程PID=2807,子进程PID=2808。

【实验心得体会】

1、通过本次实验,深切体会到管道通信在进程之间的应用,了解并掌握了进程建立与通信的相关机制;

2、了解了Linux操作系统的一些简单操作,并体会到了Linux操作系统与Windows操作系统的不同与共通点,Linux有强大的编译系统,而Windows则有更加简便的操作。

实验二进程间的同步

【实验目的】

理解进程同步和互斥模型及其应用

【实验软硬件环境】

Windows 2007

【实验内容】

利用通信API实现进程之间的同步:

建立司机和售票员进程;

并实现他们间的同步运行。

【实验程序及分析】

分析:司机与售票员要协同工作:一方面只有售票员把门关好之后司机才可开车,因此售票员关好门之后要通知司机开车,然后售票;另一方面,也只有司机把车停下之后售票员才能开门让乘客下车和上车,因此,此时司机应通知售票员。汽车当前正在始发站停车让乘客让乘客上车,因此,必须设置一定的信号量来实现他们之间的同步问题。

设计:设置司机与售票员的信号量为全局变量,并且客车的人数:现在人数、下车人数、上车人数为全局变量;设置司机与售票员的线程。考虑到第一站和最后一站的问题,应单独处理,故在各自的线程中分情况讨论:

具体的思路是下面的图示。其中S1是司机的信号量,S2是售票员的信号量。

程序的实现(代码):

#include<stdlib.h>

#include<stdio.h>

#include<windows.h>

#include<time.h>

#define Total_num    59  //客车的最大容量

#define Total_station    5    //总的站数

//全局变量

int     Recent_num=0;       //某一时刻的客车上的人数

int     Get_on_num;         //上车的人数

int     Get_off_num;        //下车的人数

int     station=1;             //客车到达路线的站数

HANDLE  Semaphore_driver;   //Driver的信号量

HANDLE  Semaphore_conductor;//Conductor的信号量

//产生一定范围的随机数,可避免下面程序的判断是否超出客车的最大容量问题

int  Get_random(int min,int max)

{

     int a;

     srand((int)time(0));

     while(1)

     {

        a=rand()%(Total_num+1);

        if(a>=min && a<=max)   return a;

     }

}

//Driver的线程

DWORD WINAPI Thread_Driver(LPVOID Driver)

{

     while(station<=Total_num)

     {

       if(station==Total_station)

        {

          WaitForSingleObject(Semaphore_driver,INFINITE); //P操作

          printf("It is the final station ,thank you for taking this bus,good luck!\n");

          printf("there are %d people remain.\n",Recent_num);

          ReleaseSemaphore(Semaphore_conductor,1,NULL);//V操作

          return 0;  

       }

        else

        { if(station==1)printf("It's time to start,this is the %dth station.\n",station);

                     else printf("This is %dth station\n", station);

              if(station!=1)printf("The bus has been stopped.\n");

        ReleaseSemaphore(Semaphore_conductor,1,NULL);//V操作

              WaitForSingleObject(Semaphore_driver,INFINITE); //P操作

        printf("The door has been closed,let's go!\n");

        ReleaseSemaphore(Semaphore_conductor,1,NULL);//V操作

        }

       Sleep(1000);

     }

     return 0;

}  

//Conductor的线程 

DWORD WINAPI Thread_Conductor(LPVOID Conductor) 

{

      while(1)

      {

        if(station < Total_station)

        { 

         WaitForSingleObject(Semaphore_conductor,INFINITE);//P操作

         if(station ==1)

         {

            

             Get_on_num=Get_random(0,Total_num-Recent_num);

             printf("There are %d people get on the bus.\n",Get_on_num);

             Recent_num+=Get_on_num;    

             }

         else

         {

             printf("Please open the door.\n");

             Get_off_num=Get_random(0,Recent_num);

             printf("There are %d people get off the bus from the %dth station.\n",Get_off_num, station);

             Sleep(1000);        //避免了时间的问题带来的不是随机数的现象

             Recent_num-=Get_off_num;

             Get_on_num=Get_random(0,Total_num-Recent_num);

             printf("There are %d people get on the bus from the %dth station.\n",Get_on_num, station);

             Recent_num+=Get_on_num;

            }

         printf("The recent number of the passagers is:%d\n",Recent_num);

         printf("It's time to go,please close the door.\n");

         ReleaseSemaphore(Semaphore_driver,1,NULL); //V操作

         WaitForSingleObject(Semaphore_conductor,INFINITE);//P操作

            printf("The conducter sell tickets.\n");

               printf("\n\n\n\n");

         station++;

              }

       if(station==Total_station){

          ReleaseSemaphore(Semaphore_driver,1,NULL);//V操作

                WaitForSingleObject(Semaphore_conductor,INFINITE);//P操作

          printf("Please open the door.\n");

          return 0;

         }   

      Sleep(1000);

      }

      return 0;

}

//主函数    

int main()

{

   HANDLE Driver;

   HANDLE Conductor; 

   Semaphore_driver=CreateSemaphore(NULL,0,1,"semaphore_driver"); //创建Driver的信号量   

   Semaphore_conductor=CreateSemaphore(NULL,0,1,"semaphore_conductor");//创建Conductor的信号量

   Driver=CreateThread(NULL,0,Thread_Driver,&Driver,0,NULL);           //创建Driver的线程

   Conductor=CreateThread(NULL,0,Thread_Conductor,&Conductor,0,NULL);  //创建Conductor的线程  

   CloseHandle(Driver);       //关闭Driver的线程                                       

   CloseHandle(Conductor);    //关闭Conductor的线程

   GetLastError();

   while(1);

   system("pause");

   return 0;

}      

运行结果如下图:

【实验心得体会】

通过本次实验,加深了我们对进程同步与互斥的理解,而且懂WaitForSingleObject函数的作用,它相当于P操作;而ReleaseSemaphore函数相当于V操作,用这两个函数来实现P,V操作,以改变信号量的值,从而实现进程同步。

           

实验三文件系统的设计与基本操作的实现

【实验目的】

理解文件系统的组织结构

理解文件系统的描述结构

【实验软硬件环境】

Windows 2007

【实验内容】

选择一种操作系统,理解其文件系统结构。

设计并实现文件系统的描述结构;

显示文件系统中文件的属性;

给出文件系统最基本操作的仿真实现。

【实验程序及分析】

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

char a[100][2][20];

int  in,num=0;

void ls()

{

       int i;

       if(num==0)

       printf("There is nothing.Please input them!\n");

       else{

       printf("There are %d directories!\nThey are:\n",num);

       printf("  directory      file\n");

       for(i=0;i<num;i++)

       {

              printf("  %s          %s\n",a[i][0],a[i][1]);

       }

    }

}

void touch()

{

     int i;

        char inchar[20];

        if(num==0)

        printf("There is no directory,please input them firstly!\n");

        else{

     printf("Please input %d files!\n",num);

     num=0;

     while(scanf("%s",inchar))

        {  

         if(inchar[0]=='$')

         break;

               strcpy(a[num][1],inchar);

               num++;

     }

     }

}

void rm()

{

        int i,pro=1;

     char inchar[20],flag=0,c;

     while(pro)

     {

     scanf("%s",inchar);

     for(i=0;i<num;i++)

        {

               if(strcmp(a[i][1],inchar)==0)

               {

                     flag=1;

               }

               if(flag==1)

               {

                      if(num-i==1)

                      {

                             num--;

                             break;

                      }

                      strcpy(a[i][1],a[i+1][1]);

               }

               

        }

        if(flag==0)

               {printf("File don't exist!!\n");}

         printf("Continue or not(Y/N)?\n");

         scanf(" %c",&c);

      if(c=='N'||c=='n')

      pro=0;

      }

}

void mkdir()

{

    char inchar[20];

    while(scanf("%s",inchar))

        {  

         if(inchar[0]=='$')

         break;

               strcpy(a[num][0],inchar);

               num++;

     }

     printf("The total directories are %d!\n",num);

}

void rmdir()

{

        int i,pro=1;

     char inchar[20],flag=0,c;

     while(pro)

     {

     scanf("%s",inchar);

     for(i=0;i<num;i++)

        {

               if(strcmp(a[i][0],inchar)==0)

               {

                     flag=1;

               }

               if(flag==1)

               {

                      if(num-i==1)

                      {

                             num--;

                             break;

                      }

                      strcpy(a[i][0],a[i+1][0]);

               }

               

        }

        if(flag==0)

               {printf("File don't exist!!\n"); }

         printf("Continue or not(Y/N)?\n");

         scanf(" %c",&c);

      if(c=='N'||c=='n')

      pro=0;

      }

}

int main()

      

       while(1)

       {

       printf("-------Welcome to use this file system!-------\n");

       printf("1--查看目录\n2--创建文件\n3--删除文件\n4--创建目录\n5--删除目录\n6--退出系统\n");

    printf("-----Please input something as we define!-----\n");

       scanf("%d",&in);

       if(in==1) ls();

       if(in==2) touch();

       if(in==3) rm();

       if(in==4) mkdir();

       if(in==5) rmdir();

       if(in==6) break;

       }

       return 0;

}

【实验截图】

【实验心得体会】

实验四 LINUX/UNIX Shell部分

(一)系统基本命令

 1.登陆系统,输入 whoami 和 pwd ,确定自己的登录名和当前目录;

 登录名            , 当前目录            

 2.显示自己的注册目录?命令在哪里?

a.键入 echo $HOME,确认自己的主目录;主目录为          

b.键入 echo $PATH,记下自己看到的目录表;

                 

c.键入 which abcd,看看得到的错误信息;

                   

再键入 which ls 和 which vi,对比刚刚得到的结果的目录是否在a.、b.两题看到的目录表中;   

                  

3.ls 和 cd 的使用:

a.键入 ls, ls -l , ls -a , ls -al 四条命令,观察输出,说明四种不同使用方式的区别。

答:           

b.利用 cd 转到 /bin, /sbin , /etc , /dev,再利用 ls 列出各个目录的内容, 如果"迷路",可以利用 pwd 确定位置,或键入cd , cd ~ 观察效果.

cd , cd ~的区别                    

(二)基本操作

 1.用 cp 将 /usr/share 目录下的 exercise 子目录连同目录下的文件拷贝到自己的主目录下,然后进入自己的 exercise 目录.

答:        

 2.输入/输出重定向和 cat,more 等显示命令的配合使用:

 a.输入 cat 命令列出目录下 longtext 文件中的内容;

                                     

 b.输入 cat 命令列出目录下 longtext 文件中的内容,是否发现一屏显示不完?

                                

 c.使用 more 命令列出 longtext 的内容;

                                          

d.输入 cat hello.txt> hello2.txt,再输入 cat hello.txt>>hello2.txt,再检查hello2.txt的内容有何变化;说明>与>>的区别。

                    

e.对比两条命令: more longtext 和 cat longtext | more ,两者有何不同点?

             

f.执行命令 echo <hello.txt ,观察发生了什么?

              

 g.输入命令 cat <<end ,看看屏幕上的反应(This 文档的使用);

               

 h.设计一条命令,使该命令可以从标准输出中读入;

             

3.特殊字符:

 输入 cat [also a text].txt,看看能否打开目录下的[also a text].txt文

 件,若不能,该怎么办?               

4.文件链接:

a.用 ln 命令为目录下的longtext文件建立一个硬链接,链接名为longtext2, 然后把longtext复制一个新文件longtext3,用 cat 命令将 hello.txt 的内容追加到longtext 的末尾,再用 diff 命令比较longtext,longtext2和longtext3,看看有什么结果,特别是比较一下longtext和longtext2是否相同;

                          

b.用 ln 命令给longtext3建立一个符号链接longtext4,用 cat 命令看看longtext4;然后删去longtext3,再用 cat 命令看看longtext4,是否有什么不同?

                                     

c.删去longtext2,看看能否用 cat 命令看到longtext?

                                       

d.试着执行 ln -s ./abcde ./nulllink,看看是否能建立文件链接.

                                           

5.查找命令 find:

用 find 命令查找当前目录下所有以del开头或以del结尾的文件,并将其删除,要求删除前征求用户许可.

                             

 6.文件的属性:

 a.用 ls -l 列出 exercise 目录下所有的文件和目录,观察其权限位;

                

 b.将hello2.txt 的读权限去掉,看看还能否用 cat 打开该文件;           

 c.将 program 目录的读权限去掉,看看是否能用 ls 命令看到其中内容?

                                        

(三)Shell程序设计

编写一个Shell过程完成如下功能:

1.合并两个$1、$2文件为$3,并显示。

2.如果缺少$3,那么先报告缺少$3,将合并后的内容输出到 experiment.txt,显示。

3.如果缺少$2、$3那么先报告缺少$2、$3,只显示$1的内容。

(四)实验心得体会

相关推荐