System Call 3

Source: Internet
Author: User

1.7 background

In the previous article, we have understood the concepts of parent and child processes, and have mastered the usage of System Call exit, but few people may realize that, after a process calls exit, it does not disappear immediately, but leaves a data structure called Zombie. Among the five States of a Linux Process, a zombie process is a very special one. It has abandoned almost all the memory space, no executable code, and cannot be scheduled, only one location is retained in the process list, and information such as the exit status of the process is recorded for collection by other processes. In addition, zombie processes no longer occupy any memory space. From this point of view, although the zombie process has a cool name, its influence is far from reaching those real botnets. Real botnets can always be scary, however, the zombie process does not have any effect on the system except for the information left by others.

Maybe readers are still curious about this new concept. Let's take a look at what the zombie process looks like in Linux.

When a process has exited, but its parent process has not called the system to call wait (which will be introduced later), it will remain zombie for the time before it is collected, let's write a simple Applet:

/* Zombie. C */# include <sys/types. h> # include <unistd. h> main () {pid_t PID; pid = fork (); If (PID <0)/* if an error occurs */printf ("error occurred! \ N "); else if (pid = 0)/* if it is a sub-process */exit (0); else/* if it is a parent process */sleep (60 ); /* sleep for 60 seconds. During this time, the parent process cannot do anything */Wait (null);/* collect zombie processes */}

Sleep is used to sleep a process for a specified number of seconds. Within these 60 seconds, the child process has exited, and the parent process is busy sleeping and cannot be collected, we can keep the sub-process in zombie status for 60 seconds.

Compile this program:

$ cc zombie.c -o zombie

Run the program in the background so that we can execute the next command

$ ./zombie &[1] 1577

List PROCESSES IN THE SYSTEM

$ ps -ax...  ... 1177 pts/0    S      0:00 -bash 1577 pts/0    S      0:00 ./zombie 1578 pts/0    Z      0:00 [zombie <defunct>] 1579 pts/0    R      0:00 ps -ax

Have you seen the "Z" in the middle? That is the sign of the botnet process. It indicates that process 1578 is now a zombie process.

We have learned that the system calls exit. Its function is to exit a process, but it is only limited to converting a normal process into a zombie process and cannot be completely destroyed. Although the zombie process has almost no impact on other processes, it does not take up CPU time, and the memory consumption is almost negligible. However, it still makes people feel uncomfortable when they stay there. In addition, the number of processes in Linux is limited. In some special cases, too many zombie processes may affect the generation of new processes. So how can we eliminate these zombie processes?

First, let's take a look at the reason for the zombie process. We know that Linux and UNIX are always rooted in different ways. The concept of a zombie process is also inherited from UNIX, the UNIX pioneers did not design this thing because they were bored and bored with other programmers. A zombie process stores a lot of information that is very important to programmers and system administrators. First, how does this process die? Is it normal to exit, is there an error, or is it forced to exit by other processes? Secondly, what is the total system CPU time used by the process and the total user CPU time? Number of page errors and the number of signals received. This information is stored in the zombie process. If there is no zombie process, all the information related to the process will be invisible as soon as the process exits. At this time, programmers or system administrators need to use this information, then I had to be dumpfounded.

So how can we collect this information and end these zombie processes? It depends on the waitpid call and wait call we will talk about below. Both of them are used to collect information left by the zombie process and completely disappear the process. The two calls are described in detail below.

1.8 wait

1.8.1 Overview

The wait function is prototype:

# Include <sys/types. h>/* provides the pid_t type definition */# include <sys/Wait. H> pid_t wait (int * Status)

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.

The status parameter is used to save some statuses when the collection process exits. It is a pointer to the int type. But if we don't care about how this sub-process died, we just want to destroy it (in fact, in most cases, we will think like this ), we can set this parameter to null, as shown below:

pid = wait(NULL);

If the call succeeds, wait returns the ID of the collected sub-process. If the call process does not have a sub-process, the call fails. In this case, wait returns-1 and errno is set to echild.

1.8.2 practice

Next let's use an example to apply wait calls. Fork is called by the system in the program. If you are not familiar with this or have forgotten about it, refer to the previous article process management-related system calls (1).

