Question: distruibuted program... okay so sim having trouble with my: Both programs are suppose attach to the same shared memory segment. The consumer reads the message,

distruibuted program... okay so sim having trouble with my:
Both programs are suppose attach to the same shared memory segment.
The consumer reads the message, modifies it (e.g., converts it to uppercase), and then writes the modified message back into the shared memory. please attach photo of the teminals showing the output:
my consusmesr files: #include
#include
#include
#include
#include
#include
#include "shm_com.h"
void to_uppercase(char *str){
while (*str){
*str = toupper((unsigned char)*str);
str++;
}
}
int main(){
void *shared_memory =(void *)0;
struct shared_use_st *shared_stuff;
int shmid;
shmid = shmget((key_t)1234, sizeof(struct shared_use_st),0666| IPC_CREAT);
if (shmid ==-1){
fprintf(stderr, "shmget failed
");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid,(void *)0,0);
if (shared_memory ==(void *)-1){
fprintf(stderr, "shmat failed
");
exit(EXIT_FAILURE);
}
printf("Memory attached at %p
", shared_memory);
shared_stuff =(struct shared_use_st *)shared_memory;
int running =1;
while (running){
if (shared_stuff->written_by_you){
printf("Received message: %s", shared_stuff->some_text);
// Modify the message
to_uppercase(shared_stuff->some_text);
printf("Modified message to uppercase: %s", shared_stuff->some_text);
shared_stuff->written_by_you =0;
if (strncmp(shared_stuff->some_text, "END", 3)==0){
running =0;
}
} else {
sleep(1);
}
}
if (shmdt(shared_memory)==-1){
fprintf(stderr,"shmdt failed
");
exit(EXIT_FAILURE);
}
// Delete the shared memory segment
exit(EXIT_SUCCESS);
}
and here is my producer file:
#include
#include
#include
#include
#include
#include "shm_com.h"
int main(){
int running =1;
void *shared_memory =(void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
int shmid;
shmid = shmget((key_t)1234, sizeof(struct shared_use_st),0666| IPC_CREAT);
if (shmid ==-1){
fprintf(stderr, "shmget failed
");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid,(void *)0,0);
if (shared_memory ==(void *)-1){
fprintf(stderr, "shmat failed
");
exit(EXIT_FAILURE);
}
printf("Memory attached at %p
",(int)shared_memory);
shared_stuff =(struct shared_use_st *)shared_memory;
while(running){
while(shared_stuff->written_by_you ==1){
sleep(1);
printf("waiting for client...
");
}
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer, TEXT_SZ);
shared_stuff->written_by_you =1;
if (strncmp(buffer, "end", 3)==0){
running =0;
}
}
if (shmdt(shared_memory)==-1){
fprintf(stderr,"shmdt failed
");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!