I am trying to understand the memory usage of my program excluding the contribution of custom made API that creates and attaches to shared memory segment.
Following is the API code. I have included reasonably significant local memory allocation in the API.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
#define MAX_RECORDS 259200
typedef struct {
double Dummy_d ;
unsigned long int Dmmy_ui8;
}DummyStruct ;
int AttachToShmApi(key_t key,char *ErrMsg) {
int LoopCount = 0;
// Local memory declaration
DummyStruct DummyRecord[MAX_RECORDS];
// Initialize local memory
for(LoopCount=0;LoopCount<MAX_RECORDS;++LoopCount) {
DummyRecord[LoopCount].Dummy_d = 0.0 ;
DummyRecord[LoopCount].Dmmy_ui8 = 0.0 ;
}
printf("\nSize of DummyRecord : %lu",sizeof(DummyRecord));
// shmget returns an identifier in shmid
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
if (shmid == -1) {
sprintf(ErrMsg,"AttachToShmApi : Shared memory creation problem");
return -1 ;
}
char *shmp;
// shmat to attach to shared memory
shmp = (char*)shmat(shmid, NULL, 0);
if (shmp == (void *) -1) {
sprintf(ErrMsg,"AttachToShmApi : Shared memory attachment problem");
return -1 ;
}
cout<<endl<<"Shared memory created with id : "<<shmid<<endl<<endl;
return 1;
}
Now the program that invokes the API to attach to the shared memory segments.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
using namespace std;
int AttachToShmApi(key_t,char *);
int main(int argc,char *argv[])
{
if ( argc < 5 ) {
cout<<endl<<"Usage : "<<argv[0]<<" Key1(numeric)"<< " Key2(numeric)"<< " Key3(numeric)"<< " Key4(numeric)"<<endl<<endl;
exit(EXIT_FAILURE);
}
key_t key ;
char ErrMsg[200];
memset(ErrMsg,0,sizeof(ErrMsg));
key = key_t(atoi(argv[1]));
if ( AttachToShmApi(key,ErrMsg) <= 0 ) {
exit(EXIT_FAILURE);
}
cout<<endl<<"Attached to 1st shared memory segment!!"<<endl;
key = key_t(atoi(argv[2]));
if ( AttachToShmApi(key,ErrMsg) <= 0 ) {
exit(EXIT_FAILURE);
}
cout<<endl<<"Attached to 2nd shared memory segment!!"<<endl;
key = key_t(atoi(argv[3]));
if ( AttachToShmApi(key,ErrMsg) <= 0 ) {
exit(EXIT_FAILURE);
}
cout<<endl<<"Attached to 3rd shared memory segment!!"<<endl;
key = key_t(atoi(argv[4]));
if ( AttachToShmApi(key,ErrMsg) <= 0 ) {
exit(EXIT_FAILURE);
}
cout<<endl<<"Attached to 4th shared memory segment!!"<<endl;
while(1) {
sleep(1);
}
return 0;
}
I am trying to attach to 4 shared memory segments.
./ShmApiConnector3.exe 1500 2500 3500 4500
Size of DummyRecord : 4147200
Shared memory created with id : 196626
Attached to 1st shared memory segment!!
Size of DummyRecord : 4147200
Shared memory created with id : 196628
Attached to 2nd shared memory segment!!
Size of DummyRecord : 4147200
Shared memory created with id : 196629
Attached to 3rd shared memory segment!!
Size of DummyRecord : 4147200
Shared memory created with id : 196633
Attached to 4th shared memory segment!!
Following is the summarised output of pmap command.
mapped: 17944K writeable/private: 4288K shared: 16K
As we can see 16k shared output is contributed by 1 page ( 4096 bytes ) each of 4 shared memory segments.
Now I have written another program to independently attach to 4 shared memory segments.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
int main(int argc,char *argv[])
{
if ( argc < 5 ) {
cout<<endl<<"Usage : "<<argv[0]<<" Key1(numeric)"<< " Key2(numeric)"<< " Key3(numeric)"<< " Key4(numeric)"<<endl<<endl;
exit(EXIT_FAILURE);
}
key_t key ;
int shmid ;
key = key_t(atoi(argv[1]));
// shmget returns an identifier in shmid
shmid = shmget(key, 1024, 0644 | IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
exit(EXIT_FAILURE);
}
char* str1;
// shmat to attach to shared memory
str1 = (char*)shmat(shmid, (void*)0, 0);
if (str1 == (void *) -1) {
perror("Shared memory not attached!!");
exit(EXIT_FAILURE);
}
cout<<endl<<"Attached to 1st shared memory segment!!"<<endl;
key = key_t(atoi(argv[2]));
// shmget returns an identifier in shmid
shmid = shmget(key, 1024, 0644 | IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
exit(EXIT_FAILURE);
}
char* str2;
// shmat to attach to shared memory
str2 = (char*)shmat(shmid, (void*)0, 0);
if (str2 == (void *) -1) {
perror("Shared memory not attached!!");
exit(EXIT_FAILURE);
}
cout<<endl<<"Attached to 2nd shared memory segment!!"<<endl;
key = key_t(atoi(argv[3]));
// shmget returns an identifier in shmid
shmid = shmget(key, 1024, 0644 | IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
exit(EXIT_FAILURE);
}
char* str3;
// shmat to attach to shared memory
str3 = (char*)shmat(shmid, (void*)0, 0);
if (str3 == (void *) -1) {
perror("Shared memory not attached!!");
exit(EXIT_FAILURE);
}
cout<<endl<<"Attached to 3rd shared memory segment!!"<<endl
key = key_t(atoi(argv[4]));
// shmget returns an identifier in shmid
shmid = shmget(key, 1024, 0644 | IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
exit(EXIT_FAILURE);
}
char* str4;
// shmat to attach to shared memory
str4 = (char*)shmat(shmid, (void*)0, 0);
if (str4 == (void *) -1) {
perror("Shared memory not attached!!");
exit(EXIT_FAILURE);
}
cout<<endl<<"Attached to 4th shared memory segment!!"<<endl;
while(1) {
sleep(1);
}
return 0;
}
Following is the summarised output of pmap command.
mapped: 14012K writeable/private: 356K shared: 16K
The difference between 2 approach is the presence of API in one approach.
My doubt is why writeable/private: 4288K
, when API is used but writeable/private: 356K
when API is not used ?
Test environment.
Linux soumajit-HP-Pavilion-Desktop-590-p0xxx 5.4.0-71-generic #79~18.04.1-Ubuntu SMP Thu Mar 25 05:45:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux