当前位置:网站首页>Interview 3 (multiple processes call the same dynamic library)
Interview 3 (multiple processes call the same dynamic library)
2022-07-22 13:46:00 【QQ851301776】
Created by QQ:851301776, mailbox :[email protected], Welcome to technical exchange , This blog is mainly my own learning experience , Just to make a little progress every day !
Personal motto :
1. No one was born , As long as it is thick, it will happen .
2. You can have a low degree , You can skip school , But you have to learn
One 、 problem
Suppose there is a global variable in the dynamic library , The calling function provided by the dynamic library , The essence is to operate this global variable . If multiple processes call this dynamic library at the same time , Can you , Is it safe .
My answer is : Sure .
The next question is : Why? ?
Then I was confused .
alas , How many times have you been asked , There has been no actual test summary , Today I want to summarize , I hope I can really understand .
Two 、 actual combat
1. Make a dynamic library (libhello.so)
(1) Content of header file
#ifndef __HELLO_H__
#define __HELLO_H__
int hello_add();
#endif
(2) Contents in the source file
#include <stdio.h>
#include "hello.h"
int num = 0;
int hello_add()
{
printf("num=%d\n", num++);
return 0;
}
2. Write two processes
(1) process 1 Content
#include <stdio.h>
#include "./so/hello.h"
int main(void)
{
int i = 100;
while(i--)
{
sleep(1);
printf("%s %d %s\n", __FILE__, __LINE__, __func__);
hello_add();
}
}
(2) process 2 Content
#include <stdio.h>
#include "../so/hello.h"
int main(void)
{
int i = 100;
while(i--)
{
sleep(1);
printf("%s %d %s\n", __FILE__, __LINE__, __func__);
hello_add();
}
}
3. Print the results
4. Conclusion
Multiple processes access the same dynamic library at the same time , Operate on global variables , Don't affect each other .
3、 ... and 、 reason
explain :dll yes windows Dynamic library in
When the process that accesses the dynamic link library is loaded , The system will allocate 4GB Private address space ( If it is 32 Bit machine ), Then the system will analyze the executable module , Find the executable module that will be called DLL, Then the system is responsible for searching these DLL Find these DLL After that, I put these DLL Load into memory , And allocate virtual memory space for them , The final will be DLL The page of is mapped to the address space summary of the calling process ,DLL Virtual memory has code pages and data pages , They are mapped separately to processes A Code page and data page , If this process B Also launched , And the process B Maybe you want to visit this DLL, At this time , Just put the DLL The code page and data page in the virtual memory can be mapped to the address space of the second process . There is only one copy of the unchanged code segment and data segment , The content that will change is the copy of each process itself .
Four 、 The difference between a dynamic library and a static library
Static library : Executable generated using static library links , It is to copy the modules used in the static library to the executable .
therefore , Such executables no longer depend on static library files
Dynamic library : When the code is loaded into memory for execution , Just happened . Delayed link , Also called on-demand loading , In the process of program execution , When you need to use dynamic libraries , Then apply to the system to load the dynamic library into memory .
5、 ... and 、 Memory management of processes
Each process has the following segments :
(1) Code segment : Read only executable
(2) Data segment : Can read but write , Unenforceable , It mainly stores initialized global variables and static Declared variables
(3) Stack segment : Can read but write , Unenforceable , A stack segment contains multiple stack frames . Each function has its own stack frame . During function execution , Stack frame of function exists , End of function call , The stack frame corresponding to the function disappears . The life cycle of the stack segment is the whole process The life cycle of a process .
(4) Reactor section : Can read but write , Unenforceable
(5)BSS paragraph : Can be read , It mainly stores uninitialized global variables and initializes to 0 Global variable of
边栏推荐
- Capacitive touch chip used in touch panel
- leetcode-zj-future03:快递中转站选址
- leetcode-386:字典序排数
- leetcode-6112:装满杯子需要的最短总时长
- Autojs微信研究:多次测试发现偶尔出现调用了click()返回了true,但实际并未点击成功的情况,例如“通讯录”(已解决)
- ApacheCon Asia 2022 开启报名:Pulsar 技术议题重磅亮相
- MVN multi module selection specified module deploy
- Leetcode-735: planetary collision
- Research on autojs wechat: many tests have found that occasionally click() returned true, but it did not click successfully, such as "address book" (solved)
- Small knowledge points with notes
猜你喜欢
leetcode-zj-future04:門店商品調配
JUC-进程和线程
FAST-LIO2: 快速且直接的激光雷达与惯导里程计
leetcode-2337:移动片段得到字符串
Sentinel vs Hystrix 限流对比,到底怎么选?
【C】 General template of information management system / address book (introduce static, dynamic and file versions)
直播回顾| Apache Pulsar Meetup 精彩回放(含 PPT 下载)
Leetcode-6119: subarray whose element value is greater than the change threshold
Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
Leetcode-6113: the smallest number in an infinite set
随机推荐
How to install MySQL
[PostgreSQL 15] PostgreSQL 15 improves unique and null
Leetcode-735: planetary collision
JUC-并发包
HAProxy 在 TiDB 中的最佳实践
Stale Read 功能的使用场景
Support millions of concurrent server tests
支持百万并发的服务器测试
三节点混合部署的最佳实践
torch. jit. Trace and torch jit. Differences between scripts
Leetcode-6117: the latest time to take the bus
Leetcode-676: implement a magic dictionary
【PostgreSQL 15】PostgreSQL 15对UNIQUE和NULL的改进
JUC-内存
JUC-线程池
leetcode-386:字典序排数
Best practices for monitoring tidb with grafana
Mass region cluster tuning best practices
Talk about the top 10 mistakes often made in implementing data governance
Leetcode-745: prefix and suffix search