#Trying to share memory between 2 C files
9 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#define BUF_SIZE 1024
#define SHM_KEY 0x1234
struct shmseg {
int cnt;
int new;
int complete;
char buf[BUF_SIZE];
};
int main(int argc, char *argv[]) {
int shmid, numtimes;
struct shmseg *shmp;
char *bufptr;
int spaceavailable;
shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0666|IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
return 1;
}
// Attach to the segment to get a pointer to it.
shmp = shmat(shmid, NULL, 0);
if (shmp == (void *) -1) {
perror("Shared memory attach");
return 1;
}
/***********************************************************
* write code here to contiuesly get message from user so the logserver can log the information to log file
* unless user give the input to quit the process.
* ********************************************************/
do{
printf("Type in something. type quit to exit the program: ");
scanf("%s", shmp->buf);
shmp->new = 1;
numtimes++;
} while (strcmp(shmp->buf, "quit") != 0);
printf("Writing Process: Wrote %d times\n", numtimes);
/*****************************************************************
this is code to quit the process, and terminate the logserver program
***********************************************************************/
shmp->complete = 1;
/*************************************************************
write the code to detach the share memory from the process
*******************************************************/
if (shmdt(shmp) == -1) {
perror("shmdt");
return 1;
}
printf("Writing Process: Complete\n");
return 0;
}```
thats the first file. its the log client file
This is the second file:
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#define BUF_SIZE 1024
#define SHM_KEY 0x1234
struct shmseg {
int cnt;
int new;
int complete;
char buf[BUF_SIZE];
};
int main(int argc, char *argv[]) {
int shmid;
struct shmseg *shmp;
FILE *file_e3;
file_e3 = fopen("log_file.log", "a+");
if (file_e3 == NULL) {
perror("Error");
return 1;
}
shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0666|IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
return 1;
}
// Attach to the segment to get a pointer to it.
shmp = shmat(shmid, NULL, 0);
if (shmp == (void *) -1) {
perror("Shared memory attach");
return 1;
}
/* Transfer blocks of data from shared memory to log file
if the message from client set shmp->complete ==1, the we log server will end its operation
if the message from the client is new, then the message should be log in the log file
*/
while(shmp->complete != 1)
{
while (shmp->new == 1) {
fwrite(shmp->buf, sizeof(char), strlen(shmp->buf), file_e3);
// after the message send to file, put the shmp->new to false
shmp->new = 0;
}
}
fclose(file_e3);
/*************************************************************
write the code to detach the share memory from the process
*******************************************************/
if (shmdt(shmp) == -1) {
perror("shmdt");
return 1;
}
/*********************************************************
write the code to mark the shared memory to be destroyed
*********************************************************/
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl");
return 1;
}
return 0;
}```
this is the server part
Nah, it is working. You probably won't see any log file contents unless the client types 'quit' and closes the file. You could to a fflush after every fwrite and then you would see it.
Thank you. I got it to work 🙂
!solved