• Using Fork () In Applications
  • Linux: This Book Includes 4 Manuscripts. The Underground Bible to the unix operating System with Tools On Security and Kali Hacking to Understand Computer Programming, Data Science and Command Line




    Download 6,34 Mb.
    Pdf ko'rish
    bet32/203
    Sana10.01.2024
    Hajmi6,34 Mb.
    #134102
    1   ...   28   29   30   31   32   33   34   35   ...   203
    Bog'liq
    Linux This Book Includes 4 Manuscripts The Underground Bible

    Creation Process
    Implementation Of The Fork () in Linux
    In UNIX- compatible systems, processes create an already known system
    call fork (). Let's consider its implementation in Linux.


    1. 
    Allocate memory for the new process control block ( task _ struct
    ). If memory is not enough, it returns an error
    2. 
    All values from the ancestral data structure are copied to the
    descendant data structure. After that, the fields whose values should
    be different from the original ones will be changed. If the user
    exceeds the specified number of processes for him or if the number
    of processes in the system exceeds the maximum possible value (
    which depends on the amount of available memory), the process
    creation stops and an error returns
    3. 
    An identifier ( pid ) is generated for the process using a special
    algorithm to guarantee uniqueness
    4. 
    For the descendant, the necessary additional ancestral data
    structures (table of file descriptors, information about the current
    directory, table of signal handlers, etc.) are copied
    5. 
    Form the address space of the process
    6. 
    The process data structure is placed in the list and hash table of
    system processes
    7. 
    The process is put into readiness for execution
    Using Fork () In Applications
    Consider an example of using a fork () system call to create a process. The
    description of fork () according to POS IX looks like this:
    # Include < unistd . h >
    pid _ t fork (); // type pid _ t is an integer
    Since executing fork () causes the control of both the ancestor and the
    descendant to switch to an operator, which after calling fork () (both begin


    to execute one instruction), practically the only difference between the
    ancestor and the descendant in the programmer's view is in the code
    return fork () .
    For descendant fork () returns zero and for ancestor the id ( pid ) of the
    created descendant process. When for some reason the descendant was not
    created, fork () will return –1. Therefore, the normal code for and fork ()
    looks like this:
    pid _ t pid ;
    if (( pid = fork ()) == - 1)
    { / * error, crash * /) }
    if (pid == 0)
    {
    // is a descendant
    }
    else
    {
    // this is the ancestor
    printf (" Descendant started with code % d \ n ", pid );
    }
    Once the process is created, it can access the identifying information by a
    system call to getpid () which returns the current process ID, and getppid (),
    which returns the process ID of the ancestor .
    #include
    pid_t mypid, parent_pid;
    mypid = getpid ();
    parent_pid = getppid ();

    Download 6,34 Mb.
    1   ...   28   29   30   31   32   33   34   35   ...   203




    Download 6,34 Mb.
    Pdf ko'rish

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Linux: This Book Includes 4 Manuscripts. The Underground Bible to the unix operating System with Tools On Security and Kali Hacking to Understand Computer Programming, Data Science and Command Line

    Download 6,34 Mb.
    Pdf ko'rish