Hi,
Unless I am missing some serious differences in Mac and linux in terms of C programming, I dont know why this would happen. Please take a look at the following piece of code fragment:
bool add_input_to_db(Cons *new_data) {
// Set the attributes of the lock
struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
int fd = open(LOGFILE, O_APPEND | O_SYNC | O_CREAT | O_WRONLY, 0600);
if (fd == -1) {
perror(LOGFILE);
return false;
}
// Get our PID
fl.l_pid = getpid();
// Return false on error
if (fcntl(fd, F_SETLKW, &fl) < 0){
perror("fcntl");
return false;
}
FILE *database = fdopen(fd, "a");
int result = write_questions(new_data, database, false);
// Set to unlock before closing the file
fl.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &fl) < 0){
perror("fcntl");
return false;
}
fclose(database);
return (bool) result;
}
This function is part of a bigger programs that takes in user input and logs it into a file. As you might see, I am trying to get an write lock on the logfile as soon as it opens and release the lock before the file is closed. When I run it on linux (ubuntu hardy), the program runs without any problems. However, when I run it on my mac (Leopard), I get:
fcntl: Invalid argument
If someone could explain why this would be, it'd be helpful.
Thanks.