sem_open中信號量命名


問題:
sem_open will failed with "No such file or directory"
 
解釋1:
這是由於在Linux內核中,創建信號量的默認路徑是/dev/shm。當你要創建一個信號量/tmp/mysem時,實際上是創建了一個/dev/shm /sem.tmp/mysem,而這里由於/dev/shm/tmp目錄根本就不存在,所以會出錯。
解決方法:
    *直接寫信號量文件的名字或宏定義不帶路徑,將會創建在/dev/shm中:sem_open("mysem", ...) 
 
解釋2:
 
If define name as "/tmp/xxxx", and invoke sem_open(name, flag...), sem_open will failed with "No such file or directory". 
Looked into the code sem_open.c, the reason is that the name ("/tmp/xxxx")will map to /dev/shm/sem.tmp/xxxx, but sem.tmp directory is not there, so there will be an error.
I think in sem_open.c, it maybe needs a check of whether name has more slash characters other than the leading slash character.
If it has, mkdir the related directory before rename operation (sem_open.c:234), such as the following:
  1. tmpname = strrchr(finalname,'/');// get the file name without directory
  2. strncpy(pathname, finalname, strlen(finalname)- strlen(tmpname));// get the directory path
  3. mkdir(pathname,0777)
To the POSIX std, it says the interpretation of slash characters other than the leading slash character in name is implementaion defined. I think it will be more friendly to allow the user using a name with directory.






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM