1. Fork Process Creation
Code:
#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
Running result:
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
Conclusion: After the fork function creates a sub-process, the Parent and Child processes run independently and at the same time, and there is no sequential division.
2. vfork Process Creation
Code:
#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
Running result:
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
Summary: The execution sequence of the Parent and Child processes generated by calling the fork function is not fixed. The Parent and Child processes generated by calling the vfork function must be completed by the child process and then run by the parent process.
3. Exec function family
Code:
#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
Running result:
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
Summary: The exec function family starts another process for execution in one process. Use it to replace the data segment, code segment, and stack segment of the original calling process. After executing the exec function call, all the content of the original calling process except the process number is replaced by the new process.
4. Waiting for processes
Code:
#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
Running result:
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
Conclusion: after a child process is created using fork, the execution sequence of the Parent and Child processes cannot be determined. We can use the wait and waitpid functions to block the parent process and wait for the child process to exit to ensure that the child process ends first.