如何使用Mmap共享内存.请更正我的代码
How to use Mmap to share memory. Please correct My code
这里的问题是,我希望我们的看跌期权是93。我想让所有线程共享这个变量。就像静态变量对所有对象都是通用的一样,我想要一个对所有线程都通用的变量。
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
static int *glob_var;
int main(void)
{
glob_var = (int*)mmap(NULL, sizeof *glob_var, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*glob_var = 1;
int ppid =fork();
if (ppid == 0) {
*glob_var = 92; printf("%dn", *glob_var);
} else if(ppid!=0){
(*glob_var)++; /////I want a 93 over here???
printf("%dn", *glob_var); /////I want a 93 over here??? print
munmap(glob_var, sizeof *glob_var);
}
return 0;
}
两个进程都更新glob_var
。您需要协调对这个共享内存的访问。需要保证数据修改的顺序,即先赋值92
。
信号量通常用于在以下情况下同步操作:
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
// Binary semaphore implementation. Initial state 0
union semun
{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
int binary_semaphore_allocation (key_t key, int sem_flags)
{
return semget (key, 1, sem_flags);
}
int binary_semaphore_deallocate (int semid)
{
union semun ignored_argument;
return semctl (semid, 1, IPC_RMID, ignored_argument);
}
int binary_semaphore_initialize (int semid)
{
union semun argument;
unsigned short values[1];
values[0] = 0;
argument.array = values;
return semctl (semid, 0, SETALL, argument);
}
int binary_semaphore_wait (int semid)
{
struct sembuf operations[1];
operations[0].sem_num = 0;
/* Decrement by 1. */
operations[0].sem_op = -1;
operations[0].sem_flg = SEM_UNDO;
return semop (semid, operations, 1);
}
int binary_semaphore_post (int semid)
{
struct sembuf operations[1];
operations[0].sem_num = 0;
/* Increment by 1. */
operations[0].sem_op = 1;
operations[0].sem_flg = SEM_UNDO;
return semop (semid, operations, 1);
}
int main(void)
{
key_t ipc_key;
ipc_key = ftok(".", 'S');
int sem_id;
glob_var = (int*)mmap(NULL, sizeof *glob_var, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*glob_var = 1;
if ((sem_id=binary_semaphore_allocation(ipc_key, IPC_CREAT|IPC_EXCL)) != -1)
{
if (binary_semaphore_initialize(sem_id) == -1)
{
printf("Semaphore initialization failed");
return 2;
}
}
int ppid = fork();
if (ppid == 0)
{
if ((sem_id = binary_semaphore_allocation(ipc_key, 0)) == -1)
{
printf("Child process failed to open semaphore");
return 3;
}
}
else
{
// Wait in parent process until child update glob_var
binary_semaphore_wait(sem_id);
}
if (ppid == 0)
{
*glob_var = 92;
printf("%dn", *glob_var);
binary_semaphore_post(sem_id);
}
else if(ppid!=0)
{
(*glob_var)++;
printf("%dn", *glob_var);
munmap(glob_var, sizeof *glob_var);
binary_semaphore_deallocate(sem_id);
}
return 0;
}
输出:
92
93
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么我的代码在输出中增加了93天
- 0-1背包代码中的错误.我的代码中有什么错误
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 在我的代码中,获得最大的Pair Wise产品C++和输出并不总是正确的
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 我的代码运行良好,但在游戏循环中中断
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- 我的代码中的意外价值以及我如何修复它
- 为什么 rand 不在我的代码中生成随机数?
- 编写了一个C++代码来检查表达式是否具有平衡括号并且我的代码未运行.我已经卡了一天了
- 如何改进我的代码,使其不会因超时而终止?(黑客排名挑战)
- 无法找出我的代码中的内存泄漏
- 我不明白为什么我的代码不起作用并且需要更长的时间来运行
- 我正在尝试创建一个菜单,但我的代码一直在循环