Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #define NELEMS(x) (sizeof (x) / sizeof (x)[0])
- static void testsignaled(void) {
- kill(getpid(), SIGINT);
- }
- static void teststopped(void) {
- kill(getpid(), SIGSTOP);
- }
- static void testcontinued(void) {
- kill(getpid(), SIGSTOP);
- /* Busy-work to keep us from exiting before the parent waits.
- * This is a race.
- */
- alarm(1);
- while(1) {}
- }
- int main(void) {
- void (*test[])(void) = {testsignaled, teststopped, testcontinued};
- pid_t pid[NELEMS(test)];
- int i, status;
- for(i = 0; i < sizeof test / sizeof test[0]; ++i) {
- pid[i] = fork();
- if(0 == pid[i]) {
- test[i]();
- return 0;
- }
- }
- /* Pause to let the child processes to do their thing.
- * This is a race.
- */
- sleep(1);
- /* Observe the stoppage of the third process and continue it. */
- wait4(pid[2], &status, WUNTRACED, 0);
- kill(pid[2], SIGCONT);
- /* Wait for the child processes. */
- for(i = 0; i < NELEMS(test); ++i) {
- wait4(pid[i], &status, WCONTINUED | WUNTRACED, 0);
- printf("%d%s%s%sn", i, WIFCONTINUED(status) ? " CONTINUED" : "", WIFSIGNALED(status) ? " SIGNALED" : "", WIFSTOPPED(status) ? " STOPPED" : "");
- }
- return 0;
- }
- pid_t pid = fork();
- if (pid == 0) {
- /* child */
- sleep(200);
- }
- else {
- /* parent */
- kill(pid, SIGSTOP);
- /* do wait(), waitpid() stuff */
- }
Add Comment
Please, Sign In to add comment