程序(program)可执行文件(二进制文件)
程序的执行实例被称为进程(process),具有并行性,互不干扰的特点
Linux中的进程包含3段,和C差不多,分别为代码段,数据段和堆栈段
使用fork函数得到的子进程是继承了整个父进程的地址空间
exec()只是用另一个新程序替换了当前进程的正文,数据,堆和栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(void) { pid_t result; result = fork(); if(result == -1){ perror("fork"); exit; } else if(result == 0){ printf("child process:%d\n My PID is %d\n",result,getpid()); if(execlp("ps","ps","-ef",NULL)<0){ perror("execlp error"); } } else{ printf("father process:%d\n My PID is %d\n",result,getpid()); } }
|
管道pipe:单向的,先进先出,无结构的,固定大小的字节流.数据读出后将从管道中移走,其它读进程都不能再读到这些数据.
进程试图读空管道时,在有数据定稿管道之前,里程将一直阻塞.同样,管道已经满时,进程再试图琯管道,在其它进程从管道中移走数据之前,写进程将一直阻塞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <unistd.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> int main() { int pipe_fd[2]; if(pipe(pipe_fd)<0) { printf("pipe create error\n"); return -1; } else { printf("pipe create success \n"); } close(pipe_fd[0]); close(pipe_fd[1]); }
|
父进程写,子进程读
有名管道(FIFO)
信号(signal)
共享内存
消息队列(FIFO)
信号量
套接字(Socket)