/* Wait1.c */# include <sys/types. h> # include <sys/Wait. h> # include <unistd. h> # include <stdlib. h> main () {pid_t PC, PR; Pc = fork (); If (Pc <0)/* if an error occurs */printf ("error ocurred! \ N "); else if (Pc = 0) {/* if it is a sub-process */printf (" this is child process with PID of % d \ n ", getpid (); sleep (10);/* sleep for 10 seconds */} else {/* if it is a parent process */PR = wait (null ); /* Wait here */printf ("I catched a child process with PID of % d \ n"), Pr);} exit (0 );}

Compile and run:

$ cc wait1.c -o wait1$ ./wait1This is child process with pid of 1508I catched a child process with pid of 1508

We can obviously note that there is a waiting time of 10 seconds before the results of the first row are printed. This is the time we set for the sub-process to sleep. Only the sub-process wakes up from its sleep, it can exit normally and be captured by the parent process. In fact, no matter how long the sub-process sleeps, the parent process will keep waiting. If you are interested, you can try to modify the value by yourself, see what results will appear.

1.8.3 parameter status

If the value of the status parameter is not null, wait will take out the status of the sub-process and store it in it. This is an integer (INT ), it indicates whether the sub-process Exits normally or is ended abnormally (a process can also be ended by another process with a signal, which will be introduced in future articles ), and the return value at the normal end, or information about which signal is terminated. Because the information is stored in different binary bits of an integer, It is very troublesome to read it using the conventional method. People have designed a special macro (macro) to complete this work, next, let's take a look at two of the most common ones:

1. The wifexited (Status) macro is used to indicate whether the sub-process Exits normally. If yes, it returns a non-zero value.

(Please note that, although the name is the same, the parameter status here is not the only parameter of wait -- the pointer to the integer status, but the integer pointed to by the pointer. Remember not to confuse it .)

2. wexitstatus (Status) When wifexited returns a non-zero value, we can use this macro to extract the return value of the sub-process. If the sub-process calls exit (5) to exit, wexitstatus (Status) 5 is returned. If the sub-process calls exit (7), wexitstatus (Status) returns 7. Note that if the process does not exit normally, that is, if wifexited returns 0, this value is meaningless.

The following is an example of what we just learned:

/* Wait2.c */# include <sys/types. h> # include <sys/Wait. h> # include <unistd. h> main () {int status; pid_t PC, PR; Pc = fork (); If (Pc <0)/* if an error occurs */printf ("error ocurred! \ N "); else if (Pc = 0) {/* sub-process */printf (" this is child process with PID of % d. \ n ", getpid (); exit (3);/* the sub-process returns 3 */} else {/* parent process */PR = wait (& status ); if (wifexited (Status) {/* If wifexited returns a non-zero value */printf ("the child process % d exit normally. \ n ", Pr); printf (" The return code is % d. \ n ", wexitstatus (Status);} else/* If wifexited returns zero */printf (" the child process % d exit abnormally. \ n ", Pr );}}

Compile and run:

$ cc wait2.c -o wait2$ ./wait2This is child process with pid of 1538.the child process 1538 exit normally.the return code is 3.

The parent process accurately captures the returned value 3 of the child process and prints it out.

Of course, there are more than two macros that process exit state, but most of them are rarely used in normal programming, so it is not a waste of space here, interested readers can refer to Linux man pages to learn about their usage.

1.8.4 Process Synchronization

Sometimes, the parent process requires the calculation result of the child process to perform the next operation, or the sub-process function is a prerequisite for the parent process to perform the next step (for example, the sub-process creates a file, the parent process writes data). At this time, the parent process must stop at a certain position and wait until the child process finishes running. If the parent process runs directly without waiting, you can imagine that, there will be great confusion. This situation is called synchronization between processes. More accurately, this is a special case of process synchronization. Process Synchronization is to coordinate more than two processes so that they can be executed in order. There are more common methods to solve the process synchronization problem. We will introduce it later, but we can simply use the wait system to call this situation. Please refer to the following procedure:

# Include <sys/types. h> # include <sys/Wait. h> main () {pid_t PC, PR; int status; Pc = fork (); If (Pc <0) printf ("error occured on forking. \ n "); else if (Pc = 0) {/* sub-process work */exit (0 );} else {/* parent process work */PR = wait (& status);/* result of using sub-process */}}

This program is just an example and cannot be used for execution, but it illustrates some problems. First, when the fork call is successful, the Parent and Child processes perform various tasks separately, however, when the work of the parent process has come to an end and the sub-process results need to be used, it will stop and call wait until the sub-process stops running and then continue to execute with the sub-process results, in this way, we have successfully solved the problem of process synchronization.

1.9 waitpid

1.9.1 Introduction

The prototype of waitpid system calling in the Linux function library is:

# Include <sys/types. h>/* provides the pid_t type definition */# include <sys/Wait. H> pid_t waitpid (pid_t PID, int * status, int options)

In essence, the functions of the system call waitpid and wait are exactly the same, but the waitpid has two more user-controlled parameters PID and options, this provides us with a more flexible way of programming. The following two parameters are described in detail:

PID

From the parameter name PID and type pid_t, we can see that what is needed here is a process ID. But when the PID gets different values, it has different meanings here.

  1. When the PID is greater than 0, only the child process whose process ID is equal to the PID is waiting. No matter how many other child processes have ended and exited, as long as the specified child process has not ended, waitpid will keep waiting.
  2. When pid =-1, wait for any sub-process to exit without any restrictions. At this time, waitpid and wait play the same role.
  3. When pid = 0, wait for any sub-process in the same process group. If the sub-process has been added to another process group, waitpid will not ignore it.
  4. PID <-1, wait for any sub-process in a specified process group, the ID of this process group is equal to the absolute value of PID.

Options

Options provides some additional options to control waitpid. Currently, only the wnohang and wuntraced options are supported in Linux. These two constants can be connected using the "|" operator, for example:

ret=waitpid(-1,NULL,WNOHANG | WUNTRACED);

If you do not want to use them, you can set options to 0, for example:

ret=waitpid(-1,NULL,0);

If the wnohang parameter is used to call waitpid, even if no sub-process exits, it will return immediately and will not wait forever like wait.

However, the wuntraced parameter involves some tracking and debugging knowledge and is rarely used. There is not much to worry about here. Interested readers can check the relevant materials on their own.

As you can see, smart readers may already see the clues-isn't wait a packaged waitpid? Check the <kernel source code directory>/include/unistd. h file 349-352 to find the following program segments:

static inline pid_t wait(int * wait_stat){return waitpid(-1,wait_stat,0);}

1.9.2 returned values and errors

The return value of waitpid is slightly more complex than that of wait. There are three cases:

  1. When the returned result is normal, waitpid returns the ID of the collected sub-process;
  2. If the wnohang option is set, and waitpid in the call finds that no child process has exited to collect data, 0 is returned;
  3. If an error occurs in the call,-1 is returned. errno is set to a value to indicate the error;

When the sub-process indicated by the PID does not exist or the process exists but is not a sub-process that calls the process, waitpid will return an error, and errno is set to echild;

/* Waitpid. C */# include <sys/types. h> # include <sys/Wait. h> # include <unistd. h> main () {pid_t PC, PR; Pc = fork (); If (Pc <0)/* If fork Error */printf ("error occured on forking. \ n "); else if (Pc = 0) {/* if it is a sub-process */sleep (10);/* sleep for 10 seconds */exit (0 );} /* if the process is parent */do {Pr = waitpid (PC, null, wnohang);/* If the wnohang parameter is used, waitpid will not wait here */If (Pr = 0) {/* If the sub-process is not collected */printf ("No child exited \ n "); sleep (1) ;}}while (Pr = 0);/* If the sub-process is not collected, go back and try again */If (Pr = pc) printf ("successfully get child % d \ n", Pr); elseprintf ("some error occured \ n ");}

Compile and run:

$ cc waitpid.c -o waitpid$ ./waitpidNo child exitedNo child exitedNo child exitedNo child exitedNo child exitedNo child exitedNo child exitedNo child exitedNo child exitedNo child exitedsuccessfully get child 1526

After 10 failed attempts, the parent process finally collected the exited child process.

Because this is just an example program, it is inconvenient to write too complicated, so we let the Parent and Child processes sleep for 10 seconds and 1 second, respectively, it means that they work for 10 seconds and 1 second respectively. Both parent and child processes have work to do. The parent process uses the short interval of work to check whether the child process exits. If the child process exits, it is collected.

1.10 Exec

Some readers may have read this series of articles from the very beginning, but there is still a big question: Since all new processes are generated by fork, in addition, the sub-processes generated by fork are almost identical with the parent process. Doesn't that mean that all the processes in the system should be identical? Moreover, in our common sense, when we execute a program, the content of the new process should be the content of the program. Did we get it wrong? Obviously not. To solve these questions, we must mention the exec system call that we will introduce below.

1.10.1 Introduction

It is called by the exec system. In fact, in Linux, there is no exec () function form. Exec refers to a group of functions, with a total of six functions:

#include <unistd.h>int execl(const char *path, const char *arg, ...);int execlp(const char *file, const char *arg, ...);int execle(const char *path, const char *arg, ..., char *const envp[]);int execv(const char *path, char *const argv[]);int execvp(const char *file, char *const argv[]);int execve(const char *path, char *const argv[], char *const envp[]);

Among them, only execve is a real system call, and others are packaged library functions on this basis.

The exec function family finds an executable file based on the specified file name and uses it to replace the content of the calling process. In other words, it executes an executable file within the calling process. The executable file can be either a binary file or any script file that can be executed in Linux.

Unlike in general, functions in the exec function family are not returned after successful execution, because the entity of the calling process, including the code segment, data segment, and stack, has been replaced by new content, only some superficial information such as the process id remains unchanged, which is quite similar to the "golden shell" in the "Plan ". It looks like an old shell, but it has injected a new soul. Only when the call fails will they return a-1, which will be executed from the original program's call point.

Now we should understand how a new program is executed in Linux. Every time a process thinks that it cannot make any contribution to the system and support, it can make full use of the last point, call any exec to regenerate itself with a new look; or, more commonly, if a process wants to execute another program, it can fork a new process, then call any exec, which looks like a new process is generated by executing the application.

In fact, the second scenario is so widely used that Linux has made special optimizations for it. We already know that, fork will copy all the content of the calling process to the newly generated child process, which consumes a lot of time. If fork is finished, we will call exec immediately, these hard-to-copy items will be immediately erased, which seems very uneconomical, so people have designed a technology called "Copy-on-write, so that the fork does not copy the content of the parent process immediately after the end, but is copied only when the fork is actually practical, so that if the next statement is exec, it will not be useless, this improves efficiency.

1.10.2 a little deeper

The above six functions seem very complex, but in fact they are very similar in both functions and usage, with only a small difference. Before learning about them, let's take a look at the common main function.

The form of the main function below may be somewhat unexpected:

int main(int argc, char *argv[], char *envp[])

It may be different from what is described in most textbooks, but in fact, this is the real complete form of the main function.

The argc parameter specifies the number of command line parameters when the program is run. The array argv stores all command line parameters, and the array envp stores all environment variables. Environment Variables refer to a set of values that have existed since user login. Many applications need to rely on them to determine system details. Our most common environment variables are path, it points out where to search for applications, such as/bin; home is also a common environment variable, it points out our personal directory in the system. Environment variables generally exist as strings "xxx = xxx". xxx indicates the variable name, and XXX indicates the variable value.

It is worth mentioning that the argv array and the envp array both store pointer to the string. Both arrays end with a null element.

We can use the following program to view what is uploaded to argc, argv, and envp:

/* main.c */int main(int argc, char *argv[], char *envp[]){printf("\n### ARGC ###\n%d\n", argc);printf("\n### ARGV ###\n");while(*argv)printf("%s\n", *(argv++));printf("\n### ENVP ###\n");while(*envp)printf("%s\n", *(envp++));return 0;}

Compile it:

$ cc main.c -o main

When running, we intentionally add a few command line parameters that do not have any function:

$ ./main -xx 000### ARGC ###3### ARGV ###./main-xx000### ENVP ###PWD=/home/leiREMOTEHOST=dt.laser.comHOSTNAME=localhost.localdomainQTDIR=/usr/lib/qt-2.3.1LESSOPEN=|/usr/bin/lesspipe.sh %sKDEDIR=/usrUSER=leiLS_COLORS=MACHTYPE=i386-redhat-linux-gnuMAIL=/var/spool/mail/leiINPUTRC=/etc/inputrcLANG=en_USLOGNAME=leiSHLVL=1SHELL=/bin/bashHOSTTYPE=i386OSTYPE=linux-gnuHISTSIZE=1000TERM=ansiHOME=/home/leiPATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/lei/bin_=./main

We can see that the program uses "./main" as 1st command line parameters, so we have three command line parameters. This may be a little different from what you usually get used to. Be careful not to make a mistake.

Now let's look at the exec function family and focus on execve:

int execve(const char *path, char *const argv[], char *const envp[]);

Compare the complete form of the main function. Do you see the problem? Yes, the argv and envp in these two functions have a one-to-one relationship. The execve 1st parameter path is the complete path of the application to be executed, and the 2nd parameter argv is the command line parameter passed to the application to be executed, the 3rd envp parameter is the environment variable passed to the application to be executed.

Take a look at these six functions and you can find that the first three functions start with execl, and the last three functions start with execv. The difference between them is that, the functions starting with execv pass the command line parameters in the form of "char * argv []", while the functions starting with execl use a method that we prefer to list parameters one by one, end with a null expression. Here, the role of null is the same as that of null in the argv array.

Of all six functions, only execle and execve use char * envp [] to pass environment variables. None of the other four functions have this parameter, this does not mean that they do not pass environment variables. These four functions will pass the default environment variables to the executed applications without any modification. Execle and execve Replace the default ones with the specified environment variables.

There are two other functions execlp and execvp ending with P. It seems that they have very little difference with execl and execv, and that is true, too, all four functions except execlp and execvp require that their 1st parameter paths must be a complete path, such as "/bin/ls "; the 1st file parameters of execlp and execvp can be simply a file name, such as "ls". These two functions can be automatically searched in the directory specified by the Environment Variable path.

1.10.3 practice

The knowledge is almost introduced. Next let's look at the actual application:

/* exec.c */#include <unistd.h>main(){char *envp[]={"PATH=/tmp","USER=lei","STATUS=testing",NULL};char *argv_execv[]={"echo", "excuted by execv",NULL};char *argv_execvp[]={"echo", "executed by execvp", NULL};char *argv_execve[]={"env", NULL};if(fork()==0)if(execl("/bin/echo", "echo", "executed by execl", NULL)<0)perror("Err on execl");if(fork()==0)if(execlp("echo", "echo", "executed by execlp", NULL)<0)perror("Err on execlp");if(fork()==0)if(execle("/usr/bin/env", "env", NULL, envp)<0)perror("Err on execle");if(fork()==0)if(execv("/bin/echo", argv_execv)<0)perror("Err on execv");if(fork()==0)if(execvp("echo", argv_execvp)<0)perror("Err on execvp");if(fork()==0)if(execve("/usr/bin/env", argv_execve, envp)<0)perror("Err on execve");}

The program calls two common Linux system commands, ECHO and Env. Echo will print out the command line parameters that follow, and env will be used to list all environment variables.

As the execution sequence of each sub-process cannot be controlled, a chaotic output may occur-the printed results of each sub-process are mixed together, rather than strictly following the order listed in the program.

Compile and run:

$ cc exec.c -o exec$ ./execexecuted by execlPATH=/tmpUSER=leiSTATUS=testingexecuted by execlpexcuted by execvexecuted by execvpPATH=/tmpUSER=leiSTATUS=testing

As expected, the result of execle output goes to the front of execlp.

In normal programming, if you use the exec function family, you must add an incorrect judgment statement. Compared with other system calls, exec is prone to injury, and the execution file location, permissions, and many other factors can cause the call to fail. The most common errors are:

  1. The file or path cannot be found, and errno is set to enoent;
  2. Array argv and envp forget to end with null. errno is set to efault;
  3. You are not authorized to run the file to be executed. errno is set to eacces.

1.11 process life

Let me use some image metaphor to make a small summary of the process's short life:

With a fork statement, a new process is born, but it is only a clone of the old process.

Then, with exec, the new process was reborn and independent from home, and started a career serving the people.

A person is dead, and the process is the same. It can be a natural death, that is, the last "}" that runs to the main function. It can also be a suicide, there are two ways to commit suicide. One is to call the exit function, and the other is to use return in the main function. Either way, it can leave a suicide note, stored in the return value; it can even be murdered and ended by other processes in other ways.

After the process dies, a zombie will be left behind. Wait and waitpid act as the zombie and push the zombie to be credened, making it invisible.

This is the complete life of the process.

 

Conclusion 1.12

This article focuses on the wait, waitpid, and exec Function Families for system calls. The introduction to system calls related to process management is coming to an end here. In the next article, this is also the last article about system calls related to process management. We will review our recent knowledge through two cool practical examples.

References

  • Linux man pages

  • Advanced Programming in the Unix environment by W. Richard Steven S, 1993
  • Linux core source code analysis Peng Xiaoming, Wang Qiang, 2000

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.