Question

In: Computer Science

how to accept string from shared memory using c language ?

how to accept string from shared memory using c language ?

Solutions

Expert Solution

Inter Process Communication through shared memory is a concept where two or more process can access the common memory & communication is done via this shared memory where changes made by one process can be viewed by another process.

Steps:-

1. Server reads from the input file.
2. The server writes this data in a message using either a pipe, fifo or message queue.
3. The client reads the data from the IPC channel,again requiring the data to be copied from kernel’s IPC buffer to the client’s buffer.
4. Finally the data is copied from the client’s buffer.
A total of four copies of data are required (2 read and 2 write). So, shared memory provides a way by letting two or more processes share a memory segment. With Shared Memory the data is only copied twice – from input file into shared memory and from shared memory to the output file.

Some concepts used are:-

1. ftok(): is use to generate a unique key.

2. shmget(): int shmget(key_t,size_tsize,intshmflg); upon successful completion, shmget() returns an identifier for the shared memory segment.

3. shmat(): Before you can use a shared memory segment, you have to attach yourself to it using shmat(). void *shmat(int shmid ,void *shmaddr ,int shmflg);

shmid is shared memory id. shmaddr specifies specific address to use but we should set it to zero and OS will automatically choose the address.

4. shmdt(): When you’re done with the shared memory segment, your program should
detach itself from it using shmdt(). int shmdt(void *shmaddr);

5. shmctl(): when you detach from shared memory,it is not destroyed. So, to destroy shmctl() is used. shmctl(int shmid,IPC_RMID,NULL);

Program of Shared Memory for Writer Process:-

#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

using namespace std;

int main()
{

// ftok to generate unique key

key_t key = ftok("shmfile",65);

// shmget returns an identifier in shmid

int shmid = shmget(key,1024,0666|IPC_CREAT);

// shmat to attach to shared memory

char *str = (char*) shmat(shmid,(void*)0,0);

  cout<<"Write Data : ";

gets(str);

   printf("Data written in memory: %s\n",str);

//detach from shared memory  

shmdt(str);

return 0;
}

Program of Shared Memory for Reader Process:-

#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

using namespace std;

int main()
{

// ftok to generate unique key

key_t key = ftok("shmfile",65);

  // shmget returns an identifier in shmid

int shmid = shmget(key,1024,0666|IPC_CREAT);

    // shmat to attach to shared memory

char *str = (char*) shmat(shmid,(void*)0,0);

printf("Data read from memory: %s\n",str);

//detach from shared memory  

shmdt(str);

  // destroy the shared memory

shmctl(shmid,IPC_RMID,NULL);

   return 0;
}


Related Solutions

How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory If(shared memory not full) { //sendthe string to the shared memory //send signal to process; }
Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword...
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword Cipher algorithm. Note: The encryption only works on alphabets. Numbers and symbols such as 1 to 9, -, &, $ etc remain unencrypted. Input:     Zombie Here     secret     where: First line represents the unencrypted string S. Second line represents the keyword string K. Output:     ZLJEFT DTOT Explanation: We used "secret" keyword there. Plain Text: A B C D E F G H I J K...
Write in C++ programming language Given the following postfix string, from left to right, use the...
Write in C++ programming language Given the following postfix string, from left to right, use the stack push operation to put items on the stack and the pop operation whenever an operator is encountered and to get two items and apply the operator to the operands then push back the result to evaluate the expression … rewrite the entire stack as it appears before each operator is applied. 12 3  / 5  + 32 2  3  ^  2  *  /  -
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a pointer to an int variable. It computes the length of the string and updates the int variable with the length. stringCopy() - Copies one string onto another using pointers #include<stdio.h> #define MAX_STR_LEN 2048 void stringLength(char *str, int *len) { // This function computes the length of the string // at the address in the pointer *str. Once the length // has been determined, it...
In C++ Create two functions called TheNumber. One version of TheNumber should accept a string and...
In C++ Create two functions called TheNumber. One version of TheNumber should accept a string and output the accepted string 10 times. The other version of TheNumber should accept a double and output the accepted double 10 times. This uses function overloading.
Using either your own C-string functions of Lab 7.1 or the ones from the standrd C++...
Using either your own C-string functions of Lab 7.1 or the ones from the standrd C++ cstring library, create a String class, which — while having minimal functionality — illustrates the use of and need for the canonical form. Overview Here is the .h file for the class (note the class name — String with a capital S; trying to force the use of the classname string was to much of an issue: class String { friend std::ostream &operator <<(std::ostream...
using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes...
using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes from user and show how best fit algorithm allocates them.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT