本文主要是介绍linux 进程间通信--systemV 共享内存 实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1:共享内存代码
#include <sys/shm.h>
#include <sys/types.h> /* Type definitions used by many programs */
#include <stdio.h> /* Standard I/O functions */
#include <stdlib.h> /* Prototypes of commonly used library functions, plus EXIT_SUCCESS and EXIT_FAILURE constants */
#include <unistd.h> /* Prototypes for many system calls */
#include <errno.h> /* Declares errno and defines error constants */
#include <string.h> /* Commonly used string-handling functions */
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <stddef.h> /* For definition of offsetof() */
#include <stdarg.h> /* For definition of offsetof() */
#include <limits.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h> /* Type definitions used by many programs */
#include <unistd.h> /* Prototypes for many system calls */
#include <signal.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <time.h>
#include <syslog.h>
void usageErr(const char *format, ...)
{va_list argList;fflush(stdout); /* Flush any pending stdout */fprintf(stderr, "Usage: ");va_start(argList, format);vfprintf(stderr, format, argList);va_end(argList);fflush(stderr); /* In case stderr is not line-buffered */exit(EXIT_FAILURE);
}static void printShmDS(const struct shmid_ds *ds)
{printf("Size: %ld\n", (long) ds->shm_segsz);printf("# of attached processes: %ld\n", (long) ds->shm_nattch);printf("Mode: %lo",(unsigned long) ds->shm_perm.mode);
#ifdef SHM_DESTprintf("%s", (ds->shm_perm.mode & SHM_DEST) ? " [DEST]" : "");
#endif
#ifdef SHM_LOCKEDprintf("%s", (ds->shm_perm.mode & SHM_LOCKED) ? " [LOCKED]" : "");
#endifprintf("\n");printf("Last shmat(): %s", ctime(&ds->shm_atime));printf("Last shmdt(): %s", ctime(&ds->shm_dtime));printf("Last change: %s", ctime(&ds->shm_ctime));printf("Creator PID: %ld\n", (long) ds->shm_cpid);printf("PID of last attach/detach: %ld\n", (long) ds->shm_lpid);
}
void showmemDs(int shmId)
{struct shmid_ds ds;memset(&ds,0,sizeof(struct shmid_ds));if (shmctl(shmId, IPC_STAT, &ds) == -1){perror("shmctl");}else{printShmDS(&ds);}
}
int shareMemChildAndParent(int argc, char *argv[])
{int childpid;int id;int i;int buf[10];char *ptr;int totalbytes = 0;if((childpid = fork ())==-1){perror("fork");exit(EXIT_FAILURE);}if(childpid==0){if ((id = shmget((key_t)12345,50*sizeof(char), IPC_CREAT)) == -1){perror("Failed to create shared memory segment");exit(EXIT_FAILURE);}showmemDs(id);if ((ptr = (char *)shmat(id, NULL, 0)) == NULL){if (shmctl(id, IPC_RMID, NULL) == -1)perror("Failed to remove memory segment");exit(EXIT_FAILURE);}for(i=0;argv[1][i]!='\0';i++){*ptr=argv[1][i];ptr++;}printf("this is child,write argv[1] to shm.\nyou input charater count is %d\n",i);sleep(5);exit(EXIT_SUCCESS);}else{wait(NULL);if ((id = shmget((key_t)12345, 50*sizeof(char), IPC_CREAT)) == -1){perror("Failed to create shared memory segment");exit(EXIT_FAILURE);}showmemDs(id);if ((ptr = (char *)shmat(id, NULL, 0)) == NULL){perror("shmat");if (shmctl(id, IPC_RMID, NULL) == -1)perror("Failed to remove memory segment");exit(EXIT_FAILURE);}printf("this is parent,input charater is %s\n",ptr);if (shmctl(id, IPC_RMID, NULL) == -1){perror("Failed to remove memory segment");exit(EXIT_FAILURE);}sleep(3);exit(EXIT_SUCCESS);}
}int main(int argc, char *argv[])
{shareMemChildAndParent(argc,argv);exit(EXIT_SUCCESS);
}
2:执行结果
这篇关于linux 进程间通信--systemV 共享内存 实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!