和菜鸟一起深入学习国嵌实验之进程创建,exec函数,进程等待
来源:互联网 发布:doom4优化 编辑:程序博客网 时间:2024/05/17 05:14
1、 fork进程创建
代码:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <errno.h>#include <math.h> int main(int argc, char *argv[]){ pid_t child; //create child process if((child = fork()) == -1) { printf("Fork Error: %s\n", strerror(errno)); exit(1); } else { if(child == 0) { printf("I am the child : %d\n", getpid()); exit(0); } else { printf("I am the father : %d\n", getpid()); return 0; } } return 0;}
Makefile:
CC = gcc CURTDIR = $(shell pwd)TARGET = myfork %.o:%.c $(CC)-c $(EXTRAFLAGS) $< -o $@%.o:%.S $(CC)-c $(EXTRAFLAGS) $< -o $@ .PHONY: all clean $(TARGET): $(TARGET).o $(CC) -o $@ $^ clean: rm-rf $(TARGET) $(TARGET).o
运行结果:
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.8$makegcc -c myfork.c -o myfork.ogcc -o myfork myfork.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.8$lsMakefile myfork myfork.c myfork.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.8$./myforkI am the father : 8694eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.8$I am the child : 8695
总结:fork函数创建子进程后,父子进程是独立、同时运行的,并没有先后顺序之分。
2、vfork进程创建
代码:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <errno.h>#include <math.h> int main(int argc, char *argv[]){ pid_t child; //create child process if((child = vfork()) == -1) { printf("Fork Error: %s\n", strerror(errno)); exit(1); } else { if(child == 0) { sleep(1); printf("I am the child :%d\n", getpid()); exit(0); } else { printf("I am the father : %d\n", getpid()); return 0; } } return 0;}
Makefile:
CC = gcc CURTDIR = $(shell pwd)TARGET = myvfork %.o:%.c $(CC)-c $(EXTRAFLAGS) $< -o $@%.o:%.S $(CC)-c $(EXTRAFLAGS) $< -o $@ .PHONY: all clean $(TARGET): $(TARGET).o $(CC) -o $@ $^ clean: rm-rf $(TARGET) $(TARGET).o
运行结果:
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.9$makegcc -c myvfork.c -o myvfork.ogcc -o myvfork myvfork.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.9$lsMakefile myvfork myvfork.c myvfork.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.9$./myvforkI am the child : 8870I am the father : 8869
总结:调用fork函数产生的父子进程的运行顺序是不定的,而调用vfork函数产生的父子进程必定是子进程先运行完,父进程再运行的。
3、 exec函数族
代码:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <errno.h>#include <math.h> int main(int argc, char *argv[]){ if(argc < 2) { perror("you haven't input the filename, please try again!\n"); exit(EXIT_FAILURE); } if(execl("./file_create", "file_creat", argv[1],NULL) < 0) perror("execl error!\n"); return 0;}
Makefile:
CC = gcc CURTDIR = $(shell pwd)TARGET = myexec %.o:%.c $(CC)-c $(EXTRAFLAGS) $< -o $@%.o:%.S $(CC)-c $(EXTRAFLAGS) $< -o $@ .PHONY: all clean $(TARGET): $(TARGET).o $(CC) -o $@ $^ clean: rm-rf $(TARGET) $(TARGET).o
运行结果:
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.10$makegcc -c myexec.c -o myexec.ogcc -o myexec myexec.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.10$lsfile_create Makefile myexec myexec.c myexec.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.10$./myexec filecreate file file success! eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.10$lsfile file_create Makefile myexec myexec.c myexec.o
总结:exec函数族会在一个进程中启动另一个进程执行。并用它来取代原调用进程的数据段、代码段和堆栈段。在执行完exec函数调用后,原调用进程的内容除了进程号之外,其他全部都被新的进程替换了。
4、 等待进程
代码:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <errno.h>#include <math.h> int main(int argc, char *argv[]){ pid_t child; //create child process if((child = vfork()) == -1) { printf("Fork Error: %s\n", strerror(errno)); exit(1); } else { if(child == 0) { printf("the child process is run\n"); sleep(1); printf("I am the child : %d\n", getpid()); exit(0); } else { wait(NULL); printf("the father process is run\n"); printf("I am the father : %d\n", getpid()); return 0; } } return 0;}
Makefile:
CC = gcc CURTDIR = $(shell pwd)TARGET = mywait %.o:%.c $(CC)-c $(EXTRAFLAGS) $< -o $@%.o:%.S $(CC)-c $(EXTRAFLAGS) $< -o $@ .PHONY: all clean $(TARGET): $(TARGET).o $(CC) -o $@ $^ clean: rm-rf $(TARGET) $(TARGET).o
运行结果:
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.11$makegcc -c mywait.c -o mywait.ogcc -o mywait mywait.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.11$lsMakefile mywait mywait.c mywait.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.11$./mywaitthe child process is runI am the child : 9275the father process is runI am the father : 9274
结论:用fork创建子进程后,父子进程的执行顺序fork函数不能确定。我们可以用wait函数和waitpid函数使父进程阻塞等待子进程退出的性质,来确保子进程先结束。
- 和菜鸟一起深入学习国嵌实验之进程创建,exec函数,进程等待
- 和菜鸟一起深入学习国嵌实验之进程间通信
- 和菜鸟一起深入学习国嵌实验之linux常用命令
- 和菜鸟一起深入学习国嵌实验之vim常用命令
- 和菜鸟一起深入学习国嵌实验之脚本编程
- 和菜鸟一起深入学习国嵌实验之gcc分步编译&&gdb调试
- 和菜鸟一起深入学习国嵌实验之简单Makefile
- 和菜鸟一起深入学习国嵌实验之文件编程
- 和菜鸟一起深入学习国嵌实验之线程编程
- 和菜鸟一起深入学习国嵌实验之网络编程
- (三)和菜鸟一起学习unix之创建进程 fork getpid getppid
- (三)和菜鸟一起学习unix 进程终止
- (三)和菜鸟一起学习unix 僵尸进程
- 【经典转载】Linux进程学习系列之五 等待进程结束wait()和waitpid()函数
- 进程控制之exec函数
- 进程控制之exec函数
- linux创建进程和等待进程退出
- Linux进程控制函数之exec()函数的学习
- 中国国家气象局天气预报信息接口
- 如何更有效的发布招聘信息
- 使用mm单独编译android的APP时出现错误
- 直接运行内存中的程序
- VS addin 例子(附源码)
- 和菜鸟一起深入学习国嵌实验之进程创建,exec函数,进程等待
- 我自己写的一个makefile,研究了2周,写的还是这么粗糙,惭愧。。
- Problem B bfs
- Service----样例一
- hdu_3407_Zjnu Stadium(带权并查集)
- ucore lab1 任务三
- xcode 快捷键
- Linux 下 socket 编程示例
- Android 简单的使用api 播放在线视频