Fix runtime bug. Tested on Linux

master
Kirigaya Kazuto 2017-12-27 20:50:01 +08:00
parent cdfb210052
commit 8f5f2ea1f7
1 changed files with 11 additions and 3 deletions

View File

@ -8,6 +8,9 @@
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <error.h>
#include <stdlib.h>
#include <vector>
using namespace std;
@ -81,12 +84,12 @@ ProcessSemaphore::ProcessSemaphore(int key, int action, int default_value) : _p(
if(action==1)
{
_p->fd=open(_p->buff,O_RDWR|O_CREAT|O_TRUNC|O_EXCL);
_p->fd=open(_p->buff,O_RDWR|O_CREAT|O_EXCL,0666);
_p->owner=true;
}
else
{
_p->fd=open(_p->buff,O_RDWR);
_p->fd=open(_p->buff,O_RDWR,0666);
}
if(_p->fd>=0) /// Opened.
@ -103,6 +106,10 @@ ProcessSemaphore::ProcessSemaphore(int key, int action, int default_value) : _p(
flock(_p->fd,LOCK_UN);
}
}
else
{
perror("Failed to open file. ");
}
if(_p->status!=0&&_p->status!=2) /// Clean up
{
@ -152,7 +159,7 @@ int ProcessSemaphore::p()
typedef void (*fnvi)(int);
_global_process_semaphore_value=0;
fnvi old=signal(SIGUSR1,_global_process_semaphore_handler);
//*/
*/
flock(_p->fd,LOCK_UN);
@ -162,6 +169,7 @@ int ProcessSemaphore::p()
while(1)
{
flock(_p->fd,LOCK_EX);
x=impl::LockInfo::getFrom(_p->fd);
if(x.current_value>0) /// Someone has released resource!
{
x.current_value=x.current_value-1;