embedded Linux interprocess communication (i)--process
A process is an instantiation of a program, which is a running program. The program is compiled with a linker, and the runtime uses the loader. The process runs in the virtual address space, and each process in the operating system runs in a separate address space, with each process having a logical address space of 4GB( 0-1g ) bit OS , 1g-4g for the application.
Each process has a process number that is unique within the operating system, and the FETCH function for the process number is:
#include <sys/types.h>
#include <unistd.h>
pid_t getpid (void);// Returns the process ID of the current running process
pid_t getppid (void);// returns the ID of the parent process of the current running process
uid_t getuid (void);// Returns the user ID of the currently running process
uid_t geteuid (void);// Returns a valid user ID for the currently running process
gid_t getgid (void);// Returns the group ID of the currently running process
gid_t getegid (void);// Returns the valid group ID of the current running process
When you set-User-id(SUID) bit settings, the active userIDequals the owner of the file.UID, rather than the actual userID; Similarly, if you set the settings-User Groups-id(SGID) bit, the valid user groupIDequals the file owner'sGIDInstead of the actual user groupID. Linuxthe system passes the active user of the process ID and a valid user group ID To determine the process's access rights to system resources.
I. Creation of processes
#include <unistd.h>
pid_t fork (void);
Creates a new process in the same way that the current running process is replicated, and the new process acts as a child process.
Succeeds, returns the PIDof the child process in the parent process,returns 0in the child process, and thechild process creation failure returns -1in the parent process, set errno .
#include <sys/types.h>
#include <unistd.h>
pid_t vfork (void);
Vfork , like fork, is used to create a new process, but does not copy the page table of the parent process.
Second,
Parent-Child process action on files
The child process inherits the open file from the parent process, and the parent-child process writes to the file consecutively. The parent and child processes open the file individually, and the parent and child processes write to the file separately, that is, the parent-child process overwrites the file's read and write to each other. If the parent process runs first, the child process overrides the action of the parent process on the file, ando_append can associate the file pointer of the file opened separately by the parent-child process to implement the continuation write.
Iii. the end of the process
The process runs with system resources, and system resources are freed at the end of the process. When the process exits, the operating system automatically reclaims the system resources that the process consumes. However, the 8KB of memory (task_struct and Stacks) that the parent process allocates to the process itself cannot be recycled by the operating system.
#include <stdlib.h>
int atexit (void (*function) (void));
Registers a function that is called when a normal abort of the process, returns 0successfully, fails to return non 0
When registering multiple functions, the function that is registered first executes, and the function that is registered is executed first.
Normal abort of process:return,exit,_exit(function not performing atexit registration)
#include <sys/types.h>
#include <sys/wait.h>
pid_t Wait (int *status);
pid_t waitpid (pid_t pid, int *status, int options);
wifexited (status) child process returns 1 if normal terminates
wifsignaled (status) child process is returned by signaled 1
Wtermsig (status) returns the number of the signal that triggered the termination of the child process
How wait Works:
A, the system sends a sigchild signal to the parent process at the end of the child process
B, the parent process blocks after calling the wait function
C. The parent process is awakened by the sigchild Signal and then recycles the zombie subprocess.
D, if the parent process does not have any child processes , wait returns an error
The wait function waits for the state of the child process of the current process to change, including the child process termination, the child process being signaled, the child process being signaled, and, in the case of a child process termination, the resources associated with the child process, but the child process is not released in zombie state.
Waitpid can reclaim sub-processes with specified PID , with blocking and non-blocking modes.
This article from "Endless life, Struggle not only" blog, reprint please contact the author!
Embedded Linux interprocess communication (i)--process