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 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...
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 {...
I am given this starter code and I am suppose to debug it and all of...
I am given this starter code and I am suppose to debug it and all of that and it will not work after I change ">" to "<=" and then i'm not sure after that what else I have to do. Could someone help me solve this? // Start with a penny // double it every day // how much do you have in a 30-day month? public class DebugSix1 {    public static void main(String args[])    {      ...
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...
Needs to be coded in Python. Hello i am working on a project for my computer...
Needs to be coded in Python. Hello i am working on a project for my computer programming course and i am having trouble with one part. The code needs to be able to produce two versions of an output given inputs by the user. for instance. Here is the code i have to produce the following output. The input from the user are num1 num2 and asc which is just asking if they want the output to be ascending or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT