int main() { pid_t pid; char* message; int n; pid = fork(); if(pid < 0) { perror("fork failed\n"); exit(1); } if(pid == 0) { message ="this is child \n"; n = 6; } else{ message ="this is the parent\n"; n = 3; } for(; n>0;n--) { printf(message); sleep(1); } return 0; }
我们执行以下命令: $ gcc -g ex_fork.c $ ./ex_fork
得到的结果如下所示: xiaolluo@ubuntu:~/workspace$ gcc -g -o ex_fork ex_fork.c ex_fork.c: In function ‘main’: ex_fork.c:28: warning: format not a string literal and no format arguments xiaolluo@ubuntu:~/workspace$ ./ex_fork this is the parent this is child this is the parent this is child this is the parent this is child this is child xiaolluo@ubuntu:~/workspace$ this is child this is child
int execl(constchar*path,constchar*arg,...); int execlp(constchar*file,constchar*arg,...); int execle(constchar*path,constchar*arg,...,char*const envp[]); int execv(constchar*path,char*const argv[]); int execvp(constchar*file,char*const argv[]); int execve(constchar*path,char*const argv[],char*const envp[]);
我执行后得到的结果是: xiaolluo@ubuntu:~/workspace$ vim file.txt xiaolluo@ubuntu:~/workspace$ gcc -g -o upper upper.c xiaolluo@ubuntu:~/workspace$ gcc -g -o wrapper wrapper.c xiaolluo@ubuntu:~/workspace$ cat file.txt this is some words in low case and we want the program to change them to upper case . xiaolluo@ubuntu:~/workspace$ ./wrapper file.txt THIS IS SOME WORDS IN LOW CASE AND WE WANT THE PROGRAM TO CHANGE THEM TO UPPER CASE . xiaolluo@ubuntu:~/workspace$