Issues encountered by Process Control

Source: Internet
Author: User

This week, we learned about process environment and process operations, a general understanding of what a process is, but not really understanding what a process is. At the beginning of the process control this chapter, when trying to understand what is the process, I understand the process is functional functions, each realization of a function called a process, can be said to be called a process, is a more specific process.

First question:

11th. The first example of process management is to print the process ID of the current process, with the following code:

1#include <stdio.h>2 3#include <unistd.h>4 5 intMain ()6 7 {8 9 pid_t pid, Ppid, uid, Euid, GID, Egid;Ten  OnePID = Getpid ();/*Get Process ID*/ A  -Ppid = Getppid ();/*get parent Process ID*/ -  theUID = Getuid ();/*Get user ID*/ -  -Euid = Geteuid ();/*get a valid user ID*/ -  +GID = Getgid ();/*get the group ID*/ -  +Egid = Getegid ();/*get valid Group ID*/ A  atprintf"ID of current process:%u\n", PID);/*pid_t is actually an unsigned integral type, -  - so use%u instead of%d*/ -  -printf"Parent ID of current process:%u\n", ppid); -  inprintf"User ID of current process:%u\n", UID); -  toprintf"effective User ID of current process:%u\n", euid); +  -printf"group ID of current process:%u\n", GID); the  *printf"effective group ID of current process:%u\n", Egid); $ Panax Notoginseng     return 0; -  the}

Operation Result:

As you can see from the running results, each time you run this program, the ID of the child process is added 1 at a time, and the other process IDs do not change. The ID of the child process can also be found each time plus 1, and then I debug, and then recompile and then run the results such as:

This figure shows that the process ID is more than 1 compared to the previous one, and then the process ID plus 8 may be related to the middle running GCC pid.c–o app. Then there's the test. The result shows that an LS instruction also takes up a process, so it prints the process ID plus 2.

Later, the test, found that after running a program, after a while to run the process ID will also have a great change, I think this ID change is relatively large because of this time interval within the system there are other processes running results.

Looking back at the GCC pid.c–o app this command before running the program, the process ID change is 6, then I have tested several times, found each interval is 6.

The three tests found that every time the program was recompiled, the process ID interval was 6, indicating that the Gcc–o PID.C app used 6 processes. Before we go back to the GCC compilation process, the GCC compilation process is 4 steps, the. c file is preprocessed. i files, the. I file is compiled into assembly language to generate the. s file, and the. s file is compiled to generate the target file. o file, and finally the. o file is linked together to generate the. exe file and finally run. There are 4 processes from a. c file to an. exe file, but this process takes up 6 processes, and is there a process that takes 1 to 2 processes in 4 of these processes?

Second question:

The process ID program for printing the parent and child processes is as follows:

1#include <stdio.h>2 3#include <stdlib.h>4 5#include <unistd.h>6 7 intMainvoid)8 9 {Ten  Onepid_t pid;/*Save Process ID*/ A  -PID = fork ();/*Create a new process*/ -  the     if(PID <0){/*Fork Error*/ -  -printf"fail to fork\n"); -  +Exit1); -  +}Else if(PID = =0){/*Child Process*/ A  atprintf"This is child, PID is:%u\n", Getpid ()); -  -                         /*process ID of the print child process*/ -  -}Else{ -  inprintf"This is parent, PID are:%u, Child-pid is:%u\n", Getpid (), -  toPID);/*prints the process ID of the parent process and its child processes*/ +  -     } the  *     return 0; $ Panax Notoginseng}

The result of this code operation is:

Based on the results found, the process ID of the parent process is printed, and then the process ID of the child process is printed. Why is the PID equal to 0. The book says, "For a subprocess, the fork () function returns 0. Because the system's NO. 0 process is the kernel process, so the process number of the child process can not be 0, thus distinguishing between the parent process and the child process ", this sentence with this program is a bit less understanding, PID equals 0 to execute" printf ("This is the children, PID is:%u\n", Getpid ()); " This sentence, but the program PID execution of this sentence, is not to say that the PID here equals 0, then this PID value why equals 0?

Issues encountered by Process Control

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.