Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <unistd.h>
- int main(int argc, const char* argv[])
- {
- const int chunk = 100000000;
- int status;
- int err;
- pid_t pid;
- pid = fork();
- for (;;) {
- switch (pid) {
- case -1: /* Fork failed */
- exit(EXIT_FAILURE);
- break;
- case 0: /* Child */
- printf("Try to eat up memory");
- for (;;) {
- char* m = malloc(chunk);
- for (char* p = m; p < m + chunk; p++)
- *p = 0xff;
- printf(".");
- fflush(stdout);
- }
- return 0;
- break;
- default: /* Parent */
- err = wait(&status);
- if (err == -1) {
- printf("wait failed with errno = %d\n", errno);
- exit(0);
- } else {
- if (WIFEXITED(status)) {
- printf("Child exited successfully. Exit parent too.\n");
- exit(EXIT_SUCCESS);
- } else {
- printf("Child exited abnormally. Wait for 5 seconds....\n");
- sleep(5);
- printf("Restarting child again.\n");
- pid = fork();
- }
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement