博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
和菜鸟一起深入学习国嵌实验之进程创建,exec函数,进程等待
阅读量:4555 次
发布时间:2019-06-08

本文共 4909 字,大约阅读时间需要 16 分钟。

1、 fork进程创建

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
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 
#include
#include
#include
#include
#include
#include
#include
#include
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 
#include
#include
#include
#include
#include
#include
#include
#include
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 
#include
#include
#include
#include
#include
#include
#include
#include
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函数使父进程阻塞等待子进程退出的性质,来确保子进程先结束。

 

转载于:https://www.cnblogs.com/wuyida/archive/2013/01/30/6300022.html

你可能感兴趣的文章
Codeforces Round #354 (Div. 2)
查看>>
HDU4791【杂】
查看>>
PHP系列之一traits的应用
查看>>
利刃 MVVMLight 2:Model、View、ViewModel结构以及全局视图模型注入器的说明
查看>>
被废弃的 Thread.stop, Thread.suspend, Thread.resume 和Runtime.runFinalizersOnExit
查看>>
冲刺1
查看>>
git rebase之前需要commit才行
查看>>
python,os操作文件,文件路径(上一级目录)
查看>>
jquery格式化数字
查看>>
解决多线程委托二义性问题
查看>>
love2d教程3--输入和音乐
查看>>
php代码检查
查看>>
MyBatis笔记
查看>>
Android 四大组件之BroadcastReceiver
查看>>
0927-练习
查看>>
android TextView属性详解
查看>>
redis 主从同步搭建
查看>>
Wp7中,使你的程序在程序列表中的名称自适应手机语言设置
查看>>
error: .repo/manifests/: contains uncommitted changes
查看>>
逆元(inv)
查看>>