Fork
  • pid = fork()
  • The implementation order of parent process and child process is random
    1. #include <unistd.h>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4.  
    5. int main()
    6. {
    7. pid_t fpid;
    8.  
    9. fpid = fork();
    10.  
    11. if(fpid < 0)
    12. {
    13. printf("Not able to create child process ...");
    14. exit(1);
    15. }
    16.  
    17. if(fpid == 0)
    18. {
    19. printf("I am the child process %d, child id is: %d\n", getpid(), fpid);
    20. }
    21. else
    22. {
    23. printf("I am the parent process %d, parent id is: %d\n", getpid(), fpid);
    24. }
    25.  
    26. return 0;
    27. }
    1. #include <unistd.h>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4.  
    5. int main()
    6. {
    7. int i = 0;
    8. pid_t fpid;
    9.  
    10. printf("%10s %10s %10s %10s\n", "index", "parent", "current", "child");
    11.  
    12. for(i = 0; i < 2; i++)
    13. {
    14. fpid = fork();
    15.  
    16. if(fpid == 0)
    17. {
    18. printf("%10d %10d %10d %10d\n", i, getppid(), getpid(), fpid);
    19. }
    20. else
    21. {
    22. printf("%10d %10d %10d %10d\n", i, getppid(), getpid(), fpid);
    23. }
    24. }
    25.  
    26. return 0;
    27. }
  • let parent process implement after child process is implemented
    1. #include <unistd.h>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <sys/wait.h>
    5.  
    6. int main()
    7. {
    8. pid_t fpid;
    9.  
    10. fpid = fork();
    11.  
    12. if(fpid == 0)
    13. {
    14. printf("I am the child process %d, child id is: %d\n", getpid(), fpid);
    15. }
    16. else
    17. {
    18. wait(&fpid);
    19. printf("I am the parent process %d, child id is: %d\n", getpid(), fpid);
    20. }
    21.  
    22. return 0;
    23. }
    execv()
  • int execl(const char *path, const char *arg, ...)
  • int execlp(const char *file, const char *arg, ...)
  • int execle(const char *path, const char *arg, ..., char *const envp[])
  • int execv(const char *path, char *const argv[])
  • int execvp(const char *file, char *const argv[])
  • int execve(const char *path, char *const argv[], char *const envp[])
    1. #include <unistd.h>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <sys/wait.h>
    5.  
    6. int main()
    7. {
    8. pid_t fpid;
    9.  
    10. fpid = fork();
    11.  
    12. if(fpid == 0)
    13. {
    14. printf("I am the child process %d, child id is: %d\n", getpid(), fpid);
    15. char * execv_str[] = {"ls", "-l", "/home", NULL}; // ended with NULL
    16. if (execv("/bin/ls",execv_str) <0 ){
    17. perror("error on exec");
    18. exit(1);
    19. }
    20. }
    21. else
    22. {
    23. wait(&fpid);
    24. printf("I am the parent process %d, parent id is: %d\n", getpid(), fpid);
    25. }
    26.  
    27. return 0;
    28. }
    1. #include <unistd.h>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <sys/wait.h>
    5.  
    6. int main()
    7. {
    8. pid_t fpid;
    9.  
    10. fpid = fork();
    11.  
    12. if(fpid == 0)
    13. {
    14. char * execve_str[] = {"env",NULL};
    15. char * env[] = {"PATH=/tmp", "USER=lchen", "STATUS=testing", NULL};
    16. if (execve("/usr/bin/env",execve_str,env) <0 ){
    17. perror("error on exec");
    18. exit(1);
    19. }
    20. }
    21. else
    22. {
    23. wait(&fpid);
    24. printf("I am the parent process %d, parent id is: %d\n", getpid(), fpid);
    25. }
    26.  
    27. return 0;
    28. }
    system
  • system() automatically create a child process, does not support thread
  • exec functions support thread
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3.  
    4. int main()
    5. {
    6. printf("Call system() to implement a child process ...");
    7.  
    8. system("ls -l /home");
    9.  
    10. printf("Return from the child process ...");
    11.  
    12. return 0;
    13. }