process control

1.  Learning process creation , waiting, and termination. Use code to implement. 

( 1 ) Process creation

fork: Create a new process in an existing process, the new process is a child process, and the original process is a parent process

--The value returned by the child process is 0, and the value returned by the parent process is the pid of the child process

The result of running is

vfork : is also used to create child processes



The result is the same as above

- Does not copy the memory space, shares the address space with the parent process (after defining the variable, the value is the same), -- and can ensure that the child process runs first , and the parent process starts to run after the child process runs first

-- must call exit or exec series functions

Otherwise, the running result is shown in the following figure:

--Vfrok implemented more or less on any one system , don't use

(2) Process waiting

wait: means that the parent process is blocked until the child process exits, the zombies are recycled, and then wait returns to the parent process. 

 

--As shown in the left figure, it is the parent process. After the child process is created, go to the right figure and run the child process code, while the parent process is in a blocked state. After the child process is executed, it exits. The wait of the parent process recycles the zombie process. Then proceed to the parent process.

2.  Write an autonomous shell. 

The basic steps when writing a shell are:

(1) Get the command line

(2) Parse the command line

(3) Create a child process

( 4) Replace subprocess

( 5 ) The parent process waits for the child process to exit

(1) Caution is required when using system under linux. The standard I/O function library provides the popen function, which starts another process to execute a shell command line.

Here we call the process that calls popen the parent process, and the process started by popen is called the child process .

The popen function also creates a pipe for communication between parent and child processes. The parent process either reads information from the pipe or writes information to the pipe, depending on the parameters passed when the parent process calls popen. The definitions of popen and pclose are given below:

Let's look at the use of popen through an example:

If we want to get the number of files in the current directory, we can use: 


Use popen in your program:

The above popen only captures the standard output of the command. If the command fails to execute, the child process will print the error information to the standard error output, and the parent process cannot get it. For example, the command command is "ls nofile.txt", in fact we don't have the file nofile.txt at all, then the shell will output "ls: nofile.txt: No such file or directory". This output is on standard error. It cannot be obtained through the above procedure.

( 2 ) system function

system() will call fork() to generate a child process , and the child process will call /bin/sh-c string to execute the command represented by the parameter string string. After the command is executed, it will return to the original calling process. The SIGCHLD signal is temporarily put on hold during a call to system(), and the SIGINT and SIGQUIT signals are ignored. Return value =-1 : An error occurred=0: The call succeeded but no child process appeared >0: The id of the child process that exited successfully If system() fails when calling /bin/sh, it returns 127, and other failure reasons return -1 . If the parameter string is a null pointer (NULL), a non-zero value > is returned. If the system() call is successful, the return value after executing the shell command will be returned at the end, but this return value may also be 127 returned by the system() call /bin/sh failure, so it is best to check errno again to confirm the success of the execution .  

( 3 ) So the biggest difference between system and popen: system is to execute the shell command and finally return whether the execution is successful, popen executes the command and communicates with the shell command through the pipe.

 

 

 

 





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326521527&siteId=291194637