Preliminary understanding of processes

Source: Internet
Author: User

I have been watching "Process Control" for the past few days. I used to listen to the teacher, Senior xuejie, And said about the magic process and thread. At that time, I was completely blinded by the magic. However, the process and threads are really awesome ~

The following is my preliminary understanding of the process:

As we all know, the core concept of an operating system is process. The biggest difference between it and the program is that the process is dynamic, followed by the Code stored in the memory of the process, and the program is the executable code stored on the hard disk. In Linux, each process is identified by a unique process ID.

<1> obtain various process identifiers

Pid_t getpid (); // obtain the idpid_t getppid () of the process; // obtain the idpid_t getuid () of the parent process; // obtain the actual user idpid_t geteuid () of the process (); // obtain the valid user idpid_t getgid () of the process; // obtain the actual group idpid_t getegid () of the process; // obtain the valid group ID of the process


<2> Linux Process status

Among them, the process has the following states: Running State R (runnnable), can interrupt wait State S (sleeping), can not interrupt State D (uninterruptible sleep), dead State Z (zombile ), stop status T (traced and stopped ).


<3> various processes ~

Commonly used functions in the process are: A, fork (); used to create a new process; B, exit (); used to terminate the process; C, exec (); used to execute an application process; D, wait (); suspends the parent process to wait for the child process; E, getpid (); The process ID of the previous process obtained; f. Nice (); change the priority of the process.

There is also a memory image of the process, where the generation of a program requires four stages: Pre-compilation, compilation, assembly, and link. The image of a process in the memory is actually how the operating system copies executable programs from the hard disk to the memory, and then how the kernel stores executable program files in the memory. For the memory image, I will also .... You 'd better take a look at my master's blog: http://toqianmo.sinaapp.com/2013/05/23/jinchengyanzhongdexianxingdizhikongjian.html

Well, it means that a program is allocated a stack only when it is loaded into the memory, so the executable program does not have a stack.


<4> about creating process functions fork (); and vfork ();

As we all know, when a process transmits its termination information to its parent process, it means the end of the entire lifecycle of the process. In this case, all resources, including IDs, occupied by the process are also released. (From Linux C Programming Practice) where the INIT process is implemented by calling kernel_thread (INT, null, 0); this function is the parent process of all other processes, the orphan process was adopted by it.

A. Fork (); function prototype

#include <sys/types.h>#include <unisted.h>pid_t fork(void);

Generally, a function has only one return value, but fork (); a function has two return values. One is that the return value of a sub-process is 0, the other is the ID of the newly created sub-process returned by the parent process. Fork (); whether the parent process is executed first or the child process is executed first depends on the kernel of your operating system ~

Because the operating system generally gives the process the same execution power, it generally allows your parent and child processes to execute in turn.

B. vfork (); function prototype

#include <sys/types.h>#include <unisted.h>pid_t vfork(void);

The vfork (); function also has two return values, which are the same as the fork (); function, but there is a big difference between the two:

(1) fork (); The created sub-process only completely copies the parent process; and vfork (); The created sub-process shares the address space of the parent process;

(2) fork (); During process creation, it cannot be ensured that the parent process or child process is executed first; while vfork () ensures that the child process runs first, after it calls exec or exit, the parent process can be scheduled.

<5> daemon

When it comes to processes, you may say that the daemon process is difficult. (This is not quite understandable, so I can only copy books> <!!!)

First, let's take a look at its concept. daemon is a process that runs in the background and does not control the connection of the terminal. It is independent of the control terminal and usually periodically executes a certain task. Daemon is a very useful process. Most Linux servers use daemon, such as inted and HTTP.

When writing a daemon, try to avoid unnecessary interactions. Call setsid to create a new conversation ~~

There is still a lot of process-related knowledge. You can get started with "Linux C programming practices! It's really a good book...



The point is, this is the biggest achievement today. It can help you really understand how to create a process function fork (). It's a magic thing ~

Wonderful fork 1:

#include <unistd.h>#include <assert.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){    int i = 0;    for (i = 0; i < 2; i++)    {        fork();        printf("-");    }return 0;}


Well, this program is a typical program for creating processes. In the for loop, there is a fork (); function for creating processes... Sorry, there was an illustration to illustrate this process, but I don't know the illustration, so I can't help it. Use words to describe it.

First, the result of this program is --------, that is, 8-. Why is it 8? After asking for advice, I understood this thing. Assume that the initial parent process is represented by a. When I = 0, a fork is set to B at one time. At this time, both A and B have a "-" in the memory. there is no buffer clearing step, so in I = 1, when a is fork again, It is C. At this time, both A and C have a "-", then execute the statement printf ("-"). In this case, there will be two "-" in the memory units of A and C, and the same is true for B, so it's easy to get the answer, that is, eight ~~


Imagine if a statement printf ("\ n") is added out of the loop, and the output results of the program are different?


Fork's amazing 2:


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


The results of this program may be unexpected. If you don't take a closer look, you may think that this program is the same as the above program. Haha, so you are wrong. These two are qualitative differences. A single "\ n" leads to a little difference in results. first think about the answer as a magic?


First, when I = 0, a will fork a B. Because "\ n" exists, when I = 1, when both A and B fork a C and D, the memory of C and D will not have a "-". Therefore, the result of this operation is two "-" fewer than that of the above program. So, there will be six "-".



This is what I learned recently. Thank you for the detailed explanation provided by Lu wenxue today. Thx ~

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.