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 having difficulty with these questions. Please provide answers to all parts and show...
Hello, I am having difficulty with these questions. Please provide answers to all parts and show some work so I can learn. Thank you so much in advance! :) Suppose that you began a one-year study of tuberculosis (TB) in a subsidized housing community in the Lower East Side of New York City on January 1st, 2016. You enrolled 500 residents in your study and checked on their TB status on a monthly basis. At the start of your study...
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 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...
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...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
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...
Please show work! I have the answers to these parts, but I am having trouble working...
Please show work! I have the answers to these parts, but I am having trouble working through the problem to find the answer. Find the solution of the IVP: d.) 5y" - y' = 0, y(0) = -1, y'(0) = -1 (answer: y(t)=4 - 5 et/5) e.) 3y" - y = 0, y(0) = 3, y(t) is bounded for 0 <= t < infinity (answer: y = 3 exp(-(1/3)sqrt(3)t) ) g.) y" + 2y' + y = 0, y(0) =...
Hello, I have created the following code in C++. The goal of this code is to...
Hello, I have created the following code in C++. The goal of this code is to read 3 types of employee information(String/*Name*/, String/*Phone number*/,Int/*Office number*/, ) from a .txt file and store that information into an array which can be accessed by various functions. The code below is within a header file, and im trying to define some functions (within a .cpp file) of my class to carry out the following tasks. I have already managed to define a couple...
I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT