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_DEST printf("%s", (ds->shm_perm.mode & SHM_DEST) ? " [DEST]" : "");#endif#ifdef SHM_LOCKED printf("%s", (ds->shm_perm.mode & SHM_LOCKED) ? " [LOCKED]" : "");#endif printf("\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:執行結果