Thinking about fork interview questions and fork interview questions

Source: Internet
Author: User

Thinking about fork interview questions and fork interview questions
I. Reasons for the article

It is still a convention to explain why the article came about. Good evening, you asked questions on the Internet and saw an interesting question. So I started my research and got this article.

Ii. Enter the subject

The questions are as follows:

#include <stdio.h>#include <sys/types.h>#include <unistd.h>int main(void){   int i;   for(i=0; i<2; i++){      fork();      printf("-");   }   return 0;}

Drawing comprehension:

If you are familiar with the fork () mechanism, it is not difficult to understand that the number of superhumans should be six.
However, in fact, this program will output 8 "-" very tricky.

Change "-" to "s"

The following is a reference to Daniel's blog
To clarify this question, we must first understand the features of fork () system calls.

Therefore, why does the above program input 8 "-"? This is because the printf ("-"); statement has buffer, so for the above program, printf ("-"); Put "-" into the cache, and there is no real output (see the first question in "questions about C Language"). During fork, the cache is copied to the sub-process space. Therefore, if there are two more, eight instead of six.

In addition, for more information, we know that Unix devices have the concepts of Block devices and character devices. Block devices are devices that access data one by one, A character device is a device that accesses one character at a time. Disks and memory are both Block devices. Character devices such as keyboards and serial ports. Block devices generally have caches, while character devices generally do not.

For the above problem, if we modify the printf statement above:

printf("-\n");

Or

printf("-");fflush(stdout);

There will be no problem (that is, six "-"), because the program Encounters "\ n", or "EOF", or is slow to full, or the file descriptor is closed, or the active flush, or exit the program, and the data is flushed out of the buffer zone. It should be noted that the standard output is a row buffer, so the buffer will be flushed out when "\ n" is encountered, but for the disk block device, "\ n" does not cause the buffer to be flushed out. It is a full buffer. You can use setvbuf to set the buffer size or use fflush to flush the cache.

I guess some friends may not know much about fork (), so let's change the above program to the following:

# Include <stdio. h> # include <sys/types. h> # include <unistd. h> int main (void) {int I; for (I = 0; I <2; I ++) {fork (); // note: the following printf contains "\ n" printf ("ppid = % d, pid = % d, I = % d \ n", getppid (), getpid (), i);} sleep (10); // Let the process stay for 10 seconds, so that we can use pstree to check the process tree return 0 ;}

Therefore, the above program will output the following results. (Note: The name of the compiled executable program is fork)

ppid=8858, pid=8518, i=0ppid=8858, pid=8518, i=1ppid=8518, pid=8519, i=0ppid=8518, pid=8519, i=1ppid=8518, pid=8520, i=1ppid=8519, pid=8521, i=1$ pstree -p | grep fork|-bash(8858)-+-fork(8518)-+-fork(8519)---fork(8521)|            |            `-fork(8520)

In the face of such a picture, you may still not understand it. It's okay. I am doing a good thing to the end and draw a picture for you to see:

Note: I used several colors in the same process. So our pstree diagram can be like the following: (the color and the corresponding)

In this way, for the printf ("-"); statement, we can clearly know which sub-process has copied the content in the standard output of the parent process, this results in multiple outputs. (As shown in, there are two sub-processes that I shadow and have dual borders)

-END-

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.