mlockall function

本文详细介绍了mlockall函数的功能及使用方法。该函数能够锁定进程地址空间中的所有页面,防止这些页面被交换到磁盘上。通过MCL_CURRENT和MCL_FUTURE两个标志位的不同组合,可以实现对当前已分配的内存页面以及未来将要分配的内存页面的锁定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From:http://alpha-supernova.dev.filibeto.org/lib/rel/4.0D/APS33DTE/DOCU_005.HTM


Themlockallfunction locks all of the pages mapped by aprocess's address space.On a successful call tomlockall, the specified process becomes locked andmemory-resident.Themlockallfunction takes two flags,MCL_CURRENT and MCL_FUTURE, which determine whether the pages to be lockedare those currently mapped, or if pages mapped in the future are to belocked.You must specify at least one flag for themlockallfunction to lock pages.If you specify bothflags, the address space to be locked is constructed from the logical OR ofthe two flags.

If you specify MCL_CURRENT only, all currently mapped pages of the process'saddress space are memory-resident and locked.Subsequent growth in any areaof the specified region is not locked into memory.If you specify theMCL_FUTURE flag only, all future pages are locked in memory.If you specifyboth MCL_CURRENT and MCL_FUTURE, then the current pages are locked andsubsequent growth is automatically locked into memory. 

简言之,MCL_CURRENT 就是会Lock住当前驻留内存的页,MCL_FUTURE会Lock住将来会驻留内存的页,比如后面有malloc的处理时,malloc的空间也不会交换到swap分区。


示例:

Example 4-2:  Using the mlockall Function

#include <unistd.h>     /* Support all standards    */
#include <stdlib.h>     /* malloc support           */
#include <sys/mman.h>   /* Memory locking functions */

#define BUFFER 2048

main()
{
  void *p[3];  /* Array of 3 pointers to void */

  p[0] = malloc(BUFFER);

       /* Currently no memory is locked */

  if ( mlockall(MCL_CURRENT) == -1 )
    perror("mlockall:1");

       /* All currently allocated memory is locked */

  p[1] = malloc(BUFFER);

       /* All memory but data pointed to by p[1] is locked */

  if ( munlockall() == -1 )
    perror("munlockall:1");

       /* No memory is now locked */

  if ( mlockall(MCL_FUTURE) == -1 )
    perror("mlockall:2");

       /* Only memory allocated in the future */
       /*   will be locked */

  p[2] = malloc(BUFFER);

       /* Only data pointed to by data[2] is locked */

  if ( mlockall(MCL_CURRENT|MCL_FUTURE) == -1 )
    perror("mlockall:3");

       /* All memory currently allocated and all memory that  */
       /* gets allocated in the future will be locked         */
}


[Contents]

