初识进程

fork函数:

    在Linux中fork函数非常重要,它从已经存在的进程中创建一个新进程。新进程为子进程原进程为父进程。

#include<unistd.h>
pid_t fork(void);
返回值:子进程返回0,父进程返回子进程id,错误-1。

进程调用fork,当控制转移到内核中的fork代码后,内核做:

1.分配新的内存块和内存数据结构给子进程

2.将父进程部分数据结构内容拷贝至子进程

3.添加子进程到系统进程列表当中

4.fork返回,开始调度器调度

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
    pid_t pid;
    printf("before:pid is %d\n",getpid());
    if((pid = fork()) == -1)
    {
        perror("fork");
        exit(1);
    }
    printf("after:pid is %d,fork return is %d\n",getpid(),pid);
    sleep(1);
    return 0;

}


[root@localhost fork]# ./a.out 
before:pid is 4303
after:pid is 4303,fork return is 4304
after:pid is 4304,fork return is 0

仔细想一下原理图:


fork之前,父进程独立执行,fork之后,父子进程两个执行流分别在执行。但是fork之后,谁先执行完全由调度器决定。

写实拷贝:父子代码共享,当任意一方试图写入,便以写实拷贝的方式各自一份副本。

fork用法:

1.一个父进程希望复制自己,使父子进程执行不同的代码段。

2.一个进程要执行一个不同的程序。

fork失败原因:

1.系统中有太多的进程。

2.用户的进程数超过限制。


vfork

1.vfork用于创建一个子进程,而子进程和父进程共享地址空间,fork的子进程具有独立的地址空间。

2.vfork保证子进程先运行,在它调用exec或(exit)后父进程才可能被调度运行。

#include <sys/types.h>
#include <unistd.h>

pid_t vfork(void);
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include<stdlib.h>


int num = 100;

int main()
{
    pid_t pid;
    if((pid = vfork()) == -1)
    {
        perror("vfork");
        exit(1);
    }
    if(pid == 0)//child
    {
        sleep(3);
        num = 200;
        printf("child num:%d\n",num);
        exit(0);
    }
    printf("father num:%d\n",num);//father
    return 0;
}

[root@localhost vfork]# gcc vfork.c 
[root@localhost vfork]# ./a.out 
child num:200
father num:200

子进程直接改变了父进程的变量值,因为子进程在父进程的地址空间内运行。

进程终止

1.代码运行结束,结果正确

2.代码运行结束,结果不正确

3.代码异常终止

退出的方法

1.从main返回

2调用exit

3._exit

#include <unistd.h>
void _exit(int status);

参数status定义的进程的终止状态,父进程通过wait获取该值。

#include <unistd.h>
void _exit(int status);

代码实验:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    printf("hello world!");
    exit(0);
}
[root@localhost exit]# gcc exit.c 
[root@localhost exit]# ./a.out 
hello world![root@localhost exit]#

#include<stdio.h>
#include<unistd.h>

int main()
{
    printf("hello world!\n");
    _exit(0);
}

[root@localhost exit]# gcc _exit.c 
[root@localhost exit]# ./a.out 
[root@localhost exit]# 

以上需要注意两点:

1.部分编译器版本可能头文件封装的库不同,最好查看以下man手册。

2.\n 有刷新缓冲区的功能,如果在打印中加入\n,两个函数都会输出语句,不能看出实验结果。

return和exit(n)等同,main的返回值将会当作exit的参数。


进程等待

必要性:

1.僵尸进程,造成内存泄漏。

2.无法杀死一个已经死去的进程。kill-9 也无能无力。

3.父进程需要知道子进程任务完成情况。

4.父进程通过进程等待方式,回收子进程资源,获取子进程退出信息。


等待方法

wait

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

返回值:wait:成功返回等待进程pid,失败-1,

参数:输出型参数,获取子进程状态,不关心可以设置为NULL

返回值:waitpid:

1.正常返回,收集子进程的id,

2.若设置了WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,返回0

3.调用出错,返回-1.error会被设置成相应的值,指示错误所在。

参数:

pid:

pid = -1:等待任一个子进程,与wait等效

pid > 0:等待其进程ID与pid相等的子进程。

