Linux进程线程初探(多进程与进程间通信-管道)
先上一个多进程的简单多进程实例,此实例将在同一个程序中创建两个进程: /* 程序说明: 创建两个进程,进程一执行”ls -l“操作,进程二做sleep操作 主进程阻塞等待进程1结束,再等待进程2结束,程序结束 */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> int main() { pid_t child1,child2; child1 = fork(); //进程1 if(child1 == -1){ perror("child1 creat error:"); exit(1); }else if(child1 == 0){ if(execlp("ls","ls","-l",NULL) < 0){ perror("execlp error:"); exit(1); } exit(0); } child2 = fork(); //进程2 if(child2 == -1){ perror("child2 creat error:"); exit(1); }else if(child2 == 0){ sleep(3); exit(0); } // waitpid(child1,NULL,0); 主进程 wait(0);