#include<stdio.h> #include<unistd.h> #include<sys/shm.h> #include<stdlib.h> #include<error.h> #define SHM_PATH "/mnt/hgfs/shared/TestShm4" int main(){ int shm_id=-1; int *share; int num; srand(time(NULL)); key_t key =ftok(SHM_PATH, 0); shm_id = shmget(key,64,0666|IPC_CREAT); printf("key :iShmID = %d:%d\n",key, shm_id); //printf("errno:%d",errno); perror("shmget:"); if(shm_id == -1){ perror("shmget()"); } share = (int *)shmat(shm_id, 0, 0); perror("shmat:"); while(1){ num = random() % 1000; *share = num; printf("write a random number %d\n", num); sleep(1); } return 0; } read.c /* * read from the shm every 1 second */ #include<stdio.h> #include<unistd.h> #include<sys/shm.h> #include<stdlib.h> #include<error.h> #define SHM_PATH "/mnt/hgfs/shared/TestShm4" int main(){ int shm_id; int *share; key_t key =ftok(SHM_PATH, 0); shm_id = shmget (key, 0, 0660); if(shm_id == -1){ perror("shmget()"); } share = (int *)shmat(shm_id, 0, 0); while(1){ sleep(1); printf("%d\n", *share); } return 0; }
时间: 2024-04-21 21:28:56 浏览: 17
这是一个利用共享内存实现进程间通信的示例程序,主要分为两个部分:一个写进程和一个读进程。下面我简单介绍一下程序的作用和实现方法。
首先,我们需要定义一个共享内存区域,可以使用shmget函数进行创建。在这个示例程序中,使用了一个字符串常量"/mnt/hgfs/shared/TestShm4"作为共享内存的键值,通过ftok函数将其转换成一个唯一的key。然后使用shmget函数创建一个大小为64字节的共享内存区域,并将其标记为IPC_CREAT,表示如果这个共享内存区域不存在,则创建一个新的。
接下来,我们需要使用shmat函数将共享内存区域映射到当前进程的地址空间中,并返回指向共享内存区域的指针。在写进程中,我们可以通过修改共享内存中的变量来向另一个进程发送消息;在读进程中,我们可以通过读取共享内存中的变量来接收消息。
在写进程中,我们使用了一个while循环来不断向共享内存中写入一个随机数,并使用sleep函数使进程等待1秒钟。在读进程中,我们同样使用了一个while循环来不断从共享内存中读取变量的值,并使用sleep函数使进程等待1秒钟。
需要注意的是,共享内存区域在使用完毕后需要及时释放,可以使用shmdt函数将其从当前进程的地址空间中解除映射,使用shmctl函数将其删除。在这个示例程序中,没有进行共享内存的释放操作,需要手动进行释放。
总之,通过这个示例程序,我们可以初步了解共享内存的使用方法和实现原理,同时也可以看到共享内存在进程间通信中的应用。
相关问题
补全代码#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/shm.h> #include <pthread.h> #include "mypthread.h" #include "serial.h" #include "zigbee.h" #include "my_camera.h" #include "server.h" static int zgbfd; static pthread_mutex_t cam_mutex; void *pthread_cam(void *arg) { } void *pthread_zgb(void *arg) { } void *pthread_cli(void *arg) { }
下面是对代码的补全:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/shm.h>
#include <pthread.h>
#include "mypthread.h"
#include "serial.h"
#include "zigbee.h"
#include "my_camera.h"
#include "server.h"
static int zgbfd;
static pthread_mutex_t cam_mutex;
void *pthread_cam(void *arg)
{
// 摄像头相关操作的具体实现
while (1) {
// 在这里实现摄像头的操作逻辑
// 例如获取摄像头图像并处理
// 使用互斥锁保护临界区
pthread_mutex_lock(&cam_mutex);
// 执行摄像头操作
pthread_mutex_unlock(&cam_mutex);
// 休眠一段时间
usleep(1000);
}
return NULL;
}
void *pthread_zgb(void *arg)
{
// Zigbee 相关操作的具体实现
while (1) {
// 在这里实现 Zigbee 的操作逻辑
// 例如发送或接收数据
// 使用互斥锁保护临界区
pthread_mutex_lock(&cam_mutex);
// 执行 Zigbee 操作
pthread_mutex_unlock(&cam_mutex);
// 休眠一段时间
usleep(1000);
}
return NULL;
}
void *pthread_cli(void *arg)
{
// 客户端连接和通信操作的具体实现
int client_sock = *((int*)arg);
char buffer[1024];
while (1) {
// 在这里实现客户端连接和通信的操作逻辑
// 例如接收客户端消息并处理
memset(buffer, 0, sizeof(buffer));
int ret = read(client_sock, buffer, sizeof(buffer));
if (ret <= 0) {
// 客户端断开连接或出错,结束线程
close(client_sock);
break;
}
// 处理客户端消息
}
return NULL;
}
```
以上补全了 `pthread_cam`、`pthread_zgb` 和 `pthread_cli` 函数的代码实现部分。你可以根据具体需求在这些函数中添加逻辑来完成相应的操作。注意使用互斥锁来保护临界区,以避免多个线程同时访问造成的竞争条件。
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #define SIZE 128 int main(int argc,char *argv[]) { key_t key = ftok(".",'s'); if(key < 0) { perror("ftok"); return -1; } int shmid = shmget(key,SIZE,IPC_CREAT | 0777); if(shmid < 0) { perror("shmget"); return -1; } printf("shmid:%d\n",shmid); char *p = shmat(shmid,NULL,0); if((char *)-1 == p) { perror("shmat"); return -1; } while(1) { puts(p); sleep(1); if(strncmp(p,"quit",4) == 0) break; } if(-1 == shmdt(p)) { perror("shmdt"); exit(-1); } if(-1 == shmctl(shmid,IPC_RMID,NULL)) { perror("shmctl"); exit(-1); } return 0; }修改代码,使其正确输出读入端的代码一次
这里是修改后的代码,可以正确输出读入端的代码一次:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#define SIZE 128
int main(int argc, char *argv[]) {
key_t key = ftok(".", 's');
if (key < 0) {
perror("ftok");
return -1;
}
int shmid = shmget(key, SIZE, IPC_CREAT | 0777);
if (shmid < 0) {
perror("shmget");
return -1;
}
printf("shmid:%d\n", shmid);
char *p = shmat(shmid, NULL, 0);
if ((char *)-1 == p) {
perror("shmat");
return -1;
}
char buf[SIZE];
while (1) {
memset(buf, 0, SIZE);
fgets(buf, SIZE, stdin);
strncpy(p, buf, strlen(buf));
if (strncmp(p, "quit", 4) == 0)
break;
}
if (-1 == shmdt(p)) {
perror("shmdt");
exit(-1);
}
if (-1 == shmctl(shmid, IPC_RMID, NULL)) {
perror("shmctl");
exit(-1);
}
return 0;
}
```