contentprovider实现跨进程通信_linux进程间通信----IPC篇(二)----共享内存实现进程间通信...

先给自己打个广告,本人的微信公众号正式上线了,搜索:张笑生的地盘,主要关注嵌入式软件开发,足球等等,希望大家多多关注,有问题可以直接留言给我,一定尽心尽力回答大家的问题

17c3b9aaaed13adab1887d585f53832e.png

一 what

在《linux进程间通信----IPC篇(一)----共享内存初识篇》文章中,我们知道了共享内存是什么,通过几个常用的函数shmget、ftok、shmat、shmdt、shmctl,了解了如何创建共享内存,但是创建好的共享内存之间,如何实现进程间通信呢?

共享内存对象是存在于内核中的,两个进程间如何访问这个共享内存对象呢?这篇文章介绍两种访问方式,分别是有亲缘关系的父子进程访问同一个共享内存,以及非亲缘关系之间访问同一个共享内存。

二 how

2.1 亲缘关系的进程访问同一个共享内存

要想让共享内存能够在亲缘关系的进程中访问,fork函数必须在shmget函数之后;鉴于我们前面好几篇文章都提到了fork函数,后期打算出一篇文章谈谈fork函数创建子进程的过程中究竟做了什么。

#include 

2.2 非亲缘关系的进程访问同一个共享内存

进程server.c

#include "stdio.h"
#include "stdlib.h"
#include <unistd.h>
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "string.h"

struct mybuf
{
    int pid;
    char buf[124];
};

void myfun(int signum)
{
    return;
}

int main(int argc, char *argv[])
{
    int shmid;
    int key;
    struct mybuf *p;
    int pid;

    key = ftok("./a.c", 'a');
    if (key , 0) {
        printf("create key failn");
        return -1;
    }
    printf("create key sucessn");

    shmid = shmget(key, 128, IPC_CREAT | 0777);
    if (shmid < 0) {
        printf("create shared memory failn");
        return -1;
    }
    printf("create shared memory sucess, shmid = %dn", shmid);

    signal(SIGUSR2, myfun);
    p = (struct mybuf *)shmat(shmid, NULL, 0);
    if (p == NULL) {
        printf("shmat failn");
        return -1;
    }
    printf("parent process shmat sucessn");

    // get client pid
    p->pid = getpid(); //write server pid to share memory
    pause();  // wait client to read server pid
    pid=p->pid;

    while (1) {
        //write share memory
        printf("parent process begin to write memory datan");
        fgets(p->buf, 124, stdin);
        kill(pid, SIGUSR1);   // tell client process to read data
        pause();              // wait client process read
    }

    //在用户空间删除共享内存的地址
    shmdt(p);

    //memcpy(p, "abcd", 4);  //执行这个语句会出现segment fault

    shmctl(shmid, IPC_RMID, NULL);
    system("ipcs -m");
    return 0;
}

进程client.c

#include "stdio.h"
#include "stdlib.h"
#include <unistd.h>
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "string.h"

struct mybuf
{
    int pid;
    char buf[124];
};

void myfun(int signum)
{
    return;
}

int main(int argc, char *argv[])
{
    int shmid;
    int key;
    struct mybuf *p;
    int pid;

    key = ftok("./a.c", 'a');
    if (key , 0) {
        printf("create key failn");
        return -1;
    }
    printf("create key sucessn");

    shmid = shmget(key, 128, IPC_CREAT | 0777);
    if (shmid < 0) {
        printf("create shared memory failn");
        return -1;
    }
    printf("create shared memory sucess, shmid = %dn", shmid);

    signal(SIGUSR1, myfun);
    p = (struct mybuf *)shmat(shmid, NULL, 0);
    if (p == NULL) {
        printf("shmat failn");
        return -1;
    }
    printf("client process shmat sucessn");

    // get server pid
    //read share memory
    pid = p->pid;
    // write client pid to share memory
    p->pid = getpid();
    kill(pid, SIGUSR2);   // tell client process to read data

    //client start to read share memory

    while (1) {
        pause();              // wait server process write share memory
        printf("client process read data:%sn", p->buf); // read data
        kill(pid, SIGUSR2);   // server can  write share memory
    }

    //在用户空间删除共享内存的地址
    shmdt(p);

    //memcpy(p, "abcd", 4);  //执行这个语句会出现segment fault

    shmctl(shmid, IPC_RMID, NULL);
    system("ipcs -m");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android进程间通信(Inter-process Communication, IPC)是指在不同进程之间进行数据交互和通信的方法。Android提供了多种方式实现进程间通信,以下是常用的几种方式: 1. Binder机制:Binder是一种进程通信技术,它基于Linux内核提供的Binder驱动。通过Binder,我们可以将一个Service注册为Binder服务,其他进程可以通过Binder进行远程调用,实现进程间的通信。 2. 文件共享:进程可以通过共享文件的方式实现通信。一个进程将数据写入文件,其他进程读取该文件数据,从而实现进程间的信息传递。 3. Socket通信:可以使用Socket套接字进行进程间通信。一个进程作为服务器,另一个进程作为客户端,通过Socket建立连接进行数据交互。 4. ContentProviderContentProvider是Android中用于实现进程间共享数据的一种组件。通过ContentProvider,一个进程可以提供数据给其他进程进行读写操作。 5. BroadcastReceiver:广播是一种常见的进程间通信方式。一个进程发送广播消息,其他进程通过注册相应的广播接收器来接收并处理广播消息。 6. Messenger:Messenger是一种轻量级的进程间通信方式。通过Messenger,一个进程可以发送消息给另一个进程,实现进程间的通信。 以上是常用的几种Android进程间通信方式,开发者可以根据具体需求选择合适的方式来实现进程间通信
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值