#include #include #include using std::cout; int lock(const char * name) { char * namedotlck = new char[strlen(name)+4]; strcpy(namedotlck, name); strcpy(namedotlck+strlen(name), ".lck"); int status=link(name, namedotlck); delete[] namedotlck; return status+1; } int unlock(const char * name) { char * namedotlck = new char[strlen(name)+4]; strcpy(namedotlck, name); strcpy(namedotlck+strlen(name), ".lck"); int status=unlink(namedotlck); delete[] namedotlck; return status+1; } int main() { while (true) { //wait to obtain a lock while (!lock("somefile")); int fd=open("somefile", O_WRONLY | O_APPEND); write(fd, "hello", 6); write(fd, "bye", 4); close(fd); //release lock unlock("somefile"); } }