#include<apue.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
int main(int argc , char *argv[])
{
int fd = open("test.txt",O_WRONLY | O_CREAT | O_TRUNC);
int pid,status;
const char *str_for_parent = "str_for_parent\n";
const char *str_for_child = "str_for_child\n";
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0)
{
write(fd,str_for_child,strlen(str_for_child)+1);
_exit(0);
}
wait(&status);
write(fd,str_for_parent,strlen(str_for_parent)+1);
return 0;
}
The test.txt is created by open().But it’s permission(---------x) is different with those files(-rw-rw-r--) created by touch or any other softwares in my system.my umask is 0002
openactually takes an (optional) third parameter:The mode of the new file is set to the AND of
modeand the inverse of yourumask(ie,mode & ~umask). Since you’re not passingmode, it’s getting initialized to some garbage, and you’re getting the wrong mode as a result.Generally if you’re passing
O_CREAT, you should pass amodeof0666, unless you have some specific reason to use some other mode (eg, if you want to make sure the file is not readable by other users regardless of what the umask is set to).