존재는하지만 당신은, (이 시스템 호출이기 때문에이 아마뿐만 아니라 다른 많은 언어에 존재) Ruby 및 C++에 flock
를 사용하여 "history.txt"에 대한 잠금을 배치하여 당신이 원하는 것을 성취 할 수 있어야한다 이 메서드를 사용하는 동안 발생할 수있는 몇 가지 gotchas 인 것 같습니다.
다음은 메소드 테스트에 사용한 코드입니다.
#include <iostream>
#include <fstream>
#include <sys/file.h>
int main()
{
FILE *h;
h = fopen("history.txt","a"); //open the file
std::cout << "Press enter to lock\n";
std::cin.get();
int hNum = fileno(h); //get the file handle from the FILE*
int rt = flock(hNum, LOCK_EX); //Lock it down!
std::cout << "Writing!"<<rt<<"\n";
fprintf(h,"Shoop da woop!\n");
std::cout << "Press enter to unlock\n";
std::cin.get();
rt = flock(hNum, LOCK_UN);
fflush(h);
fclose(h);
return 0;
}
이 두 가지 방법을 실행하면 C++ 프로세스가 잠겨 때 루비 프로세스가 중지 확인할 수 있습니다 : 여기
File.open("history.txt", "r+") do |file|
puts "before the lock"
file.flock(File::LOCK_EX)
puts "Locking until you press enter"
gets
puts file.gets
file.flock(File::LOCK_UN)
end
는 C++ 코드 : 여기
는 루비 코드 파일 및 그 반대로.