그 중에서 공유되는 자원이 변수일 수 도 있고 file일 수도 있다.
공유되는 file에 대해서 일관성을 유지하기 위해서 시스템 api
상호배제 개념이 적용되어야한다.
공유자원에 한번에 하나만 접근해야한다.
조금더 mutual exclusion 효율적으로 하는법
읽기만 할때에는 기다릴 필요없다 // 동일한 exclusive를 적용할 수 있다. shared lock
쓰기를 위한 process는 완전히 exclusive하게
db나 다른 process는 read가 많아서 성능향상 할 수 잇다.
Writer's lock(Exclusive lock)
Reader's lock(Shared lock)
reader가 lock하고 잇는 reader가 접근하면 같이 write면 lock
#include <fcntl.h>
int fcntl(int fildes, int cmd, struct flock *lock)
file에 대해서 exclusive lock, shared lock 형태로 설정하면서 동기화 하는 것 특정영역에 대해 설정 |
fildes : file descriptor cmd : command – F_GETLK : check if the lock can be acquired 현재 file에 어떤 lock이 걸려있나 » if already locked by someone, return a filled lock structure lock 걸려있으면 *lock에 data를 가져옴 » if it can be acquired, return a lock structure with F_UNLCK (l_type) lock을 얻을 수 있다면 lock이 안걸렸다는 정보를 가지는 F_UNLCK data strcut를 반환한다. – F_SETLK : try to set lock as designated in lock argument. *lock에 기재된 해당 파일의 특정 부분에 lock을 시도한다. » called with l_type == F_RDLCK or F_WRLCK » if already locked, return -1 immediately » on an error, errno = EACCESS or EAGAIN lock : data struct - F_SETLKW lock이 걸려있는 상태에서 다른 누군가 그 lock을 unlock할때까지 기다린다. » blocking version for F_SETLK » if already locked, the calling process must be blocked until it can get the lock. – F_SETLK and l_type == F_UNLCK » release a lock designated by the flock structure |
return ? / -1(이미 lock) / 0 |
struct flock {
.......
short l\_type; // type of lock: F\_RDLCK, F\_WRLCK, F\_UNLCK
// F\_RDLCK : reader’s lock
// F\_WRLCK : writer’s lock
// F\_UNLCK : unlock
short l\_whence; // SEEK\_SET, SEEK\_CUR, SEE\_END
off\_t l\_start; // lock start position
off\_t l\_len; // lock region length (0 for entire file)
pid\_t l\_pid: // pid of a process that has a lock // (used only in F\_GETLK)
}
file 전체에 lock을 경우법 |
fd : file descriptor operation : – LOCK_SH: place a shared lock / – LOCK_EX: place an exclusive lock – LOCK_UN: remove an existing lock held by this process – can be OR’d with LOCK_NB (non-blocking) |
return |
Lock interitance & release
-Inheritance
fork()에서 child를 생성하더라도 부모의 lock은 상속되지 않는다.
exec의 경우 기존의 lock이 inheritance 된다.
-Release
process가 종료될 때 모든 lock이 해제된다.
특정 file의 descriptor를 닫을 때 file에 대한 모든 lock이 해제된다. kernel의 table에서 해제한다.
fd1 fd2 해제 fd1 해제 x, fd2 해제
file에 대해서 lock을 걸 수 있는 system call을 살펴보았다.
process에서 lock을 걸어서 사용할 경우 별도의 process가 lock을 걸지 않고 접근을 한다면 ?
default로 lock을 걸지 않고 접근을 한다면 접근을 할 수 있다. -> advisory locking
- Advisory locking
- 다른 것들이 접근을 하려고 할 때 막을 수 있는 방법이 없다.
- Mandatory locking
- locking protocol이 kernel에서 적용된다.
- 그래서 어떤 R/W 접근 할 수 없다.
- aber overhead가 많이들어서 기본적으로 전자를 많이 씀
- 방법 : group id에 대해서 manadaroy locking을 setting해야한다.
'시스템 프로그래밍' 카테고리의 다른 글
시스템 프로그램 9장 - Semaphore (0) | 2020.06.10 |
---|---|
시스템 프로그래밍 8장 - Memory 1 (0) | 2020.06.10 |
시스템 프로그래밍 7장 - Thread : Condition variable (0) | 2020.05.06 |
시스템 프로그래밍 6장 - Thread : Synchronization (0) | 2020.05.06 |
시스템 프로그래밍 6장 - Thread : Cancellation (0) | 2020.05.06 |