gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:38:32 24 4
gpt4 key购买 nike

我必须创建一个程序,多次显示字符,每个字符之间有一个延迟。通过我将给您提供的示例,您会更好地理解。每次显示都是由一个进程完成,所以程序是多进程的,显示是并行完成的。

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

a 4 2
b 2 1

因此,对于示例,字符“a”显示 4 次,延迟 2 秒;同时,“b”显示 2 次,延迟 1 秒。

我编写的代码可以编译并执行,没有任何错误。但是,对于这个文件,它只显示“bb”。如果我删除“ sleep ”调用,它会正确显示所有“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;
}

关于c - 我的函数 'sleep' 阻止其他进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37973236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com