我们知道,每个进程在内核中都有一个进程控制块( PCB)来维护进程相关的信息, Linux内核的进程控制块是task_struct结构体。具体内容如下:
exec系统调用执行新程序时会把命令行参数和环境变量表传递给main函数,它们在整个进程地址空间中的位置如下图所示。
环境变量字符串都是name=value这样的形式,大多数name由大写字母加下划线组成,一般把name的部分叫做环境变量, value的部分则是环境变量的值。
环境变量也是一组字符串。libc中定义的全局变量environ指向环境变量表, environ没有包含在任何头文件中,所以在使用时要用extern声明。
例如:
#include <stdio.h>
int main(void)
{
extern char **environ;
int i;
for(i=0; environ[i]!=NULL; i++)
printf("%s\n", environ[i]);
return 0;
}
由于父进程在调用fork创建子进程时会把自己的环境变量表也复制给子进程,所以a.out打印的环境变量和Shell进程的环境变量是相同的。
给出name要在环境变量表中查找它对应的value,可以用getenv函数。
#include <stdlib.h>
char *getenv(const char *name);
/*返回值是指向value的指针,若未找到则为NULL*/
#include <stdlib.h>
int setenv(const char *name, const char *value, int rewrite);
void unsetenv(const char *name);
/*成功则返回为0,若出错则返回非0*/
如果已存在环境变量name,那么若rewrite非0,则覆盖原来的定义;若rewrite为0,则不覆盖原来的定义,也不返回错误。
fork的作用是根据一个现有的进程复制出一个新进程,原来的进程称为父进程( Parent Process) ,新进程称为子进程( Child Process) 。系统中同时运行着很多进程,这些进程都是从最初只有一个进程开始一个一个复制出来的。在Shell下输入命令可以运行一个程序,是因为Shell进程在读取用户输入的命令之后会调用fork复制出一个新的Shell进程,然后新的Shell进程调用exec执行新的程序。
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
/*失败返回-1,成功子进程返回0,父进程返回子进程PID*/
实例
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
pid_t pid;
char *message;
int n;
pid = fork();
if (pid < 0)
{
perror("fork failed");
exit(1);
}
if (pid == 0)
{
message = "This is the child\n";
n = 6;
}
else
{
message = "This is the parent\n";
n = 3;
}
for(; n > 0; n--)
{
printf(message);
sleep(1);
}
return 0;
}
注:gdb只能跟踪一个进程
当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程开始执行。调用exec并不创建新进程,所以调用exec前后该进程的id并未改变。调用exec后,原来打开的文件描述符仍然是打开的。其实有六种以exec开头的函数,统称exec函数:
#include <unistd.h>
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char *const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve(const char *path, char *const argv[], char *const envp[]);
调用实例
char *const ps_argv[] ={"ps", "-o", "pid,ppid,pgrp,session,tpgid,comm", NULL};
char *const ps_envp[] ={"PATH=/bin:/usr/bin", "TERM=console", NULL};
execl("/bin/ps", "ps", "-o", "pid,ppid,pgrp,session,tpgid,comm", NULL);
execv("/bin/ps", ps_argv);
execle("/bin/ps", "ps", "-o", "pid,ppid,pgrp,session,tpgid,comm", NULL, ps_envp);
execve("/bin/ps", ps_argv, ps_envp);
execlp("ps", "ps", "-o", "pid,ppid,pgrp,session,tpgid,comm", NULL);
execvp("ps", ps_argv);
注:事实上,只有execve是真正的系统调用,其它五个函数最终都调用execve。
一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的PCB还保留着,内核在其中保存了一些信息:如果是正常终止则保存着退出状态,如果是异常终止则保存着导致该进程终止的信号是哪个。这个进程的父进程可以调用wait或waitpid获取这些信息,然后彻底清除掉这个进程。如果一个进程已经终止,但是它的父进程尚未调用wait或waitpid对它进行清理,这时的进程状态称为僵尸( Zombie) 进程。
如果一个父进程终止,而它的子进程还存在(这些子进程或者仍在运行,或者已经是僵尸进程了),则这些子进程的父进程改为init进程。
僵尸进程是不能用kill命令清除掉的,因为kill命令只是用来终止进程的,而僵尸进程已经终止了。
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
/*调用成功则返回清理掉的子进程id,若调用出错则返回-1*/
父进程调用wait或waitpid时可能会:
wait waitpid区别:
status:如果参数status不是空指针,则子进程的终止信息通过这个参数传出,如果只是为了同步而不关心子进程的终止信息,可以将status参数指定为NULL。
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror("fork failed");
exit(1);
}
if (pid == 0)
{
int i;
for (i = 3; i > 0; i--)
{
printf("This is the child\n");
sleep(1);
}
exit(3);
}
else
{
int stat_val;
waitpid(pid, &stat_val, 0);
if (WIFEXITED(stat_val))
printf("Child exited with code%d\n", WEXITSTATUS(stat_val));
else if (WIFSIGNALED(stat_val))
printf("Child terminated abnormally, signal %d\n", WTERMSIG(stat_val));
}
return 0;
}
子进程的终止信息在一个int中包含了多个字段,用宏定义可以取出其中的每个字段:
每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,内核提供的这种机制称为进程间通信( IPC, InterProcess Communication) 。
管道是最简单的一种IPC机制。可以用pipe函数创建。
#include <unistd.h>
int pipe(int filedes[2]);
/*pipe函数调用成功返回0,调用失败返回-1*/
调用pipe函数时在内核中开辟一块缓冲区(称为管道)用于通信,它有一个读端一个写端,然后通过filedes参数传出给用户程序两个文件描述符, filedes[0]指向管道的读端, filedes[1]指向管道的写端。
如图,父进程可以往管道里写,子进程可以从管道里读,管道是用环形队列实现的,数据从写端流入从读端流出,这样就实现了进程间通信。
实例
#include <stdlib.h>
#include <unistd.h>
#define MAXLINE 80
int main(void)
{
int n;
int fd[2];
pid_t pid;
char line[MAXLINE];
if (pipe(fd) < 0)
{
perror("pipe");
exit(1);
}
if ((pid = fork()) < 0)
{
perror("fork");
exit(1);
}
if (pid > 0)
{ /* parent */
close(fd[0]);
write(fd[1], "hello world\n", 12);
wait(NULL);
}
else
{ /* child */
close(fd[1]);
n = read(fd[0], line, MAXLINE);
write(STDOUT_FILENO, line, n);
}
return 0;
}
使用管道有一些限制:
使用管道需要注意以下4种特殊情况(假设都是阻塞I/O操作,没有设置O_NONBLOCK标志):
FIFO和UNIX Domain Socket这两种IPC机制都是利用文件系统中的特殊文件来标识的。
可以用mkfifo命令创建一个FIFO文件:
FIFO文件在磁盘上没有数据块,仅用来标识内核中的一条通道,各进程可以打开这个文件进行read/write,实际上是在读写内核通道(根本原因在于这个file结构体所指向的read、 write函数和常规文件不一样),这样就实现了进程间通信。
UNIX Domain Socket和FIFO的原理类似,也需要一个特殊的socket文件来标识内核中的通道,例如/var/run目录下有很多系统服务的socket文件:
文件类型s表示socket,这些文件在磁盘上也没有数据块。 UNIX Domain Socket是目前最广泛使用的IPC机制。