和菜鸟一起深入学习国嵌实验之进程创建,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函数使父进程阻塞等待子进程退出的性质,来确保子进程先结束。

 

原创粉丝点击