进程控制

本文详细介绍了进程控制的基本概念、核心函数及其在实际应用中的实现方式,包括创建子进程、执行新程序、获取进程ID等关键操作,并讨论了僵尸进程的处理策略及示例程序演示。
进程控制函数
创建子进程
#include <unistd.h> 

int fork(); // 子进程中返回0,在父进程中返回子进程ID,出错返回-1
int vfork(); // vfork与fork在语义上一致,但vfork不会复制父进程空间,用vfork创建的子进程以与父进程共享地址空间的方式运行;另外调用vfork后父进程将会阻塞,直到子进程调用exit或exec为止

执行新程序
#include <unistd.h>

int execl(cosnt char* pathname, const char* arg0, ..., (const char*)0);
int execle(const char* pathname, const char* arg0, ..., (const char*)0, const char* env); 
int execlp(const char* filename, const char* arg0, ..., (const char*)0);
int execv(const char* pathname, const char *argv[]);
int execve(const char* pathname, const char* argv[], const char* env);
int execvp(const char* filename, const char* argv[]);

上述函数以一个新的程序替换当前进程空间(包括代码段、数据段、堆、栈段)。若失败返回-1,成功不返回。
所有exec函数中只有execve是系统调用,其他都是库函数。六个exec函数的关系如下:


获取进程ID
#include <unistd.h>

pid_t getpid(); // 返回调用进程的进程ID
pid_t getppid(); // 返回调用进程的父进程ID

进程同步函数
#include <sys/wait.h>

int wait(int *status); // 阻塞,直到任意进程返回
int waitpid(pid_t pid, int *status, int options); // 阻塞,直到指定进程返回;另可设置options使其不阻塞
返回值:若成功返回进程ID,若失败返回-1
waitpid的第一个参数说明:
pid==-1等待任意子进程
pid>0等待进程ID等于pid的子进程
pid==0等待进程组ID等于调用进程组ID的任意子进程
pid<0等待进程组ID等于pid绝对值的任意子进程

示例程序:
cat.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

static const int s_buffSize = 8;
char buff[s_buffSize];

int main(int argc, char* argv[])
{
     int n = 0;
     while ((n=read(STDIN_FILENO, buff, s_buffSize)) > 0)
     {
          if (write(STDOUT_FILENO, buff, n) != n)
          {
               printf("write error! pid=%d\n", getpid());
               exit(1);
          }
     }
     exit(0);
}
fork.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

static const int s_maxChildP = 10;

int main(int argc, char *argvp[])
{
     pid_t childPid = -1;
     if ((childPid = fork()) < 0)
     {
          printf("fork error!\n");
          abort();
     }
     else if (childPid == 0) // 子进程
     {
          execlp("./cat", "cat", NULL);
     }

     waitpid(childPid, NULL, 0);
    
     exit(0);
}
基本进程控制原语   
fork,exec,exit,wait组成了基本的进程控制原语:fork创建新进程,exec执行新程序,exit处理终止,wait等待终止。

僵尸进程
若子进程结束时,父进程未对其进行善后处理(waitpid),那么该子进程就成为了僵尸进程。
若子进程的父进程先于自己结束,那么该子进程讲会被init(pid=1)进程领养。
若父进程不想等待子进程,而子进程又不至沦为僵尸进程的处理方式是:调用fork函数两次,一次在父进程中调用,一次在子进程中调用。
示例如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
     int pid = -1;
     if ((pid = fork()) < 0)
     {
          printf("fork error.\n");
          abort();
     }
     else if(pid == 0)
     {
          if ((pid = fork()) < 0)
          {
               printf("fork error.\n");
          }
          else if(pid > 0)
               exit(0);

          sleep(2);
          printf("child process. pid=%d  ppid=%d\n", getpid(), getppid());
          exit(0);
     }

     printf("parent process. pid=%d\n", getpid());
    
     exit(0);    
}
内容概要:本文介绍了ENVI Deep Learning V1.0的操作教程,重点讲解了如何利用ENVI软件进行深度学习模型的训练与应用,以实现遥感图像中特定目标(如集装箱)的自动提取。教程涵盖了从数据准备、标签图像创建、模型初始化与训练,到执行分类及结果优化的完整流程,并介绍了精度评价与通过ENVI Modeler实现一键化建模的方法。系统基于TensorFlow框架,采用ENVINet5(U-Net变体)架构,支持通过点、线、面ROI或分类图生成标签数据,适用于多/高光谱影像的单一类别特征提取。; 适合人群:具备遥感图像处理基础,熟悉ENVI软件操作,从事地理信息、测绘、环境监测等相关领域的技术人员或研究人员,尤其是希望将深度学习技术应用于遥感目标识别的初学者与实践者。; 使用场景及目标:①在遥感影像中自动识别和提取特定地物目标(如车辆、建筑、道路、集装箱等);②掌握ENVI环境下深度学习模型的训练流程与关键参数设置(如Patch Size、Epochs、Class Weight等);③通过模型调优与结果反馈提升分类精度,实现高效自动化信息提取。; 阅读建议:建议结合实际遥感项目边学边练,重点关注标签数据制作、模型参数配置与结果后处理环节,充分利用ENVI Modeler进行自动化建模与参数优化,同时注意软硬件环境(特别是NVIDIA GPU)的配置要求以保障训练效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值