Fork is used to create a new sub-process, a simple example:
#include <fcntl.h>//provides the Open function # include <sys/types.h>//the header file that provides system call flags # include <SYS/S Tat.h>//This header file provides system state information and related functions # include <sys/uio.h>//The header file provides process I Related functions for/O operations # include <unistd.h>//Standard function library # include <fcntl.h> File Operations Correlation Library # include <string.h>//string manipulation function library #i Nclude <sys/wait.h>//Wait call related function library # include <stdio.h> Standard input and Output function library # include <stdlib.h>//common Tools library int mai N () {char buf[100];p id_t cld_pid;int fd;int status;if (fd = open ("temp", O_creat | O_rdwr | O_trunc, 0664)) = =-1) {//Open or new file <s Pan style= "White-space:pre" ></span> PerroR ("Create File"); Exit (1);} strcpy (BUF, "Parent process data"), if ((Cld_pid = fork ()) = = 0) {strcpy (buf, "child process Data"),//printf ("1\n");p UTS ("Child process is working:");p rintf (" Sub-process PID is%d\n ", getpid ()); Pidprintf of the output subprocess ("parent process pid is%d\n", getppid ()); Outputs the parent process's Pidwrite (FD, buf, strlen (BUF)); Close (FD); exit (0);} else {puts ("parent process is working:"),//printf ("2\n");p rintf ("parent process pid is%d\n", getpid ()); Outputs the parent process's pidprintf ("subprocess pid is%d\n", cld_pid); Pidwrite of the output subprocess (FD, buf, strlen (BUF)), close (FD); Wait (&status); return 0;}
The basic operation of the process, fork (), creates a multi-process.