diff --git a/qa/lib/newlib/020-waitpid.c b/qa/lib/newlib/020-waitpid.c new file mode 100644 index 0000000..aaa96a4 --- /dev/null +++ b/qa/lib/newlib/020-waitpid.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + pid_t cpid, w; + int status; + + + cpid = fork(); + if(cpid == -1){ + perror("fork"); + exit(EXIT_FAILURE); + } + + if (cpid == 0) { /* Code executed by child */ + printf("Child PID is %ld\n", (long) getpid()); + _exit(10); + } + /* Code executed by parent */ + do { + w = waitpid(cpid, &status, WUNTRACED); + if (w == -1) { + perror("waitpid"); + exit(EXIT_FAILURE); + } + + if (WIFEXITED(status)) { + printf("exited, status=%d\n", WEXITSTATUS(status)); + } else if (WIFSIGNALED(status)) { + printf("killed by signal %d\n", WTERMSIG(status)); + exit(EXIT_FAILURE); + } else if (WIFSTOPPED(status)) { + printf("stopped by signal %d\n", WSTOPSIG(status)); + exit(EXIT_FAILURE); + } + } while (!WIFEXITED(status) && !WIFSIGNALED(status)); + exit(EXIT_SUCCESS); + +} diff --git a/qa/lib/newlib/build.json b/qa/lib/newlib/build.json index 57eaad2..e0c852c 100644 --- a/qa/lib/newlib/build.json +++ b/qa/lib/newlib/build.json @@ -32,6 +32,7 @@ "SourceFilesCmd": [ "000-hello.c", "010-fork.c", + "020-waitpid.c", "100-files.c", "101-files.c", "200-signals.c"