试图用C子进程打印文件中的单词



目标是为文件中的每个单词创建一个子进程,并让子进程打印该单词。该文件只有以下五个单词,每个单词位于单独的一行:

Aachen
Biscay
Capri
Dino
Ellis

问题是,当我试图打印到一个文件时,我得到一个单词打印两次。

Aachen
Ellis
Biscay
Capri
Ellis
Dino

下面是代码。这似乎很简单,但我不明白为什么我得到了额外的单词。

int main (int argc, char *argv[])
{
  char word[50];
  int i;
  pid_t p;
  while (fscanf(stdin, "%s", word) != EOF) {
    i = 0;
    while (i < sizeof(word)) {
      if (word[i] < 'A' || word[i] > 'z') {
        word[i] = '';
        break;
      }
      i++;
    }
    p = fork();
    if (p != 0) 
      continue;
    break;
  }
  fprintf(stdout, "%sn", word);
  return 0;
}

我运行程序如下:

$ ./printwords

父亲也在最后打印最后一个单词。试着在循环之后打印:

if (p == 0) {
  fprintf(stdout, "%sn", word);
}
else {
  continue;
}

Scanf在使用%s时自动为字符串添加空终止符,因此您可以从代码中删除这整个部分:

i = 0;
while (i < sizeof(word)) 
{
    if (word[i] < 'A' || word[i] > 'z') 
    {
        word[i] = '';
        break;
    }
    i++;
}

这是我发现的一个有效的解决方案。

#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
    char word[50];
    int i;
    pid_t p;
    while (fscanf(stdin, "%s", word) != EOF) 
    {
        p = fork();
        if (p != 0)
            break; //this will allow the parent process to exit the loop    
        fprintf(stdout, "%sn", word);
        fflush(stdout);
    }
    return 0;
}

有必要在再次分叉之前刷新输出缓冲区。如果在输出缓冲区刷新之前fork,子进程也将继承该输出缓冲区,这将导致重复。

// remove pid_t p; attribution in the beginning then:
pid_t = fork();
if( pid_t == 0){
// here we're a child process. put your worker code here
}
else if (pid_t < 0){
printf("Fork failed!");
exit(1);
}
else { // here we're parent process
}

最新更新