Git Product home page Git Product logo

linux-ipc-shared-memory's Introduction

NAME: S.KAVIYA

RESIGTER NO: 212223040090

Linux-IPC-Shared-memory

Ex06-Linux IPC-Shared-memory

AIM:

To Write a C program that illustrates two processes communicating using shared memory.

DESIGN STEPS:

Step 1:

Navigate to any Linux environment installed on the system or installed inside a virtual environment like virtual box/vmware or online linux JSLinux (https://bellard.org/jslinux/vm.html?url=alpine-x86.cfg&mem=192) or docker.

Step 2:

Write the C Program using Linux Process API - Shared Memory

Step 3:

Execute the C Program for the desired output.

PROGRAM:

Write a C program that illustrates two processes communicating using shared memory.

//shm.c

#include<unistd.h>

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

#include<sys/shm.h>

#define TEXT_SZ 2048

struct shared_use_st

{

int written_by_you;

char some_text[TEXT_SZ];

}

;

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);

printf("Shared memort id = %d \n",shmid);

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);

}

//shmry2.c

#include<unistd.h>

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

#include<sys/shm.h>

#define TEXT_SZ 2048

struct shared_use_st

{

int written_by_you;

char some_text[TEXT_SZ];

};

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);

printf("Shared memort id = %d \n",shmid);

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);

}

OUTPUT

$ ./shm.o

Shared memort id = 720902

Memory Attached at 1dcd5000

Enter Some Text: Hello

waiting for client.

Enter Some Text: World

waiting for client.

waiting for client.

waiting for client.

Enter Some Text: end

$ ipcs

------ Message Queues --------

key msqid owner perms used-bytes messages

------ Shared Memory Segments --------

key shmid owner perms bytes nattch status

0x00000000 327680 gganesh 600 524288 2 dest

0x00000000 229377 gganesh 600 16777216 2 dest

0x00000000 360450 gganesh 600 524288 2 dest

0x00000000 458755 gganesh 600 524288 2 dest

0x00000000 491524 gganesh 600 524288 2 dest

0x00000000 589829 gganesh 600 524288 2 dest

0x000004d2 720902 gganesh 666 2052 2

------ Semaphore Arrays --------

key semid owner perms nsems

RESULT:

The program is executed successfully.

linux-ipc-shared-memory's People

Contributors

gowriganeshns avatar kaviyashanmugam19 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.