fork函数实现进程复制,相似于动物界的单性繁殖,fork函数直接建立一个子进程。这是Linux建立进程最经常使用的方法。在这一小节中,子进程概念指fork产生的进程,父进程指主动调用fork的进程。程序员
fork后,子进程继承了父进程不少属性,包括:shell
文件描述符:至关与dup,标准输入标准输出标准错误三个文件数组
帐户/组ID:session
进程组ID多线程
会话ID函数
控制终端this
set-user-ID和set-group-ID标记spa
当前工做目录命令行
根目录线程
umask
信号掩码
文件描述符的close-on-exec标记
环境变量
共享内存
内存映射
资源限制
可是也有一些不一样,包括:
fork返回值
进程ID
父进程
进程运行时间记录,在子进程中被清0
文件锁没有继承
闹钟
信号集合
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { printf("before fork\n"); // 在父进程中打开的文件描述符 // int fd = open("a.txt", O_RDWR|O_CREAT, 0777); // FILE* fp = fopen("a.txt", "r"); int fd = open("a.txt", O_RDWR); pid_t pid = fork(); // 建立一个新进程 if(pid == 0) { // 子进程可使用父进程的描述符 // write(fd, "hello", 5); // char ch = fgetc(fp); char ch; read(fd, &ch, 1); printf("ch is %c\n", ch); printf("this is in child, ppid=%d\n", (int)getppid()); } else if(pid > 0) { // write(fd, "world", 5); char ch; read(fd, &ch, 1); printf("ch is %c\n", ch); // 当fork返回值大于0时,说明该进程是父进程 // 此时,返回值就是子进程的pid printf("this is in parent, pid=%d\n", (int)getpid()); } else { printf("error fork\n"); } printf("hello fork\n"); }
#include <stdio.h> #include <unistd.h> int global_var = 0;//fork()出来的子进程的值改变,不会影响父进程 由于开开辟了新的空间 int main() { int var = 0; int* p = (int*)malloc(sizeof(int)); *p = 0; pid_t pid = fork(); if(pid == 0) { global_var = 100; *p = 100; var = 100; printf("set var\n"); } else if(pid > 0) { sleep(1); // 肯定的结果,就是0 printf("%d\n", global_var); printf("var is %d\n", var); // 0 printf("*p = %d\n", *p); } printf("hello world\n"); }
#include <stdio.h> #include <unistd.h> void forkn(int n) { int i; for(i=0; i<n; ++i) { pid_t pid = fork(); if(pid == 0) break; } } int main() { forkn(10); printf("hello world\n"); }
进程有许多终止方法:
方法 | |
---|---|
main函数return | 正常退出 |
调用exit或者_Exit或者_exit | 正常退出 |
在多线程程序中,最后一个线程例程结束 | 正常退出 |
在多线程程序中,最后一个线程调用pthread_exit | 正常退出 |
调用abort | 异常退出 |
收到信号退出 | 异常退出 |
多线程程序中,最后一个线程响应pthread_cancel | 异常退出 |
当进程退出时,内核会为进程清除它申请的内存,这里的内存是指物理内存,好比栈空间、堆、代码段、数据段等,而且关闭全部文件描述符。
通常来讲,进程退出时,须要告诉父亲进程退出的结果,若是是正常退出,那么这个结果保存在内核的PCB中。若是是异常退出,那么PCB中保存退出结果的字段,是一个不肯定的值。所以程序员应该避免程序的异常退出。
进程退出时,除了它的PCB所占内存,其余资源都会清除。
一个进程终止后,其实这个进程的痕迹尚未彻底被清除,由于还有一个PCB在内核中,若是不回收,那么会致使内存泄漏。父进程能够调用wait函数来回收子进程PCB,并获得子进程的结果。
wait
是一个阻塞调用,它的条件是一个子进程退出或者一个子进程有状态变化。wait
获得的status,包含了子进程的状态变化缘由和退出码信息等等。
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main() { pid_t pid = fork(); if(pid == 0) { sleep(1); printf("child process\n"); return 18; } else if(pid > 0) { printf("parent process\n"); // 等待子进程结束,而且回收子进程的PCB int status; wait(&status); // 如何获得子进程的返回值 if(WIFEXITED(status)) { printf("normal child process exit\n"); // 正常退出 int code =WEXITSTATUS(status); printf("code is %d\n", code); } else if(WIFSIGNALED(status)) { printf("signal\n"); } else if(WIFSTOPPED(status)) { printf("child stopped\n"); } else if(WIFCONTINUED(status)) { printf("child continue...\n"); } printf("after wait\n"); } return 0; }
wait和waitpid可能会阻塞父进程,因此通常使用SIGCHLD信号来监控子进程
是指已经退出的进程,可是父进程没有调用wait回收的子进程。僵尸进程没有任何做用,惟一的反作用就是内存泄漏。若是父进程退出,那么它的全部僵尸儿子会获得清理,所以僵尸进程通常指那些用不停歇的后台服务进程的僵尸儿子。
程序员应该避免僵尸进程的产生。
#include <stdio.h> #include <unistd.h> int main() { pid_t pid = fork(); if(pid == 0) { // 子进程什么事儿都不干,退出了,此时子进程是僵尸进程 } else if(pid > 0) { getchar(); // 父进程不退出 } return 0; }
父进程退出了,而子进程没有退出,那么子进程就成了没有父亲的孤儿进程。孤儿进程不会在系统中出现很长时间,由于系统一旦发现孤儿进程,就会将其父进程设置为init进程。那么未来该进程的回收,由init来负责。
exec函数执行一个进程,当一个进程调用exec后,调用该函数的进程的虚拟地址空间的代码段、数据段、堆、栈被释放,替换成新进程的代码段、数据段、堆、栈,而PCB依旧使用以前进程的PCB。这个函数用中文来讲就是鸠占鹊巢。
exec后使用的是同一个PCB,因此exec以后和以前,由不少进程属性是相同的,包括:
进程ID和父进程ID
帐户相关
进程组相关
定时器
当前目录和根目录
umask
文件锁
信号mask
未决的信号
资源限制
进程优先级
进程时间
没有close-on-exec属性的文件描述符
使用fork和exec来执行一个新程序
#include <unistd.h> #include <stdio.h> // execle, e表示环境变量environ // int main(int argc, char* argv[]) { char* args[] = { "/bin/ls", "-a", "-l", NULL }; execv("/bin/ls", args); } int main2(int argc, char* argv[]) { // p表示在PATH的环境变量中寻找这个程序 execlp("ls", "ls", NULL); } int main1(int argc, char* argv[]) { // 执行一个程序 execl("/bin/ls", "/bin/ls", "-a", "-l", NULL); // 该函数不会被执行 printf("hello world\n"); }
#include <stdio.h> #include <fcntl.h> #include <sys/types.h> #include <unistd.h> int main() { // fd is 3 int fd = open("exec.txt", O_RDWR|O_CREAT|O_CLOEXEC, 0777); execl("./exec_test", "./exec_test", NULL); }
函数后缀 | 解析 |
---|---|
l | list 用不定参数列表来表示命令参数,若是用不定参数列表,那么用NULL表示结束 |
v | vector 用数组来传递命令行参数 |
p | path 表示程序使用程序名便可,在$PATH中搜索该程序,不带p的须要提供全路径 |
e | environ 表示环境变量 |
不定参数函数定义:
int main() { int a = add(3, 12, 13, 14); int b = add(2, 12, 13); int c = add(4, 12, 13, 14, 15); printf("%d, %d, %d\n", a, b, c); char* p = concat("abc", "bcd", NULL); printf("p is %s\n", p); // 最后的NULL,被称之为哨兵 p = concat("aaaa", "bbbb", "cccc", NULL); printf("p is %s\n", p); }
#include <stdio.h> #include <fcntl.h> #include <sys/types.h> // 若是没有__VA_ARGS__不带##,表示__VA_ARGS__至少要表示一个参数 // #define mylog(fmt, ...) printf("[%s:%d] "fmt, __FILE__, __LINE__, __VA_ARGS__) // __VA_ARGS__若是有##,表示能够没有参数 #define mylog(fmt, ...) printf("[%s:%d] "fmt, __FILE__, __LINE__, ##__VA_ARGS__) int main() { int fd = open("a.txt", O_RDWR); if(fd < 0) { mylog("error open file\n"); } }
#include <stdio.h> // 转字符串 abc "abc" #define STR(a) #a // 拼接标识符 #define CC(a, b) a##b int main() { int abcxyz = 100; printf("%d\n", CC(abc, xyz)); }
在Linux系统中,进程间除了有父子关系,还有组关系、Session关系、进程和终端进程关系。设计这些关系是为了更好的管理进程。
一次登录算一个session,exit命令能够退出session,session包括多个进程组,一旦session领导退出,那么一个session内全部进程退出(它的全部进程收到一个信号)。
#include <unistd.h> int main() { pid_t pid = fork(); if(pid == 0) { // 独立一个session setsid(); } while(1) { sleep(1); } }
在终端执行进程,就会生成一个进程组。执行的进程fork以后,子进程和父进程在一个组中。
进程组长退出后,进程组的其余进程的组号依旧没有变化。
使用-job
定义进程数量,加速文件拷贝。
#include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> // ls // mkdir aaa // cp ../aa bb // cd void handle_cmd(char* cmd) { char* args[1024]; char* p = strtok(cmd, " "); int i = 0; while(p) { args[i++] = p; p = strtok(NULL, " "); } args[i] = NULL; // 表示参数结束位置 if(strcmp(args[0], "cd") == 0) { // 切换当前目录 chdir(args[1]); return; } pid_t pid = fork(); if(pid == 0) { execvp(args[0], args); // 若是命令执行失败,应该让子进程退出 printf("invalid command\n"); exit(0); } else { wait(NULL); } } int main() { while(1) { printf("myshell> "); // 等待用户输入 char buf[4096]; fgets(buf, sizeof(buf), stdin); buf[strlen(buf)-1] = 0; // remove \n if(strlen(buf) == 0) { continue; } handle_cmd(buf); } }
fork:建立子进程exec:执行新的程序wait/waitpid:等待子进程结束,回收子进程PCB内存。va_list:va_start:定义指向不定参数的第一个参数的地址va_arg:从参数列表中获取一个参数,而且让指针指向下一个参数va_end:清除ap