Create a process:
/* ******************* * Function: Create a new process by copying the current process (as with the parent process) and performing the same position *pid_t: generally 16-bit signed integer number, not enough to typedef * Return value: 0: " In the parent process,---> Return child process Number * 0: In child process---> return 0 * -1: Failed and set errno * *************** */pid_t Fork (void);
Parent-Child process difference:Fork return value is not the same
Different PID
Ppid different
Pending signals (unresponsive signals) and file locks do not inherit
Resource Utilization 0
Init process: Is the ancestor process of all processes---process No. 1th
Eg: simple creation of a child process
/************************ * Create a child process ***********************/#include<stdio.h>#include<stdlib.h>#include<unistd.h>intMain () {pid_t pid; printf ("[%d]:begin\n", Getpid ()); //0. Be sure to flush the previously opened stream fflush (NULL) before fork; //1. Create a processPID =Fork (); if(PID <0) {perror ("Fork ()"); Exit (1); } //2. Sub-process if(PID = =0) {printf ("[%d]:child is working\n", Getpid ()); } Else{printf ("[%d]:p arent is working\n", Getpid ()); } printf ("[%d]:end\n", Getpid ()); Exit (0);}
There are two kinds of results without fflus.
Result 1:
[root]#./forkbase
[3862]:begin
[3862]:p arent is working
[3862]:end
[3863]:child is working
[3863]:end
Result 2:
[root]#./forkbase >/tmp/out
[root]# Cat/tmp/out
[3997]:begin
[3997]:p arent is working
[3997]:end
[3997]:begin
[3998]:child is working
[3998]:end
*[root]#./forkbase output to terminal (row buffer, add \ n to flush buffer, so begin prints once)
*[root]#./forkbase >/tmp/out Redirect to a file, the file is in full buffer mode (\ n is just a newline), because the child process inherits everything from the parent process, then the buffer has two begin, so begin will be printed 2 times,
*: Scheduler scheduling policy to determine which process runs first
Get PID Number:
#include <sys/types.h><unistd.h>/******** get current process PID number ********* / pid_t getpid (void); /* ******* Gets the parent process PID number of the current process ******** */ pid_t getppid (void);
=======================================================================
Man PS
Command PS: Some information about the output process
PS AXF: Information about the printing process PID TTY STAT time COMMAND
Process number occupies terminal state consumption time which command triggered the
PS AXM: More information view
PS ax-l : View LWP (Lightweight process)---threads in a Linux-specific way
=======================================================================
Process number is used down
Process Creation fork, Getpid, Getppid