简体   繁体   English

使用fork()的单词频率

[英]Frequency of word using fork()

I have the following program and I've got stuck at some point.(the program won't create more child processes but just one. Could anyone help me out ? 我有以下程序,但有时会卡住。(该程序不会创建更多的子进程,而只会创建一个子进程。有人可以帮我吗?

Write a program that counts occurrences of a string as a substring in another string (the two strings are given as arguments on the command line). 编写一个程序,该程序将一个字符串的出现作为另一个字符串的子字符串进行计数(这两个字符串在命令行中作为参数给出)。 Every time it checks if the first string appears as a substring starting from a position, checking will be done by a child process (obtained with fork) and the father process is not expecting for the child process to finish to initiate a search starting from a different position - so the verifications are being made in parallel. 每次它检查第一个字符串是否从一个位置开始显示为子字符串时,将由一个子进程(用fork获取)进行检查,并且父进程并不期望该子进程完成从一个开始的搜索。不同的位置-因此验证是并行进行的。 Each child process returns 0 = did not checked (not shown as substring from that position), 1 = has been verified. 每个子进程返回0 =未检查(未显示为该位置的子字符串),1 =已验证。 After conducting all searches, the father process is expected to finish all sons processes and gather their return codes - this value is will be printed(is the number of times as a substring). 进行所有搜索之后,父进程将完成所有子进程并收集其返回码-将打印此值(是子字符串的次数)。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
char *s1,*s2;
int verificare(char *s1, char *s2, int lungime)
{
  pid_t pid;
  int i,status;
  for (i = 0; i < lungime; i++) {
    pid = fork();
    switch (pid) {
    case -1:
      return EXIT_FAILURE;

    case 0: 
      if (strstr(s1, s2) != NULL)
        return 1;
      else
        return 0;
    _exit(0);

    default:
        waitpid(pid, &status, 0);
    if (WIFEXITED(status))
      printf("Child %d, Return code %d\n", pid,
             WEXITSTATUS(status));
    return status;
}
  }

}
int main(int argc,char **argv){
    if (argc!=3){
        printf("Too less arg");
        return 0;
    }
    s1=argv[1];
    s2=argv[2];
verificare(s1,s2,strlen(s2));
return 0;
}
  1. You are using strstr which will do just what your own program should have done. 您正在使用strstr ,它将做您自己的程序应该做的事情。 Shouldn't you rather loop over the following characters and compare them to the pattern? 您是否应该循环遍历以下字符并将它们与模式进行比较?
  2. Your program essentially runs sequential since you are wait() ing for the child processes to finish immediately after you have started one. 您的程序实际上是按顺序运行的,因为您正在wait()子进程在启动子进程后立即完成。 Shouldn't you store all child PIDs away in some array and only after all children are started begin to wait? 您是否不应该将所有子PID都存储在某个数组中,仅在所有子PID开始等待之后才存储?
  3. Your parent process will never see the return value of verificare in the child processes because your main ignores it and always returns 0. Maybe use exit to return from the child? 您的父进程永远不会在子进程中看到verificare的返回值,因为您的main进程将忽略它并始终返回0。也许使用exit从子进程中返回?
  4. Your program has no way to report to the user whether a match was found. 您的程序无法向用户报告是否找到匹配项。

I understand that this is a homework assignment and you are not supposed to do it differently than they asked for. 我了解这是一项家庭作业,您不应按照他们的要求去做。 Note however that this is not a very good usage of fork . 但是请注意,这不是fork很好用法。 Occurrence of a sub-string can be checked with a single processor in time linear in the length of the string to search (using finite automatons). 子字符串的出现可以用单个处理器在时间上线性地检查要搜索的字符串的长度(使用有限自动机)。 So this parallel algorithm doesn't really buy you anything here. 因此,这种并行算法在这里并不能真正为您带来任何收益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM