exec族函数
fork()函数生成的子进程只能运行与父进程相同的代码函数,如果想要子进程执行另外一个程序,需要用到exec族的函数,如下: execlp()函数 int execlp(const char *file,const char *arg,…); /*** execlp.c ***/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main() { pid_t pid; pid = fork(); if(-1 == pid) { perror("fork error:"); exit(1); } else if(0 < pid) { sleep(2); printf("parentn"); } else { execlp("ls","ls","-l","-a",NULL); } return 0; } 运行结果: [email?protected]:~/wangqinghe/linux/20190806$ ./execlp 总用量 28 drwxrwxr-x 2 ubuntu1604 ubuntu1604 4096 8月?? 6 13:19 . drwxrwxr-x 3 ubuntu1604 ubuntu1604 4096 8月?? 6 13:06 .. -rwxrwxr-x 1 ubuntu1604 ubuntu1604 8856 8月?? 6 13:19 execlp -rw-rw-r-- 1 ubuntu1604 ubuntu1604? 274 8月?? 6 13:19 execlp.c -rw-rw-r-- 1 ubuntu1604 ubuntu1604?? 37 8月?? 6 13:10 test.c parent ? execl()函数 execl()函数第一个参数跟的是程序路径地址 int execl(const char *path,const char*arg,…); /*** execl.c ***/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main() { pid_t pid; pid = fork(); if(-1 == pid) { perror("fork error:"); exit(1); } else if(0 < pid) { sleep(2); printf("parentn"); } else { execlp("/bin/ls",NULL); } return 0; } 运行结果: [email?protected]:~/wangqinghe/linux/20190806$ gcc execl.c -o execl [email?protected]:~/wangqinghe/linux/20190806$ ./execl 总用量 44 drwxrwxr-x 2 ubuntu1604 ubuntu1604 4096 8月?? 6 13:23 . drwxrwxr-x 3 ubuntu1604 ubuntu1604 4096 8月?? 6 13:06 .. -rwxrwxr-x 1 ubuntu1604 ubuntu1604 8856 8月?? 6 13:23 execl -rw-rw-r-- 1 ubuntu1604 ubuntu1604? 280 8月?? 6 13:23 execl.c -rwxrwxr-x 1 ubuntu1604 ubuntu1604 8856 8月?? 6 13:19 execlp -rw-rw-r-- 1 ubuntu1604 ubuntu1604? 275 8月?? 6 13:21 execlp.c -rw-rw-r-- 1 ubuntu1604 ubuntu1604?? 37 8月?? 6 13:10 test.c parent 该函数可以执行自定义函数: /*** * test.c **/ #include<stdio.h> #include<unistd.h> int main() { printf("This is a testn"); sleep(1); return 0; } /*** execl.c ***/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main() { pid_t pid; pid = fork(); if(-1 == pid) { perror("fork error:"); exit(1); } else if(0 < pid) { sleep(2); printf("parentn"); } else { execlp("./test","test",NULL); } return 0; } 运行结果: [email?protected]:~/wangqinghe/linux/20190806$ ./execl This is a test parent ? execv()函数 int execv(const char * file,char *const argv[]); /*** execv.c ***/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main() { pid_t pid; pid = fork(); if(-1 == pid) { perror("fork error:"); exit(1); } else if(0 < pid) { sleep(2); printf("parentn"); } else { char *argv[] = {"ls","-a","-h",NULL}; execv("/bin/ls",argv); } return 0; } 运行结果: [email?protected]:~/wangqinghe/linux/20190806$ gcc execv.c -o execv [email?protected]:~/wangqinghe/linux/20190806$ ./execv 总用量 76K drwxrwxr-x 2 ubuntu1604 ubuntu1604 4.0K 8月?? 6 13:43 . drwxrwxr-x 3 ubuntu1604 ubuntu1604 4.0K 8月?? 6 13:06 .. -rwxrwxr-x 1 ubuntu1604 ubuntu1604 8.7K 8月?? 6 13:34 execl -rw-rw-r-- 1 ubuntu1604 ubuntu1604? 271 8月?? 6 13:34 execl.c -rwxrwxr-x 1 ubuntu1604 ubuntu1604 8.7K 8月?? 6 13:19 execlp -rw-rw-r-- 1 ubuntu1604 ubuntu1604? 275 8月?? 6 13:21 execlp.c -rwxrwxr-x 1 ubuntu1604 ubuntu1604 8.8K 8月?? 6 13:43 execv -rw-rw-r-- 1 ubuntu1604 ubuntu1604? 343 8月?? 6 13:42 execv.c -rwxrwxr-x 1 ubuntu1604 ubuntu1604 8.5K 8月?? 6 13:32 test -rw-rw-r-- 1 ubuntu1604 ubuntu1604? 106 8月?? 6 13:34 test.c -rw-rw-r-- 1 ubuntu1604 ubuntu1604?? 55 8月?? 6 13:28 while.c parent (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |