linux下进程间通信实例,linux 进程间通信--systemV 共享内存 实例

#include

#include /* Type definitions used by many programs */

#include /* Standard I/O functions */

#include /* Prototypes of commonly used library functions, plus EXIT_SUCCESS and EXIT_FAILURE constants */

#include /* Prototypes for many system calls */

#include /* Declares errno and defines error constants */

#include /* Commonly used string-handling functions */

#include

#include

#include

#include /* For definition of offsetof() */

#include /* For definition of offsetof() */

#include

#include

#include

#include

#include /* Type definitions used by many programs */

#include /* Prototypes for many system calls */

#include

#include

#include

#include

#include

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值