OK, so I’m supposed to use the creat() function in C++ with UNIX to create 5 files and only 5 files. I then want to test to see if those files are in use by using creat() on them again. My professor states that when creat() makes a new file, it is open for writing, however, when it truncates an existing file, it fails if there is no write permission. This allows a file to be used as a semaphore.
Here is my code:
#include
#include
#include
#include
#include
using namespace std;
int main() {
int fd, fd2;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
char *filename = “Fork1.txt”;
char *filename2 = “Fork1.txt”;
fd = creat(“Fork1.txt”, mode);
fd2 = creat(“Fork1.txt”, mode);
cout << "Test" << endl << fd << endl << fd2 << endl;
return 0;
}
The second creat() should return -1 as an error because the file already exists, but it is returning a successful value…please help! Thx.