#include <stdio.h> #include <unistd.h> int main() { char *arglist[3]; arglist[0] = "ls"; arglist[1] = "-l"; arglist[2] = 0 ;//NULL printf("* * * About to exec ls -l\n"); execvp( "ls" , arglist ); printf("* * * ls is done. bye"); return 0; }
頭文件:#includeshell
定義函數:int execvp(const char file, char const argv []);數組
函數說明:execvp()會從PATH 環境變量所指的目錄中查找符合參數file 的文件名, 找到後便執行該文件, 而後將第二個參數argv 傳給該欲執行的文件。併發
返回值 若是執行成功則函數不會返回,執行失敗則直接返回-1,失敗緣由存於errno中。函數
此處執行的execvp("ls",arglist);就會從PATH環境變量所指的目錄中尋找ls的文件名,找到後執行ls,而後將-l傳給該文件。ui
運行結果以下:
spa
跟ls -l命令相比,發現exec1的結果並未將可執行文件、文件夾與文件區分開來。指針
#include <stdio.h> #include <unistd.h> int main() { char *arglist[3]; arglist[0] = "ls"; arglist[1] = "-l"; arglist[2] = 0 ; printf("* * * About to exec ls -l\n"); execvp( arglist[0] , arglist ); printf("* * * ls is done. bye\n"); }
與exec1.c執行的功能是同樣的,僅僅是在調用execvp函數時,用arglist[0]替換"ls",實現用數組傳參。code
運行結果:
進程
int main()
{
char arglist[3];
char myenv[3];
myenv[0] = "PATH=:/bin:";
myenv[1] = NULL;ip
arglist[0] = "ls";
arglist[1] = "-l";
arglist[2] = 0 ;
printf("* * * About to exec ls -l\n");
// execv( "/bin/ls" , arglist );
// execvp( "ls" , arglist );
// execvpe("ls" , arglist, myenv);
該程序運用了execlp函數
表頭文件 #include
定義函數 int execlp(const char * file,const char * arg,……);
函數說明 execlp()會從PATH 環境變量所指的目錄中查找符合參數file的文件名,找到後便執行該文件,而後將第二個之後的參數當作該文件的argv[0]、argv[1]……,最後一個參數必須用空指針(NULL)做結束。
返回值 若是執行成功則函數不會返回,執行失敗則直接返回-1,失敗緣由存於errno 中。
因此execlp("ls", "ls", "-l", NULL)等同於execvp( "ls" , arglist ),其中arglist[0] = "ls";arglist[1] = "-l"; arglist[2] = 0 ;
運行結果:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { int ret_from_fork, mypid; mypid = getpid(); printf("Before: my pid is %d\n", mypid); ret_from_fork = fork(); sleep(1); printf("After: my pid is %d, fork() said %d\n", getpid(), ret_from_fork); return 0; }
#include <stdio.h> #include <unistd.h> int main() { printf("before:my pid is %d\n", getpid() ); fork(); fork(); printf("aftre:my pid is %d\n", getpid() ); return 0; }
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int fork_rv; printf("Before: my pid is %d\n", getpid()); fork_rv = fork(); /* create new process */ if ( fork_rv == -1 ) /* check for error */ perror("fork"); else if ( fork_rv == 0 ){ printf("I am the child. my pid=%d\n", getpid()); exit(0); } else{ printf("I am the parent. my child is %d\n", fork_rv); exit(0); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int fork_rv; printf("Before: my pid is %d\n", getpid()); fork_rv = fork(); /* create new process */ if ( fork_rv == -1 ) /* check for error */ perror("fork"); else if ( fork_rv == 0 ){ printf("I am the child. my pid=%d\n", getpid()); printf("parent pid= %d, my pid=%d\n", getppid(), getpid()); exit(0); } else{ printf("I am the parent. my child is %d\n", fork_rv); sleep(10); exit(0); } return 0; }
再進行子進程,打印子進程當前的識別碼與其對應的父進程的識別碼。
運行結果:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int gi=0; int main() { int li=0; static int si=0; int i=0; pid_t pid = fork(); if(pid == -1){ exit(-1); } else if(pid == 0){ for(i=0; i<5; i++){ printf("child li:%d\n", li++); sleep(1); printf("child gi:%d\n", gi++); printf("child si:%d\n", si++); } exit(0); } else{ for(i=0; i<5; i++){ printf("parent li:%d\n", li++); printf("parent gi:%d\n", gi++); sleep(1); printf("parent si:%d\n", si++); } exit(0); } return 0; }
父進程先打印一個再休息一秒,子進程打印兩個再休息一秒,兩個進程併發,因此出現parent li:0;parent gi:0;接下來不是parent si:0;而是child li:0。
運行結果
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define MAXARGS 20 #define ARGLEN 100 int execute( char *arglist[] ) { execvp(arglist[0], arglist); perror("execvp failed"); exit(1); } char * makestring( char *buf ) { char *cp; buf[strlen(buf)-1] = '\0'; cp = malloc( strlen(buf)+1 ); if ( cp == NULL ){ fprintf(stderr,"no memory\n"); exit(1); } strcpy(cp, buf); return cp; } int main() { char *arglist[MAXARGS+1]; int numargs; char argbuf[ARGLEN]; numargs = 0; while ( numargs < MAXARGS ) { printf("Arg[%d]? ", numargs); if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n' ) arglist[numargs++] = makestring(argbuf); else { if ( numargs > 0 ){ arglist[numargs]=NULL; execute( arglist ); numargs = 0; } } } return 0; }
該函數的功能爲輸入命令,用回車表示結束命令的輸入,而後將它們傳入arglist之中,利用execute來調用執行命令。
運行結果:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <signal.h> #define MAXARGS 20 #define ARGLEN 100 char *makestring( char *buf ) { char *cp; buf[strlen(buf)-1] = '\0'; cp = malloc( strlen(buf)+1 ); if ( cp == NULL ){ fprintf(stderr,"no memory\n"); exit(1); } strcpy(cp, buf); return cp; } void execute( char *arglist[] ) { int pid,exitstatus; pid = fork(); switch( pid ){ case -1: perror("fork failed"); exit(1); case 0: execvp(arglist[0], arglist); perror("execvp failed"); exit(1); default: while( wait(&exitstatus) != pid ) ; printf("child exited with status %d,%d\n", exitstatus>>8, exitstatus&0377); } } int main() { char *arglist[MAXARGS+1]; int numargs; char argbuf[ARGLEN]; numargs = 0; while ( numargs < MAXARGS ) { printf("Arg[%d]? ", numargs); if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n' ) arglist[numargs++] = makestring(argbuf); else { if ( numargs > 0 ){ arglist[numargs]=NULL; execute( arglist ); numargs = 0; } } } return 0; }
#include <stdio.h> #include <stdlib.h> int main() { printf("hello"); fflush(stdout); while(1); }
#include <stdio.h> int main() { printf("hello\n"); while(1); }
運行結果:
#include <stdio.h> int main() { fprintf(stdout, "1234", 5); fprintf(stderr, "abcd", 4); }
#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { printf("my pid: %d \n", getpid()); printf("my parent's pid: %d \n", getppid()); return 0; }
#include <stdio.h> #include <stdlib.h> int main() { char **pp; pp[0] = malloc(20); return 0; }
段錯誤 通常是非法訪問內存形成的
核心已轉儲 (core dump) -- 內存清除,早期的內存用磁芯存儲器
#include <stdlib.h> int main ( int argc, char *argv[] ) { system(argv[1]); system(argv[2]); return EXIT_SUCCESS; }
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #define DELAY 4 void child_code(int delay) { printf("child %d here. will sleep for %d seconds\n", getpid(), delay); sleep(delay); printf("child done. about to exit\n"); exit(17); } void parent_code(int childpid) { int wait_rv=0; /* return value from wait() */ wait_rv = wait(NULL); printf("done waiting for %d. Wait returned: %d\n", childpid, wait_rv); } int main() { int newpid; printf("before: mypid is %d\n", getpid()); if ( (newpid = fork()) == -1 ) perror("fork"); else if ( newpid == 0 ) child_code(DELAY); else parent_code(newpid); return 0; }
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #define DELAY 10 void child_code(int delay) { printf("child %d here. will sleep for %d seconds\n", getpid(), delay); sleep(delay); printf("child done. about to exit\n"); exit(27); } void parent_code(int childpid) { int wait_rv; int child_status; int high_8, low_7, bit_7; wait_rv = wait(&child_status); printf("done waiting for %d. Wait returned: %d\n", childpid, wait_rv); high_8 = child_status >> 8; /* 1111 1111 0000 0000 */ low_7 = child_status & 0x7F; /* 0000 0000 0111 1111 */ bit_7 = child_status & 0x80; /* 0000 0000 1000 0000 */ printf("status: exit=%d, sig=%d, core=%d\n", high_8, low_7, bit_7); } int main() { int newpid; printf("before: mypid is %d\n", getpid()); if ( (newpid = fork()) == -1 ) perror("fork"); else if ( newpid == 0 ) child_code(DELAY); else parent_code(newpid); }
#include <stdio.h> #include <stdlib.h> int main(void) { printf("PATH=%s\n", getenv("PATH")); setenv("PATH", "hello", 1); printf("PATH=%s\n", getenv("PATH")); #if 0 printf("PATH=%s\n", getenv("PATH")); setenv("PATH", "hellohello", 0); printf("PATH=%s\n", getenv("PATH")); printf("MY_VER=%s\n", getenv("MY_VER")); setenv("MY_VER", "1.1", 0); printf("MY_VER=%s\n", getenv("MY_VER")); #endif return 0; }
#include <stdio.h> int main(void) { extern char **environ; int i; for(i = 0; environ[i] != NULL; i++) printf("%s\n", environ[i]); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <limits.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/myfifo" #define BUFFER_SIZE PIPE_BUF int main() { int pipe_fd; int res; int open_mode = O_RDONLY; char buffer[BUFFER_SIZE + 1]; int bytes = 0; memset(buffer, 0, sizeof(buffer)); printf("Process %d opeining FIFO O_RDONLY \n", getpid()); pipe_fd = open(FIFO_NAME, open_mode); printf("Process %d result %d\n", getpid(), pipe_fd); if (pipe_fd != -1) { do { res = read(pipe_fd, buffer, BUFFER_SIZE); bytes += res; } while (res > 0); close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("Process %d finished, %d bytes read\n", getpid(), bytes); exit(EXIT_SUCCESS); }
#include <stdio.h> main( int ac, char *av[] ) { int i; printf("Number of args: %d, Args are:\n", ac); for(i=0;i<ac;i++) printf("args[%d] %s\n", i, av[i]); fprintf(stderr,"This message is sent to stderr.\n"); }
運行結果:
運行結果: