共享内存控制程序示例
时间:2010-10-14 来源:aaron_xueli
原码如下:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 4000
#define MALLOC_SIZE 10000
#define SHM_SIZE 10000
#define SHM_MODE (SHM_R | SHM_W)
char array[ARRAY_SIZE];
void printf_shmpemid(struct shmid_ds *buf)
{
printf(" struct ipc_perm:\n");
printf(" uid=%d:\n", buf->shm_perm.uid);
printf(" gid=%d:\n", buf->shm_perm.gid);
printf(" cuid=%d:\n", buf->shm_perm.cuid);
printf(" cgid=%d:\n", buf->shm_perm.cgid);
return;
}
int main(void)
{
int shmid;
char *ptr, *shmptr;
struct shmid_ds shmds;
printf("array[] from %x to %x\n",
(unsigned int)&array[0], (unsigned int)&array[ARRAY_SIZE]);
printf("stack around %x \n", (unsigned int)&shmid);
if (NULL == (ptr = malloc(MALLOC_SIZE))) {
fprintf(stderr, "malloc error\n");
exit(1);
}
if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE)) <0) {
fprintf(stderr, "shmget error!\n");
exit(1);
}
if ((shmptr = shmat(shmid, 0, 0)) == (void *) -1) {
fprintf(stderr, "shmat error!\n");
exit(1);
}
if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
fprintf(stderr, "GET shmid_ds ERROR!\n");
exit(1);
}
printf_shmpemid(&shmds);
if (shmctl(shmid, IPC_RMID, 0) < 0) {
fprintf(stderr, "shmctl error\n");
exit(1);
}
exit(0);
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 4000
#define MALLOC_SIZE 10000
#define SHM_SIZE 10000
#define SHM_MODE (SHM_R | SHM_W)
char array[ARRAY_SIZE];
void printf_shmpemid(struct shmid_ds *buf)
{
printf(" struct ipc_perm:\n");
printf(" uid=%d:\n", buf->shm_perm.uid);
printf(" gid=%d:\n", buf->shm_perm.gid);
printf(" cuid=%d:\n", buf->shm_perm.cuid);
printf(" cgid=%d:\n", buf->shm_perm.cgid);
return;
}
int main(void)
{
int shmid;
char *ptr, *shmptr;
struct shmid_ds shmds;
printf("array[] from %x to %x\n",
(unsigned int)&array[0], (unsigned int)&array[ARRAY_SIZE]);
printf("stack around %x \n", (unsigned int)&shmid);
if (NULL == (ptr = malloc(MALLOC_SIZE))) {
fprintf(stderr, "malloc error\n");
exit(1);
}
if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE)) <0) {
fprintf(stderr, "shmget error!\n");
exit(1);
}
if ((shmptr = shmat(shmid, 0, 0)) == (void *) -1) {
fprintf(stderr, "shmat error!\n");
exit(1);
}
if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
fprintf(stderr, "GET shmid_ds ERROR!\n");
exit(1);
}
printf_shmpemid(&shmds);
if (shmctl(shmid, IPC_RMID, 0) < 0) {
fprintf(stderr, "shmctl error\n");
exit(1);
}
exit(0);
}
相关阅读 更多 +