-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockFile.cpp
56 lines (45 loc) · 1.12 KB
/
lockFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "lockFile.h"
#include <string.h>
#include <unistd.h>
LockFile :: LockFile ( char* nombre ) {
strcpy ( this->nombre, nombre );
this->fl.l_type = F_WRLCK;
this->fl.l_whence = SEEK_SET;
this->fl.l_start = 0;
this->fl.l_len = 0;
this->fl.l_pid = getpid ();
this->fd = open ( this->nombre, O_CREAT | O_WRONLY , 0777 );
this->closeFd = true;
}
LockFile::LockFile ( int fd ) {
this->fl.l_type = F_WRLCK;
this->fl.l_whence = SEEK_SET;
this->fl.l_start = 0;
this->fl.l_len = 0;
this->fl.l_pid = getpid ();
this->fd = fd;
this->closeFd = false;
}
int LockFile :: tomarLock () {
this->fl.l_type = F_WRLCK;
int resultado = fcntl ( this->fd,F_SETLKW,&(this->fl) );
return resultado;
}
int LockFile :: liberarLock () {
this->fl.l_type = F_UNLCK;
int resultado = fcntl ( this->fd,F_SETLK,&(this->fl) );
return resultado;
}
int LockFile :: escribir ( const char* buffer,int buffsize ) {
lseek ( this->fd,0,SEEK_END );
int resultado = write ( this->fd,buffer,buffsize );
return resultado;
}
LockFile :: ~LockFile () {
if( this->closeFd ) {
close ( this->fd );
}
}
int LockFile::getFd() {
return this->fd;
}