The basic operation of the process, fork (), creates a multi-process.

Source: Internet
Author: User


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 &LT;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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.