libposix: introduce pause() (still to be integrated in newlib)
This commit is contained in:
parent
f3753df931
commit
6dd00801dc
67
qa/lib/newlib/030-pause.c
Normal file
67
qa/lib/newlib/030-pause.c
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
|
||||||
|
void sighup() {
|
||||||
|
signal(SIGHUP,sighup); /* reset signal */
|
||||||
|
printf("CHILD: SIGHUP received\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
pid_t cpid, w;
|
||||||
|
int ret, status;
|
||||||
|
|
||||||
|
|
||||||
|
cpid = fork();
|
||||||
|
if(cpid == -1){
|
||||||
|
perror("fork");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cpid == 0) { /* Code executed by child */
|
||||||
|
signal(SIGHUP, sighup); /* set function calls */
|
||||||
|
|
||||||
|
printf("Child PID is %ld, calling pause()...\n", (long) getpid());
|
||||||
|
ret = pause();
|
||||||
|
|
||||||
|
if(ret == -1 && errno == EINTR){
|
||||||
|
printf("Pause returned %d with errno = EINTR\n", ret);
|
||||||
|
_exit(0);
|
||||||
|
}
|
||||||
|
if(ret != -1)
|
||||||
|
printf("Pause returned %d\n", ret);
|
||||||
|
if(errno != EINTR)
|
||||||
|
printf("Pause returned but errno != EINTR\n");
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Code executed by parent */
|
||||||
|
sleep(5);
|
||||||
|
kill(cpid, SIGHUP);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
@ -34,6 +34,7 @@
|
|||||||
"000-hello.c",
|
"000-hello.c",
|
||||||
"010-fork.c",
|
"010-fork.c",
|
||||||
"020-waitpid.c",
|
"020-waitpid.c",
|
||||||
|
"030-pause.c",
|
||||||
"100-files.c",
|
"100-files.c",
|
||||||
"101-files.c",
|
"101-files.c",
|
||||||
"200-signals.c",
|
"200-signals.c",
|
||||||
|
@ -60,6 +60,13 @@ POSIX_usleep(int *errnop, unsigned int usec)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
POSIX_pause(int *errnop)
|
||||||
|
{
|
||||||
|
rendezvous((void*)~0, 1);
|
||||||
|
*errnop = __libposix_get_errno(PosixEINTR);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
clock_t
|
clock_t
|
||||||
POSIX_times(int *errnop, void *buf)
|
POSIX_times(int *errnop, void *buf)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user