1概念
进程ID
• 每一个进程都有一个唯一的标识符,进程ID 简称pid
– 进程id 一般默认的最大值为32768,不过也是可以修改的,当然一般情
况下不需要这么做。如果当前进程是1000,那么下一个分配的进程就是
1001,它是严格线性分配的
– 除了init 进程,其它进程都是由其它进程创立的。创立新进程的进程叫父
进程,新进程叫子进程
• man 2 getpid
• man 2 getppid
2获取进程编号
进程ID
• 获取子进程的函数
• pid_t getpid(void)
– 参数:无
– 返回值:成功返回进程号
• 获取父进程的函数
• pid_t getppid(void);
– 参数:无
– 返回值:成功返回父进程
3exec运行程序
exec函数族
• linux中,可以使用exec函数族将程序载入内存,实现多个程序的运
行
man 3 exec
• exec函数族参数
– “l”和“v”表示参数是以列表还是以数组的方式提供的
– “p”表示这个函数的第一个参数是*path,就是以绝对路径来提供程序的
路径,也可以以当前目录作为目标
– “e”表示为程序提供新的环境变量
4 fork函数创建新的process
linux中,可以使用fork函数创建和当前进程一模一样的进程,叫子进
程,原来的进程叫父进程
创建进程函数
• pid_t fork(void);
– 参数:无
– 返回值:执行成功,子进程pid 返回给父进程,0 返回给子进程;出现错
误-1,返回给父进程。执行失败的唯一情况是内存不够或者id 号用尽,
不过这种情况几乎很少发生
fork创建新进程
• fork函数(看了例子之后再回头理解一次下面几句话)
– 系统函数fork 调用成功,会创建一个新的进程,它几乎会调用差不多完
全一样的fork 进程
– 子进程的pid 和父进程不一样,是新分配的
– 子进程的ppid 会设置为父进程的pid,也就是说子进程和父进程各自的
“父进程”不一样
– 子进程中的资源统计信息会清零
– 挂起的信号会被清除,也不会被继承
– 所有文件锁也不会被子进程继承
5应用程序举例
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#if 0
extern char **environ;
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 execvpe(const char *file, char *const argv[],
char *const envp[]);
#endif
int main(int argv ,char*argc[])
{
char *arg[] = {"ls","-a",NULL};
printf("\n=================show how to user getpid==============================================\n");
printf("getpid() returns the process ID of the calling process pid:%d \n",getpid());
printf("getppid()returns the process ID of the parent of the calling process pid:%d \n",getppid());
printf("//======================================================================================\n");
printf("\n=================show how to user fork excel ==============================================\n");
if(fork() == 0)
{
//in child1
printf("fork1 is OK;execl\n");
if(execl("/bin/ls","ls","-a",NULL) == -1)
{
perror("execl error");
exit(1);
}
}
usleep(20000);
if(fork() == 0)
{
//in child2
printf("fork2 is OK;execlp\n");
if(execlp("ls","ls","-a",NULL) == -1)
{
perror("execlp error");
exit(1);
}
}
usleep(20000);
if(fork() == 0)
{
//in child3
printf("fork3 is OK;execle\n");
if(execle("/bin/ls","ls","-a",NULL,NULL) == -1)
{
perror("execle error");
exit(1);
}
}
usleep(20000);
if(fork() == 0)
{
//in child4
printf("fork4 is OK;execv\n");
if(execv("/bin/ls",arg) == -1)
{
perror("execv error");
exit(1);
}
}
usleep(20000);
if(fork() == 0)
{
//in child5
printf("fork5 is OK;execvp\n");
if(execvp("ls",arg) == -1)
{
perror("execvp error");
exit(1);
}
}
usleep(20000);
if(fork() == 0)
{
//in child6
printf("fork6 is OK;execvpe\n");
if(execve("bin/ls",arg,NULL) == -1)
{
perror("execvpe error");
exit(1);
}
}
//加入小延时可以避免发生混乱的情况
usleep(20000);
printf("//======================================================================================\n");
}
网友评论