[LINUX] About fork () function and execve () function

--Personal memo --Leave what you learned about kernel process generation --Run in C

About the fork () function

--When you execute the fork () function, it spawns a child process from the parent process.

fork.c


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

static void child()
{
    printf("Child. pid: %d.\n", getpid());
    exit(EXIT_SUCCESS);
}

static void parent(pid_t pid_child)
{
    printf("Parent. parent's pid: %d. child's pid: %d\n", getpid(), pid_child);
    exit(EXIT_SUCCESS);
}

int main()
{
    pid_t pid;
    pid = fork();
    
    if(pid == -1) err(EXIT_FAILURE, "fork() failed");
    
    //Child process returns 0
    if(pid == 0)
        child();
    //The process ID of the child process whose return value is the parent process(> 0)
    else 
        parent(pid);
}
Parent. parent's pid: 27701. child's pid: 27702
Child. pid: 27702.

You can see that the child process is spawned from the parent process and each has a different process ID

About the execv () function

--The kernel executes the process according to the following flow

  1. Read the executable file
  2. Overwrite the memory of the current process with the data of the new process
  3. Run a new process

execve.c


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

static void child()
{
    char *args[] = {"/bin/echo", "Hello", NULL};
    printf("child. child's pid: %d.\n", getpid());
    execve("/bin/echo", args, NULL);
    err(EXIT_FAILURE, "execve() failed");
}

static void parent(pid_t pid_child)
{
    printf("parent. parent's pid: %d. child's pid: %d\n", getpid(), pid_child);
    exit(EXIT_SUCCESS);
}


int main(void)
{
    pid_t pid;
    pid = fork();
    if(pid == -1)
        err(EXIT_FAILURE, "fork() failed");
    
    if(pid == 0)
        child();
    else
        parent(pid);
    
}
parent. parent's pid: 28034. child's pid: 28035
child. child's pid: 28035.
Hello

After the process branches with the fork () function, you can see that / bin / echo is executed by the execve () function in the child process.

Recommended Posts

About fork () function and execve () function
About _ and __
About Class and Instance
About function arguments (python)
About cumprod and cummax
About the Unfold function
Python: About function arguments
About cross-validation and F-number
Learn about python's print function and strings for beginners.
This and that about pd.DataFrame
Code reduction-Pipeline and Function Transformer-
Linux (about files and directories)
About python objects and classes
About Python variables and objects
About LINUX files and processes
About python beginner's memorandum function
About Raid group and LUN
About the enumerate function (python)
About Django's deconstruct and deconstructible
About Python, len () and randint ()
About Python datetime and timezone
About Sharpe Ratio and Sortino Ratio
About Python and regular expressions
About Python and os operations
About http.Handle () and http.NewServeMux (). Handle ()
Python # About reference and copy
About Numpy array and asarray
About Python sort () and reverse ()
Introduction and implementation of activation function
Function pointer and objdump ~ C language ~
Summary and common errors about cron
About python dict and sorted functions
About dtypes in Python and Cython
About MkDocs themes and their customs
About Python pickle (cPickle) and marshal
[Python] About Executor and Future classes
About Python, from and import, as
About time series data and overfitting
Function synthesis and application in Python
[Python] Difference between function and method
Roughly think about the loss function
[Python] Function arguments * (star) and ** (double star)