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_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:执行结果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

家有工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值