进程体系相关内容

进程控制块PCB

task_struct结构体:

进程id。系统中每个进程有唯一的id,在C语言中用pid_t类型表示,其实就是一个非负整数。

进程的状态。有运行(运行、就绪)、挂起、停止、僵尸等状态。

进程切换时需要保存和恢复的一些CPU寄存器。

描述虚拟地址空间的信息。

描述控制终端的信息。

当前工作目录(Current Working Directory)。

umask掩码。

文件描述符表。包含很多指向file结构体的指针。

和信号相关的信息。

用户id和组id。

控制终端、Session和进程组。

进程可以使用的资源上限(Resoruce Limit)。

进程控制fork

fork 的作用是根据一个现有的进程复制出一个新进程,原来的进程称为父进程(Parent Process),新进程称为子进程(Child Process)。系统中同时运行着很多进程,这些进程都是从最初只有一个进程开始一个一个复制出来的。

在Shell下输入命令可以运行一个程序,是因为Shell进程在读取用户输入的命令之后会调用fork复制出一个新的Shell进程。

写一个简单程序,主进程打印3次,子进程打印5次,了解进程执行顺序。

#include "./common/head.h"

/*功能:
 *主进程打印3次,子进程打印4次。
*/

int main()
{
    int n;    //打印次数,在主进程和子进程中,分别赋不同的值
    char *msg;    //打印内容,主进程和子进程赋不同的值

    pid_t pid = fork();
    if(pid < 0){
        perror("fork");
        exit(1);
    }

    if(pid == 0){    //子进程
        n = 5;
        msg = "child process~\n";
    }else{    //主进程
        n = 3;
        msg = "parent process\n";
    }
    
    //无论主进程还是子进程,都会执行到这里
    while(n--){
        printf(msg);
        sleep(1);
    }

    return 0;
}

结果: 

 

注意:父进程和子进程,到底哪个先运行,完全由CPU调度,不可控。

bash先复制一个进程./a.out,a.out进程又复制了一个子进程。即bash是a.out的爹,a.out又是子进程的爹。当a.out结束以后,它的父进程bash会给a.out收尸,但a.out的子进程却还在运行,a.out的子进程结束之后,因为父亲a.out已经死了,所以它变成孤儿进程,由1号进程收养。

解析:主进程、子进程执行过程,如下图所示:

 

 getpid/getppid函数:

#include <sys/types.h>

#include <unistd.h>

pid_t getpid(void);

pid_t getppid(void);

fork在子进程中返回0,子进程可以调用getpid函数得到自己的进程id,也可以调用getppid函数得到父进程的id。在父进程中调用getpid可以得到自己的进程id,然而想要得到子进程的id,只有将fork的返回值记录下来,别无它法。

创建10个子进程,并打印它们的pid和父亲的pid,代码演示如下:

#include "./common/head.h"

/*功能:
 *创建10个子进程,并打印它们的pid和它们父亲的pid。
*/

int main()
{
    pid_t pid;
    for(int i = 0; i < 10; i++)
    {
        pid = fork();
        if(pid < 0){
            perror("fork");
            exit(1);
        }
        if(pid == 0){    //子进程,等价于if(!pid)
            printf("child[%d], pid = %d, parentpid = %d\n", i, getpid(), getppid());
            break;    //子进程跳出循环,否则编号9的子进程创建出8个子进程,编号为8的子进程又会创建出7个子进程,以此类推。爆栈。
        }
    }

    return 0;
}

结果:

解析: 

由于CPU的调度问题,可能运行结果与上图有差异,由此可见,父子进程甚至子进程之间,都存在竞争关系,CPU调度到哪个,哪个就开始执行,并没有先后顺序。也有可能出现子进程变成孤儿,parentpid=1的情况。

 

 

自编译tensorflow1.python3.5,tensorflow1.12; 2.支持cuda10.0,cudnn7.3.1,TensorRT-5.0.2.6-cuda10.0-cudnn7.3; 3.支持mkl,无MPI; 软硬件硬件环境:Ubuntu16.04,GeForce GTX 1080 配置信息: hp@dla:~/work/ts_compile/tensorflow$ ./configure WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown". You have bazel 0.19.1 installed. Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 Found possible Python library paths: /usr/local/lib/python3.5/dist-packages /usr/lib/python3/dist-packages Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages] Do you wish to build TensorFlow with XLA JIT support? [Y/n]: XLA JIT support will be enabled for TensorFlow. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: No OpenCL SYCL support will be enabled for TensorFlow. Do you wish to build TensorFlow with ROCm support? [y/N]: No ROCm support will be enabled for TensorFlow. Do you wish to build TensorFlow with CUDA support? [y/N]: y CUDA support will be enabled for TensorFlow. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 10.0]: Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-10.0 Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.3.1 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-10.0]: Do you wish to build TensorFlow with TensorRT support? [y/N]: y TensorRT support will be enabled for TensorFlow. Please specify the location where TensorRT is installed. [Default is /usr/lib/x86_64-linux-gnu]:/home/hp/bin/TensorRT-5.0.2.6-cuda10.0-cudnn7.3/targets/x86_64-linux-gnu Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]: Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1,6.1,6.1]: Do you want to use clang as CUDA compiler? [y/N]: nvcc will be used as CUDA compiler. Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Do you wish to build TensorFlow with MPI support? [y/N]: No MPI support will be enabled for TensorFlow. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: Not configuring the WORKSPACE for Android builds. Preconfigured Bazel build configs. You can use any of the below by adding "--config=" to your build command. See .bazelrc for more details. --config=mkl # Build with MKL support. --config=monolithic # Config for mostly static monolithic build. --config=gdr # Build with GDR support. --config=verbs # Build with libverbs support. --config=ngraph # Build with Intel nGraph support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. --config=noignite # Disable Apacha Ignite support. --config=nokafka # Disable Apache Kafka support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished 编译: hp@dla:~/work/ts_compile/tensorflow$ bazel build --config=opt --config=mkl --verbose_failures //tensorflow/tools/pip_package:build_pip_package 卸载已有tensorflow: hp@dla:~/temp$ sudo pip3 uninstall tensorflow 安装自己编译的成果: hp@dla:~/temp$ sudo pip3 install tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值