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 ]

  • 相关阅读:
    C#之反射
    关系数据库中的函数依赖
    关系型数据库中关系的完整性
    sql的自连接
    sql中的union和union all查询
    c# 泛型之约束
    c#之泛型
    PTA 乙级 1009 说反话(20分) C/C++、Python
    PTA 乙级 1008 数组元素循环右移问题 (20分) C、C++
    PTA 乙级 1007 素数对猜想 (20分) C/C++
  • 原文地址:https://www.cnblogs.com/gaojian/p/2747444.html
Copyright © 2011-2022 走看看