1

I think that it will not crash. But it crash sometimes.

#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
void sig_child(int s)
{
    printf("test here\n");
    fflush(stdout);
    int status = 0;
    wait(&status);
}
int main()
{
    signal(SIGCHLD, sig_child);
    pid_t id = fork();
    if (id < 0)
    {
        printf("fork error.\n");
        fflush(stdout);
    }
    else if (id == 0)
    {
        exit(0);
    }
    else
    {
        printf("%d\n", id);
        fflush(stdout);
    }
}

my test bash

for ((i=1; i<=1000; i++))
do
  ./a.out
done

sometimes, the program will crash as the following:

Segmentation fault

you test test it on your computer1

you test test it on your computer2

you test test it on your computer3

you test test it on your computer4

you test test it on your computer5

you test test it on your computer6

W.Z.Hai
  • 105
  • 6
  • 2
    `you test test it on your computer1` ? Are you sure it's that program? I do not see how it could have printed "you test test it on your computer1"? – KamilCuk Mar 03 '21 at 15:24
  • 9
    There are [very strict rules](https://stackoverflow.com/questions/11675040/does-linux-allow-any-system-call-to-be-made-from-signal-handlers) about what you can do in a signal handler. Calling `printf` is not allowed. – Thomas Mar 03 '21 at 15:24
  • the website reminders me that my submission are mostly code. so.... – W.Z.Hai Mar 03 '21 at 15:27
  • `wait()` is also not allowed in a signal handler – user3629249 Mar 04 '21 at 16:51
  • I’m voting to close this question because the supposted output from the OPs program cannot be obtained from the OPs program – user3629249 Mar 04 '21 at 16:54
  • A more simple description will be blocked by the submission. – W.Z.Hai Mar 12 '21 at 04:04
  • Why can not you rethink the submission mechanism? – W.Z.Hai Mar 12 '21 at 04:05

0 Answers0