简体   繁体   English

fork() - 多个进程和系统调用

[英]fork() - multiple processes and system calls

I am writing a mapreduce program that uses multiple I/O pipes (one pipe per process) to get some final results. 我正在编写一个mapreduce程序,它使用多个I / O管道(每个进程一个管道)来获得最终结果。 I am having a problem with creating the processes. 我在创建流程时遇到问题。 Specifically, I am getting the following error: 具体来说,我收到以下错误:

wait error: Interrupted system call

This is my code that spawns processes: 这是我产生进程的代码:

while (values[inc]!=NULL) //provided array of text lines
{
    if ((pid = fork()) == -1) {
        perror("fork error");
        exit(EXIT_FAILURE);
    }    

    else if (pid == 0) {             /* start of child process      */
        printf("Child process...\n");
        /* pipes[inc][1] is a file descriptor to which myMap writes some data
           using the write() system call
           mr is a struct that holds other function pointers */
        mr->myMap(pipes[inc][1],values[inc]); 
        exit(0);
    }
    else {                           /* start of parent process     */
        printf("Parent process...\n");

        if ((wpid = wait(&status)) == -1)
        /* Wait for child process.      */  
            perror("wait error");
        else {                       /* Check status.                */
            if (WIFSIGNALED(status) != 0)
                printf("Child process ended because of signal %d\n",
                       WTERMSIG(status));
            else if (WIFEXITED(status) != 0)
                printf("Child process ended normally; status = %d\n",
                       WEXITSTATUS(status));
            else
                printf("Child process did not end normally\n");
        }
        //close(fd[1]);

        printf("Parent process ended\n");
    }
    inc++;
}

After this I am creating one thread 在此之后我创建了一个线程

pthread_t newThread;
pthread_create(&newThread,NULL,threadFunction,values);
pthread_join(newThread,NULL);

The threadFunction uses select() function to find out which file descriptor is ready to be read and reads it and puts data in a dictionary. threadFunction使用select()函数来找出准备好读取的文件描述符并将其读取并将数据放入字典中。

When running form gdb debugger, the program output: 运行表单gdb调试器时,程序输出:

Parent process...
Child process...
wait error: Interrupted system call
Parent process ended
Parent process...
Child process ended normally; status = 0
Parent process ended
Parent process...
Child process...
Child process...
wait error: Interrupted system call
Parent process ended

I don't know how to resolve the issue. 我不知道如何解决这个问题。 Any suggestions? 有什么建议?

Thanks! 谢谢!

You need to put your wait() call into a loop and if it returns an error (-1) and errno == EINTR continue the loop. 您需要将wait()调用放入循环中,如果它返回错误(-1)并且errno == EINTR继续循环。 Any other error is a real error and should be treated as such. 任何其他错误都是真正的错误,应该这样对待。

Things like profiling timers can cause signals to be sent to the process, however the signal probably causing the interruption is SIGCHLD , which as you know is called when a child process changes state . 类似于分析定时器的事情可能会导致信号被发送到进程,但是可能导致中断的信号是SIGCHLD ,正如您所知,当子进程改变状态时会调用该信号。

EDIT: OK, I'll write the answer in code: 编辑:好的,我会在代码中写下答案:

do
{
    wpid = wait(&status);
}
while (wpid == -1 && errno == EINTR);
if (wpid == -1)
{
    perror("wait error");
    return -1;
}
else
{
    // we have wait status
    ...
}

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

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