以下は私のテストコードです:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
pid_t pid;
/* create first child process */
pid = fork();
if (pid < 0) {
perror("fork error");
exit(1);
}
if (pid > 0) { // parent process
pid = fork();
if (pid < 0) {
perror("fork error");
exit(1);
}
if (pid > 0) { // parent process
for (int i = 0; i < 100; i++) {
printf("aaaaaaaaaaa\n");
}
} else { // second child process
for (int i = 0; i < 100; i++) {
printf("cccccccccc\n");
}
}
} else { // first child process
for (int i = 0; i < 100; i++) {
printf("bbbbbbbbbb\n");
}
}
exit(0);
}
3 つのプロセスは printf から stdout にコンテンツを送信しますが、コードを実行するたびに、3 つのプロセスが 1 つずつ実行されることが判明し、期待する出力が表示されません。これは、CPU が速すぎるためであることがわかっています。期待する出力を確認することはほとんど不可能です。そのため、デッド ループである別のプログラムを次のように記述します。 while (1) { i++; CPU使用率を高くするために、しかし、私はまだ私が期待する出力を見ることができません. 私に何ができる?