unix高级编程-fork和execve

简介: unix高级编程-fork和execve

fork和vfork

vfork是老的实现方法又很多问题

vfork

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{    
    pid_t pid;
    printf("befor fork pid: %d\n",getpid());
    int a = 10;
    pid = vfork();
    if(pid == -1)
    {
         perror("失败!\n");
         return -1;       
    }
    if(pid > 0){
        printf("parent:pid: %d\n",getpid());    
        printf("parent:a:%d\n",a);    
    }
    else if(pid == 0)
    {    
        printf("child:%d,parent:%d\n",getpid(),getppid());   
        printf("child:a:%d\n",a);                         
    }
    return 0;
}

可以看到这里是有问题的

vfork常和 execve() 一起使用

就像Python中的os.system(cmd)这个函数,我们可以用这个函数来执行我们的shell脚本,单独的shell命令,或者是调用其他的程序,我们的execve()这个函数就和Python中的os.system函数类似,可以调用其他程序的执行,执行shell命令,,调用脚本等等功能。

int execve(const char *filename, char *const argv[], 
           char *const envp[]);

execve()执行程序由 filename决定。

filename必须是一个二进制的可执行文件,或者是一个脚本以#!格式开头的解释器参数参数。如果是后者,这个解释器必须是一个可执行的有效的路径名,但是不是脚本本身,它将调用解释器作为文件名。

argv是要调用的程序执行的参数序列,也就是我们要调用的程序需要传入的参数。

envp 同样也是参数序列,一般来说他是一种键值对的形式 key=value. 作为我们是新程序的环境。

注意,argv 和envp都必须以null指针结束。 这个参数向量和我们的环境变量都能够被我们的main函数调用,比如说我们可以定义为下面这个形式:

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{    
    pid_t pid;
    printf("befor fork pid: %d\n",getpid());
    pid = vfork();
    if(pid == -1)
    {
         perror("失败!\n");
         return -1;       
    }
    if(pid > 0){
        printf("parent:pid: %d\n",getpid());    
        wait(NULL);  //这里不等子进程会变孤儿进程  
    }
    else if(pid == 0)
    {    
        printf("child:%d,parent:%d\n",getpid(),getppid());   
        int r = 0;
        r =  execve("./hello", NULL,NULL);                         
        if(r == -1)
        {    
            perror("失败!\n");
        }
        exit(0);
    }
    return 0;
}

hello文件包含的东西

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{    
    printf("你好!");
    return 0;
}

我们调用系统的命令

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{    
    pid_t pid;
    printf("befor fork pid: %d\n",getpid());
    pid = vfork();
    if(pid == -1)
    {
         perror("失败!\n");
         return -1;       
    }
    if(pid > 0){
        printf("parent:pid: %d\n",getpid());    
        wait(NULL);  //这里不等子进程会变孤儿进程  
    }
    else if(pid == 0)
    {    
        printf("child:%d,parent:%d\n",getpid(),getppid());   
        int r = 0;
        r =  execve("/bin/ls", NULL,NULL);                         
        if(r == -1)
        {    
            perror("失败!\n");
        }
        exit(0);
    }
    return 0;
}

这里最好还是用fork函数

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{    
    pid_t pid;
    printf("befor fork pid: %d\n",getpid());
    pid = fork();
    if(pid == -1)
    {
         perror("失败!\n");
         return -1;       
    }
    if(pid > 0){
        printf("parent:pid: %d\n",getpid());    
        wait(NULL);  //这里不等子进程会变孤儿进程  
    }
    else if(pid == 0)
    {    
        printf("child:%d,parent:%d\n",getpid(),getppid());   
        int r = 0;
        r =  execve("/bin/ls", NULL,NULL);                         
        if(r == -1)
        {    
            perror("失败!\n");
        }
        exit(0);
    }
    return 0;
}
相关文章
|
7月前
|
Ubuntu Unix Shell
unix高级编程-fork之后父子进程共享文件
unix高级编程-fork之后父子进程共享文件
39 0
|
7月前
|
Unix Linux
unix高级编程-僵尸进程和孤儿进程
unix高级编程-僵尸进程和孤儿进程
43 0
|
7月前
|
Unix Linux 调度
unix编程-fork
unix编程-fork
43 0
|
Unix Shell Windows
UNIX Shell 编程(1)
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/4070059 UNIX Shell 编程(1) Unix只能识别3种基本的文件类型:普通文件、目录文件和特殊文件。
765 0
|
Unix Shell
UNIX Shell 编程(2)
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/4072516 UNIX Shell 编程(2)   字符匹配星号(*)匹配0个以上的字符;而问号(?)则匹配1个字符。
638 0
|
Unix Shell 存储
UNIX Shell 编程(3)-UNIX Shell的正则表达式
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/4073384 UNIX Shell 编程(3)-UNIX Shell的正则表达式   匹配任何单个字符:句点(.)比如:r.表示匹配r后跟任一个字符的模式。
751 0
|
1月前
|
缓存 网络协议 Unix
Linux(UNIX)五种网络I/O模型与IO多路复用
Linux(UNIX)五种网络I/O模型与IO多路复用
122 0
|
1月前
|
Unix Shell Linux
在Unix/Linux操作系统中,Shell脚本广泛用于自动化任务
在Unix/Linux操作系统中,Shell脚本广泛用于自动化任务
37 2
|
1月前
|
Unix Shell Linux
在Linux和类Unix系统中,Shell提供了多种命令用于用户和权限管理
在Linux和类Unix系统中,Shell提供了多种命令用于用户和权限管理
43 4
|
1月前
|
Oracle Ubuntu Unix
Unix与Linux区别
Unix: Unix是一个操作系统家族的名称,最早由贝尔实验室(Bell Labs)的肖像电机公司(AT&T)开发。最早的Unix版本是在1969年创建的。 Linux: Linux是由芬兰计算机科学家Linus Torvalds在1991年创建的。它是作为一个免费、开放源代码的Unix克隆而开始的。
24 1

相关课程

更多