LINUX进程控制程序设计——进程控制编程1
获取ID
#include <sys/types.h>
#include <unistd.h>
获取本进程ID:pid_t getpid(void)
获取父进程ID:pid_t getppid(void)
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/types.h>
4
5 int main(void)
6 {
7 pid_t pid;
8
9 pid = getpid();
10 printf("my pid is %d\n", pid);
11
12 pid_t ppid;
13 ppid = getppid();
14 printf("my ppid is %d\n", ppid);
15
16 return 0;
17 }
进程创建——fork
#include <unistd.h>
pid_t fork(void)
功能:创建子进程
fork被调用一次,返回两次,它可能有三种不同的返回值
1、在父进程中,fork返回新创建的子进程的PID;
2、在子进程中,fork返回0;
3、如果出现错误,fork返回一个负值。
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 int main(void)
8 {
9 pid_t pid;
10
11 pid = fork();
12
13 if(pid < 0)
14 {
15 perror("fork");
16 exit(1);
17 }
18 else if(pid == 0)
19 {
20 printf("this is child process, the id is %d\n", getpid());
21 }
22 else
23 {
24 printf("this is parent process, the id is %d\n", getpid());
25 }
26 return 0;
27 }
在pid = fork()之前,只有一个进程在执行,但在这条语句执行之后,就变成两个进程在执行了,这两个进程的共享代码段,将要执行的下一条语句都是if(pid == 0)。两个进程中,原来就存在的那个进程被称作“父进程”,新出现的那个进程被称作“子进程”,父子进程的区别在于进程标识符(PID)不同。
子进程的数据空间、堆栈空间都会从父进程得到一个拷贝,而不是共享。在子进程中对count进行加1的操作,并没有影响到父进程中的count值,父进程中的count值仍然为0。
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int main(void)
7 {
8 pid_t pid;
9 int count = 0;
10
11 pid = fork();
12
13 count++;
14 printf("count = %d\n", count);
15
16 return 0;
17 }
输出结果
count = 1
count = 1
进程创建——vfork
#include <sys/types.h>
#include <unistd.h>
pid_t vfork(void)
功能:创建子进程
fork PK vfork
区别:
1、fork:子进程拷贝父进程的数据段;vfork:子进程与父进程共享数据段
2、fork:父、子进程的执行次序不确定;vfork:子进程先运行,父进程后运行
exec函数族
exec用被执行的程序替换调用它的程序
区别:
fork创建一个新的进程,产生一个新的PID
exec启动一个新程序,替换原有的进程,因此进程的PID不会改变
#include <unistd.h>
int execl(const char *path, const char *arg1,…)
参数:
path:被执行程序名(含完整路径)
arg1 - argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束。
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 int main(void)
6 {
7 pid_t pid;
8
9 pid = vfork();
10 if(pid == 0)
11 {
12 execl("./forkmore", "forkmore", NULL);
13 }
14 return 0;
15 }
这里父进程的pid大于0,因此不进入execl函数,直接return 0。而子进程pid = vfork()后,返回0,因此进入if语句执行execl函数。
#include <unistd.h>
int execlp(const char *path, const char *arg1, …)
参数:
path:被执行程序名(不含路径,将从path环境变量中查找该程序)。
arg1 - argn:被执行程序所需的命令行参数,含程序名,以空指针(NULL)结束。
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 int main(void)
6 {
7 pid_t pid;
8 pid = vfork();
9
10 if(pid == 0)
11 {
12 execlp("ls", "ls", "-al", "/bin", NULL);
13 }
14
15 return 0;
16 }
#include <unistd.h>
int execv(const char *path, char * const argv[ ])
参数:
path:被执行程序名(含完整路径)。
argv[ ]:被执行程序所需的命令行参数数组。
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 int main(void)
6 {
7 pid_t pid;
8 pid = fork();
9
10 if(pid == 0)
11 {
12 char *argv[] = {"ls", "-al", "/bin", NULL};
13 execv("/bin/ls", argv);
14 }
15
16 return 0;
17 }
int execvp(const char *path, char * const argv[ ])
类似于execlp,参数变为数组的形式
#include <unistd.h>
int execve(const char *path, char *const argv[ ], char * const envp[ ])
#include <unistd.h>
int execle(const char *path, char *const arg, …, char * const envp[ ])
将自己定义的环境变量带入其中
#include <stdlib.h>
int system(const char *string)
功能:
调用fork产生子进程,由子进程来调用/bin/sh -c string来执行参数string所代表的命令。
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4
5 int main(void)
6 {
7 system("./a.sh");
8 return 0;
9 }
输出结果:执行a.sh脚本内容
进程的退出
void exit(int status);
void _exit(int statux);
status是一个整型的参数,可以利用这个参数传递进程结束时的状态。一般来说,0表示正常结束;其他的数值表示出现了错误,进程非正常结束。在实际编程时,可以用wait系统调用接收子进程的返回值,从而针对不同的情况进行不同的处理。
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 int main(void)
5 {
6 printf("using exit\n");
7 printf("this is content in the buffer");
8 exit(0);
9 }
运行结果:
using exit
this is content in the buffer
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4
5 int main(void)
6 {
7 printf("using exit\n");
8 printf("this is content in the buffer");
9 _exit(0);
10 }
运行结果:
using exit