Header files: #include <unistd.h>
pid_t fork (void);
1. Create a child process, fail back-1.
2. Call once and return two times. Returns the PID and 0 of the child process, respectively, in the parent-child process. Different processing branches can be written for parent-child processes, respectively, using the return value.
#include <stdio.h> #include <unistd.h>int main (void) {printf ("%u process: I'm going to call fork () ... \ n", Getpid ());p id_t pid = Fork (); if (pid = =-1) {perror ("fork"); return-1;} if (PID = = 0) {printf ("%u" process: I am a child of the%u process.) \ n ", Getpid (), Getppid ()); return 0;} printf ("%u process: I am the parent process of the%u process.") \ n ", Getpid (), PID); sleep (1); return 0;}
3. A child process is a copy of the parent process that obtains a copy of the parent process data segment and the stack segment (including the I/O stream buffer), but the child process shares the code snippet of the parent process.
#include <stdio.h> #include <stdlib.h> #include <unistd.h>int global = 100;int Main (void) {int local = 20 0;char* heap = (char*) malloc (sprintf (char)) (Heap, "ABC");p rintf ("Parent process:%d%d%s\n", Global, Local, heap);p I d_t pid = fork (), if (PID = =-1) {perror ("fork"); return-1;} if (PID = = 0) {global++;local++;sprintf (heap, "XYZ");p rintf ("sub-process:%d%d%s\n", Global, local, heap); free (heap); return 0; }sleep (1);p rintf ("Parent process:%d%d%s\n", Global, Local, heap), free (heap); return 0;}
#include <stdio.h> #include <unistd.h>int main (void) {printf ("ABC");p id_t pid = fork (), if (PID = =-1) {Perro R ("fork"); return-1;} if (PID = = 0) {printf ("xyz\n"); return 0;} Sleep (1);p rintf ("\ n"); return 0;}
4. The parent-child process continues to run after the function call, and its sequence is indeterminate. Some implementations can guarantee that the child process is dispatched first.
#include <stdio.h> #include <unistd.h>int main (void) {printf ("Parent process:"); int A, B, c;scanf ("%d%d%d", &a, &am P;b, &c);p id_t pid = fork (), if (PID = =-1) {perror ("fork"); return-1;} if (PID = = 0) {scanf ("%d%d%d", &a, &b, &c);p rintf ("sub-process:%d%d%d\n", A, B, c); return 0;} Sleep (1);p rintf ("Parent process:%d%d%d\n", A, B, c); return 0;}
5. After a function call, the File descriptor table (process level) of the parent process is also copied to the child process, both of which share the same file tables (kernel level).
#include <stdio.h> #include <string.h> #include <fcntl.h> #include <unistd.h>int main (void) { int fd = open ("Ftab.txt", O_RDWR | O_creat | O_trunc, 0644); if (fd = =-1) {perror ("open"); return-1;} Const char* Text = "Hello, world!"; if (write (fd, text, strlen (text) * sizeof (text[0])) = =-1) {perror ("write"); return-1;} pid_t pid = fork (), if (PID = =-1) {perror ("fork"); return-1;} if (PID = = 0) {if (Lseek (FD,-7, seek_cur) = =-1) {perror ("Lseek"); return-1;} Close (FD); return 0;} Sleep (1); text = "Linux"; if (write (fd, text, strlen (text) * sizeof (text[0])) = =-1) {perror ("write"); return-1;} Close (FD); return 0;}
Result: Hello, Linux!
6. The total number of processes or the actual user ID has more processes than the system limits, and the function will fail.
7. A process can use this function if you want to create your own copy and execute the same code, or if you want to run concurrently with another program.
Note: The code before the fork is only executed by the parent process, and the parent-child process after the fork has an opportunity to execute, which is controlled by the code logic and enters different branches.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding of the Fork function