当前位置:网站首页>Study notes of Tutu - process
Study notes of Tutu - process
2022-07-06 16:16:00 【Larry_ Yanan】
process
Create a process
fork And vfork explain :
fork and vfork Are used to create sub processes
vfork— The parent process should wait for the child process to exit before running , ( When writing copy )
fork Create child process , Who executes the parent-child process first is decided by the system scheduling
Process resource recycling — Recycling resources by the parent process
Zombie process : Subprocess exit , The parent process did not recycle resources for it , At this time, the child process is a zombie process ( We must avoid )
Orphan process : The child process is still running , The parent process exits , Then the child process at this time is the orphan process
Process exits (exit(1))
Process exit mode :
1. The process exits after running
2. adopt exit() sign out process – Close the file descriptor , Empty buffer
3._exit() sign out Just shut down the process , Does not process file descriptors , buffer exec Function family running process
Use... In the parent process fork Create child process , Use... In subprocesses exec Function family to execute a new program ,
exec The function will use the new program data segment , The code segment covers the data and code of the process , Start execution from the entry of the new program ( Heap and stack are in prototype subprocesses , stay exec Try not to use heap space and stack space in front of functions )
Example is ls And so on. …
Interprocess communication IPC
The Conduit
map Map space sharing
The signal
Message queue
Shared memory
Semaphore
Socket
————————————————————————
Pipeline communication
Pipeline files -----FIFO ,
1. Nameless pipe : pipe — There are no actual documents , Just allocate a memory space in the kernel area to simulate the pipeline
pipe Creating an anonymous pipeline will get two file descriptors ( One for reading , One is for writing )
int pipe(int pipefd[2]);
// Create pipeline file
int fds[2];
int ret = pipe(fds);//fds[0]– Read file descriptor , fds[1]- Write file descriptors
When reading the pipeline file, if there is no data in the pipeline ,read It will block
Anonymous pipeline communication is only applicable to parent-child processes
- Famous pipeline — Realize non kinship interprocess communication
1). There must be a pipeline file
mkfifo Function to create a pipeline file fio
2). stay A Open pipeline file in process fio
stay B Open pipeline file in process fio
3). stay A Write data to the pipeline in the process
stay B Read data from the pipeline in the process
//A
//1 Open the pipeline file
int fd = open("/home/gec/fio", O_RDWR);
//2 Write data into the pipeline
write(fd, "hello world linux", 18);
//3 Close the pipe file
close(fd);
//B
//1 Open the pipeline file
int fd = open("/home/gec/fio", O_RDWR);
//2 Read data from the pipeline
char buffer[32];
read(fd, buffer, 32);
printf("%s\n", buffer);
//3 Close the pipe file
close(fd);
mmap Map memory to realize interprocess communication
The parent and child processes share a piece of memory , It can be operated at the same time
#include <sys/mman.h>
void *mmap(void addr, size_t length, int prot, int flags, int fd, off_t offset); // mapping
Parameters :void addr Specified user space ( Set to NULL, Automatic system assignment )
size_t length Map space size
int prot jurisdiction ---- If it is a file, it must be related to the file permission object
int flags— Set space private MAP_PRIVATE, share MAP_SHARED Such as permissions
MAP_SHARED|MAP_ANONYMOUS– Anonymous mapping (fd To set up for -1, offset It's also 0)
int fd -- File to map
off_t offset -- Map the offset of the corresponding device
Return value :void Map to the first address in user space ( If you fail (void)-1)
int munmap(void *addr, size_t length);// Release mapping
use mmap Anonymous mapping realizes the communication between parent and child processes
- linux The next signal occurs
command :kill The signal process id Number
function :
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig); // Parameters : process id Number , The signal - Capture signal ( Intercept the signal )
#include <signal.h>
typedef void (*sighandler_t)(int); // Define a function pointer type sighandler_t
sighandler_t signal(int signum, sighandler_t handler);
handler Point to another function
kill Sending signal — For any process
------》
Send a signal to this process
raise abort alarm setitimer
1)raise
int raise(int sig); ===》kill(getpid(), sig);
raise(SIGUSR1);
Send a signal directly to this process
SIGUSE1: User defined The signal of . That is, the programmer can define and use the signal in the program . The default action is to terminate the process .
2)abort
abort() — Send... To the current process SIGABRT The signal , And terminate the process , Close the file and empty the buffer , Store the process information in the core file core in
3)alarm
alarm — Alarm Send to this process regularly SIGALRM The signal
unsigned int alarm(unsigned int seconds);
Parameters :seconds Set timing time
If the current process is sleep in , Receive alarm Sent SIGALRM The signal will wake up
4)setitimer
setitimer— Send to this process SIGALRM The signal -- timing send out
// Intercept the signal
signal(SIGALRM, handle);
// Sending signal
struct itimerval newit, oldit;
// initialization newit
newit.it_interval.tv_sec = 1;
newit.it_interval.tv_usec = 0;
newit.it_value.tv_sec = 1;
newit.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &newit, &oldit);
summary : The signal can be intercepted , The program can send signals in many ways , It can also be timed
Repeat :
Interprocess communication IPC
The Conduit
map Map space sharing
The signal
Message queue
Shared memory
Semaphore
Socket
Message queue
1. Get key value
2. Get the message queue according to the key value int msgget(key_t key, int msgflg);
3. Send to the queue / Receive data
msgsnd(msgid, &msgbuffer, sizeof(msgbuffer), 0);
This buffer Need another peripheral , Contains the size and an array
Superficial thinking is to create a “ Message queue ” Well , Then transmit data like a pipe
Shared memory
1. Get key value
2. Get the shared memory according to the key value int shmget(key_t key, size_t size, int shmflg);
3. Mapping space ( Kernel shared memory maps user space )
4. Release mapping shmdt(mp);
5. Free up shared memory shmctl(shmid, IPC_RMID, NULL);
Create a piece in the kernel “ Shared memory ”, After mapping to user space, you can operate and use ; Through the key value to multi process communication
Semaphore
Realize the synchronization and mutual exclusion of two processes with semaphores
No communication has been found for the time being …
Daemon ( Elvish process ), Background services , The member name is in d ending
The daemon is separated from the console , Running in the background ( Generally used for background service programs )— Orphan process
system log
边栏推荐
- QWidget代码设置样式表探讨
- QT模拟鼠标事件,实现点击双击移动拖拽等
- [exercise-8] (UVA 246) 10-20-30== simulation
- Browser print margin, default / borderless, full 1 page A4
- 1689. Ten - the minimum number of binary numbers
- socket通讯
- 力扣:第81场双周赛
- Suffix expression (greed + thinking)
- 2027. Minimum number of operations to convert strings
- [exercise-7] crossover answers
猜你喜欢
1529. Minimum number of suffix flips
Data storage in memory & loading into memory to make the program run
第 300 场周赛 - 力扣(LeetCode)
1689. Ten - the minimum number of binary numbers
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
[exercise-5] (UVA 839) not so mobile (balance)
1005. Maximized array sum after K negations
QT按钮点击切换QLineEdit焦点(含代码)
去掉input聚焦时的边框
树莓派4B64位系统安装miniconda(折腾了几天终于解决)
随机推荐
628. Maximum product of three numbers
Basic Q & A of introductory C language
AcWing——第55场周赛
socket通讯
useEffect,函數組件掛載和卸載時觸發
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
Acwing - game 55 of the week
PySide6 信号、槽
双向链表—全部操作
(POJ - 3258) River hopper (two points)
HDU - 6024 building shops (girls' competition)
[exercise-7] crossover answers
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
C language is the watershed between low-level and high-level
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Specify the format time, and fill in zero before the month and days
Pyside6 signal, slot
Is the sanic asynchronous framework really so strong? Find truth in practice
antd upload beforeUpload中禁止触发onchange