For some reason, flock(fd, LOCK_UN) returns 0 and the file is still locked from the second iteration on. So the first time, the file gets unlocked by the write section and the read section can lock it successfully. When i = 1, the write section can't unlock the file anymore and the lock of the read section fails.
I edited open() now as follows.
fd = open(filename, O_WRONLY | O_APPEND| O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
After I did, every lock and unlock works. Those parameters give read, write and execute permissions to the user. So I am guessing that after the deletion of the file, something goes wrong. I now added perror() to every check and during the second iteration (i = 1), the lock of the read section fails with "Bad file descriptor". Can someone explain this to me?
// Tell the headers to use the POSIX extension
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <errno.h>
#define POLL_WAIT_TIME_S 2
const char testString[] = "Test test test\n";
int main() {
int fd; // Create file descriptor variable
char *buff = (char *) calloc(255, sizeof(char)); // Create buffer for read()
char* filename = "testfile"; // Set filename
int status;
while(1) {
// Create a file and write a text into it
fd = open(filename, O_WRONLY | O_APPEND| O_CREAT); // Create file descriptor
printf("%d\n", fd);
status = flock(fd, LOCK_EX); // Lock file using file descriptor
if(status == 0)
printf("\"%s\" locked successfully by write process!\n", filename);
else {
printf("\"%s\" not locked successfully by write process!\n", filename);
perror("Error: ");
}
size_t charsWritten = write(fd, testString, sizeof(testString)); // Write to file using file descriptor
printf("%ld characters written to \"%s\" \n", charsWritten, filename);
if(status == 0) {
status = flock(fd, LOCK_UN);
if(status == 0)
printf("\"%s\" unlocked by write process!\n", filename);
else {
printf("\"%s\" could not be unlocked by write process!\n", filename);
perror("Error: ");
}
}
close(fd); // Close file descriptor
// Open a file and read its content
fd = open(filename, O_RDONLY);
status = flock(fd, LOCK_EX); // Lock file using file descriptor
if(status == 0) {
printf("\"%s\" locked successfully by read process!\n", filename);
printf("\nReading file content: \n");
size_t sz = read(fd, buff, (size_t) sizeof(testString));
buff[sz] = '\0';
printf("%s", buff); // Print buffer content to the screen
printf("\n");
}
else {
printf("\"%s\" not locked successfully by read process!\n", filename);
perror("Error: ");
}
if(status == 0) {
status = flock(fd, LOCK_UN);
if(status == 0)
printf("\"%s\" unlocked by read process!\n", filename);
else {
printf("\"%s\" could not be unlocked by read process!\n", filename);
perror("Error: ");
}
}
close(fd);
// Delete the file
if(remove(filename) == 0)
printf("\"%s\" deleted successfully!\n", filename);
else {
printf("Unable to delete %s\n", filename);
perror("Error: ");
}
printf("\n");
sleep(POLL_WAIT_TIME_S);
}
free(buff);
return 0;
}