Process creation
#include <sys/types.h><unistd.h>pid_t fork (void), fork Call 2 times, so 2 return values, The parent process returns a child process ID of0 , 0 is returned in the child process, and an error returns-1
Process instance
#include <unistd.h>#include<string.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>intMainvoid) {printf ("Now PID:%d\n", Getpid ()); pid_t pid; PID=Fork (); inti; if(pid<0) {printf ("Create Progress error\n"); return 1; } Else if(PID = =0) { for(i=0;i<Ten; i++) {printf ("I am son progress,pid:%d ppid:%d\n", Getpid (), Getppid ()); I++; Sleep (1); } } Else { for(i=0;i<Ten; i++) {printf ("I am Father Progress,pid:%d ppid:%d\n", Getpid (), Getppid ()); I++; Sleep (1); }} printf ("PID:%d\n", Getpid ()); Sleep (1); return 0;}
Run results
Now PID:3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324PID:3324PID:3325
You can see that the parent-child process is running alternately
Parent-child processes which run first based on system scheduling
Child process Inheritance
Child process Inheritance Properties user information permissions, directory information, signal information, environment, shared bucket, resource limit, heap, stack and data segment, shared code satin process-specific attribute process ID, lock information, run time, pending signal operation file kernel structure change The child process inherits only the file descriptor table of the parent process, does not inherit the single-shared file tables entry and the I-node parent process creates a child process, and the reference counter of the file table entry is incremented by 1 to 2, and when the parent process is close, the counter is reduced by 1, and the child process can still use the file table entry. The file table entry is freed only if the counter is 0 o'clock
Only body segments share physical memory
Static variables, global variables stored in body segments, local variable storage Yu Ching
First, process and signal process creation