进程控制

进程控制函数
创建子进程
#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);    
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值