systemV 信号灯问题

dungeonsnd 2011-04-13 02:31:45
仿照(Unix网络编程 第二卷第二版)的第11章SystemV信号灯 写了下面的程序,运行出现问题,请问原因,谢谢!
编译和运行都可以进行,但是设置信号灯集中的各信号灯值时显示信号灯集中的信号灯数目为nsems=4436512,显示值各信号灯值时显示信号灯数目为nsems=-1075867592,当然也没有设置成功了。 而我创建时明明创建成功了,设置成3个。
没有找到redhat下的原因,特请教。

代码比较长,不过去掉一些处理性的真正有用的只有不到十行,请耐心查看帮忙,谢谢!


代码1: semcreate.c

#include "../LinuxComm.h" //自己写的包含头文件.

int main(int argc , char ** argv)
{
int c,oflag,semid,nsems;
oflag =IPC_EXCL| IPC_CREAT;
printf("Program start...\n");

while( (c=getopt(argc,argv,"e"))!=-1 )
{
switch(c)
{
case 'e': oflag |=IPC_EXCL;
break;
}
}
if(optind!=argc-2)
{
printf("Usage: semcreate [-e] <pathname> <nsems> ,Program will exit...\n");
exit(2);
}
printf("optind=%d, argc=%d\n",optind,argc);

nsems =atoi( argv[optind+1] );
printf("nsems=%d\n",nsems);
semid =semget( ftok(argv[optind],0) , nsems , oflag );

if(-1==semid)
{
printf("semget Failed,errno=%d, Program will exit...\n",errno);
exit(2);
}

printf("Program will exit...\n");
exit(0);
}



代码2: semsetvalues.c

#include "../LinuxComm.h"

int
main(int argc, char **argv)
{
int semid, nsems, i;
struct semid_ds seminfo;
unsigned short *ptr;
union semun arg;

printf("Program start...\n");

if (argc < 2)
{
printf("usage: semsetvalues <pathname> [ values ... ]\n");
exit(0);
}

/* 4first get the number of semaphores in the set */
semid = semget(ftok(argv[1], 0), 0, 0);
arg.buf = &seminfo;
semctl(semid, 0, IPC_STAT, arg);
nsems = arg.buf->sem_nsems;
printf("nsems=%d\n",nsems);

/* 4now get the values from the command line */
if (argc != nsems + 2)
{
printf("%d semaphores in set, %d values specified\n", nsems, argc-2);
exit(0);
}

/* 4allocate memory to hold all the values in the set, and store */
ptr = calloc(nsems, sizeof(unsigned short));
arg.array = ptr;
for (i = 0; i < nsems; i++)
ptr[i] = atoi(argv[i + 2]);
semctl(semid, 0, SETALL, arg);

printf("Program will exit...\n");
exit(0);
}




代码3: semgetvalues.c

#include "../LinuxComm.h"

int
main(int argc, char **argv)
{
int semid, nsems, i;
struct semid_ds seminfo;
unsigned short *ptr;
union semun arg;

printf("Program start...\n");

if (argc < 2)
{
printf("usage: semgetvalues <pathname>\n");
exit(0);
}

/* 4first get the number of semaphores in the set */
semid = semget(ftok(argv[1], 0), 0, 0);
if(-1==semid)
{
printf("semget Failed,errno=%d, Program will exit...\n",errno);
exit(2);
}
else
printf("semget succeeded!\n");

arg.buf = &seminfo;
semctl(semid, 0, IPC_STAT, arg);
nsems = arg.buf->sem_nsems;

printf("nsems=%d\n",nsems);

/* 4allocate memory to hold all the values in the set */
ptr = calloc(nsems, sizeof(unsigned short));
arg.array = ptr;

/* 4fetch the values and print */
semctl(semid, 0, GETALL, arg);
for (i = 0; i < nsems; i++)
printf("semval[%d] = %d\n", i, ptr[i]);

printf("Program will exit...\n");
exit(0);
}




运行结果



[pro@rhel5 ~/lnxpro/sysvsema 02:46:57]$l
total 64
-rw-rw-r-- 1 pro pro 473 Apr 13 00:39 makefile
-rwxrwxr-x 1 pro pro 5893 Apr 13 02:43 semcreate
-rw-r--r-- 1 pro pro 732 Apr 13 02:42 semcreate.c
-rwxrwxr-x 1 pro pro 5876 Apr 13 02:43 semgetvalues
-rw-r--r-- 1 pro pro 976 Apr 13 02:43 semgetvalues.c
-rwxrwxr-x 1 pro pro 5871 Apr 13 00:44 semops
-rw-r--r-- 1 pro pro 971 Apr 13 00:44 semops.c
-rwxrwxr-x 1 pro pro 5291 Apr 13 00:39 semrmid
-rw-r--r-- 1 pro pro 325 Apr 13 00:35 semrmid.c
-rwxrwxr-x 1 pro pro 5824 Apr 13 02:10 semsetvalues
-rw-r--r-- 1 pro pro 968 Apr 13 02:10 semsetvalues.c
[pro@rhel5 ~/lnxpro/sysvsema 02:46:58]$./semcreate /tmp.path 3
Program start...
optind=1, argc=3
nsems=3
Program will exit...
[pro@rhel5 ~/lnxpro/sysvsema 02:47:07]$./semsetvalues /tmp.path
Program start...
nsems=4436512
4436512 semaphores in set, 0 values specified
[pro@rhel5 ~/lnxpro/sysvsema 02:47:19]$./semgetvalues /tmp.path
Program start...
semget succeeded!
nsems=-1075867592
Program will exit...
[pro@rhel5 ~/lnxpro/sysvsema 02:47:25]$

...全文
87 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dungeonsnd 2011-05-11
  • 打赏
  • 举报
回复
晕! 今天再把这几个程序拿出来运行,竟然正确了! 汗!
虚拟机没变。

运行结果如下。

创建:


[root@rhel5 /home/pro/lnxpro/sysvsema 04:20:26]#ll
total 64
-rwxr-x--- 1 pro pro 473 Apr 13 00:39 makefile
-rwxr-x--- 1 pro pro 5893 Apr 13 02:43 semcreate
-rwxr-x--- 1 pro pro 732 Apr 13 02:42 semcreate.c
-rwxr-x--- 1 pro pro 5876 Apr 13 02:43 semgetvalues
-rwxr-x--- 1 pro pro 976 Apr 13 02:43 semgetvalues.c
-rwxr-x--- 1 pro pro 5871 Apr 13 00:44 semops
-rwxr-x--- 1 pro pro 971 Apr 13 00:44 semops.c
-rwxr-x--- 1 pro pro 5291 Apr 13 00:39 semrmid
-rwxr-x--- 1 pro pro 325 Apr 13 00:35 semrmid.c
-rwxr-x--- 1 pro pro 5824 Apr 13 02:10 semsetvalues
-rwxr-x--- 1 pro pro 968 Apr 13 02:10 semsetvalues.c
[root@rhel5 /home/pro/lnxpro/sysvsema 04:20:27]#ipcs

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

------ Semaphore Arrays --------
key semid owner perms nsems

------ Message Queues --------
key msqid owner perms used-bytes messages

[root@rhel5 /home/pro/lnxpro/sysvsema 04:20:30]#./semcreate -e /sysvsema 4
Program start...
optind=2, argc=4
nsems=4
Program will exit...
[root@rhel5 /home/pro/lnxpro/sysvsema 04:20:55]#ipcs

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

------ Semaphore Arrays --------
key semid owner perms nsems
0xffffffff 32768 root 0 4

------ Message Queues --------
key msqid owner perms used-bytes messages

[root@rhel5 /home/pro/lnxpro/sysvsema 04:20:58]#




设定systemV的信号灯集中的各信号灯的值,然后再获取这些值。

[root@rhel5 /home/pro/lnxpro/sysvsema 04:21:40]#./semsetvalues /sysvsema 123 456 0 1
Program start...
nsems=4
Program will exit...
[root@rhel5 /home/pro/lnxpro/sysvsema 04:22:01]#./semgetvalues /sysvsema
Program start...
semget succeeded!
nsems=4
semval[0] = 123
semval[1] = 456
semval[2] = 0
semval[3] = 1
Program will exit...
[root@rhel5 /home/pro/lnxpro/sysvsema 04:22:14]#



可以发现运行正确无误!

此时查看ipc资源情况!(顶楼当时运行时没有查看ipc资源情况,不知道是不是因为当时运行环境下ipc资源有问题!)

[root@rhel5 /home/pro/lnxpro/sysvsema 04:22:14]#ipcs

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

------ Semaphore Arrays --------
key semid owner perms nsems
0xffffffff 32768 root 0 4

------ Message Queues --------
key msqid owner perms used-bytes messages

[root@rhel5 /home/pro/lnxpro/sysvsema 04:24:14]#






dungeonsnd 2011-04-13
  • 打赏
  • 举报
回复
有没有谁写过 systemV的信号灯的程序,给我一个小小的例子也好,但是一定要是在redhat下测试过正确的,网上一大堆是不正常的例子。
初学linux ipc, (unix网络编程)的例子 ~汗!
dungeonsnd@126.com
dungeonsnd 2011-04-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 braveyly 的回复:]

此内核是否还支持SystemV的信号等操作?
[/Quote]

支持吧,可以创建成功(创建没有提示失败),但是设置值时好像有问题。
念茜 2011-04-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 justkk 的回复:]
像你说的,代码太长,没看完
[/Quote]


too
braveyly 2011-04-13
  • 打赏
  • 举报
回复
此内核是否还支持SystemV的信号等操作?
dungeonsnd 2011-04-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 justkk 的回复:]

像你说的,代码太长,没看完
[/Quote]

关键调用代码没几行呀

我在rhel5下使用 posix的信号灯可以,各种测试都正常。
但是system5的这个问题没解决。
不知道常用的是哪种同步方式。 比如 oracle、apache、mysql等等这些软件使用的同步方式。
justkk 2011-04-13
  • 打赏
  • 举报
回复
像你说的,代码太长,没看完
dungeonsnd 2011-04-13
  • 打赏
  • 举报
回复
指定值运行还是不对,
请教,谢谢!

[pro@rhel5 ~/lnxpro/sysvsema 02:58:18]$l
total 64
-rw-rw-r-- 1 pro pro 473 Apr 13 00:39 makefile
-rwxrwxr-x 1 pro pro 5893 Apr 13 02:43 semcreate
-rw-r--r-- 1 pro pro 732 Apr 13 02:42 semcreate.c
-rwxrwxr-x 1 pro pro 5876 Apr 13 02:43 semgetvalues
-rw-r--r-- 1 pro pro 976 Apr 13 02:43 semgetvalues.c
-rwxrwxr-x 1 pro pro 5871 Apr 13 00:44 semops
-rw-r--r-- 1 pro pro 971 Apr 13 00:44 semops.c
-rwxrwxr-x 1 pro pro 5291 Apr 13 00:39 semrmid
-rw-r--r-- 1 pro pro 325 Apr 13 00:35 semrmid.c
-rwxrwxr-x 1 pro pro 5824 Apr 13 02:10 semsetvalues
-rw-r--r-- 1 pro pro 968 Apr 13 02:10 semsetvalues.c
[pro@rhel5 ~/lnxpro/sysvsema 02:58:18]$./semcreate /tmp.test 4
Program start...
optind=1, argc=3
nsems=4
Program will exit...
[pro@rhel5 ~/lnxpro/sysvsema 02:58:30]$./semsetvalues /tmp.test 4 5 6 7
Program start...
nsems=4436512
4436512 semaphores in set, 4 values specified
[pro@rhel5 ~/lnxpro/sysvsema 02:58:45]$./semgetvalues /tmp.test
Program start...
semget succeeded!
nsems=-1081802104
Program will exit...
[pro@rhel5 ~/lnxpro/sysvsema 02:58:53]$

dungeonsnd 2011-04-13
  • 打赏
  • 举报
回复
忘了说了,我的环境是:
VMWare 7.1中运行 RHEL5, Linux kernel 2.6.18.

23,216

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