c++ - Ubuntu Shared Memory Between 2 Processes, Code Not Working -



c++ - Ubuntu Shared Memory Between 2 Processes, Code Not Working -

i'm writing 2 processes separately should interact through shared memory , can't spot i'm going wrong. first process has created shared memory , if reprogrammed, can read shared memory too.

however, sec output programme can't seem read @ all. utilize shmget in both programs create memory, bring together it. insert char array namearray input program, output should read shared memory , cout name.

any help appreciated, code below :)

input:

#include <iostream> #include <sys/types.h> #include <sys/shm.h> #include <sys/ipc.h> #define shm_key 982 using namespace std; main() { int shmid; char namearray[] = "bill gates"; unsigned length = sizeof(namearray)/sizeof(namearray[0]); shmid=shmget(shm_key,256,0777|ipc_creat); if(shmid!=(-1)) /*error checking measure ensure shared memory creation successful*/ { char * ptr=(char *)shmat(shmid,0,0); /*assigns char pointer start of shared memory*/ for(int i=0; i<length;i++) /*for every letter in namearray*/ { ptr=(char *)shmat(shmid,0,i); *ptr=namearray[i]; /*sends local char array shared memory*/ } } else /*displays error message given shared memory creation failue*/ { cout << "sorry, shared memory creation failed" } }

output:

#include <iostream> #include <sys/types.h> #include <sys/shm.h> #include <sys/ipc.h> #define shm_key 982 using namespace std; main() { int shmid; char namearray[20]; shmid=shmget(shm_key,256,0777|ipc_creat); if(shmid!=(-1)) /*error checking measure ensure shared memory creation successful*/ { char * ptr=(char *)shmat(shmid,0,0); for(int i=0; i<11;i++) { cout << *((char *)shmat(shmid,0,i)); } } else{} }

the code

for(int i=0; i<length;i++) /*for every letter in namearray*/ { ptr=(char *)shmat(shmid,0,i); *ptr=namearray[i]; /*sends local char array shared memory*/ }

is not doing think doing. every phone call shmat attaches whole shared memory segment address, different flags (ranging 0 length; of them meaningless).

remove shmat calls loop. need is

for(int i=0; i<length;i++) /*for every letter in namearray*/ { ptr[i]=namearray[i]; /*sends local char array shared memory*/ }

and similar alter in other program.

c++ ubuntu memory posix shared

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -