#include #include #include using namespace std; int main(int argc, char **argv) { if (argc <= 1) { cout << "Please provide path to a file" << endl; return 1; } const auto filepath = argv[1]; cout << "Testing with " << filepath << endl; int fd = open(filepath, O_RDWR); cout << "Got new file descriptor " << fd << endl; struct flock lock; lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; int code = fcntl(fd, F_SETLK, &lock); cout << "Lock set: " << (code == 0) << endl; int fd2 = open(filepath, O_RDONLY); cout << "Second file descriptor " << fd2 << endl; char c = 'x'; int count = read(fd2, &c, 1); cout << "Read from second fd: " << c << " count: " << count << endl; int fd3 = open(filepath, O_WRONLY|O_APPEND); cout << "Third file descriptor " << fd3 << endl; c = 'o'; count = write(fd3, &c, 1); cout << "Wrote to third fd: " << count << endl; }