main.c: In function ‘cyclic_task’: main.c:66:3: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration] 66 | clock_gettime(CLOCK_TO_USE, &wakeupTime); | ^~~~~~~~~~~~~ main.c:66:17: error: ‘CLOCK_TO_USE’ undeclared (first use in this function) 66 | clock_gettime(CLOCK_TO_USE, &wakeupTime); | ^~~~~~~~~~~~ main.c:66:17: note: each undeclared identifier is reported only once for each function it appears in main.c:69:18: warning: implicit declaration of function ‘timespec_add’ [-Wimplicit-function-declaration] 69 | wakeupTime = timespec_add(wakeupTime, cycletime); | ^~~~~~~~~~~~ main.c:69:43: error: ‘cycletime’ undeclared (first use in this function) 69 | wakeupTime = timespec_add(wakeupTime, cycletime); | ^~~~~~~~~ main.c:70:5: warning: implicit declaration of function ‘clock_nanosleep’ [-Wimplicit-function-declaration] 70 | clock_nanosleep(CLOCK_TO_USE, TIMER_ABSTIME, &wakeupTime, NULL); | ^~~~~~~~~~~~~~~ main.c:70:35: error: ‘TIMER_ABSTIME’ undeclared (first use in this function) 70 | clock_nanosleep(CLOCK_TO_USE, TIMER_ABSTIME, &wakeupTime, NULL); | ^~~~~~~~~~~~~ main.c:77:42: warning: implicit declaration of function ‘TIMESPEC2NS’ [-Wimplicit-function-declaration] 77 | ecrt_master_application_time(master, TIMESPEC2NS(wakeupTime)); | ^~~~~~~~~~~ main.c:108:25: error: ‘domain1’ undeclared (first use in this function) 108 | ecrt_domain_process(domain1); | ^~~~~~~ main.c:111:5: warning: implicit declaration of function ‘check_master_state’; did you mean ‘ecrt_master_state’? [-Wimplicit-function-declaration] 111 | check_master_state(); | ^~~~~~~~~~~~~~~~~~ | ecrt_master_state main.c:112:5: warning: implicit declaration of function ‘check_domain1_state’; did you mean ‘ecrt_domain_state’? [-Wimplicit-function-declaration] 112 | check_domain1_state(); | ^~~~~~~~~~~~~~~~~~~ | ecrt_domain_state main.c:113:5: warning: implicit declaration of function ‘check_slave_config_states’; did you mean ‘ecrt_slave_config_state’? [-Wimplicit-function-declaration] 113 | check_slave_config_states(); | ^~~~~~~~~~~~~~~~~~~~~~~~~ | ecrt_slave_config_state In file included from main.c:12: main.c:115:48: error: ‘domain1_pd’ undeclared (first use in this function) 115 | uint16_t current_status_word = EC_READ_U16(domain1_pd + status_word); | ^~~~~~~~~~ ../../include/ecrt.h:2877:24: note: in definition of macro ‘le16_to_cpu’ 2877 | #define le16_to_cpu(x) x | ^ ../../include/ecrt.h:2949:18: note: in expansion of macro ‘le16_to_cpup’ 2949 | ((uint16_t) le16_to_cpup((void *) (DATA))) | ^~~~~~~~~~~~ main.c:115:36: note: in expansion of macro ‘EC_READ_U16’ 115 | uint16_t current_status_word = EC_READ_U16(domain1_pd + status_word); | ^~~~~~~~~~~ main.c:115:61: error: ‘status_word’ undeclared (first use in this function) 115 | uint16_t current_status_word = EC_READ_U16(domain1_pd + status_word); | ^~~~~~~~~~~ ../../include/ecrt.h:2877:24: note: in definition of macro ‘le16_to_cpu’ 2877 | #define le16_to_cpu(x) x | ^ ../../include/ecrt.h:2949:18: note: in expansion of macro ‘le16_to_cpup’ 2949 | ((uint16_t) le16_to_cpup((void *) (DATA))) | ^~~~~~~~~~~~ main.c:115:36: note: in expansion of macro ‘EC_READ_U16’ 115 | uint16_t current_status_word = EC_READ_U16(domain1_pd + status_word); | ^~~~~~~~~~~ main.c:116:51: error: ‘target_position’ undeclared (first use in this function); did you mean ‘target_pos_b’? 116 | int32_t actual_pos = EC_READ_S32(domain1_pd + target_position); // 或者实际位置反馈的偏移 | ^~~~~~~~~~~~~~~ ../../include/ecrt.h:2878:24: note: in definition of macro ‘le32_to_cpu’ 2878 | #define le32_to_cpu(x) x | ^ ../../include/ecrt.h:2973:17: note: in expansion of macro ‘le32_to_cpup’ 2973 | ((int32_t) le32_to_cpup((void *) (DATA))) | ^~~~~~~~~~~~ main.c:116:26: note: in expansion of macro ‘EC_READ_S32’ 116 | int32_t actual_pos = EC_READ_S32(domain1_pd + target_position); // 或者实际位置反馈的偏移 | ^~~~~~~~~~~ main.c:117:9: error: ‘counter’ undeclared (first use in this function); did you mean ‘count’? 117 | if (counter) { | ^~~~~~~ | count main.c:139:7: error: ‘blink’ undeclared (first use in this function); did you mean ‘link’? 139 | blink = !blink; | ^~~~~ | link main.c:147:9: error: ‘sync_ref_counter’ undeclared (first use in this function) 147 | if (sync_ref_counter) { | ^~~~~~~~~~~~~~~~ In file included from main.c:12: main.c:158:35: error: ‘control_word’ undeclared (first use in this function) 158 | EC_WRITE_U16(domain1_pd + control_word, 0x0006); | ^~~~~~~~~~~~ ../../include/ecrt.h:3059:25: note: in definition of macro ‘EC_WRITE_U16’ 3059 | *((uint16_t *) (DATA)) = cpu_to_le16((uint16_t) (VAL)); \ | ^~~~ main.c:176:57: error: ‘actual_position’ undeclared (first use in this function); did you mean ‘actual_position_’? 176 | int32_t actual_position_ = EC_READ_S32(domain1_pd + actual_position); | ^~~~~~~~~~~~~~~ ../../include/ecrt.h:2878:24: note: in definition of macro ‘le32_to_cpu’ 2878 | #define le32_to_cpu(x) x | ^ ../../include/ecrt.h:2973:17: note: in expansion of macro ‘le32_to_cpup’ 2973 | ((int32_t) le32_to_cpup((void *) (DATA))) | ^~~~~~~~~~~~ main.c:176:32: note: in expansion of macro ‘EC_READ_S32’ 176 | int32_t actual_position_ = EC_READ_S32(domain1_pd + actual_position); | ^~~~~~~~~~~ main.c:116:13: warning: unused variable ‘actual_pos’ [-Wunused-variable] 116 | int32_t actual_pos = EC_READ_S32(domain1_pd + target_position); // 或者实际位置反馈的偏移 | ^~~~~~~~~~ main.c: In function ‘main’: main.c:209:5: warning: implicit declaration of functionmlockall’ [-Wimplicit-function-declaration] 209 | if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { | ^~~~~~~~ main.c:209:14: error: ‘MCL_CURRENT’ undeclared (first use in this function) 209 | if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { | ^~~~~~~~~~~ main.c:209:28: error: ‘MCL_FUTURE’ undeclared (first use in this function) 209 | if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { | ^~~~~~~~~~ main.c:217:2: error: ‘domain1’ undeclared (first use in this function); did you mean ‘main’? 217 | domain1 = ecrt_master_create_domain(master); | ^~~~~~~ | main main.c:221:2: error: ‘sc’ undeclared (first use in this function); did you mean ‘sync’? 221 | sc = ecrt_master_slave_config(master, SV660NPos, SV660N_PIDVID); | ^~ | sync main.c:223:40: error: ‘slave_0_syncs’ undeclared (first use in this function) 223 | if (ecrt_slave_config_pdos(sc, EC_END, slave_0_syncs)) { | ^~~~~~~~~~~~~ main.c:228:46: error: ‘domain1_regs’ undeclared (first use in this function) 228 | if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) { | ^~~~~~~~~~~~ main.c:232:34: error: ‘PERIOD_NS’ undeclared (first use in this function) 232 | ecrt_slave_config_dc(sc, 0x0300, PERIOD_NS, 0, 0, 0); | ^~~~~~~~~ main.c:237:8: error: ‘domain1_pd’ undeclared (first use in this function) 237 | if (!(domain1_pd = ecrt_domain_data(domain1))) { | ^~~~~~~~~~ main.c:240:8: error: variable ‘param’ has initializer but incomplete type 240 | struct sched_param param = {}; | ^~~~~~~~~~~ main.c:240:20: error: storage size of ‘param’ isn’t known 240 | struct sched_param param = {}; | ^~~~~ main.c:241:25: warning: implicit declaration of function ‘sched_get_priority_max’ [-Wimplicit-function-declaration] 241 | param.sched_priority = sched_get_priority_max(SCHED_FIFO); | ^~~~~~~~~~~~~~~~~~~~~~ main.c:241:48: error: ‘SCHED_FIFO’ undeclared (first use in this function) 241 | param.sched_priority = sched_get_priority_max(SCHED_FIFO); | ^~~~~~~~~~ main.c:244:6: warning: implicit declaration of function ‘sched_setscheduler’ [-Wimplicit-function-declaration] 244 | if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) { | ^~~~~~~~~~~~~~~~~~ main.c:240:20: warning: unused variable ‘param’ [-Wunused-variable] 240 | struct sched_param param = {}; | ^~~~~ At top level: main.c:43:17: warning: ‘domainInput_pd’ defined but not used [-Wunused-variable] 43 | static uint8_t *domainInput_pd = NULL; | ^~~~~~~~~~~~~~ main.c:42:17: warning: ‘domainOutput_pd’ defined but not used [-Wunused-variable] 42 | static uint8_t *domainOutput_pd = NULL; | ^~~~~~~~~~~~~~~ main.c:37:21: warning: ‘user_alarms’ defined but not used [-Wunused-variable] 37 | static unsigned int user_alarms = 0; | ^~~~~~~~~~~ main.c:36:21: warning: ‘sig_alarms’ defined but not used [-Wunused-variable] 36 | static unsigned int sig_alarms = 0; | ^~~~~~~~~~ main.c:33:32: warning: ‘sc_motor_state’ defined but not used [-Wunused-variable] 33 | static ec_slave_config_state_t sc_motor_state = {}; | ^~~~~~~~~~~~~~ main.c:32:27: warning: ‘sc_motor’ defined but not used [-Wunused-variable] 32 | static ec_slave_config_t *sc_motor = NULL; | ^~~~~~~~ main.c:30:26: warning: ‘domainOutput_state’ defined but not used [-Wunused-variable] 30 | static ec_domain_state_t domainOutput_state = {}; | ^~~~~~~~~~~~~~~~~~ main.c:29:21: warning: ‘domainOutput’ defined but not used [-Wunused-variable] 29 | static ec_domain_t *domainOutput = NULL; | ^~~~~~~~~~~~ main.c:27:26: warning: ‘domainInput_state’ defined but not used [-Wunused-variable] 27 | static ec_domain_state_t domainInput_state = {}; | ^~~~~~~~~~~~~~~~~ main.c:26:21: warning: ‘domainInput’ defined but not used [-Wunused-variable] 26 | static ec_domain_t *domainInput = NULL; | ^~~~~~~~~~~ main.c:24:26: warning: ‘master_state’ defined but not used [-Wunused-variable] 24 | static ec_master_state_t master_state = {}; | ^~~~~~~~~~~~ make: *** [Makefile:333:ec_user_example-main.o] 错误 1
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值