Linux 进程间通信之共享内存实例

共享内存是不同进程间通过访问同一段逻辑内存,实现数据共享,非常方便。下面是一个简单实例

在share_write.c中

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/shm.h>

int main(int argc, char *argv[])
{
    int shmid = 0;
    int share_len = 64;
    void *shm = NULL;
    
    shmid = shmget((key_t)1000, share_len, 0666 | IPC_CREAT);
    if (shmid == -1)
    {
        printf("shmget fail\n");
    }

    shm = shmat(shmid, 0, 0);
    if (shm == (void *) - 1)
    {
        printf("shmat fail\n");
    }

    strcpy(shm, "TEST");
    printf("Contents of share memory: %s\n", shm);

    if (shmdt(shm) == -1)
   {
        printf("shmdt fail\n");
    }

    return 0;
}

在share_read.c中

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/shm.h>

int ma
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Linux进程通信(IPC)机制的示例代码,包括使用管道(pipe)、共享内存(shared memory)和信号量(semaphore)进行进程通信。 1. 使用管道(pipe)进行进程通信: ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define BUFFER_SIZE 25 int main() { int fd[2]; pid_t pid; char write_msg[BUFFER_SIZE] = "Greetings"; char read_msg[BUFFER_SIZE]; if (pipe(fd) == -1) { fprintf(stderr, "Pipe failed"); return 1; } pid = fork(); if (pid < 0) { fprintf(stderr, "Fork failed"); return 1; } if (pid > 0) // Parent process { close(fd[0]); write(fd[1], write_msg, BUFFER_SIZE); close(fd[1]); } else // Child process { close(fd[1]); read(fd[0], read_msg, BUFFER_SIZE); printf("Received message: %s\n", read_msg); close(fd[0]); } return 0; } ``` 2. 使用共享内存(shared memory)进行进程通信: ``` #include <stdio.h> #include <stdlib.h> #include <sys/ipc.h> #include <sys/shm.h> #define SHM_SIZE 1024 int main() { int shmid; key_t key = 1234; char *shm, *s; if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } printf("Enter message: "); for (s = shm; (*s = getchar()) != '\n'; s++) ; *s = '\0'; printf("You entered: %s\n", shm); shmdt(shm); return 0; } ``` 3. 使用信号量(semaphore)进行进程通信: ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/sem.h> #define MAX_RETRIES 10 union semun { int val; struct semid_ds *buf; unsigned short *array; }; int main() { key_t key = 1234; int semid, semval; union semun arg; struct sembuf sembuf; if ((semid = semget(key, 1, IPC_CREAT | 0666)) == -1) { perror("semget"); exit(1); } arg.val = 1; if (semctl(semid, 0, SETVAL, arg) == -1) { perror("semctl"); exit(1); } if ((semval = semctl(semid, 0, GETVAL, 0)) == -1) { perror("semctl"); exit(1); } printf("Semaphore value before decrement: %d\n", semval); sembuf.sem_num = 0; sembuf.sem_op = -1; sembuf.sem_flg =

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值