status:

    WIFEXITED(status):查看进程是否正常退出

    WEXITSTATUS(status):查看进程的退出码

options:

WNOHANG:若pid指定的子进程还没有结束,则waitpid()函数返回0,不再等待。若正常结束,则返回该子进程的ID。

测试wait:

#include<stdio.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<unistd.h>

int main()
{
    pid_t pid;
    if((pid = fork()) == -1)
    {
        perror("fork");
        exit(1);
    }
    if(pid == 0)
    {
        sleep(20);
        exit(10);
    }
    else
    {
        int st;
        int ret = wait(&st);
        if(ret > 0 && (st & 0x7F) == 0) //正常退出
        {
            printf("child exit code:%d\n",(st>>8)&0xFF);
        }
        else if(ret > 0)//异常退出
        {
            printf("sig code :%d\n",st&0x7F);
        }
    }
}
[root@localhost status]# gcc status.c 
[root@localhost status]# ./a.out 
child exit code:10
[root@localhost status]# ./a.out 
在另一个终端下查找喊到进程号,并kill -9 +进程号  杀死进程
sig code :9

waitpid:

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>


int main()
{
    pid_t pid = fork();
    if(pid < 0) 
    {
        perror("fork");
        exit(1);
    }
   else if(pid == 0)
    {
        printf("child is run ,pid is :%d\n",getpid());
        sleep(5);
        exit(257);
    }
    else
    {
        int status = 0;
        pid_t ret = waitpid(-1,&status,0);//阻塞式等待5s
        printf("this is test for wait\n");
        if(WIFEXITED(status) && ret == pid)
        {
            printf("wait child 5s success,child return code is :%d\n",WEXITSTATUS(status));
        }
        else
        {
            printf("wait child failed, return.\n");
            return 1;
        }
    }
    return 0;
}
[root@localhost status]# gcc waitpid.c 
[root@localhost status]# ./a.out 
child is run ,pid is :4915
this is test for wait
wait child 5s success,child return code is :1

程序替换

    用fork创建子进程后执行的是和父进程相同的程序(可能执行不同的代码分支),子进程往往要调用一种exec函数来执行另一个程序。当进程调用一种exec函数时,该进程的用户空间代码和数据完全被新的程序替换。exec并不会创建新的进程,所以调用前后该进程的id不会改变。

       

 #include <unistd.h>

       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[]);

这些函数的第一个参数是待执行程序的路径名(文件名).

       在函数execl,execlp,和execle中,const char *arg 以及省略号 代表 的 参数 可被 视为  arg0,  arg1,  ...,  argn. 他们合起来描述了指向null结尾的字符串的指针 列表, 即执行程序的参数列表. 作为约定, 第一个arg参数应该指向执行程序名自身. 参数列表必须用NULL指针结束!
       execv和execvp函数提供指向null结尾的字符串的指针数组作为新程序的参数列表.  作为约定, 指针数组中第一个元素应该指向执行程序名自身. 指针数组必须 用 NULL 指针 结束!
      execle函数同时说明了执行进程的环境(environment), 他在NULL指针后面要求一个附加参数,NULL指针用于结束
       参数列表, 或者说,argv数组.这个附加参数是指向null结尾的字符串的指针组, 他必须用NULL指针结束!其他函数从当前进程的environ外部变量中获取新进程的 环境.
       某些函数有特殊的语义.
       如果提供的文件名中不包含斜杠符(/),函数execlp和execvp将同shell一样搜索 执行文件. 搜索路径由环境变量PATH指定.如果该变量不存在,则使用缺省路径 ``:/bin:/usr/bin''.另外, 某些错误要特殊处理.如果对某个文件的访问遭到拒绝 ( execve 返回 EACCES), 这些函数将在搜索路径中继续寻找. 如果没有找到符合的 文件,他们返回时把 errno 置为 EACCES.

        如果无法识别文件首部(execve返回ENOEXEC), 这些函数将以文件名作为第一个参数调用shell. (如果这个尝试失败就不再进行搜索 了.)

返回值 (RETURN VALUE)

       任何exec函数返回均表明发生了错误. 返回值是 -1, 全局变量errno指出错误类型.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值