Question

how to send string from process to shared memory using c language?

how to send string from process to shared memory using c language?

Homework Answers

Answer #1

//writer process
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

int main()
{
  
key_t k = ftok("shmfile",65); //ftok to generate unique key

int shmid = shmget(k,1024,0666|IPC_CREAT);
char *str = (char*) shmat(shmid,(void*)0,0);

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

//reader process

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

  
int main()
{
key_t key = ftok("shmfile",65);
int shmid = shmget(key,1024,0666|IPC_CREAT);
char *str = (char*) shmat(shmid,(void*)0,0);

printf("Data read from memory: %s\n",str);
shmdt(str);
shmctl(shmid,IPC_RMID,NULL);

return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to convert Sudo Code to C language ? CRT display(){ //wait for signal to come...
How to convert Sudo Code to C language ? CRT display(){ //wait for signal to come from iprocess //check shared memory; If(shared memory full){ //take items from shared memory //printf(“the string”); }
Write a function to create a copy of a string with the prototype below. (C Language)...
Write a function to create a copy of a string with the prototype below. (C Language) char * strcpy (const char *); The function should dynamically allocate the necessary memory for the new string. You can do that with char * newstr = (char *) malloc(sizeof(char) * (strlen(str)+1)); assuming str is your input string. Think though how you would copy the characters of the string, and don't forget about the end of string character.
for c language What function is used to deallocate dynamic memory when it is no longer...
for c language What function is used to deallocate dynamic memory when it is no longer required by your program? In your answer give the name of the function and its syntax, and how the C runtime system knows how much memory to deallocate. Give an appropriate example showing typical usage of this function
Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need...
Client / Server using named pipes with thread and fork() in C/C++ **Note** You will need to write a client program and a server program for this question to be correct. ** **Cannot use socket** client the client application that will get the command from the user and pass the command to be executed from the command line, parses the command and puts a binary representation of the parse into a shared memory segment. At this point, the client will...
Write an Assembler Program to input a string from the keyboard, store the string in memory....
Write an Assembler Program to input a string from the keyboard, store the string in memory. Your program should check and display if the string contains vowel letters (A, E, I, O, U), Hint: your program should check both lower and upper case
C++ language 1. Programs with memory leaks a should be executed in a secure environment to...
C++ language 1. Programs with memory leaks a should be executed in a secure environment to prevent theft of data by hackers. b will eventually crash if allowed to execute for long periods of time. c should be compiled with a special compiler that detects and flags the memory leaks. d should be executed under an operating system that can dynamically plug the leaks. e none of these. 2. An  lvalue is a a value of type long. b a memory...
ow do you check if a string starts with a certain character in c programing language?...
ow do you check if a string starts with a certain character in c programing language? For example if a user puts in a word like cake how od i check if that word starts with c?
Assume a string is stored in memory using Hamming codes that may experience 1 bit errors...
Assume a string is stored in memory using Hamming codes that may experience 1 bit errors per 8 bit ASCII character (12 bit Hamming codes). write the string with explanation? The answer was given on another post which is; 010000110000: C 000001101000: h 000010100110: n 111000001011: r 011100100000: s 110001110011: ( 0000101100101: 00000000101 Please type the line by line explanation.
IN C++ The Catalan numbers are an integer sequence Cn that appears in tree-enumeration problems. The...
IN C++ The Catalan numbers are an integer sequence Cn that appears in tree-enumeration problems. The first Catalan numbers for n = 1, 2, 3, ... are 1, 2, 5, 14, 42, 132, .... A formula generating Cn is: Cn=(1÷(n+1))=(2n)!÷(n+1)!n! Design two programs that communicate with shared memory using the Win32 API as outlined in Section 8.7.2. The producer process will generate the Catalan sequence and write it to a shared memory object. The consumer process will then read and...
perform the following using Ruby language A DNA strand is represented by a string of the...
perform the following using Ruby language A DNA strand is represented by a string of the characters A, C, G, and T, each of which represents a nucleotide. Each nucleotide has its complement as indicated by this Ruby hash: NUCLEOTIDE_COMPLEMENT = { 'A' => 'T', 'T' => 'A', 'C' => 'G', 'G' => 'C' } The reverse-complement of a DNA string is a new string in which each nucleotide is replaced by its complement and the string is reversed. Reverse-complements...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT