【问题标题】:How to have child processes change the parent's variables?如何让子进程更改父进程的变量?
【发布时间】:2012-12-06 02:53:31
【问题描述】:

我声明了一个数组:

char * words[1000] = {NULL};

现在我有一系列分叉的子进程将单词添加到该数组中,但它们不会影响父程序。我该如何改变呢?

【问题讨论】:

  • 仅供参考,您的第一个代码块可以简化为char* words[1000] = {NULL};,并且数组中的每个项目都将设置为NULL
  • 请不要再写这个:words[i][strlen(words[i])] = '\0';。您已经知道字符串有多长(来自strlen(temp),请将 NUL 放在正确的位置。
  • 嗯。这段代码对我来说是正确的:清除指针数组,然后每次你想放东西时,从头开始搜索,直到找到一个空指针,然后占据那个位置。所以,也许你每次放东西时都会运行你的初始化代码?还是您的单词数组被覆盖?最好是你发布你的整个程序。
  • "...because each day he gets farther away from the paint can."。除了扫描之外,您可能还需要考虑以某种方式跟踪表中孔的位置。
  • 子进程和父进程有不同的地址空间,你必须做额外的工作让它们能够共享内存。 inter-process communicationshared memory 可能是你想要的。

标签: c fork arrays


【解决方案1】:

嗯,对于您的编辑情况:不要使用 fork,使用线程,因为那样一切都在一个地址空间中运行...
当然,然后使用互斥锁来保护您的单词数组...

【讨论】:

  • 学校作业。别无选择。
【解决方案2】:

你还没有在 while 块中添加 if 块!!!

   while(i < 1000 && words[i] != NULL)
   {
   i++;
 if(i<1000){
    words[i] = (char*) malloc(strlen(temp)+1);
    strcpy(words[i], temp);
   words[i][strlen(words[i])] = '\0';
    printf("Added: %s at location %d\n", words[i], i);
   }
  }

【讨论】:

  • 哦,不,不是这样。您在此处看到的while 循环不是运行十次的while 循环。 if 语句不应该在 while 循环内。 while 循环应该找到数组中的下一个开放槽,如果 iif 语句会在该槽中添加一个单词。
【解决方案3】:

试试这个你弄错了while大括号:

int i = 0;
while(i < 1000 && words[i] != NULL){
    i++;
    if(i<1000){
        words[i] = (char*) malloc(strlen(temp)+1);
        strcpy(words[i], temp);
        words[i][strlen(words[i])] = '\0';
        printf("Added: %s at location %d\n", words[i], i);
    }
}

【讨论】:

    【解决方案4】:

    请粘贴整个代码...我编写代码来做我认为您正在做的事情,它对我有用...

        #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      char* words[1000];
      int j;
      for(j = 0; j<1000; j++)
        words[j] = NULL;
      char *temp = "dummy";
    
      for (j = 0; j < 10; j++)
        {
          int i = 0;
          while(i < 1000 && words[i] != NULL)
            i++;
          printf("Adding something to %d vs %d\n",i,j);
          if(i<1000){
            words[i] = (char*) malloc(strlen(temp)+1);
            strcpy(words[i], temp);
            words[i][strlen(words[i])] = '\0';
            printf("Added: %s at location %d\n", words[i], i);
          }
        }
    }
    
    /* prints:
    Adding something to 0 vs 0
    Added: dummy at location 0
    Adding something to 1 vs 1
    Added: dummy at location 1
    Adding something to 2 vs 2
    Added: dummy at location 2
    Adding something to 3 vs 3
    Added: dummy at location 3
    Adding something to 4 vs 4
    Added: dummy at location 4
    Adding something to 5 vs 5
    Added: dummy at location 5
    Adding something to 6 vs 6
    Added: dummy at location 6
    Adding something to 7 vs 7
    Added: dummy at location 7
    Adding something to 8 vs 8
    Added: dummy at location 8
    Adding something to 9 vs 9
    Added: dummy at location 9
    */
    

    【讨论】:

    • 从您的新编辑中,听起来您需要使用共享内存。 (此外,如果您不理解为什么会看到您所看到的行为,您可能需要查看 stackoverflow.com/questions/7455161/… (假设您使用的是 fork 而不是其他方法)
    • 哦,我知道它是如何工作的。我只是想验证我想做的事情是不可能的。
    • 不是我期望它有帮助,而是戴上我的白胡子......如果你在 VxWorks 5.4 上运行并使用他们提供的任何东西,就像 fork 但不是真的,你的代码会正常工作(ish ...仍然存在人们指出的性能问题)...但是 Unix(及相关)下的 fork 将使其成为可能,因此进程 A 不能轻易弄乱它的子/父/表亲进程。否则,餐饮哲学家会用筷子或其他东西互相刺伤。
    【解决方案5】:

    当您使用 fork 创建孩子时,每个孩子都会在孩子更改数组中的某些内容时获得自己的数组副本,它实际上是在更改自己的副本,对于您想要做的事情,您需要进程间通信 (IPC),您需要创建共享内存或创建管道,以便所有子和父的数组值发生变化

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      相关资源
      最近更新 更多