进程间通信-1

来源:互联网 发布:word转pdf软件 编辑:程序博客网 时间:2024/06/02 00:45

进程间通信-1

进程相关知识

启动新进程

system

定义

#include<stdlib.h>int system(const char* string);

作用

  • system 调用一个shell来启动想要执行的程序
  • system 可以放入后台执行
  • 程序必须等待system函数启动的进程结束后才能继续

exec

定义

#include<unistd.h>int execl(const char* path,const char* arg0,....,(char*)0);int execlp(const char* file,const char* arg0,...,(char*)0);int execv(const char* path,char *const argv[]);int execvp(const char* file,char * const argv[]);

参数

  • path:文件的绝对路径
  • file:若文件在PATH中,则不需要提供绝对路径,提供文件名即可
  • (char* )0:参数结尾的标志

作用

  • exec会调用所给出来的程序,并替换当前进程,新进程的PID,PPID和nice值和原来完全一样

fork

定义

#include<sys/types.h>#include<unistd.h>pid_t fork(void);

作用

  • 系统复制当前进程,子进程有自己的数据空间,环境和文件描述符
  • 子进程返回的pid_t为0,原进程为子进程的值,由此可以区分父子进程

进程相关函数

getpid

定义

#include<unistd.h>pid_t getpid(void);

作用

  • 获取当前进程的pid

waitpid

定义

#include<sys/types.h>#include<sys/wait.h>pid_t waitpid(pid_t pid,int * stat_loc,int options);

参数

  • pid:等待的子进程的pid
  • stat_loc:如果非空就将状态信息写入
  • options:改变waitpid的行为

作用

  • 等待子进程结束
0 0
原创粉丝点击