The establishment of process under Linux

Source: Internet
Author: User

The establishment of process under Linux

As we all know, a process is a program that is being executed. In Linux, you can use one process to create another process. In this case, the organization of the Linux process is a bit like the Linux directory tree, is a hierarchical structure, you can use the Pstree command to view. At the top is the execution process of the INIT program. It is the ancestor of all processes. Linux provides two functions to create a process.

1.fork ()

Fork () provides the basic operation for creating a process, which can be said to be the basis of a multi-tasking of Linux systems. The function is declared in the Unistd.h library.

#include <stdio.h> #include <unistd.h> #include <stdlib.h>   int main () {     printf ("before creating process \ n"); c3/>pid_t pid = fork ();       if (!pid) {         printf ("I am the child process yo, my pid is:%d\n", Getpid ());     } else if (pid>0) {         printf ("I am the parent process, my PID is:%d, my child process pid is:%d\n", Getpid (), PID);     } else{         printf ("The creation process has failed yo \ n");         Exit (1);     }       return 1; }

  

 

Before calling fork (), there is only one process, but after fork (), a child process of the process is generated, which completely replicates the parent process, at which time the parent-child two processes run concurrently. At fork (), if the return is 0, the process is a child process. If the return is greater than 0, the description is the parent process. If it is less than 0 (actually-1), the creation process fails.

Each process has a unique identifier, PID, which can be obtained using getpid (). The PID returned by the parent process is actually the PID of the child process.

As it seems, fork () has no effect after all. In fact, if the fork () after the use of other Linux functions, or very useful. For example, we can communicate through the communication protocol in the parent-child process, and we can accomplish some tasks together.

2.exec Series functions

If only fork (), it is definitely not perfect, because fork () can only parameter a copy of a parent process. The Exec series functions can help us build a new process.

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[]), the above function is declared in Unistd.h. Let's take the Execl () function as an example: #include <stdio.h> #include <unistd.h>   int main () {     execl ("/bin/ls", "ls", "-L", NULL);       printf ("If execl execution fails, this will print out \ n");     return 1; }

  

When the program runs to Execle (), it loads the LS program and overwrites the current program's space. This parameters a new process, but note that the PID of the new process is the same as the process that loads it.

3.fork () and exec () call together

Fork () can create a child process, but the child process is just a copy of the parent process. We can use the EXEC () function to reload a completely new process in the subprocess. Let's look at a couple of the two functions.

#include <stdio.h> #include <stdlib.h> #include <unistd.h>   int main () {     pid_t pid = fork (); 
   switch (PID)     {case     0:         printf ("sub-process \ n");         Execl ("/bin/ls", "ls", "-l", NULL);     Case-1:         printf ("Fork failed \ n");         Exit (1);     Default:         wait (NULL);         printf ("Finished Yo! \ n ");         Exit (0);     } }

First, fork establishes the subprocess and then uses EXECL () in the subprocess to produce a process for the LS program. The parent process then calls wait () to wait until the child process call ends.

The establishment of process under Linux

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.