Understanding of fork functions and understanding of fork Functions

Source: Internet
Author: User

Understanding of fork functions and understanding of fork Functions

Header file: # include <unistd. h>

pid_t fork (void);

1. Create a sub-process and return-1 If the sub-process fails.

2. Call once and return twice. The child process PID and 0 are returned respectively in the parent and child processes. Using different return values, you can write different processing branches for the parent and child processes respectively.
# Include <stdio. h> # include <unistd. h> int main (void) {printf ("% u process: I want to call fork... \ n ", getpid (); pid_t pid = fork (); if (pid =-1) {perror (" fork "); return-1 ;} if (pid = 0) {printf ("% u process: I am a child process of % u process. \ N ", getpid (), getppid (); return 0;} printf (" % u process: I am the parent process of % u process. \ N ", getpid (), pid); sleep (1); return 0 ;}


3. A child process is a copy of the parent process. The child process obtains a copy of the data segment and stack segment of the parent process (including the I/O Stream Buffer), but the child process shares the code segment of the parent process.
# Include <stdio. h> # include <stdlib. h> # include <unistd. h> int global = 100; int main (void) {int local = 200; char * heap = (char *) malloc (256 * sizeof (char); sprintf (heap, "ABC"); printf ("parent process: % d % s \ n", global, local, heap); pid_t pid = fork (); if (pid =-1) {perror ("fork"); return-1 ;}if (pid = 0) {global ++; local ++; sprintf (heap, "XYZ"); printf ("sub-process: % d % s \ n", global, local, heap); free (heap ); return 0;} sleep (1); printf ("parent process: % d % s \ n", global, local, heap); free (heap ); return 0 ;}

#include <stdio.h>#include <unistd.h>int main (void) {printf ("ABC");pid_t pid = fork ();if (pid == -1) {perror ("fork");return -1;}if (pid == 0) {printf ("XYZ\n");return 0;}sleep (1);printf ("\n");return 0;}


4. After the function is called, the Parent and Child processes continue to run, and their sequence is uncertain. Some implementations can ensure that sub-processes are scheduled first.
# Include <stdio. h> # include <unistd. h> int main (void) {printf ("parent process:"); int a, B, c; scanf ("% d", &, & B, & c); pid_t pid = fork (); if (pid =-1) {perror ("fork"); return-1 ;} if (pid = 0) {scanf ("% d", & a, & B, & c); printf ("sub-process: % d \ n ", a, B, c); return 0;} sleep (1); printf (" parent process: % d \ n ", a, B, c); return 0 ;}


5. After a function is called, the file descriptor table (process-level) of the parent process is also copied to the child process. The two share the same file table (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. If the total number of processes or the number of processes owned by the actual user ID exceeds the system limit, this function will fail.

7. If a process wants to create its own copy and execute the same code, or want to run concurrently with another program, you can use this function.

Note: the code before fork is executed only by the parent process. The Parent and Child processes after fork have the opportunity to execute the code and enter different branches under the control of the Code logic.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.