该程序用于家庭作业。基本上,从文件中读取文本,然后模拟家族树。这是示例输入和输出:

输入:

A B 3 C D X
D Y 2 M E
M F 0
C P 1 K


输出:

A-B
   C-P
      K
   D-Y
      M-F
      E
   X


基本上,每行的前两个字符是“一对”。然后数字代表夫妇拥有的孩子数,然后列出孩子。第一行中的夫妇是最老的,文件中后面的夫妇可能是其他夫妇的孩子。 (显然,在我的程序完全完成此任务之前,我还有很多事情要做)

无论该程序做什么,这都是我的问题:对于每个最终有孩子的孩子,我希望它使用相同的for循环来创建它们。例如,C-P对需要迭代循环一次以创建K。因此,inumChilds的值分别设置为01,以便迭代一次。但是,一旦循环再次开始迭代,将重置这些值。查看我标记为调试的程序的两个部分。变量在两个地方都应该相同,但是它们以某种方式恢复为以前的值。也许我不完全了解分叉如何影响变量范围,但是有人可以向我解释一下吗?有什么方法可以解决此问题,还是需要实施新的解决方案?

#include <stdio.h>
#include <sys/types.h>


int main (int argc, char* argv[] ){

   pid_t pid;

   FILE* inFile = fopen(argv[1], "r");

   if(inFile==0){
      printf( "Error opening file, terminating program\n");
      return 1;
      }

   char person;
   char partner;
   int numChilds;

   int inInt;
   int i=0;

   // boolean flag
   int match;

   // prime the loop by importing the first two parents
   inInt = fgetc(inFile);
   while(inInt<33){
      inInt = fgetc(inFile);
      }
   person = (char) inInt;

   inInt = fgetc(inFile);
   while(inInt<33){
      inInt = fgetc(inFile);
      }
   partner = (char) inInt;

   printf("\n%c-%c\n", person, partner);

   // get number of children for first pair
   inInt = fgetc(inFile);
   while(inInt<33){
      inInt = fgetc(inFile);
      }
   numChilds = inInt - 48;

   // loop once for each new child to be created
   for(i=0; i<numChilds; i++){

//////////DEBUGGING///////////////////////////////
      /*
      printf("%i", i);
      printf("%i", numChilds);
      printf("%c", '\n');
      */
//////////DEBUGGING///////////////////////////////


      // get name of next child from file, set it as current person
      inInt = fgetc(inFile);
      while(inInt<33){
         inInt = fgetc(inFile);
         }
      person = (char) inInt;

      pid = fork();

      if(pid == 0){ // child process

         // search for self in file to find partner
         match = 0;
         while(((inInt = fgetc(inFile)) != EOF) && match==0){

            // if match found
            if((char) inInt == person){

               // set flag to stop searching file
               match = 1;

               // grab partner which is next name in file
               inInt = fgetc(inFile);
               while(inInt<33){
                  inInt = fgetc(inFile);
                  }
               partner = (char) inInt;

               // grab number of children for that pair
               inInt = fgetc(inFile);
               while(inInt<33){
                  inInt = fgetc(inFile);
                  }
               numChilds = inInt - 48;
               printf("%i", numChilds);

               // reset for loop index so it will now execute for child processes
               i=0;

               }
            }

         // if partner was never found, child will have no children, so force for loop to stop
         if(match==0){
            i = numChilds;
            }

         printf("\n%c-%c\n", person, partner);

//////////DEBUGGING///////////////////////////////
/*
         printf("%i", i);
         printf("%i", numChilds);
         printf("%c", '\n');
*/
//////////DEBUGGING///////////////////////////////

         }

      else if(pid>0){ // parent process
         wait(NULL);
         }

      }

   return 0;
   }

最佳答案

对fork的调用将复制堆栈(存储变量的位置)和程序(代码)在内存中,然后在派生程序中的fork调用之后立即开始运行新进程。

两个分叉的进程将具有不同的堆栈,并且将使用不同的变量运行。

您可以阅读http://linux.die.net/man/2/fork以获得有关fork行为的完整描述。

要在进程之间进行通信,您可以使用共享内存:How to share memory between process fork()?

关于c - 在循环中 fork ,变量随每次迭代而变化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25893887/

10-09 07:17
查看更多