admin管理员组

文章数量:1794759

操作系统实验报告——实验一:熟悉Linux命令及进程管理

操作系统实验报告——实验一:熟悉Linux命令及进程管理

  • 实验目的
  • 熟悉Linux系统,掌握Linux系统的登入、退出等操作;
  • 熟悉Linux命令及进程管理、作业控制;
  • 学会使用Linux下C语言编程的基本知识;
  • 掌握Linux中vi的基本操作;
  • 掌握Linux下进程创建、调用、终止、阻塞等操作。
    • 实验内容和步骤
  • Linux基本操作;
  • 文件与目录操作;
  • 进程查看与在线帮助;
  • 全屏幕文本编辑器vi的使用;
  • Linux下C语言编程基础知识
  • 编译并运行以下程序:hello.c,getpid_test.c,fork_test.c,exit_test1.c,zombie.c,wait1.c。
    • 代码及运行结果分析

    1.hello.c

    #include<stdio.h>

    int main(){

    printf("Hello Linux!\\n");

    return 0;

    }

     2.getpid_test.c

    #include <unistd.h>

    #include <stdio.h>

    int main(){

    printf("The current process ID is %d\\n",getpid());

    return 0;

    }

    分析:getpid的作用是返回当前进程的进程ID

    3.fork_test.c

    #include<stdio.h>

    #include<sys/types.h>

    #include<unistd.h>

    int main(){

    pid_t pid;

    pid=fork();

    if(pid<0)

    printf("error in fork!");

    else if(pid==0)

    printf("I am the child process,my process ID is %d\\n",getpid());

    else

    printf("I am parent process,my process ID is %d\\n",getpid());

    分析:fork系统调用的作用是复制一个进程。当一个进程调用它,完成后就出现两个几乎一模一样的进程,由此得到一个新进程。 fork调用的一个奇妙之处就是它仅仅被调用一次,却能够返回两次,它可能有三种不同的返回值:

    1.在父进程中,fork返回新创建子进程的进程ID; 

    2.在子进程中,fork返回0; 

    3.如果出现错误,fork返回一个负值; 

       fork出错可能有两种原因:

    (1)当前的进程数已经达到了系统规定的上限;

    (2)系统内存不足。 

    pid=fork(),当pid<0时,说明出现问题;当pid=0时,此时是子进程;否则,此时是父进程;

     4.exit_test1.c

    #include<stdio.h>

    #include<stdlib.h>

    int main(){

    printf("this process will exit!\\n");

    exit(0);

    printf("never be displayed!\\n");

    }

    分析:exit()调用用来终止一个进程。无论在程序中的什么位置,只要执行到exit系统调用,进程就会停止剩下的所有操作,清除包括PCB在内的各种数据结构,并终止本进程的运行。 

    程序运行到exit(0)后会退出,之后的printf()语句不会执行,因此程序运行后只输出this process will exit!

    5.zombie.c

    #include<stdio.h>

    #include<sys/types.h>

    #include<sys/wait.h>

    #include<stdlib.h>

    #include<unistd.h>

    int main(){

    pid_t pid;

    pid=fork();

    if(pid<0)

    printf("error occurred!\\n");

    else if(pid==0)

    exit(0);

    else

    sleep(60);

    pid=wait(NULL);

    }

      

    分析:进程一旦调用了wait,就立即阻塞自己, pid = wait(NULL),如果成功,wait会返回被收集的子进程的进程ID,如果调用进程没有子进程,调用就会失败,此时wait返回-1。sleep的作用是让进程休眠指定的秒数,在这60秒内,子进程已经退出,父进程处于休眠状态,无法进行收集,所以只能保持60秒的僵尸状态。

    6.wait1.c

    #include<stdio.h>

    #include<sys/types.h>

    #include<sys/wait.h>

    #include<stdlib.h>

    #include<unistd.h>

    int main(){

    pid_t pc,pr;

    pc=fork();

    if(pc<0)

    printf("error ocurred!\\n");

    else if(pc==0){

    printf("This is child process with pid of %d\\n",getpid());

    sleep(10);

    }

    else{

    pr=wait(NULL);

    printf("I catched a child process with pid of %d\\n",pr);

    }

    exit(0);

    }

      分析:第二行结果需等待10秒才能出来,此时正在调用sleep(10)使子进程处于休眠状态,过了这10秒,子进程苏醒,就会被父进程捕捉到。

    • 心得体会

            通过本次实验,我更好地熟悉了Linux的各种操作,使用vi进行C语言程序编译与运行,加深了进程管理、作业控制的理解。掌握Linux中vi的基本操作,掌握Linux下进程创建、调用、终止、阻塞等操作。对getpid()、sleep()、wait()、exit()等函数调用进行一个深度认识,以及相应c语言头文件的引用,在实践过程中认识到了自己的不足,在进程理论知识的一些短缺,并通过自己的亲身实践得到改正。

    本文标签: 进程熟悉命令操作系统报告