当前位置: 技术问答>linux和unix
共享内存的问题 请高手指教
来源: 互联网 发布时间:2016-01-16
本文导语: extern "C" { #include #include #include #include #include #include #include #include #include } void usage(int argc, char** argv) { printf("%s argumentsn", argv[0]); prin...
extern "C" {
#include
#include
#include
#include
#include
#include
#include
#include
#include
}
void usage(int argc, char** argv)
{
printf("%s argumentsn", argv[0]);
printf("-h help mode.n");
printf("-c create shm.n");
printf("-m write message into shm.n");
printf("-r read message from shm.n");
printf("-d destroy shm.n");
}
typedef enum {SHM_UNKNOW = 0, SHM_CREATE, SHM_DESTROY, SHM_WRITE, SHM_READ} SHM_MODE;
#define SHM_KEY 1234
#define SHM_SIZE 1000
/**IPC_ALLOC is solaris specific
*/
#if !defined( IPC_ALLOC )
#define IPC_ALLOC IPC_CREAT
#endif
/** create shm with SHM_KEY
* @return shmid
*/
int shm_create();
/** open shm with SHM_KEY
* @return shmid
*/
int shm_open();
/** destroy shm which shm key is SHM_KEY
*/
void shm_destroy(int shmid);
/** write a string into shm, the string should be teminated by null.
*/
void shm_write(int, char*);
/** read message from shm
@param the shmid
*/
void shm_read(int shmid);
/** detach from shm
*/
void shm_close(const void* root);
int main(int argc, char** argv)
{
SHM_MODE shm_mode = SHM_UNKNOW;
char* message = NULL;
int shmid = 0;
int opt = 0 ;
extern int optind ;
extern char* optarg ;
while ((opt = getopt (argc, argv, "hcrm:d")) != EOF) {
switch (opt) {
case 'h' : // help mode
usage(argc, argv);
return 0;
case 'c' : //create mode
shm_mode = SHM_CREATE;
break;
case 'd' : //destroy mode
shm_mode = SHM_DESTROY;
break;
case 'r': //read message
shm_mode = SHM_READ;
break;
case 'm': //write message
shm_mode = SHM_WRITE;
message = (char*)optarg;
break;
default :
usage(argc, argv);
return -1;
}
}
switch (shm_mode){
case SHM_CREATE:
shmid = shm_create();
break;
case SHM_DESTROY:
shmid = shm_open();
shm_destroy(shmid);
break;
case SHM_READ:
shmid = shm_open();
shm_read(shmid);
break;
case SHM_WRITE:
shmid = shm_open();
shm_write(shmid, message);
break;
default:
usage(argc, argv);
return -1;
}
}
int shm_create()
{
int shmid = shmget (SHM_KEY, SHM_SIZE, IPC_CREAT | 0666);
if(shmid
#include
#include
#include
#include
#include
#include
#include
#include
#include
}
void usage(int argc, char** argv)
{
printf("%s argumentsn", argv[0]);
printf("-h help mode.n");
printf("-c create shm.n");
printf("-m write message into shm.n");
printf("-r read message from shm.n");
printf("-d destroy shm.n");
}
typedef enum {SHM_UNKNOW = 0, SHM_CREATE, SHM_DESTROY, SHM_WRITE, SHM_READ} SHM_MODE;
#define SHM_KEY 1234
#define SHM_SIZE 1000
/**IPC_ALLOC is solaris specific
*/
#if !defined( IPC_ALLOC )
#define IPC_ALLOC IPC_CREAT
#endif
/** create shm with SHM_KEY
* @return shmid
*/
int shm_create();
/** open shm with SHM_KEY
* @return shmid
*/
int shm_open();
/** destroy shm which shm key is SHM_KEY
*/
void shm_destroy(int shmid);
/** write a string into shm, the string should be teminated by null.
*/
void shm_write(int, char*);
/** read message from shm
@param the shmid
*/
void shm_read(int shmid);
/** detach from shm
*/
void shm_close(const void* root);
int main(int argc, char** argv)
{
SHM_MODE shm_mode = SHM_UNKNOW;
char* message = NULL;
int shmid = 0;
int opt = 0 ;
extern int optind ;
extern char* optarg ;
while ((opt = getopt (argc, argv, "hcrm:d")) != EOF) {
switch (opt) {
case 'h' : // help mode
usage(argc, argv);
return 0;
case 'c' : //create mode
shm_mode = SHM_CREATE;
break;
case 'd' : //destroy mode
shm_mode = SHM_DESTROY;
break;
case 'r': //read message
shm_mode = SHM_READ;
break;
case 'm': //write message
shm_mode = SHM_WRITE;
message = (char*)optarg;
break;
default :
usage(argc, argv);
return -1;
}
}
switch (shm_mode){
case SHM_CREATE:
shmid = shm_create();
break;
case SHM_DESTROY:
shmid = shm_open();
shm_destroy(shmid);
break;
case SHM_READ:
shmid = shm_open();
shm_read(shmid);
break;
case SHM_WRITE:
shmid = shm_open();
shm_write(shmid, message);
break;
default:
usage(argc, argv);
return -1;
}
}
int shm_create()
{
int shmid = shmget (SHM_KEY, SHM_SIZE, IPC_CREAT | 0666);
if(shmid