CODE: /**//* ============================================================================ Name : fork_example.c Author : newflypig Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> int main(void) ...{ int i=1; printf("in the begining,the value=%d ",i); switch(fork())...{ case -1: fprintf(stderr,"%s ","fork error"); break; case 0: printf("child process start,at this time value=%d ",i); i++; printf("child process end,at this time value=%d ",i); break; default: printf("parent process: value=%d ",i); } return 0; } |
CODE: /**//* ============================================================================ Name : execlp_example.c Author : newflypig Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> int main(void) ...{ int filedes; if((filedes=open("dd.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))<0) fprintf(stderr,"%s ","open file error"); close(1); dup(filedes); close(filedes); execlp("ls","-l",(char * )0); return 0; } |
[火星人 ] 初識Linux/C語言編程,管道和重定向暨fork與execlp函數的理解已經有911次圍觀