Advertisement
Guest User

gcc

a guest
Feb 26th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement