SHARE
TWEET

gcc

a guest Feb 26th, 2020 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6.  
  7. //sample:
  8. //gcc gcc.c -o fork1
  9. //./fork2 /bin/ls *.c
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.     pid_t pid = fork();
  14.     if(pid < 0)
  15.     {
  16.         perror("Cannot fork process");
  17.         exit(1);
  18.     }
  19.     else if (pid == 0)
  20.     {
  21.         //child
  22.         char *argv2[argc];     
  23.         argv2[0] = "fork1";//there path
  24.         for(int i = 2; i < argc; i++)
  25.         {
  26.             argv2[i-1] = argv[i];
  27.         }
  28.         argv2[argc] = NULL;
  29.         execve(argv[1], argv2, NULL);//there filename
  30.     }
  31.     printf("child returned: %d \n", WEXITSTATUS(pid));
  32.     wait(&pid);
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top