繁体   English   中英

多分叉过程无限循环

[英]Multiple Forked Processes Infinite Loop

我不确定如何描述这个问题,我很确定它不是一个无限循环(尽管它不会退出而继续运行)因为它看起来好像程序甚至都没有开始执行(我放了一个cout <在我的main函数开头的<“hello”,它不会被发送到输出)。 这是代码的一部分搞砸了(当我评论这部分程序运行正常时)。 这是我第一次使用fork()命令,所以我可能会遗漏一些明显的东西:

for (int p = 0; p < processes; p++)
{
    if ((pids[p] = fork()) == 0)
    {
        for(int v = p * (360/processes); p < (360/processes) * (p + 1); p++)
        {
            for (int i = 0; i < 360; i+=5)
            {
                match temp_match;
                float dissimilarity = 0;
                //calculate dissimilarity
                for (int j = 0; j < size; j++)
                {
                    dissimilarity += fabs(test_vector[j] - (search_set[v].vect)[(i + j) % 360]);
                }
                temp_match.x = search_set[v].x;
                temp_match.y = search_set[v].y;
                temp_match.offset = i;
                temp_match.dissimilarity = dissimilarity;
                result.push_back(temp_match);
            }
            //only keep keep the few with the smallest dissimilarity
            std::sort(result.begin(), result.end(), sort_function);
            result.resize(matches); 
        }
        for(int i = p * matches; i < (p + 1) * matches; i++)
        {
            *(shm + i * 4) = result[i].x;
            *(shm + i * 4 + 1) = result[i].y;
            *(shm + i * 4 + 2) = result[i].offset;
            *(shm + i * 4 + 3) = result[i].dissimilarity;
        }
        exit(0);
    }
    else if (pids[p] < -1)
    {
        cout << "uh oh";
        exit(1);
    }
    else
    {
        waitpid(pids[p], NULL, 0);
    }
}

所以我想我有两个问题,我是否正确地使用了所有的分支,以及什么可能导致我的程序编译但是当我运行它时甚至没有开始执行?

for(int v = p * (360/processes); p < (360/processes) * (p + 1); p++)

p++应该是v++ p <应为v <

此外,如果360不能被processes整除,则不会处理最后几个元素。 试试这个:

for(int v = (p * 360) / processes; v < ((p + 1) * 360) / processes; v++)

此外,因为您在启动它时会等待每个进程,所以您一次只运行一个进程,并且通过并行运行进程将无法获得更高的速度。

暂无
暂无

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

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