简体   繁体   English

在Unix中“创建”系统调用

[英]“creat” System Call in Unix

I am using creat system call to creat a file. 我正在使用creat系统调用来创建一个文件。 The following is the program to creat a file 以下是创建文件的程序

#include<stdio.h>
#include<fcntl.h>
void main()
{
    int fd=creat("a.txt",S_IRWXU|S_IWUSR|S_IRGRP|S_IROTH);
    printf("fd  = %d\n",fd);
}

So, At first time, the program creates a file named a.txt with appropriate permission. 因此,程序第一次创建一个具有适当权限的名为a.txt的文件。 If I execute a.out one more time, the new a.txt will be created. 如果我再次执行a.out,将创建新的a.txt。 But, the inode of the file remains same. 但是,文件的inode保持不变。 How, it will be. 怎么样,会的。

$ ./a.out
fd  = 3
$ ls -li a.txt
2444 -rw-r--r-- 1 mohanraj mohanraj 0 Aug 27 15:02 a.txt
$ cat>a.txt
this is file a.txt
$ ./a.out
fd  = 3
$ cat a.txt
$ls -li a.txt
2444 -rw-r--r-- 1 mohanraj mohanraj 0 Aug 27 15:02 a.txt
$

In the above output, a.txt have the content as "This is file a.txt". 在上面的输出中,a.txt的内容为“This is file a.txt”。 Once I execute the a.out, the new a.txt created. 一旦我执行了a.out,就会创建新的a.txt。 But, the inode 2444 remains same. 但是,inode 2444保持不变。 So, how creat system call works? 那么,创建系统调用如何工作?

Thanks in advance. 提前致谢。

creat only creates a file if it doesn't exist. creat仅在不存在时创建文件。 If it already exists, it's just truncated. 如果它已经存在,它就会被截断。

creat(filename, mode);

is equivalent to 相当于

open(filename, O_WRONLY|O_CREAT|O_TRUNC, mode);

And as specified in the open(2) documentation: 并且在open(2)文档中指定:

O_CREAT O_CREAT
If the file does not exist it will be created. 如果该文件不存在,则将创建该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM