The first time I came into contact with these two functions, they were actually the ones in the group's mind this year... I always think that I should write something really bad, not this function...
Make up the horizontal line to output welcome xiyoulinuxif (_____) printf ("xiyoulinux"); elseprintf ("welcome ");
Most people, including me, thought of filling them out at first glance.Printf ("welcome ");This is a simple, rough, and almost cool way. However, after reading the answer, I found that there is another more brutal solution --Fork () | wait ().
Actually, when I saw this answer, my real thought was: No (Shuo) Ming (ren) Jue (Hua) Li (BA). Never said that I had come into contact with it before, I have never seen these two functions at all... hello... what are you doing? = ..
So I went to step up to get to know the two functions, and thought -- (Diao )! Niu (BAO )! Force (LE )!
This question is so Tiger. The people who come out of this question are so awesome. The cute girl Shen chaomeng is so bad .......
-------------- Enter the subject segmentation line -----------------
The following describes the usage of the two functions, fork () and wait.
Fork ()
Function prototype: pid_t fork (void );
(Pid_t is a macro definition. Its essence is that int is defined in # include <sys/types. h>)
Fork () is a very powerful function. It can return two values. Generally, a function has only one returned value. Fork () has two values. Why?
This starts with the role of Fork (). What is the role of Fork ()? Prepare is to generate a sub-process. Then let's talk about its return value, which is like this:
If one call is successful, two values are returned. The child process returns 0, and the parent process returns the child process ID. Otherwise, an error is returned.-1 is returned.
For example, I just added fork () to the Code ():
if(fork())printf(“xiyou linux ”);elseprintf(“welcome ”);
What results will this cause? The xiyou Linux welcome is printed, but it seems a little different from our ideal order. How can we
Let welcome print out before xiyou Linux? This leads to the second function-Wait ();
Wait ()
Function prototype: pid_t wait (int * status );
In short: Wait () suspends the current process until the child process ends.
I did not describe very well. I quoted a blog post as follows:
"Once a process calls wait, it immediately blocks itself. Wait automatically analyzes whether a sub-process of the current process has exited. If it finds such a sub-process that has become a zombie,
Wait will collect information about this sub-process and destroy it completely and return it. If such a sub-process is not found, wait will be blocked until one appears."
Therefore, add wait () after fork (). At this time, the main process returns the sub-process ID (not 0) from fork () and is forcibly suspended by wait; the sub-process is created by fork () and returns 0. The print welcome is then destroyed,
Wait () receives a signal, the main process continues to execute, print xiyou Linux.
The diagram is as follows:
References:
Baidu encyclopedia fork (): http://baike.baidu.com/view/1952900.htm
Sina Blog Linux wait () function: http://blog.sina.com.cn/s/blog_759803690101aqeq.html
A preliminary understanding of the process in this article: http://blog.csdn.net/u011067948/article/details/9618119