Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Exemplos de memória compartilhada

From Wiki
Jump to: navigation, search
Article Metadata

Por kyllercg
Última alteração feita por hamishwillee em 09 Dec 2011

Memória compartilhada

O código a seguir mostra como usar memória compartilhada.

#include <sys/shm.h>
#include <string.h>
#include <pthread.h>
pthread_t threadid;
#define SHM_KEY 1000
void* ShmReadThread(void* arg)
int shmid = shmget(SHM_KEY, 1024, IPC_CREAT);
char* ch = (char*)shmat(shmid, 0, 0);
char name[10];
strncpy(name, ch, 3);
// nome armazenado "abc"
return 0;
void CreateThreadL()
pthread_create(&threadid,(pthread_attr_t *)NULL,ShmReadThread, NULL);
int main()
char* ch;
int shmid = shmget(SHM_KEY, 1024, IPC_CREAT);
int threadRetVal = 0;
if( shmid == -1)
return -1;
ch = (char*)shmat(shmid, 0, 0);
if((int)ch == -1)
shmctl(shmid, IPC_RMID, NULL);
return -1;
strcpy(ch, "abc");
pthread_join(threadid, (void**)threadRetVal);
shmctl(shmid, IPC_RMID, NULL);
return 0;

Links relacionados

This page was last modified on 9 December 2011, at 04:24.
62 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.