Question

In: Computer Science

Hello, I am working on a C-program that deals with memory mapping, Please show all code...

Hello,

I am working on a C-program that deals with memory mapping, Please show all code (code must be in the C) and outputs. The instructions are as follows

INSTRUCTIONS:

You have two sample c codes, one for the client and one for the server. IF the client makes a change in the shared memory, those changes will show on the server side. Your task is to adapt the change strategy to develop two applications so they can send messages from one application to another, like a chat application. You do not need to concern the user interface for this application. You can just use Linux standard input and display (terminals as chart window)

GIVEN CODE FOR CLIENT:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#defince SHSIZE 100

int main (int argc, char * argv[])
{
  
   int shmid;
   key_t key;
   char shm;
   char *s;
  
   key = 9876;
  
   shmid = shmget(key, SHSIZE, IPC_CREATE | 0666);
   id(shmid < ))
   {
       perror("shmget");
       exit(1);
   }
  
   shm =shmat(shmid, NULL,0);
  
   if(shm == (char *) -1)
   {
       perror("shmat");
       exit(1);
   }
  
  
   for(s = shm; * != 0; s++)
       printf("%c", *s);
  
   printf("\n");
   *shm = "*";
  
  
   return 0;
}

GIVEN CODE FOR SERVER:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#defince SHSIZE 100

int main (int argc, char * argv[])
{
  
   int shmid;
   key_t key;
   char shm;
   char *s;
  
   key = 9876;
  
   shmid = shmget(key, SHSIZE, IPC_CREATE | 0666);
   id(shmid < ))
   {
       perror("shmget");
       exit(1);
   }
  
   shm =shmat(shmid, NULL,0);
  
   if(shm == (char *) -1)
   {
       perror("shmat");
       exit(1);
   }
  
   memcpy(shm, "hello world", 11);
  
   s = shm,
   s+=11;
   *s = 0;
   while(*shm != '*')
       sleep(1);
  
   return 0;
      
  
}

Thank you in advance :)

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>


#define SHSIZE 100

int shmid;
int e = 0;
char *shm ;

void shmInit()
{
    key_t key;
    key = 9876;
    shmid = shmget(key, SHSIZE, 0666|IPC_CREAT);
    shm = (char*)shmat(shmid, NULL,0);
    *shm = 0;
}

void shmWrite(char *str)
{
    while(*shm != 0)
   {
       printf("Waiting to write. Sleeping...\n");
       sleep(1);
   }
   memcpy (shm, str, strlen(str));

}


void UI()
{
    printf("This is client Application. It will write in shared memory.\n");
    printf("Enter exit to close this program.\n");
    char *input = (char*) malloc(SHSIZE*sizeof(char));
    do
    {
        memset(input, 0, SHSIZE);
        scanf("%[^\n]%*c", input);
        shmWrite(input);
    } while (strcmp(input, "exit") != 0);
    
}

int main (int argc, char * argv[])
{
    shmInit();
    UI();
    return 0;
   
}

Save the above code in client.c

This will write in share memory.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>


#define SHSIZE 100

int shmid;
int e = 0;
char *shm ;

void shmInit()
{
    key_t key;
    key = 9876;
    shmid = shmget(key, SHSIZE, 0666|IPC_CREAT);
    shm = (char*)shmat(shmid, NULL,0);
    *shm = 0;
}

char* shmRead()
{
    char *str = (char*)malloc(SHSIZE * sizeof(char));
    
   strcpy(str, shm);
   
   if(strlen(str)>0)
    {
        memset(shm, 0, strlen(shm));
    }
   
    return str;
}

void UI()
{
    
    printf("This is server Application. It will read from shared memory.\n");
    char *msg = NULL;
    do
    {
        msg = shmRead();

        if(strlen(msg)>0)
        {
            printf("OTHER :-> %s\n",(msg));
        }

        sleep(1);
    } while (strcmp(msg, "exit") != 0);
    
}

int main (int argc, char * argv[])
{
    shmInit();
    UI();
    return 0;
   
}

Save the above code in Server.c

run both program

if you find this answer useful, give thumbs up.

if you have any query, ask in comment.

On your demand.


Related Solutions

Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
Hello, I am working on an assignment but I am unsure of how to solve it....
Hello, I am working on an assignment but I am unsure of how to solve it. Please help me. The assignment details are below. Consider this scenario: Your friend starts a website, nothingbutflags.com, which is not making money. Your friend asks you to help generate more traffic. You ask your friend how much traffic the website had last month? And your friend replies and says only 500 visits. You also ask how many flags did you sell? Your friend replies...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
Hello all, I am toward the beginning of my semseter and am still pretty rusty working...
Hello all, I am toward the beginning of my semseter and am still pretty rusty working with code. here is my following goal: basically create a structure and populate an array from a txt file which contains 98 45.70 72 15.0 12 0.0 56 43.26 83 123.0 28 931.96 123 12.38 "Create a struct named familyFinance that contains 2 members: ▪ int acctNos ▪ float balance o (prior to the while loop) Create an array financeAry (size 10), of type...
I am working on a C++ program, where a user puts in a notation a playing...
I am working on a C++ program, where a user puts in a notation a playing card and the output is the full name of the card.(ex: KH = King of Hearts) I have most of it working but I want to have an error come up when the user puts in the wrong info and then loop back to the original question. Am I setting it up wrong? Pasted below is my code #include<iostream> #include<string> using namespace std; int...
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
Hello I am needing an example of how to write an assembly (MIPS) code that with...
Hello I am needing an example of how to write an assembly (MIPS) code that with will ask the user for two numbers then for addition or multiplication by typing in + or * into the command prompt. For example if I type in the number 2 and 5 then + The code should add the sum between the two numbers like 2 + 3 + 4 + 5 = 14. If multiplication is implemented then it will do the...
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you! TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested. Solution Description: Write a program that calculates the final balance in an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT