Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在循环中分叉,变量随每次迭代而变化_C_Loops_For Loop_Fork - Fatal编程技术网

C 在循环中分叉,变量随每次迭代而变化

C 在循环中分叉,变量随每次迭代而变化,c,loops,for-loop,fork,C,Loops,For Loop,Fork,这个程序是用来做家庭作业的。基本上,文本是从文件中读入的,并且模拟了一个族谱。以下是示例输入和输出: 输入: 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 基本上,每行的前两个字符是一对。然后这个数字表示这对夫妇有多少个孩子,然后列出这些孩子。第一行的夫妇是年龄最大的,档案中后面的夫妇可能是其他夫妇的子女。显然,在我的程序完全实现这一点之前,我还有很多事情要做

这个程序是用来做家庭作业的。基本上,文本是从文件中读入的,并且模拟了一个族谱。以下是示例输入和输出:

输入:

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。因此,i和numchild的值分别设置为0和1,以便循环迭代一次。但是,一旦循环再次开始迭代,这些值就会重置。看看我标记为调试的程序的两个部分。变量在两个地方应该是相同的,但它们会以某种方式恢复到以前的值。也许我不完全理解分叉是如何影响变量作用域的,但有人能给我解释一下吗?有没有办法解决这个问题,或者我需要实施一个新的解决方案

#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程序中的fork调用之后立即运行一个新进程

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

您可以阅读fork行为的完整描述


要在进程之间进行通信,您可以使用共享内存:

这不是您的问题,但您是否考虑过使用printf,如:printf\n%c-%c\n,person,partner;?我是C的新手,我一直使用C++,它有CIN和CUT。我直到现在才明白,但你的例子很有道理。谢谢你我很困惑,为什么你认为这两个地方应该是一样的?代码在调试语句之间显式更改这两个变量的值。即numChilds=inInt-48;i=0;在子进程代码中,与C++ PrTNF非常相似:分配是否需要使用叉?