Linux multi-process programming __linux

Source: Internet
Author: User

The functions used are primarily to get process IDs, create processes, process exits, process waits, and execute programs.
Get process Id:getpid (), Getppid ()
Create process: fork (), Vfork ()
Process exit: Exit ()
Process waiting: Wait ()
Execute program: EXEC (), System () getpid function

Prototype: pid_t getpid (void)
header file:<unistd.h><sys/types.h>
function: Returns the process ID that called the function, and the function always runs successfully

getppid function
Prototype: pid_t getppid (void)
header file:<unistd.h><sys/types.h>
function: Returns the parent process ID of the process that called the function, and the function always runs successfully

Fork Function
Prototype: pid_t fork (void)
header file:<unistd.h>
function: Create a subprocess. Success returns the child process PID in the parent process, returns 0 in the subprocess, and the failure returns -1.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void Main ()
{
    pid _t pid;
    Pid=fork ();
    if (pid==0)
    {
        printf ("This are child program,the return is%d\n", PID);
    }
    else
    {
        printf ("This are Father Program,the return is%d\n", PID);
    }
    Exit (0);
}


When the fork function is used, the parent process produces a subprocess, and the parent and child processes execute the code after the fork function until the process exits. Because the parent process returns the subprocess PID, the child process returns 0, so the return value can be used to allow the parent process and the child process to perform different functions. The child process prints "This is children program,the return is 0" after the shell prompt to execute the parent process, the operating system ejects the shell prompt after the parent process is executed, and then executes the child process. vfork function

Prototype: pid_t vfork (void)
header file:<unistd.h><sys/types.h>
function: Create a child process and block the parent process. Success returns the child process PID in the parent process, returns 0 in the subprocess, and the failure returns -1.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h >
void Main ()
{
    pid_t pid;
    Pid=vfork ();
    if (pid==0)
    {
        printf ("This are child program,the return is%d\n", PID);
    }
    else
    {
        printf ("This are Father Program,the return is%d\n", PID);
    }
    Exit (0);
}


Executes the child process before executing the parent process.
Both the fork function and the Vfork function are used to create the subprocess, but the difference is two points:
①fork: The child process has a separate data segment, stack.
Vfork: The child process shares the data segment with the parent process, the stack.
②fork: The execution order of the parent and child processes is indeterminate
Vfork: The subprocess runs first, after the parent process runs
The parent process can exit the process using the return or exit () function 
, and the subprocess can only exit the process with the exit () function, where exit (0) indicates a normal exit, and exit (1) indicates an exception exit

Wait function
Prototype: pid_t wait (int *status)
header file:<sys/wait.h><sys/types.h>
feature: Suspends the process that calls it until one of the processes ends. Success returns the ID of the child process that ended, failure returned-1
parameter: status, if not NULL, is used to record the exit status of the child process

The function that executes the process is exec,exec is a function family, there are 6: EXECL,EXECV,EXECLE,EXECVE,EXECLP,EXECVP, take execl as an example to explain execl function

Prototype: int execl (const char* pathname,const char* arg,...)
Header file:<unistd.h>
function: Runs the executable file, and the original process code is no longer executing, only new code is executed. Does not return on success, and returns -1 upon failure.
Parameters: Pathname is the parameter of the executable file for the path arg and subsequent parameters of the executable file to run
     , with a null as the closing flag for the parameter.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ types.h>
#include <sys/wait.h>
void Main ()
{
    pid_t pid;
    Pid=fork ();
    if (pid==0)
    {
        execl ("/bin/ls", "ls", "/home", NULL);
        printf ("This are child program,the return is%d\n", PID);
    }
    else
    {wait
        (NULL);
        printf ("This is Father Program,the return is%d\n", PID);
    }
    Exit (0);
}


As can be seen from the above, using the EXECL function, the original process code is no longer executing, to execute the new code.
Fork creates a new process that produces a new PID.
exec retains the original process and executes the new code. system function

Prototype: int system (CONST char* command)
header file:<stdlib.h>
function: Executes a shell instruction that produces child processes





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.