Advertisement
Guest User

Untitled

a guest
May 3rd, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/mman.h>
  6. #include <fcntl.h>
  7. #include <sched.h>
  8. #include <string.h>
  9. #include <sys/wait.h>
  10.  
  11. #define STACK_SIZE (1024 * 1024)
  12.  
  13. int second_function(char *str, int depth) {
  14.     printf("%sn", str);
  15.     if (depth < 10) {
  16.         char new_str[20] = "hello world";
  17.         return second_function(new_str, depth + 1);
  18.     } else {
  19.         return 0;
  20.     }
  21. }
  22.  
  23. int first_function(void *arg) {
  24.     char *str = "hello world";
  25.     return second_function(str, 1);
  26. }
  27.  
  28. int main() {
  29.     char *stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  30.     if (stack == MAP_FAILED) {
  31.         perror("mmap");
  32.         exit(EXIT_FAILURE);
  33.     }
  34.  
  35.     pid_t pid = clone(first_function, stack + STACK_SIZE, CLONE_VM | CLONE_FILES | CLONE_FS | CLONE_SIGHAND | CLONE_THREAD | CLONE_PARENT_SETTID, NULL, NULL, NULL, NULL);
  36.     if (pid == -1) {
  37.         perror("clone");
  38.         exit(EXIT_FAILURE);
  39.     }  
  40.  
  41.  
  42.     if(pid > 0){
  43.         int status;
  44.         if (waitpid(pid, &status, 0) == -1) {
  45.             perror("waitpid");
  46.             exit(EXIT_FAILURE);
  47.         }
  48.     }
  49.    
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement