Question

In: Computer Science

A) Declare 'buffer as string.'a' and 'b' as integers.Set the value for 'a' is 100. B)...

A) Declare 'buffer as string.'a' and 'b' as integers.Set the value for 'a' is 100.

B) Write a program to find the address location for buffer 'a' and 'b' and three segments in the process memory.

thank you.

pls answer with proper explaination

Solutions

Expert Solution

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

The problem with pipes, FIFO and message queue – is that for two processes to exchange information. The information has to go through the kernel.

A total of four copies of data are required (2 reads and 2 writes). 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.

SYSTEM CALLS USED ARE:

ftok(): is used to generate a unique key.

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

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.

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

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

  • The server reads from the input file.
  • The server writes this data in a message using either a pipe, FIFO or message queue.
  • 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.
  • Finally, the data is copied from the client’s buffer.

#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("a",100);

   // shmget returns an identifier in shmid
   int a = shmget(key,100|IPC_CREAT);

   // shmat to attach to shared memory
   char *b = (char*) shmat(a,(void*)0,0);

   cout<<"Write Data : ";
   gets(b);

   printf("Data written in memory: %s\n",b);
  
   //detach from shared memory
   shmdt(b);

   return 0;
}


Related Solutions

A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String variable called myString1 and initialize it to "Programming". C) Declare a String variable called myString2 and initialize it to "Language". D) Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".". E) Print the sum of the lengths of myString1 and myString2 (must call the length() method). F) Print the 2nd, 4th, and 7th character...
Declare a string variable and initialize it with your first name ( in C++)
Declare a string variable and initialize it with your first name ( in C++)
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
To each of 100 mL of three (A, B & C) 0.1 M acetate buffer solutions...
To each of 100 mL of three (A, B & C) 0.1 M acetate buffer solutions (pH of A = 4.4, pH of B = 4.76 and pH of C = 5.2), l0 mL of 0.1 M NaOH solution was added. Calculate the resulting change in the pH and the buffering capacity (AOH- /ApH) of each solution. Calculate also the change in the pH if instead of NaOH, 0.1 M HCI was used.
1. Tris buffer a. 100 mM Tris-HCl, pH 8.0 b. 100 mM Tris-HCl, pH 6.0 Make...
1. Tris buffer a. 100 mM Tris-HCl, pH 8.0 b. 100 mM Tris-HCl, pH 6.0 Make this buffer by dissolving an appropriate amount (which you must calculate using the formula weight) of solid Tris in APPROXIMATELY 80 mL of water. Use the Henderson-Hasselbalch equation to calculate how much 1 M HCl is needed to achieve the final pH?
Calculate the weight of buffer needed to make 100 mL of a 0.1 M buffer of...
Calculate the weight of buffer needed to make 100 mL of a 0.1 M buffer of acetate (MW=136.1g/mol)
How format string vulnerabilities can be exploited for buffer overflow attacks?
How format string vulnerabilities can be exploited for buffer overflow attacks?
Create a class tuck shop with following data members: String Owner String Food_Items[100] Double Price [100]...
Create a class tuck shop with following data members: String Owner String Food_Items[100] Double Price [100] Int Quantity [100]                Note: All arrays are open ended. It is not necessary that they are completely filled. All three arrays will work in synchronization with each other i-e the item at index 0 will have its price in Price array at index 0 and quantity at index 0 of Quantity array. Methods: Two Constructors (default, four-argument) set for Owner , get for...
1. Calculate the buffer capacity for 100 mL of buffer if 12.02 mL of 0.09978 M...
1. Calculate the buffer capacity for 100 mL of buffer if 12.02 mL of 0.09978 M NaOH was used to change the pH by 0.54 units. a. 0.022 b. 0.00022 c.12.22 d.0.22 2. Calculate the pH of a buffer with 0.10 M benzoic acid and 0.10 M sodium benzoate. Use activity coefficient table 8-1 in the (9th edition) textbook to determine the activities for the ions (benzoate ion =C6H5CO2¯). The pKa of benzoic acid is 4.20. a. 4.20 b. 4.28...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT