关于sem_open的疑问

liuerhu31 2013-01-31 12:02:39
char name[20];
memset(name, 0, 20);
sprintf(name, "%s", "NGBOrder");
sem = sem_open(name, O_CREAT, 0644,1);

if(sem == SEM_FAILED){
NGB_LOGO("[NGBOrderInterface]sem_open failed:(%s)\n", strerror(errno));
return 0;
}
sem_getvalue(sem, &value_sem);
NGB_LOGO("[NGBOrderInterface] name:(%s) sem_getvalue :%d\n", name, value_sem);

上面是我的代码的一部分,运行的时候会打印如下。是在是找不出参数错在哪了。
[NGBOrderInterface] sem_open failed:(Invalid argument)
各位大神,麻烦帮忙帮忙分析一下。是在android上面搞V8扩展的。博通的环境。
...全文
859 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuerhu31 2013-03-06
  • 打赏
  • 举报
回复
这段时间比较忙,没来结贴。我改用互斥锁解决问题了。谢谢各位的解答,先结贴了,等有空在琢磨一下。
liuerhu31 2013-02-01
  • 打赏
  • 举报
回复
#include <fcntl.h> #include <sys/stat.h> #include <semaphore.h> #include <errno.h> #include <stdio.h> int main() { sem_t *semid; int value = 0; char name[20] = {0}; sprintf(name, "%s", "NGBORDER"); printf("name = %s\n", name); semid = sem_open(name, O_CREAT, 0644, 1); if(semid == SEM_FAILED) { printf("open error:(%s)\n", strerror(errno)); } else printf("open success\n"); sem_getvalue(semid, &value); printf("value = %d\n", value); return 0; } [root@localhost /]# vi test.c [root@localhost /]# gcc test.c -lrt -o mytest [root@localhost /]# ./mytest name = NGBORDER open success value = 1 我试了这段代码在red hat linux上面是可以运行的。奇怪了。
赵4老师 2013-02-01
  • 打赏
  • 举报
回复
0644有问题?改改再试?
whizer 2013-01-31
  • 打赏
  • 举报
回复
应该是sem_open参数有问题,你看看这个函数的参数原型和参数说明。
赵4老师 2013-01-31
  • 打赏
  • 举报
回复
建议网上搜相关例子代码。 The O_CREAT flag requires a third and a fourth argument: mode, which is of type mode_t, and value, which is of type unsigned. The semaphore is created with an initial value of value. Valid initial values for semaphores are less than or equal to {SEM_VALUE_MAX}. RETURN VALUE Upon successful completion, the sem_open() function shall return the address of the semaphore. Otherwise, it shall return a value of SEM_FAILED and set errno to indicate the error. The symbol SEM_FAILED is defined in the <semaphore.h> header. No successful return from sem_open() shall return the value SEM_FAILED. ERRORS If any of the following conditions occur, the sem_open() function shall return SEM_FAILED and set errno to the corresponding value: EINVAL The sem_open() operation is not supported for the given name, or O_CREAT was specified in oflag and value was greater than {SEM_VALUE_MAX}.
liuerhu31 2013-01-31
  • 打赏
  • 举报
回复
回1楼的兄弟:我就是没发现错在神马地方了 回2楼的哥们:我网上找了好多,你的这个英文的我之前也仔细看过。实在是没发现错在哪了。 由于小弟英文水平有限,有些读不懂,翻译工具翻译的也只是大概。如果能看出问题,麻烦不吝赐教,小弟在这先谢谢啦。
赵4老师 2013-01-31
  • 打赏
  • 举报
