环境变量的获取 #include <stdio.h> #include <unistd.h> #include <stdlib.h> extern char **environ; int main() { char **env=environ; while(*env) { printf("%s/n",*env++); } return 0; } fork函数的用法 简单的fork函数的逻辑处理 #include <unistd.h> #include <stdio.h> int main() { int i,j; if(fork()==0) { //forked process for(i=0;i<1000;i++) printf("This is the child process!/n"); } else { //father process for(j=0;j<1000;j++) printf("This is the father process!/n"); } } 根据fork的返回值对不同进程进行处理 #include <stdlib.h> #include <stdio.h> #include <unistd.h> int main() { int pid; pid=fork(); switch(pid) { case -1: perror("fork failed"); exit(1); case 0: execl("/bin/ls","ls","-l",NULL); perror("exec failed."); exit(1); default: wait(NULL); printf("ls is completed./n"); exit(0); } } exc系列函数的用法 exec系列函数的作用是使父程序的代码空间和进程标识号全部交给另外一个可执行程序 原先的程序代码和栈空间全部被取代 #include <stdio.h> #include <unistd.h> int main() { printf("Executing ls/n"); execl("/home/woods/process/another","another",NULL); perror("execl failed to run ls/n"); return 1; } 另外一个可执行程序的源代码为: #include <stdio.h> int main() { printf("This is another program./n"); return 1; } 现对这个程序进行编译,参数的可执行文件名称为:another