//processA.c文件
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <errno.h>
#include <pthread.h>
#define BUF_SIZE 4096
int main()
{
void *shm_addr = NULL;
char buffer[BUF_SIZE];
pthread_mutex_t * sharedLock;
pthread_mutexattr_t ma;
int shmid;
// 使用约定的键值创建共享内存
shmid = shmget((key_t) 1234, BUF_SIZE, 0666 | IPC_CREAT);
printf("shmid : %u\n", shmid);
if (shmid < 0)
{
perror("shmget error!");
exit(1);
}
// 将共享内存附加到本进程
shm_addr = shmat(shmid, NULL, 0);
if (shm_addr == (void *) -1)
{
perror("shmat error!&
互相独立进程间共享内存互斥访问的解决办法
于 2024-05-14 09:56:37 首次发布