Process "post-order"

daemon process

Features: Execute in the background, irrelevant to the terminal; run when the system starts, stop running when the system shuts down

The points that need to be satisfied to realize the daemon process: the daemon process cannot have a "blood relationship" with the terminal; the daemon process does not have a control terminal

The effect achieved: the daemon process forms a group by itself, and the leader of the session group and process group is the daemon process itself.

                        The parent process of the daemon can only be systemd.

                        There is no controlling terminal (that is, a TTY is?)

Daemon process writing steps

Steps: Create a child process, and the parent process exits (function: to achieve formal separation from the terminal)

           Create a new session in the child process (call the setsid function)

           Change the current directory to the root directory (call the chdir function)

           Reset the file permission mask (call the umask function)

           Close the file descriptor (after creating a new session, the daemon has left any controlling terminal and should close unused files)

Note: The operating system provides the function daemon to create a daemon.

            Therefore, you can write it yourself, or you can call the function of the system

interprocess communication

Basic concept:

Simplex, half-duplex, full-duplex; synchronous, asynchronous; communication protocol

nameless pipe

        Features: can only be used for communication between kinship processes; half-duplex

        Create: pipe(), close: close(fd[0]), close(fd[1])

        Note: When there is no data in the pipeline, the read operation will block.

                   When writing data to the pipeline, Linux does not guarantee complete input. The writing process writes data in the pipeline. If the data in the pipeline is full, the writing process will always be blocked.

                    Writing data to a pipe only makes sense if a read end of the pipe exists

famous pipeline

        Basic: Two unrelated processes can communicate with each other;

                    Named pipes can be referred to by pathnames and are visible in the file system;

                     Processes operate well-known pipes through file IO (following the first-in-first-out rule);

        create: mkfifo(), open: open() (open pipe file), close: close()

Signal

        Concept: The signal is a simulation of the interrupt mechanism at the software level, and it is an asynchronous communication method.

        The response method of the user process to the signal: ignore the signal (SIGKILL, SIGSTOP cannot be ignored)

                                                       Capture the signal (execute the corresponding processing function)

                                                        perform default action

        signal function

                Send: kill () (can send a signal to a process or process group), raise () (send a signal to an executing program)

                Processing: signal () (also known as installation signal function or registration signal function, you can specify how the calling process receives the signal of signum)

                Alarm clock function: alarm() (when the specified time is up, the kernel sends a SIGALARM signal to the process)

                Suspend function: pause() (used to suspend the calling process until a signal is received)

Shared memory

        Concept: It is the most efficient inter-process communication method. Processes can directly read and write memory without copying any data.

        Comparison of pipes and shared memory:

                Pipeline: The process of writing the pipeline copies the content of buf in its own user space to the kernel space through the write function

                            The process of reading the pipeline copies the content of the kernel space to the buf of the user space through the read function

                Shared memory:

Steps: create/open shared memory

                        shmget() (get a shared memory identifier if present or create a shared memory object and return the shared memory identifier if not)

                        ftok() (the system must specify an ID value to establish IPC communication, and the ID value is obtained through the ftok function)

         map shared memory

                        shmat() (map the specified shared memory to the address space of the process for access)

        access shared memory

        undo shared memory

                        shmdt() (contrary to the shmat function, it is used to disconnect the address of the attachment point of the shared memory, and the process is prohibited from accessing the shared memory of this piece)

        delete shared memory object

                        shmctl() (shared memory management, complete the control of shared memory)

                        Who last operated the shared memory and who deleted it

message queue

Concept: is a list of messages. Users can add messages, read messages, etc. in the message queue

Features: Send/receive messages by type

Steps: Sender: Create and open a message queue, add a message.

           Receiver: create and open message queue, read message by type, delete message queue

Function: msgsnd()

Guess you like

Origin blog.csdn.net/qq_54704926/article/details/126694842