Fork
pid = fork()
- pid is the process id of child process
- for parent, pid is the process id of the created child process
- for child, pid equals to zero
- if error, pid is a negative value
The implementation order of parent process and child process is random
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- int main()
- {
- pid_t fpid;
-
- fpid = fork();
-
- if(fpid < 0)
- {
- printf("Not able to create child process ...");
- exit(1);
- }
-
- if(fpid == 0)
- {
- printf("I am the child process %d, child id is: %d\n", getpid(), fpid);
- }
- else
- {
- printf("I am the parent process %d, parent id is: %d\n", getpid(), fpid);
- }
-
- return 0;
- }
-
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- int main()
- {
- int i = 0;
- pid_t fpid;
-
- printf("%10s %10s %10s %10s\n", "index", "parent", "current", "child");
-
- for(i = 0; i < 2; i++)
- {
- fpid = fork();
-
- if(fpid == 0)
- {
- printf("%10d %10d %10d %10d\n", i, getppid(), getpid(), fpid);
- }
- else
- {
- printf("%10d %10d %10d %10d\n", i, getppid(), getpid(), fpid);
- }
- }
-
- return 0;
- }
-
let parent process implement after child process is implemented
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
-
- int main()
- {
- pid_t fpid;
-
- fpid = fork();
-
- if(fpid == 0)
- {
- printf("I am the child process %d, child id is: %d\n", getpid(), fpid);
- }
- else
- {
- wait(&fpid);
- printf("I am the parent process %d, child id is: %d\n", getpid(), fpid);
- }
-
- return 0;
- }
-
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[])
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
-
- int main()
- {
- pid_t fpid;
-
- fpid = fork();
-
- if(fpid == 0)
- {
- printf("I am the child process %d, child id is: %d\n", getpid(), fpid);
- char * execv_str[] = {"ls", "-l", "/home", NULL}; // ended with NULL
- if (execv("/bin/ls",execv_str) <0 ){
- perror("error on exec");
- exit(1);
- }
- }
- else
- {
- wait(&fpid);
- printf("I am the parent process %d, parent id is: %d\n", getpid(), fpid);
- }
-
- return 0;
- }
-
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
-
- int main()
- {
- pid_t fpid;
-
- fpid = fork();
-
- if(fpid == 0)
- {
- char * execve_str[] = {"env",NULL};
- char * env[] = {"PATH=/tmp", "USER=lchen", "STATUS=testing", NULL};
- if (execve("/usr/bin/env",execve_str,env) <0 ){
- perror("error on exec");
- exit(1);
- }
- }
- else
- {
- wait(&fpid);
- printf("I am the parent process %d, parent id is: %d\n", getpid(), fpid);
- }
-
- return 0;
- }
-
system
system() automatically create a child process, does not support thread
exec functions support thread
- #include <stdio.h>
- #include <stdlib.h>
-
- int main()
- {
- printf("Call system() to implement a child process ...");
-
- system("ls -l /home");
-
- printf("Return from the child process ...");
-
- return 0;
- }
-