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

    个人信息:

    姓名:张越

    班级:计算1811

    学号:201821121006

    1. 编写程序

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

     #include<stdio.h>
      2 #include<sys/types.h>
      3 #include<unistd.h>
      4 int main()
      5 {
      6     pid_t pid;
      7     int i ;
      8     for(i=0;i<2;i++){
      9         pid=fork();
     10         if(pid<=0)
     11             break;
     12     }
     13     if(pid<0){
     14         printf("error in fork
    ");
     15     }
     16     else if(pid==0){    //子进程
     17         printf("child process,process id is %d
    ",getpid());
     18     }
     19     else {    //父进程
     20         printf("parent process,process id is %d
    ",getpid());
     21     }
     22     sleep(1000);  //延迟
     23     return 0;

    2. 打印进程树

    进程结果:

    打印进程树:

    3. 解读进程相关信息

    (1) ps -ef

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

    UID        PID  PPID  C STIME TTY       TIME    CMD
    zhangyue 20383 20141  0 14:21 pts/3    00:00:00 ./firsttest
    zhangyue 20384 20383  0 14:21 pts/3    00:00:00 ./firsttest
    zhangyue 20385 20383  0 14:21 pts/3    00:00:00 ./firsttest

    分析字段的的含义

    UID: 表示用户ID

    PID: 进程的ID号,上述程序产生三个进程,它们的ID分别为20383,20384和20385

    PPID: 父进程的ID,进程20383的父进程是进程20141,进程20384和进程20385的父进程是20383 

    C: CPU使用的资源百分比

    STIME: 系统启动时间

    TTY: 终端的次要装置号码

    TIME: 进程使用CPU的时间

    CMD: 所下达的指令名称

    (2) ps -aux

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

    USER       PID  %CPU %MEM  VSZ   RSS TTY      STAT START   TIME COMMAND
    zhangyue 20383  0.0  0.0   4508   764 pts/3    S    14:21   0:00 ./firsttest
    zhangyue 20384  0.0  0.0   4508    72 pts/3    S    14:21   0:00 ./firsttest
    zhangyue 20385  0.0  0.0   4508    72 pts/3    S    14:21   0:00 ./firsttest
    • USER:进程所属用户的用户名;
    • PID:进程号;
    • %CPU:占用的CPU使用率;
    • %MEM:占用的内存使用率;
    • VSZ:占用的虚拟机内存大小;
    • RSS:占用的内存大小;
    • TTY:终端的次要装置号码;
    • START:进程开始时间;
    • COMMAND:进程执行的指令。‘

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

    1.ps-aux指令执行后 start常见的状态除了S,还有哪些?

    D:无法中断的休眠状态

    R:正在运行可中在队列中可过行的

    T:停止或被追踪

    W:进入内存交换

    2.sleep()函数的意义:

    用于延时,我个人觉得跟cmd命令行中的pause的作用很相似。

    3.我发现了在Ubuntu 用ps-aux的命令时候,每一次都有记录并且还可以查看其他用户的动态。

    4,同一个进程在执行的时候,每次执行都有不同的进程的id号。

  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    微信小程序TodoList
    C语言88案例-找出数列中的最大值和最小值
    C语言88案例-使用指针的指针输出字符串
  • 原文地址:https://www.cnblogs.com/hltz/p/12637342.html
Copyright © 2011-2022 走看看