Question
C/Ubuntu: Shared Memory: Customize the following two codes for server and client so that the server creates a 2D 5x5 array and initializes it while
C/Ubuntu: Shared Memory:
Customize the following two codes for server and client so that the server creates a 2D 5x5 array and initializes it while the client code finds the minimum and maximum element in that array through shared memory.
shm_server.c
#include
#include
#include
#include
#define SHMSZ 27
main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0)
{
perror("shmget");
exit(1);
}
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
while (*shm != '*')
sleep(1);
exit(0);
}
shm_client.c
#include
#include
#include
#include
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, SHMSZ, 0666)) < 0)
{
perror("shmget");
exit(1);
}
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}
for (s = shm; *s != NULL; s++)
putchar(*s);
putchar(' ');
*shm = '*';
shmdt(shm);
shmctl(shmid, IPC_RMID, NULL);
exit(0);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started