Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160464
  • 博文数量: 83
  • 博客积分: 3956
  • 博客等级: 中校
  • 技术积分: 663
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-24 16:29
文章分类

全部博文(83)

文章存档

2010年(83)

我的朋友

分类: C/C++

2010-10-14 16:15:19

原码如下:

#include
#include
#include
#include
#include

#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);
}
阅读(353) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~