我的函数 'sleep' 阻止了其他进程

My function 'sleep' blocks the others processes

我必须创建一个程序来多次显示字符,每个字符之间都有延迟。多亏了我要给你的例子,你会更好地理解。每个显示都是一个进程完成的,所以程序是多进程的,显示是并行完成的。

数据保存到包含以下行的文件中:

a 4 2
b 2 1

因此,对于 示例,字符 'a' 显示 4 次,延迟 2 秒;并行地,'b' 显示 2 次,延迟 1 秒。

我写的代码可以编译运行,没有任何错误。但是,对于这个文件,它只显示 'bb'。如果我删除 'sleep'-call,它会正确显示所有 'a' 和 'b'(但没有任何延迟)。

因此,'sleep' 函数似乎只允许一个进程并停止其他进程。你知道为什么吗 ?我以为它只会阻止调用进程。

这是我的代码。

#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char* argv[]) {

    pid_t pid;

    FILE* file = fopen(argv[1], "r");
    if(file == NULL) {
        perror("Open");
        exit(EXIT_FAILURE);
    }

    // <!-- The following vars are used for the reading of the file -->
    char current_char = 0;

    char char_to_print; // ie. : the first file's column

    char buffer_number_of_prints[592] = {0}; // ie. : the snd file's column
    int i1 = 0; // Will be used to write in the previous buffer

    char buffer_delay[592] = {0}; // ie. : the thd (and last) file's column
    int i2 = 0;

    int flag_column = 0; // 0 = first file's column, 1 = snd one, 2 = last one (precision : two columns are separated by a space)
    // <!-- ### -->

    while(current_char != EOF) {
        current_char = fgetc(file);

        if(current_char != ' ') { // So current_char is : the char to type XOR The number of times XOR The delay XOR \n
            if(current_char == '\n' || current_char == EOF) { // So we just read an entire line of the file : we have valued the var "char_to_print" + the 2 buffers
                pid = fork(); // A new process is created and will do its job
                if(pid != 0) { // So we just read an entire line + we are the parent process , SO : we re-init all the vars and we're going to read the next file's line
                    flag_column = 0;
                    memset(buffer_number_of_prints, 0, 592);
                    i1 = 0;
                    memset(buffer_delay, 0, 592);
                    i2 = 0;
                    continue; // We're going to read the next file's line

                } else { // If the current process is a child, it has to do its job (ie. : printing the typed char with x delay and y times)
                    int i = 0;
                    int max = atoi(buffer_number_of_prints);
                    int del = atoi(buffer_delay); // These two buffers have been filed by the parent process
                    for(i; i < max; i++) {
                        fprintf(stdout, "%c", char_to_print);
                        sleep(del);
                    }
                    return 0; // The process ended its job so we stop it

                }
            }

            // If we are the parent process (the child ones have been stopped previously thanks to the instruction `return 0`), we fill the var `char_to_print` and the two buffers according to the value of the var `flag_column`
            if(flag_column == 0) {
                char_to_print = current_char;
            } else if(flag_column == 1) {
                buffer_number_of_prints[i1] = current_char;
                i1++;
            } else if(flag_column == 2) {
                buffer_delay[i2] = current_char;
                i2++;
            }
        }  else { // If we encounter a space in the reading of the file, it means we are changing of column : so we increment this var
            flag_column++;
        }
    }

    // The parent process close the file's stream
    if(fclose(file) != 0) {
        perror("Close");
        exit(EXIT_FAILURE);
    }

    // Before ending, the parent process wait for its children ones
    wait(NULL);
    return 0;
}

我找到了问题的解决方案。是 'wait(NULL)',必须用 'while(wait(NULL) > 0) {}' 代替。

所以来源变为:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>

int main(int argc, char* argv[]) {

    pid_t pid;

    FILE* file = fopen(argv[1], "r");
    if(file == NULL) {
        perror("Open");
        exit(EXIT_FAILURE);
    }

    // <!-- The following vars are used for the reading of the file -->
    char current_char = 0;

    char char_to_print; // ie. : the first file's column

    char buffer_number_of_prints[592] = {0}; // ie. : the snd file's column
    int i1 = 0; // Will be used to write in the previous buffer

    char buffer_delay[592] = {0}; // ie. : the thd (and last) file's column
    int i2 = 0;

    int flag_column = 0; // 0 = first file's column, 1 = snd one, 2 = last one (precision : two columns are separated by a space)
    // <!-- ### -->

    while(current_char != EOF) {
        current_char = (char) fgetc(file);

        if(current_char != ' ') { // So current_char is : the char to type XOR The number of times XOR The delay XOR \n
            if(current_char == '\n' || current_char == EOF) { // So we just read an entire line of the file : we have valued the var "char_to_print" + the 2 buffers
                pid = fork(); // A new process is created and will do its job
                if(pid != 0) { // So we just read an entire line + we are the parent process, SO : we re-init all the vars and we're going to read the next file's line
                    flag_column = 0;
                    memset(buffer_number_of_prints, 0, 592);
                    i1 = 0;
                    memset(buffer_delay, 0, 592);
                    i2 = 0;
                    continue; // We're going to read the next file's line

                } else { // If the current process is a child, it has to do its job (ie. : printing the typed char with x delay and y times)
                    int i = 0;
                    int max = atoi(buffer_number_of_prints);
                    int del = atoi(buffer_delay); // These two buffers have been filed by the parent process
                    for(; i < max; i++) {
                        fprintf(stdout, "%c ", char_to_print);
                        fflush(stdout);

                        if(i < max-1) {
                            sleep(del);
                        }
                    }
                    return 0; // The process ended its job so we stop it

                }
            }

            // If we are the parent process (the child ones have been stopped previously thanks to the instruction `return 0`), we fill the var `char_to_print` and the two buffers according to the value of the var `flag_column`
            if(flag_column == 0) {
                char_to_print = current_char;
            } else if(flag_column == 1) {
                buffer_number_of_prints[i1] = current_char;
                i1++;
            } else if(flag_column == 2) {
                buffer_delay[i2] = current_char;
                i2++;
            }
        }  else { // If we encounter a space in the reading of the file, it means we are changing of column : so we increment this var
            flag_column++;
        }
    }

    // The parent process closes the file's stream
    if(fclose(file) != 0) {
        perror("Close");
        exit(EXIT_FAILURE);
    }

    // Before ending, the parent process wait for its children ones
    while(wait(NULL) > 0) { ;; }
    return 0;
}