Experimental Purpose:
Familiar with the application of process creation functions in Linux.
Experimental content:
Program 1:
- Complete the creation of the child process with the fork () function.
- Output of word in parent process, child process output Hello
- Run the Hibernate function to implement the child process first
1#include <stdio.h>2#include <sys/types.h>3#include <unistd.h>4#include <stdlib.h>5 intMain ()6 {7 pid_t pid;8 if(PID = fork ()) = =0)9 {Ten //Child Process Oneprintf"hello\n"); AExit0); - } - Else if(PID >0) the { - //Parent Process -Sleep5); -printf"word\n"); +Exit0); - } + Else A { atprintf"fork () error\n"); -Exit0); - } -}
The fork () call failed to return-1.
There are two main reasons why the fork function call failed:
- There are already too many processes in the system
- The total number of processes for the actual user ID exceeds the system limit
The fork call is successfully returned two times.
- In the parent process, it returns once, and the return value is the newly derived child process ID number
- It also returns one time in a child process, with a return value of 0, so you can distinguish whether the current process is a child or parent process by the return value.
After the fork is called, the parent and child processes continue to execute the instruction after the fork function, whether the parent process executes first or the child process is indeterminate,
Lab 5-Process Management