`
haoningabc
  • 浏览: 1466226 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

进程间通信的例子-共享内存

c 
阅读更多
消费者
root@ubuntu:~/test/SourceCode/ch14# cat shm1.c 
/* Our first program is a consumer. After the headers the shared memory segment
 (the size of our shared memory structure) is created with a call to shmget,
 with the IPC_CREAT bit specified. */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <sys/shm.h>

#include "shm_com.h"

int main()
{
    int running = 1;
    void *shared_memory = (void *)0;
    struct shared_use_st *shared_stuff;
    int shmid;

    srand((unsigned int)getpid());    

    shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);

    if (shmid == -1) {
        fprintf(stderr, "shmget failed\n");
        exit(EXIT_FAILURE);
    }

/* We now make the shared memory accessible to the program. */

    shared_memory = shmat(shmid, (void *)0, 0);
    if (shared_memory == (void *)-1) {
        fprintf(stderr, "shmat failed\n");
        exit(EXIT_FAILURE);
    }

    printf("Memory attached at %X\n", (int)shared_memory);

/* The next portion of the program assigns the shared_memory segment to shared_stuff,
 which then prints out any text in written_by_you. The loop continues until end is found
 in written_by_you. The call to sleep forces the consumer to sit in its critical section,
 which makes the producer wait. */

    shared_stuff = (struct shared_use_st *)shared_memory;
    shared_stuff->written_by_you = 0;
    while(running) {
        if (shared_stuff->written_by_you) {
            printf("You wrote: %s", shared_stuff->some_text);
            sleep( rand() % 4 ); /* make the other process wait for us ! */
            shared_stuff->written_by_you = 0;
            if (strncmp(shared_stuff->some_text, "end", 3) == 0) {
                running = 0;
            }
        }
    }

/* Lastly, the shared memory is detached and then deleted. */

    if (shmdt(shared_memory) == -1) {
        fprintf(stderr, "shmdt failed\n");
        exit(EXIT_FAILURE);
    }

    if (shmctl(shmid, IPC_RMID, 0) == -1) {
        fprintf(stderr, "shmctl(IPC_RMID) failed\n");
        exit(EXIT_FAILURE);
    }

    exit(EXIT_SUCCESS);
}

root@ubuntu:~/test/SourceCode/ch14# 


root@ubuntu:~/test/SourceCode/ch14# cat shm_com.h 
/* A common header file to describe the shared memory we wish to pass about. */

#define TEXT_SZ 2048

struct shared_use_st {
    int written_by_you;
    char some_text[TEXT_SZ];
};



生产者
root@ubuntu:~/test/SourceCode/ch14# cat shm2.c 
/* The second program is the producer and allows us to enter data for consumers.
 It's very similar to shm1.c and looks like this. */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <sys/shm.h>

#include "shm_com.h"

int main()
{
    int running = 1;
    void *shared_memory = (void *)0;
    struct shared_use_st *shared_stuff;
    char buffer[BUFSIZ];
    int shmid;

    shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);

    if (shmid == -1) {
        fprintf(stderr, "shmget failed\n");
        exit(EXIT_FAILURE);
    }

    shared_memory = shmat(shmid, (void *)0, 0);
    if (shared_memory == (void *)-1) {
        fprintf(stderr, "shmat failed\n");
        exit(EXIT_FAILURE);
    }

    printf("Memory attached at %X\n", (int)shared_memory);

    shared_stuff = (struct shared_use_st *)shared_memory;
    while(running) {
        while(shared_stuff->written_by_you == 1) {
            sleep(1);            
            printf("waiting for client...\n");
        }
        printf("Enter some text: ");
        fgets(buffer, BUFSIZ, stdin);
        
        strncpy(shared_stuff->some_text, buffer, TEXT_SZ);
        shared_stuff->written_by_you = 1;

        if (strncmp(buffer, "end", 3) == 0) {
                running = 0;
        }
    }

    if (shmdt(shared_memory) == -1) {
        fprintf(stderr, "shmdt failed\n");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}
root@ubuntu:~/test/SourceCode/ch14# 





结果为


说明:使用一个结构,标识written_by_you
当共享内存写入时就设置这个标志,,这个标志被设置时,程序就从共享内存中读取文本
  • 大小: 70.4 KB
分享到:
评论

相关推荐

    C#进程间通信-共享内存代码实例

    在C#中,实现进程间通信有多种方法,如管道、套接字、消息队列等,而共享内存是其中一种高效且直接的方式。本篇文章将深入探讨C#中的共享内存实现,并通过一个具体的代码实例来阐述其工作原理。 共享内存是一种让多...

    进程间通讯---共享内存的使用方法

    共享内存是一种高效的进程间通信(IPC,Inter-Process Communication)方式,它允许不同的进程直接读写同一块内存区域,无需通过任何中介。这种方式避免了数据复制,因此在速度上具有显著优势。本教程将深入探讨如何...

    使用共享内存及信号量实现进程间通信例子

    在计算机系统中,进程间通信(IPC,Inter-Process Communication)是多个进程协同工作时必不可少的一部分。本示例代码着重于使用共享内存和信号量来解决进程间的通信和同步问题,这是一种高效且灵活的方法,特别是在...

    java进程间通讯机制代码----RMI、共享内存、Socket、管道

    java进程间通讯机制代码----RMI、共享内存、Socket、管道,等方式,每种方法我都讲了原理和例子程序,很有参考意义。在网上很难找到的。

    进程间通信例子

    这个“进程间通信例子”可能是针对Windows Forms(WinForm)应用程序提供的一种实践示例,展示了如何在不同的进程中发送和接收信息。 1. **管道**:管道是一种半双工的通信方式,允许数据在两个进程间单向流动。...

    linux下C语言编程4-使用共享内存实现进程间通信.doc

    下面是一个使用共享内存实现进程间通信的例子: 进程 A 开辟一块新的共享内存,进程 B 修改这个共享内存,进程 C 打印输出这个共享内存的内容,进程 D 删除这个共享内存。进程 BCD 运行的命令格式为:命令 共享内存...

    UNIX网络编程卷2进程间通信--部分源代码

    进程间通信是操作系统中多进程协作的基础,它包括了管道、信号量、共享内存、消息队列、套接字等多种机制。这些机制允许不同进程之间交换数据,实现同步和协调。在这些源代码中,我们可以看到这些通信方式的实际...

    linux系统进程间通信——共享内存(System V版本)

    之前用过Prosix版本的共享内存和信号量,一直没有实践System V版本的,主要是因为其信号量集的概念操作有些复杂,今天试着写一个SV版本的共享内存进程间通信,使用信号量同步。程序提供了几个简单的用于操作SV版本...

    Linux进程间通信-信号量通信进程互斥实例.pdf

    【Linux进程间通信】在操作系统中,进程间通信(Inter-Process Communication, IPC)是不同进程之间交换数据的重要机制。Linux提供了多种IPC方式,包括管道、消息队列、共享内存、信号量等。本实例主要关注的是...

    linux下c语言编程4-使用共享内存实现进程间通信.pdf

    6. **进程间通信**:在这个例子中,进程B写入数据,进程C读取数据,这展示了共享内存作为通信媒介的能力。由于它们共享同一内存空间,所以无需额外的数据传输。 7. **注意事项**:使用共享内存时,必须确保对它的...

    使用delphi编写进程间内存共享的例子

    在编程领域,进程间通信(IPC,Inter-Process Communication)是一项关键的技术,它允许不同的进程之间交换数据。在Windows环境中,Delphi作为一种强大的Object Pascal开发工具,提供了多种实现进程间内存共享的方式...

    Linux进程间通信课件代码.rar

    3. 7-12write.c 和 7-12read.c 可能涉及到特定的进程间通信机制,如共享内存或套接字,其中"write.c"和"read.c"可能分别展示了写入和读取数据的过程。 4. Linux程序设计上机指导书5:Linux进程间通信.pdf 是一份...

    进程通信,建立共享内存

    本资源基于“进程通信,建立共享内存”的主题,使用经典的编程环境VC++ 6.0,提供了通过共享内存实现进程间通信的示例代码。 首先,我们要理解什么是共享内存。共享内存是一种高效的进程通信方式,它允许多个进程...

    delphi进程间通讯例子

    总的来说,这个Delphi例子旨在演示如何使用自定义消息和共享内存进行进程间通信,开发者可以从源代码中学习如何创建和管理这些通信机制,理解Delphi中IPC的实现细节。这不仅有助于深入理解Delphi编程,还能为解决多...

    Qt进程间共享内存区例子

    在本文中,我们将深入探讨一个基于Qt框架的C++实例,该实例展示了如何利用共享内存进行进程间通信。标题为“Qt进程间共享内存区例子”,这个例子包括两个部分:`sharedmemory_write`(数据共享发起端)和`...

    Linux进程间通信的例子

    资源中包含了Linux进程间通信的例子,同时有源文件和可执行文件。 源码主要包含了Linux下IPC机制的本地进程通信方式,包含了IPC共享内存,IPC信号量,IPC消息队列的实现,以及Linux下判断进程退出原因的示例程序。

    Linux 进程间通讯共享内存方式.docx

    本篇文档详细介绍了如何在Linux中使用共享内存进行进程间通信。 首先,创建共享内存的关键在于使用`shmget`函数。该函数接收三个参数:`key_t key`,`size_t size`和`int shmflg`。`key`通常设置为`IPC_PRIVATE`,...

    进程之间共享内存

    在计算机编程中,进程间通信(IPC,Inter-Process Communication)是多个进程间相互传递信息的方式,以便它们能协作完成任务。"进程之间共享内存"是一种常见的IPC技术,它允许两个或更多个进程访问同一块内存空间,...

    Linux进程间通信-信号通信信号发送实例.pdf

    在Linux操作系统中,进程间通信(IPC,Inter-Process Communication)是多个进程间协同工作、交换数据的关键机制。本文将重点讨论其中的一种通信方式——信号通信,并通过具体的信号发送实例来解析其工作原理。 ...

    Qt使用内存共享例子

    一个简单的Qt使用内存共享方法,进程间通信的例子。使用了Windows 函数进行内存共享。注释部分代码也有qt函数进行内存共享的例子;

Global site tag (gtag.js) - Google Analytics