【问题标题】:Forking inside a loop, variables changing with each iteration在循环内分叉,变量随每次迭代而变化
【发布时间】:2014-09-17 14:53:33
【问题描述】:

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

输入:

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;
   }

【问题讨论】:

  • 不是您的问题,但您是否考虑过使用 printf,例如:printf("\n%c-%c\n", person, partner);?
  • 我是 C 新手,我一直在使用 C++,它有 cincout。直到现在我才明白,但你的例子是有道理的。谢谢
  • 我很困惑,为什么你认为它们在两个地方应该是一样的?您的代码在调试语句之间显式更改了这两个变量的值。 IE。子进程代码中的numChilds = inInt - 48;i=0;
  • 非常类似于 C++ printf :) cplusplus.com/reference/cstdio/printf
  • 作业是否要求您使用fork

标签: c loops for-loop fork


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 2017-02-09
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2012-12-02
    相关资源
    最近更新 更多