回复
SEM_OPEN Section: POSIX Programmer's Manual (P) Updated: 2003 -------------------------------------------------------------------------------- NAME sem_open - initialize and open a named semaphore (REALTIME) SYNOPSIS #include <semaphore.h> sem_t *sem_open(const char *name, int oflag, ...); DESCRIPTION The sem_open() function shall establish a connection between a named semaphore and a process. Following a call to sem_open() with semaphore name name, the process may reference the semaphore associated with name using the address returned from the call. This semaphore may be used in subsequent calls to sem_wait(), sem_trywait(), sem_post(), and sem_close(). The semaphore remains usable by this process until the semaphore is closed by a successful call to sem_close(), _exit(), or one of the exec functions. The oflag argument controls whether the semaphore is created or merely accessed by the call to sem_open(). The following flag bits may be set in oflag: O_CREAT This flag is used to create a semaphore if it does not already exist. If O_CREAT is set and the semaphore already exists, then O_CREAT has no effect, except as noted under O_EXCL. Otherwise, sem_open() creates a named semaphore. The O_CREAT flag requires a third and a fourth argument: mode, which is of type mode_t, and value, which is of type unsigned. The semaphore is created with an initial value of value. Valid initial values for semaphores are less than or equal to {SEM_VALUE_MAX}. The user ID of the semaphore is set to the effective user ID of the process; the group ID of the semaphore is set to a system default group ID or to the effective group ID of the process. The permission bits of the semaphore are set to the value of the mode argument except those set in the file mode creation mask of the process. When bits in mode other than the file permission bits are specified, the effect is unspecified. After the semaphore named name has been created by sem_open() with the O_CREAT flag, other processes can connect to the semaphore by calling sem_open() with the same value of name. O_EXCL If O_EXCL and O_CREAT are set, sem_open() fails if the semaphore name exists. The check for the existence of the semaphore and the creation of the semaphore if it does not exist are atomic with respect to other processes executing sem_open() with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set, the effect is undefined. If flags other than O_CREAT and O_EXCL are specified in the oflag parameter, the effect is unspecified. The name argument points to a string naming a semaphore object. It is unspecified whether the name appears in the file system and is visible to functions that take pathnames as arguments. The name argument conforms to the construction rules for a pathname. If name begins with the slash character, then processes calling sem_open() with the same value of name shall refer to the same semaphore object, as long as that name has not been removed. If name does not begin with the slash character, the effect is implementation-defined. The interpretation of slash characters other than the leading slash character in name is implementation-defined. If a process makes multiple successful calls to sem_open() with the same value for name, the same semaphore address shall be returned for each such successful call, provided that there have been no calls to sem_unlink() for this semaphore. References to copies of the semaphore produce undefined results. RETURN VALUE Upon successful completion, the sem_open() function shall return the address of the semaphore. Otherwise, it shall return a value of SEM_FAILED and set errno to indicate the error. The symbol SEM_FAILED is defined in the <semaphore.h> header. No successful return from sem_open() shall return the value SEM_FAILED. ERRORS If any of the following conditions occur, the sem_open() function shall return SEM_FAILED and set errno to the corresponding value: EACCES The named semaphore exists and the permissions specified by oflag are denied, or the named semaphore does not exist and permission to create the named semaphore is denied. EEXIST O_CREAT and O_EXCL are set and the named semaphore already exists. EINTR The sem_open() operation was interrupted by a signal. EINVAL The sem_open() operation is not supported for the given name, or O_CREAT was specified in oflag and value was greater than {SEM_VALUE_MAX}. EMFILE Too many semaphore descriptors or file descriptors are currently in use by this process. ENAMETOOLONG The length of the name argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}. ENFILE Too many semaphores are currently open in the system. ENOENT O_CREAT is not set and the named semaphore does not exist. ENOSPC There is insufficient space for the creation of the new named semaphore. The following sections are informative. EXAMPLES None. APPLICATION USAGE The sem_open() function is part of the Semaphores option and need not be available on all implementations. RATIONALE Early drafts required an error return value of -1 with the type sem_t * for the sem_open() function, which is not guaranteed to be portable across implementations. The revised text provides the symbolic error code SEM_FAILED to eliminate the type conflict. FUTURE DIRECTIONS None. SEE ALSO semctl() , semget() , semop() , sem_close() , sem_post() , sem_timedwait() , sem_trywait() , sem_unlink() , sem_wait() , the Base Definitions volume of IEEE Std 1003.1-2001, <semaphore.h> COPYRIGHT Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . --------------------------------------------------------------------------------

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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