System (), exec (), fork () Three comparison of process-related functions

Source: Internet
Author: User

start a new process ( system function)

The system () function can start a new process.

int system (const char *string)

The effect of this function is equivalent to executing the SH–C string.

In general, using the system function is far from the ideal way to start other processes, because it must use a shell to start the required program. This is very much dependent on the shell installation and the shell version.

system features of the function:

Establish independent processes with separate code space, memory space

The system does not return until the new process has finished executing. Blocking

Replace the process image ( exec function)

The EXEC function can be used to replace a process image. After executing the EXEC series function, the original process will no longer execute, and the new process's PID, ppid, and nice values are exactly the same as they were originally. What happens when you execute the EXEC series function is that the running program starts executing the code in the new executable file specified in the EXEC call.

exec features of the function:

When a process invokes an EXEC function, the source process is completely substituted by the new program, and the new program executes from its main function. Because calling exec does not create a new process, the process ID before and after does not change. exec just replace the body, data, heap, and stack segments of the current process with another new program . In particular, file descriptors that have been opened in the original process will remain open in the new process unless their "Close flag on Execution" (Close on Exec flag) is set. Any directory streams that have been opened in the original process will be closed in the new process.

copy process image ( Fork function)

Fork function

Header file

    1. #include <unistd.h>
    2. #include <sys/types.h>

Function prototypes

    1. pid_t fork ( void);

return value:

If successful invocation returns two values, the child process returns 0, the parent process returns the child process ID; otherwise, an error returns-1

about the Fork function, the role of the Linux This is explained in the program design :

We can create a new process by calling fork. This system invokes the replication of the current process, creating a new table entry in the process table, and many of the properties in the new table key are the same as the current process. The new process is almost identical to the meta-process, and the code executes exactly the same, but the new process has its own data space, environment, and file descriptors.

This explanation is in fact too general, and many of the details have not been said. The following is a brief look at some of the details that occur when you call fork. Or the characteristic of the fork function:

First of all, both UNIX and Linux systems now use the write-time replication technology (Cow:copy on write). With this technique, when the fork function is called, the new process simply has its own virtual memory space and no physical memory space. The new process shares the physical memory space of the source process. and the virtual memory space of the new memory is almost a copy of the source process virtual memory space.

We know that process space can be easily divided into program segments (body segments), data segments, heaps, and stacks four parts (simple to understand). Using the fork function of copy-on-write, the process space relationship between the new process (child process) and the source process, when the fork is executed, is as follows:

For example, when Fork executes, the Linux kernel creates a virtual memory space for the new process P2, and the content in the new virtual space is a copy of the content in the P1 virtual memory space. P2 and P1 share the original P1 physical memory space.

Of course, to understand that "copy-on-write", the state shown in the changes will occur. When does change happen? is, father and son two processes in any one process to the data segment, stack area, heap area write operation, the state will be broken, this time will be the physical memory replication, which is called "Copy on Write" reason. The state transitions that occur are as follows:

We found that P2 has its own physical memory space. It is important to note that the changes that occur between segments should be independent, that is, if only the data segment has been written, then only the data segment will be copied at write time. The heap and stack areas are still shared by parent-child processes. It is also important to note that the body segment (program segment) does not occur when a write-time copy occurs because the program segment is normally read-only. The child process and the parent process are basically running independently from the fork and are not affected.

In addition, it is important to note that the file descriptor of a parent-child process also occurs when a write is replicated.

There is also a function called Vfork, which is more popular, the virtual address space structure of the kernel even child process is not created, directly share the virtual space of the parent process, of course, this practice yielded shared the physical space of the parent process

System () , exec () , fork () function comparison

First, compare the Exec () function and fork (). One of the two functions is to change the soup (execl function), the other is the new (fork function). So what is soup and what is medicine? We know that the process is a very complicated thing. From the TASK_STRUCT structure of the code can be seen (task_struct is used in the Linux kernel to describe the process of a structure, the structure of the light code seems to have several screens). We can think of the PID, ppid and nice values of the process as soup, and the process space (simple understanding is the body, data, heap, stack, etc.) as a medicine.

The exec () function is not to change the soup, that is, after executing the EXEC function, there is no new process, namely soup or soup, the PID, ppid and nice values of the process have not changed. But the EXEC () function replaces the drug, which is the process space, and the new process space is prepared to execute the new program, so the new process space has nothing to do with the original process space.

The fork () function is a new bottle, meaning that after the EXEC () function is executed, the new process is generated, the PID of the process, the Ppid and the original process are different, the parent-child process is two different processes, but exec did not replace the medicine, but the medicine copied a copy to the child process. The parent-child process is in the same state for a period of time after the fork is executed (the same thing in the process space, because Fork uses "copy on Write" and the parent-child process shares the physical memory space at the beginning). But once the parent-child process has a process that attempts to modify the process space, then the parent-child process has their own process space, a simple understanding, from this moment, the parent-child process is two independent processes, who will not affect who (in fact, there is a certain impact, can be ignored here), The association between parent and child processes leaves only the code snippets they share.

For the system function, we can look at its source code first:

intSystemConst Char*cmdstring)  {pid_t pid; intstatus; if(Cmdstring = =NULL) {            return(1); }    if(PID = fork ()) <0) {Status= -1; }  Else if(PID = =0) {execl ("/bin/sh","SH","- C", Cmdstring, (Char*)0); -exit (127);//This statement is not executed if the child process is performing normally    }  Else{         while(Waitpid (PID, &status,0) <0){          if(errno! =einter) {Status= -1;  Break; }        }    }    returnstatus;}

We see that the system () function actually executes the fork function first, and then the newly generated sub-process executes the EXEC function immediately, we say a fork function in the same way, the EXEC function does not change the soup, then the system function is changed both soup and medicine , That is, the system function generates a new process, which means that the PID, PPID, etc. of the new process are different from the original process. The system also generates new process space, and the new process space is prepared for the new program, so it has nothing to do with the process space of the original process (unlike the fork new process space is a copy of the original process space). Also note that the else part of the system function code executes the wait function, which means that the original process waits for the child process to complete (block)

The last thing to note is about the file descriptor.

After the EXEC function executes, the original open file descriptor still exists.

After the fork function executes, the original open file description Inode copied to the new process, and the file descriptor between the two processes is relatively independent.

The system function executes the fork function first, after which the file descriptors of the two processes are relatively independent. The EXEC function then does not affect the file descriptor.

System (), exec (), fork () Three comparison of process-related functions

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.