LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 961|回复: 1

求助:我这个内存共享的小程序,编译没错,运行有错,请各位大哥帮帮我,谢谢!

[复制链接]
发表于 2006-7-14 10:44:40 | 显示全部楼层 |阅读模式
include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define BUF_SIZE 1024
#define MYKEY 24

int main(void)
{
int shmid;
char *shmptr;

if(shmid=shmget(MYKEY, BUF_SIZE, IPC_CREAT)==-1) //开辟
{
printf("shmget error! \n");
exit(1);
}

if((shmptr=shmat(shmid,0,0))==( void*)-1) //附加
{
fprintf(stderr,"shmat error!\n");
exit(1);
}

while(1)
{
printf("string: %s \n", shmptr);
sleep(3000);
}

exit(0);
}

[root@localhost linuxc]# gcc -o a24 a24.c
[root@localhost linuxc]# ./a24
shmat error!

各位大哥,这是什么原因啊?

谢谢!
 楼主| 发表于 2006-7-14 15:41:22 | 显示全部楼层
上面这个问题解决了:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>

#define SEM_NUM 10
#define SEM_MODE(IPC_CREAT|0660)
//void printf_stat(union semun *arg);

void printfmode (union semun *arg)
{
            printf("  mode=%d:\n", arg->buf->sem_perm.mode);     
            //return;
}


void changemode(int sid, char *mode)
{
            int rc;
            union semun semopts;
            struct semid_ds mysemds;

            semopts.buf=&mysemds;
            rc=semctl(sid,0,  IPC_STAT, semopts);

            if(rc==-1)
            {
                printf("semctl error!\n");
                exit(1);
            }

            sscanf(mode,"%ho", &semopts.buf->sem_perm.mode);
            semctl(sid,0,  IPC_SET, semopts);

    //        return;
}


int main(void)
{
                int semid;
          union semun semopts;
    struct semid_ds semds;
   
          if ((semid=semget(IPC_PRIVATE, SEM_NUM, SEM_MODE))==-1)
             {
                     fprintf(stderr, "semget error!\n");
                     exit(1);
              }

          semopts.buf=&semds;
          if(semctl(semid,0,IPC_STAT,semopts)==-1)
             {
                      fprintf(stderr, "get semid_ds error!\n");
                       exit(1);
              }
             
          printfmode(&semopts);

          changemode(semid, 0600);
          if(semctl(semid,0, IPC_STAT, semopts)==-1)
              {
                      fprintf(stderr, "get semid_ds error!\n");
                      exit(1);
               }
    printfmode(&semopts);

          if(semctl(semid,0, IPC_RMID, 0)<0)
             {
                      fprintf(stderr,"semctl error\n");
                      exit(1);
             }
         return 1;
       
}       


[root@localhost linuxc]# gcc -o a29 a29.c
a29.c:7:27: "|" may not appear in macro parameter list
a29.c:10: warning: `union semun' declared inside parameter list
a29.c:10: warning: its scope is only this definition or declaration, which is probably not what you want
a29.c: In function `printfmode':
a29.c:12: dereferencing pointer to incomplete type
a29.c: In function `changemode':
a29.c:20: storage size of `semopts' isn't known
a29.c: In function `main':
a29.c:42: storage size of `semopts' isn't known
a29.c:45: `SEM_MODE' undeclared (first use in this function)
a29.c:45: (Each undeclared identifier is reported only once
a29.c:45: for each function it appears in.)
a29.c:60: warning: passing arg 2 of `changemode' makes pointer from integer without a cast
a29.c:75:2: warning: no newline at end of file


多谢各位大哥了!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表