2011-10-31 12 views
2

程序,要求用戶輸入的作爲參數傳遞給創建所以基本上你會做一些這樣的進程數:管和插入

$ ./program 4 

它會產生這樣的:

This is process 1 with ID 7389 and parent id 6550 
This is process 2 with ID 7390 and parent id 7389 
This is process 3 with ID 7391 and parent id 7390 
This is process 4 with ID 7392 and parent id 7391 

現在需要在給定的位置插入一個項目所以基本上

$ ./program insert 3 

它應該增加一個NE W工藝在3線會看起來像:

This is process 1 with ID 7389 and parent id 6550 
This is process 2 with ID 7390 and parent id 7389 
This is process 3 with ID 7399 and parent id 7389 //NEW 
This is process 4 with ID 7391 and parent id 7390 
This is process 5 with ID 7392 and parent id 7391 

我不知道該怎麼做的插入,我將不勝感激任何幫助或建議,如果可能的話

感謝

#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 


int main(int argc, char *argv[ ]) { 
    pid_t childpid;    /* indicates process should spawn another  */ 
    int error;     /* return value from dup2 call    */ 
    int fd[2];     /* file descriptors returned by pipe   */ 
    int i;      /* number of this process (starting with 1) */ 
    int nprocs;     /* total number of processes in ring   */ 
      /* check command line for a valid number of processes to generate */ 
    if ((argc != 2) || ((nprocs = atoi (argv[1])) <= 0)) { 
     fprintf (stderr, "Usage: %s nprocs ID\n", argv[0]); 
     return 1; 
    } 
    if (pipe (fd) == -1) {  /* connect std input to std output via a pipe */ 
     perror("Failed to create starting pipe"); 
     return 1; 
    } 
    if ((dup2(fd[0], STDIN_FILENO) == -1) || 
     (dup2(fd[1], STDOUT_FILENO) == -1)) { 
     perror("Failed to connect pipe"); 
     return 1; 
    } 
    if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) { 
     perror("Failed to close extra descriptors"); 
     return 1; 
    }   
    for (i = 1; i < nprocs; i++) {   /* create the remaining processes */ 
     if (pipe (fd) == -1) { 
     fprintf(stderr, "[%ld]:failed to create pipe %d: %s\n", 
       (long)getpid(), i, strerror(errno)); 
     return 1; 
     } 
     if ((childpid = fork()) == -1) { 
     fprintf(stderr, "[%ld]:failed to create child %d: %s\n", 
       (long)getpid(), i, strerror(errno)); 
     return 1; 
     } 
     if (childpid > 0)    /* for parent process, reassign stdout */ 
      error = dup2(fd[1], STDOUT_FILENO); 
     else        /* for child process, reassign stdin */ 
      error = dup2(fd[0], STDIN_FILENO); 
     if (error == -1) { 
     fprintf(stderr, "[%ld]:failed to dup pipes for iteration %d: %s\n", 
       (long)getpid(), i, strerror(errno)); 
     return 1; 
     } 
     if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) { 
     fprintf(stderr, "[%ld]:failed to close extra descriptors %d: %s\n", 
       (long)getpid(), i, strerror(errno)); 
     return 1; 
     } 
     if (childpid) 
     break; 
    }            /* say hello to the world */ 
    fprintf(stderr, "This is process %d with ID %ld and parent id %ld\n", 
      i, (long)getpid(), (long)getppid()); 


     close(fd[0]);   /* Close unused read end */ 
     close(fd[1]);   /* Reader will see EOF */ 
     wait(NULL);    /* Wait for child */ 
     exit(EXIT_SUCCESS); 
    return 0; 
} 
+1

什麼是插入,在你的問題的上下文?如果是作業,請給你的問題添加功課。 – ObscureRobot

+0

這聽起來很奇怪。您希望查看現有的一組進程並重新連接它們的stdin和stdout,以便新進程成爲管道的一部分? –

+0

耶的功課,我堅持 – user975582

回答

2

只處理7389可以創建一個子進程作爲父進程。

您需要發送消息的某種方式或信號處理7389,指示它創建一個新的子進程。