APUE learning process control - fork and vfork

Last edited: 2019-11-6

Version: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1 ~ 16.04.11)

First, the process identity

Each process has a unique non-negative integer ID, the type pid_tWhen the process exits or is killed, process ID system will be reused. The difference is that with file handle most of the operating system is delayed complex with algorithms, in order to avoid this new process to be mistaken for a process that has been suspended previously. (used sequentially down).

There are some specific processes in the system, for example ID = 0, the scheduling process (exchange process swapper), ID = init process 1. The init process is the ancestor of all processes.

1.1 Related Functions

pid_t getpid(void); // 获取当前进程的 ID 

pid_t getppid(void); // 获取父进程的 ID

Two, fork function

fork function can be used to create a new process, a new process called the child of the current process, relative, the current process is the parent of the new process.

pid_t fork(void);
  1. fork called once returned twice . twice to return the difference is that the child returns 0, the new parent returns the child process ID

    • New parent returns the child process ID Cause: A process can have multiple sub-process, but the system is not a function can get all the child process ID;
    • The reason the child returns a value of 0: child process can getppidobtain the parent process ID, (ID 0 on the exchange process will forever when using the kernel swap, the parent process child process never is 0);
    • The return value is less than 0 indicates abnormal
  2. Instruction after the fork a child process with parent process continues. Sub-process is the parent of a copy . But the replication technology (Copy-On-Write) write using many realize, after the fork, the parent and child zones share, but the area change read-only state, when the parent or the child you want to make changes to the region, the kernel will be the parent or child process, a copy of the region as a copy. (who has made changes and who copy).

  3. Scheduling parent-child process after fork order is uncertain, depending on what kernel scheduling algorithm;

2.1 The difference between father and son process:

As mentioned earlier, the child process is a copy of the parent process but still has the following differences:

  1. fork different return values;
  2. Different ID process;
  3. ID of the parent process is different;
  4. tms_utime / tms_stime / tms_cutime child process value / tms_ustime is set to 0;
  5. The child does not inherit file locks set by the parent;
  6. Untreated alarm clock child process is cleared; (pending signal is cleared)
  7. Unprocessed signal sets the child process is set to the empty set;

2.2 fork usage scenarios

  1. Parent want to replicate themselves, such as web services, the parent process waits for a request, after receiving a request fork yourself, let the child process to handle the request, under the parent process continues to wait for a request;
  2. A process to execute different programs such as shell terminal.;

Code Example 2.3

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int globvar = 6;
char buf[] = "a write to stdout \n";

int main(int argc, char **argv)
{
    int var;
    pid_t pid;

    var = 88;

    if (write(STDOUT_FILENO, buf, sizeof(buf) - 1) != sizeof(buf) - 1)
    {
        perror("write error");
    }

    printf("before fork \n"); // not flush stdout

    if ((pid = fork()) < 0)
    {   
        perror("fork error ");
    } 
    else if (pid == 0) // child
    {
        globvar++;
        var++;
    }
    else // parent
    {
        sleep(2); // 等待子进程先执行
    }

    printf("pid = %d, glob = %d, var = %d \n", getpid(), globvar, var);
    exit(0);
}

Results of the:

a write to stdout 
before fork 
pid = 6053, glob = 7, var = 89 
pid = 6052, glob = 6, var = 88 

fork print results redirected to an internal document to print multiple file contents, call fflush before fork () Flush the stream

  1. The terminal is a standard output device, the line buffer output device is a standard mode, when there \ n, the buffer is flushed
  2. File is a fully buffered mode, full mode buffer \ n newline is just a role

Three, vfork function

This function is defective, only for understanding

Sequences and returns the same value vfork the fork call, but both semantically different.

Vfork function creates a new process, the purpose of the process is a new exec function. Vfork the child process the parent process address space will not be fully replicated in the past, because the purpose of the process is to call the exec. However, the child process exec or exit before, child process is running in the space of the parent process, which improve efficiency to some extent.

But if the child process modifies the data of the parent process, or call other functions, or no exec / exit to return, will have an unknown error. Vfork priority to ensure that the child is returned, but if the child is dependent on the parent of an action It would have resulted in a deadlock.

Having said that, that one, do not use vfork

Guess you like

Origin www.cnblogs.com/gaox97329498/p/11900683.html