zoukankan      html  css  js  c++  java
  • 操作系统第2次实验报告:创建进程

    • 姓名:吕煜华
    • 学号:201821121046
    • 班级:计算1812

    1. 编写程序

    在服务器上用VIM编辑器编写一个程序:一个进程创建(fork)两个子进程。给出源代码:

      1 #include<sys/types.h>
      2 #include<unistd.h>
      3 #include<stdio.h>
      4 int main()
      5 {
      6     pid_t fpid1;
      7     pid_t fpid2;
      8     fpid1 = fork();
      9     if(fpid1<0)
     10         printf("error in fork!");
     11     else if(fpid1==0)
     12         printf("First Child process, my process id is %d
    ",getpid());
     13     else {
     14         fpid2 = fork();
     15         if(fpid2<0)
     16             printf("error in fork!");
     17         else if(fpid2==0) {
     18             printf("Second Child process, my process id is %d
    ",getpid());
     19         }
     20         else {
     21             printf("Parent process, my process id is %d
    ",getpid());
     22         }
     23     }
     24     sleep(100);
     25     return 0;
     26 }

    2. 打印进程树

    打印1所创建的进程树结构,给出带有自己名字的截图。

    提示:

    • 使用unsigned sleep(unsigned seconds)挂起进程,以便打印进程树
    • 打印进程树命令pstree -p pid

     编译程序并运行:

     

     打印进程树:

    3. 解读进程相关信息

    (2) ps -ef

    使用ps -ef给出所创建进程的信息,并分析每个字段的含义。

     字段含义:

    UID:用户ID(此处为lvyuhua)

    PID:进程ID(此处为27672、27673、27674等)

    PPID:该进程的父进程ID(例如子进程27673、27674对应其父进程27672)

    C:该进程占用CPU的百分比(此处为0)

    STIME:进程开始时间(此处为23:36)

    TTY:终端的次要装置号码(此处为pts/2)

    TIME:进程运行时间(此处为00:00:00)

    CMD:所执行的指令(此处为 ./fork)

    (2) ps -aux

    使用ps -aux给出所创建进程的信息,并分析每个字段的含义。

    字段含义:

    USER:用户名(此处为lvyuhua)

    PID:进程ID(此处为27672、27673、27674等)

    %CPU:占用的 CPU 使用率(0.0)

    %MEN:占用的记忆体使用率(0.0)

    VSZ:占用的虚拟记忆体大小(kb)

    RSS:占用记忆体的大小(kb)

    TTY:终端的次要装置号码(此处为pts/2)

    STAT:进程状态(此处S表示中断,R表示运行,+ 位于后台的进程组)

    该行程的状态,linux的进程有5种状态:

    – D 不可中断 uninterruptible sleep (usually IO)

    – R 运行 runnable (on run queue)

    – S 中断 sleeping

    – T 停止 traced or stopped

    – Z 僵死 a defunct (”zombie”)

    START:进程开始时间(此处为23:36)

    TIME:执行的时间(此处为0:00)

    COMMAND:所执行的指令(此处为 ./fork)

    4. 通过该实验产生新的疑问及解答

    1.编写c程序的时候没有理解好fork()的工作方式。

    直接在第一个子进程中执行fork()创建子进程,导致第二个进程的父进程变成了第一个子进程。把第二个fork()放到else里面就成功了。

  • 相关阅读:
    1442. Count Triplets That Can Form Two Arrays of Equal XOR
    1441. Build an Array With Stack Operations
    312. Burst Balloons
    367. Valid Perfect Square
    307. Range Sum Query
    1232. Check If It Is a Straight Line
    993. Cousins in Binary Tree
    1436. Destination City
    476. Number Complement
    383. Ransom Note
  • 原文地址:https://www.cnblogs.com/lvyuhua/p/12635426.html
Copyright © 2011-2022 走看看