|
#include <sys/types.h>
//#include <sys/ipc.h>
//#include <sys/sem.h>
#include <linux/sem.h>
#include <stdio.h>
#define SEM_NUM 10
#define SEM_MODE (IPC_CREAT|0660)
//void printf_stat(union semun *arg);
void changemode(int sid, char *mode);
void printfmode (union semun *arg);
void printfmode (union semun *arg)
{
printf("mode=%d:\n", arg->buf->sem_perm.mode);
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);
}
exit(0);
}
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;
}
[root@localhost linuxc]# gcc -o a40 a40.c
a40.c: In function `main':
a40.c:42: warning: passing arg 2 of `changemode' makes pointer from integer without a cast
[root@localhost linuxc]# ./a40
段错误
[root@localhost linuxc]#
a40.c:42 是 changemode(semid, 0600);
各位大哥,我哪里还需要啊修改呢?
谢谢! |
|