zoukankan      html  css  js  c++  java
  • 我对 execl 的学习

    上例子  [作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

    [root@localhost test]# cat exem.c
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            pid_t mpid;
            
            mpid=fork();
           
           if( mpid<0)
           {
    
           }
           else if (mpid==0) { //the child itself
               
                    if(execl("/soft/test/gaochange.o","gaochange",NULL)<0)
                            perror("execl error!");
                    
                    for (;;)
                    {
                       fprintf(stderr,"after execl call\n");
                       sleep(5);
                    }
    
           }else{
                    for (;;)
                    {
                       sleep(1);
                    }                  
           }
    }

    被子进程调用起来的代码:

    [root@localhost test]# cat gaochange.c
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    
       for (;;)
       {
         sleep(10);
         fprintf(stderr,"we are in gaochange\n");     
       }
    
       return 0;
    }
    [root@localhost test]# 

    运行的结果,是这样的:

    [root@localhost test]# gcc -o gaochange.o gaochange.c
    [root@localhost test]# gcc -o exem.o exem.c
    [root@localhost test]# ./exem.o
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    we are in gaochange
    ….
    
    
    [root@localhost test]# ps -ef|grep exem
    root      3173  2957  0 09:10 pts/2    00:00:00 ./exem.o
    root      3176  2917  0 09:10 pts/1    00:00:00 grep exem
    [root@localhost test]# ps -ef|grep 3173
    root      3173  2957  0 09:10 pts/2    00:00:00 ./exem.o
    root      3174  3173  0 09:10 pts/2    00:00:00 gaochange
    root      3179  2917  0 09:10 pts/1    00:00:00 grep 3173
    [root@localhost test]# 

    结束 [作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

  • 相关阅读:
    Linux Vim编辑器
    Linux sed 流编辑器
    Shell 编程 (变量和条件测试)
    Linux 下 Bash配置文件读取
    Linux 用户、权限
    Linux 指令(一)文件/目录操作
    Ubuntu 下安装 Swoole
    Mysql IN语句查询
    Mysql 查询优化
    Mysql 获取表属性
  • 原文地址:https://www.cnblogs.com/gaojian/p/2747444.html
Copyright © 2011-2022 走看看