Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6.  
  7. int main(int argc, const char* argv[])
  8. {
  9. const int chunk = 100000000;
  10. int status;
  11. int err;
  12. pid_t pid;
  13.  
  14. pid = fork();
  15. for (;;) {
  16. switch (pid) {
  17. case -1: /* Fork failed */
  18. exit(EXIT_FAILURE);
  19. break;
  20.  
  21. case 0: /* Child */
  22. printf("Try to eat up memory");
  23. for (;;) {
  24. char* m = malloc(chunk);
  25. for (char* p = m; p < m + chunk; p++)
  26. *p = 0xff;
  27. printf(".");
  28. fflush(stdout);
  29. }
  30. return 0;
  31. break;
  32.  
  33. default: /* Parent */
  34. err = wait(&status);
  35. if (err == -1) {
  36. printf("wait failed with errno = %d\n", errno);
  37. exit(0);
  38. } else {
  39. if (WIFEXITED(status)) {
  40. printf("Child exited successfully. Exit parent too.\n");
  41. exit(EXIT_SUCCESS);
  42. } else {
  43. printf("Child exited abnormally. Wait for 5 seconds....\n");
  44. sleep(5);
  45. printf("Restarting child again.\n");
  46. pid = fork();
  47. }
  48. }
  49. break;
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement