ねえ、私は失われているようです。私は、無限ループ内の子供のカウントをインクリメントし、親がシグナルを送信するたびにカウントを出力するようになっています。これは1秒ごとに行う必要があります。私は自分のコードを書いたが、フォークを使用した後、子プロセスと親プロセスが同時に実行されると考えたが、そうではないので、この問題に対処する方法がわからない。すべてのヘルプはスリープ機能を使用している間、どのようにカウントを増やしますか? (C言語で)
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int count = 0;//global count variable
void catch(int signal){
printf("Ouch! - I got signal %d \n", signal);
printf("count is %d\n", count);
count = 0;
}
int main(){
int pid;
int sec=0;
pid = fork();
int count1 = 0;
(void) signal(SIGALRM, catch);
if(pid==-1){
printf("error\n");
}
else if(pid==0){//if child
while(1){//while loop to increment count while parent to sleeping
count = count + 1;
}
//pause();
}
else{//parent
sleep(1);//1 second pause
raise(SIGALRM);//send alarm
count1 = count1 + 1;
if(count1>=5){
return 0;
}
exit(0);
}
return 0;
}
http://stackoverflow.com/questions/10328956/how-do-you-increment-a-count-while-using-the-睡眠機能 – cnicutar
編集ボタンを使って質問を変更し、基本的に同じ質問をもう一度尋ねないでください –