gpt4 book ai didi

c - 这段代码有没有逻辑错误?

转载 作者:IT王子 更新时间:2023-10-29 01:16:08 26 4
gpt4 key购买 nike

<分区>

我是新手,请温柔!代码是从书上抄来的:

#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

static int alarm_fired = 0;

void ding(int sig)
{
alarm_fired = 1;
}

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

printf("alarm application starting\n");

pid = fork();

switch(pid)
{
case -1:
perror("fork failed");
exit(EXIT_FAILURE);
case 0:
sleep(5);
kill(getpid(), SIGALRM);
exit(EXIT_SUCCESS);
}
printf("waiting for alarm to go off\n");
(void) signal(SIGALRM, ding);

pause();
if (alarm_fired)
printf("Ding!\n");

printf("done\n");
exit(EXIT_SUCCESS);
}

正如作者所写:

ding,simulates an alarm clock.the child process wait for five seconds before sending a SIGALRM signal to its parent.


我试过上面的代码,打印alarm application starting后没有反应

waiting for alarm to go off.所以我怀疑代码有逻辑错误。kill(getpid(), SIGALRM);这行可能是错误的。我说得对吗?

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