linux进程控制初步认识
来源:互联网 发布:linux 安装命令 编辑:程序博客网 时间:2024/05/12 00:11
#include<sys/types.h>
#include<unistd.h>
pid_t getpid(void) //获取本进程ID
pid_t getppid(void) //获取父进程ID
获取ID
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(void)
{
printf("PID = %d\n",getpid());
printf("PPID = %d\n",getppid());
return(EXIT_SUCCESS);
}
进程创建-fork
#include<unistd.h>
pid_t fork(void) //创建子进程
fork()调用一次,返回2次值
1,在父进程中,fork()返回新创建的子进程PID
2,在子进程中,fork()返回0
3,如果出现错去,fork()返回负值
进程创建fork(),子进程数据,堆栈从父进程得到拷贝,而不是共享
代码共享。
#include<sys/types.h>
#include<unistd.h>
int main(void)
{
pid_t pid;
pid = fork();
if(pid < 0)
puts("error in fork!");
//此时已经有32个进程在运行。
else if(0 == pid)
printf("in the child process ID is %d and PPID is %d\n",getpid(),getppid());
else
printf("in the parent process ID is %d and PPID is %d\n",getpid(),getppid());
return 0;
}
进程创建-vfork
#include<sys/types.h>
#include<unistd.h>
pid_t vfork(void) //创建子进程
区别:fork(),子进程拷贝父进程数据
vfork(),子进程与父进程共享数据段
fork(), 父子进程执行顺序不确定
vfork(),子进程先运行,父进程后运行
exec函数族
exec用被执行的的程序替换调用它的程序
区别:fork/vfork创建新进程,产生一个新的PID
exec启动新程序,替换原有进程,PID不变
#include<unistd.h>
int execl(const char *path,const char *arg1,...)
参数
path:被执行的程序名(含完整路径)
arg1~argn:被执行程序所需要的命令行参数,含程序名,以空指针结束
#include<unistd.h>
int main(void)
{
execl("/bin/ls","ls","-al","/work",NULL);
return 0;
}
#include<unistd.h>
int execlp(const char *path,const char *arg1,...)
参数
path:被执行的程序名(不含路径,从path环境变量中查找程序)
arg1~argn:被执行程序所需要的命令行参数,含程序名
#include<unistd.h>
int main(void)
{
execlp("ls","ls","-al","/work",NULL);
return 0;
}
#include<unistd.h>
int execv(const char *path,char * const argv[])
参数
path:被执行的程序名(含完整路径)
argv[]:被执行程序所需要的命令行参数组
#include<unistd.h>
int main(void)
{
char *argv[] = {"ls","-al","/work",NULL};
execv("/bin/ls",argv);
return 0;
}
#include<stdlib.h>
int system(const char * string)
功能:
调用fork()产生子进程,由子进程调用/bin/sh -c string执行参数string代表的命令
#include<unistd.h>
int main(void)
{
system("ls -al /work");
return 0;
}
进程等待
#include<sys/types.h>
#include<sys/wait.h>
pid_t wait(int *status) //阻塞该进程,直到某个子进程退出,返回子进程PID
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
pid_t pc,pr;
pc = fork();
if(0 == pc)
{
printf("child process with PID %d\n",getpid());
sleep(10);
}
else if(pc < 0)
puts("error in fork");
else
{
pr = wait(NULL);
printf("catch a child process with PID %d\n",pr);
}
return EXIT_SUCCESS;
}
#include<unistd.h>
pid_t getpid(void) //获取本进程ID
pid_t getppid(void) //获取父进程ID
获取ID
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(void)
{
printf("PID = %d\n",getpid());
printf("PPID = %d\n",getppid());
return(EXIT_SUCCESS);
}
进程创建-fork
#include<unistd.h>
pid_t fork(void) //创建子进程
fork()调用一次,返回2次值
1,在父进程中,fork()返回新创建的子进程PID
2,在子进程中,fork()返回0
3,如果出现错去,fork()返回负值
进程创建fork(),子进程数据,堆栈从父进程得到拷贝,而不是共享
代码共享。
#include<sys/types.h>
#include<unistd.h>
int main(void)
{
pid_t pid;
pid = fork();
if(pid < 0)
puts("error in fork!");
//此时已经有32个进程在运行。
else if(0 == pid)
printf("in the child process ID is %d and PPID is %d\n",getpid(),getppid());
else
printf("in the parent process ID is %d and PPID is %d\n",getpid(),getppid());
return 0;
}
进程创建-vfork
#include<sys/types.h>
#include<unistd.h>
pid_t vfork(void) //创建子进程
区别:fork(),子进程拷贝父进程数据
vfork(),子进程与父进程共享数据段
fork(), 父子进程执行顺序不确定
vfork(),子进程先运行,父进程后运行
exec函数族
exec用被执行的的程序替换调用它的程序
区别:fork/vfork创建新进程,产生一个新的PID
exec启动新程序,替换原有进程,PID不变
#include<unistd.h>
int execl(const char *path,const char *arg1,...)
参数
path:被执行的程序名(含完整路径)
arg1~argn:被执行程序所需要的命令行参数,含程序名,以空指针结束
#include<unistd.h>
int main(void)
{
execl("/bin/ls","ls","-al","/work",NULL);
return 0;
}
#include<unistd.h>
int execlp(const char *path,const char *arg1,...)
参数
path:被执行的程序名(不含路径,从path环境变量中查找程序)
arg1~argn:被执行程序所需要的命令行参数,含程序名
#include<unistd.h>
int main(void)
{
execlp("ls","ls","-al","/work",NULL);
return 0;
}
#include<unistd.h>
int execv(const char *path,char * const argv[])
参数
path:被执行的程序名(含完整路径)
argv[]:被执行程序所需要的命令行参数组
#include<unistd.h>
int main(void)
{
char *argv[] = {"ls","-al","/work",NULL};
execv("/bin/ls",argv);
return 0;
}
#include<stdlib.h>
int system(const char * string)
功能:
调用fork()产生子进程,由子进程调用/bin/sh -c string执行参数string代表的命令
#include<unistd.h>
int main(void)
{
system("ls -al /work");
return 0;
}
进程等待
#include<sys/types.h>
#include<sys/wait.h>
pid_t wait(int *status) //阻塞该进程,直到某个子进程退出,返回子进程PID
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
pid_t pc,pr;
pc = fork();
if(0 == pc)
{
printf("child process with PID %d\n",getpid());
sleep(10);
}
else if(pc < 0)
puts("error in fork");
else
{
pr = wait(NULL);
printf("catch a child process with PID %d\n",pr);
}
return EXIT_SUCCESS;
}
- linux进程控制初步认识
- Linux 进程初步认识1
- 进程的初步认识
- 初步认识进程
- 对进程的初步认识
- android进程的初步认识
- 1、初步认识Linux
- Linux初步认识
- linux的初步认识
- linux操作系统初步认识(一)
- linux操作系统初步认识(二)
- linux操作系统初步认识(三)
- Linux网络编程初步认识
- 初步认识linux与入门
- 关于Linux的初步认识
- 初步对linux的认识
- linux命令的初步认识
- Linux进程之初步了解
- C/C++ 分割字符串的多种方式
- jQuery实现全选、反选、全不选效果
- 公有云和私有云的区别
- UILable的常用用法
- 数据库关系代数表达式学习
- linux进程控制初步认识
- easyui datagrid combobox 选择后显示valueField 而不是 textValue解决方法
- C++程序设计学习第一季
- 关于C++的重载等问题
- WordPress代码实现相关文章的几种方法(一)
- huffman 文件 压缩 解压缩
- Winform程序中打开网页,并登陆
- 思维导图笔记——软件工程
- UserTransaction