linux 處理序間通訊–systemV 共用記憶體 執行個體

來源:互聯網
上載者:User
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:執行結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.