In: Computer Science
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 :)
#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.