c语言输错5次之后睡眠10秒,如何在使用睡眠功能时增加计数? (用C语言)

嘿,我似乎迷路了。我应该能够在一个无限循环内增加一个孩子的计数,并且每当家长发送一个信号时,每隔1秒打印一次计数。我写了我的代码,但我认为使用fork后,子进程和父进程同时运行,但事实并非如此,所以我不知道如何解决这个问题。任何帮助将是巨大的如何在使用睡眠功能时增加计数? (用C语言)

#include

#include

#include

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;

}

2012-04-26

ctng1213

+0

http://stackoverflow.com/questions/10328956/how-do-you-increment-a-count-while-using-the-睡眠功能 –

2012-04-26 08:12:21

+0

请使用编辑按钮对您的问题进行更改,而不要再次询问基本相同的问题 –

2012-04-26 09:15:30

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值