exec函数族可以在进程中执行另外一个程序,字符串复制函数strncpy

来源:互联网 发布:各市平均车流量数据 编辑:程序博客网 时间:2024/06/07 08:08
#include<stdio.h>#include<stdlib.h>#include <unistd.h>#include <string.h>//exec函数族可以在进程中执行另外一个程序//字符串复制函数strncpyint main(int argc,char *argv[]){    printf("exec demo\n");    //判断    if(argc != 2)    {        perror("error");        exit(1);    }    char *val[] = {"ls","-l",NULL};    char ch;    //字符串复制函数    strncpy(&ch,argv[1],1);    switch(ch)    {        case 'l':            execl("/bin/ls","ls","-l",NULL);//int execl(const char *path, const char *arg, ...);            break;        case 'v':            execv("/bin/ls",val);//第二个参数是指针数组            break;        case 'p':            execlp("ls","ls","-l",NULL);            break;    }    //执行不成功    printf("process exit\n");}/*$ ./a.out vexec demototal 53-rwxrwxrwx 1 root root  718 Dec  4 17:02 10_daemon.c-rwxrwxrwx 1 root root  646 Dec  4 18:54 11_zuoye.c-rwxrwxrwx 1 root root  689 Dec  4 18:52 12_test.c-rwxrwxrwx 1 root root 1092 Dec  4 20:11 13_fork.c-rwxrwxrwx 1 root root 1872 Dec  4 21:49 14_deamon.c-rwxrwxrwx 1 root root  423 Dec  4 21:58 1_fork.c*/
原创粉丝点击