How to get child process from parent process

前端 未结 9 728
清歌不尽
清歌不尽 2020-11-30 23:05

Is it possible to get the child process id from parent process id in shell script?

I have a file to execute using shell script, which leads to a new process

9条回答
  •  春和景丽
    2020-11-30 23:23

    #include
    #include 
    #include 
    
    int main()
    {
        // Create a child process     
        int pid = fork();
    
        if (pid > 0)
        {
    
                int j=getpid();
    
                printf("in parent process %d\n",j);
        }
        // Note that pid is 0 in child process
        // and negative if fork() fails
        else if (pid == 0)
        {
    
    
    
    
    
                int i=getppid();
                printf("Before sleep %d\n",i);
    
                sleep(5);
                int k=getppid();
    
                printf("in child process %d\n",k);
        }
    
        return 0;
    

    }

提交回复
热议问题