c编程是系统调用吗
The system call is a way for programs to interact with the operating system. When the program makes a system call at that time it makes a request to the operating system's kernel.
系统调用是程序与操作系统进行交互的一种方式。 当程序在那时进行系统调用时,它将向操作系统的内核发出请求。
There are 5 different categories of system calls:
有5种不同的系统调用类别 :
Process Control
过程控制
File Management
文件管理
Device Management
设备管理
Information Management
信息管理
Communication
通讯
Here, we will discuss about the system calls for file management in Unix system,
在这里,我们将讨论Unix系统中用于文件管理的系统调用 ,
There are four system calls for file management,
文件管理有四个系统调用 ,
open ()
开启()
read ()
阅读()
write ()
写()
close ()
关 ()
1.打开() (1. open ())
open() system call is used to know the file descriptor of user-created files. Since read and write use file descriptor as their 1st parameter so to know the file descriptor open() system call is used.
open()系统调用用于了解用户创建的文件的文件描述符。 由于读写操作使用文件描述符作为第一个参数,因此要使用文件描述符open()系统调用。
Syntax:
句法:
fd = open (file_name, mode, permission);
Example:
fd = open ("file", O_CREAT | O_RDWR, 0777);
Here,
这里,
file_name is the name to the file to open.
file_name是要打开的文件的名称。
mode is used to define the file opening modes such as create, read, write modes.
mode用于定义文件打开模式,例如创建,读取,写入模式。
permission is used to define the file permissions.
权限用于定义文件权限。
Return value: Function returns the file descriptor.
返回值:函数返回文件描述符。
2.阅读() (2. read ())
read() system call is used to read the content from the file. It can also be used to read the input from the keyboard by specifying the 0 as file descriptor (see in the program given below).
read()系统调用用于从文件中读取内容。 通过将0指定为文件描述符,它也可以用于从键盘读取输入(请参见下面给出的程序)。
Syntax:
句法:
length = read(file_descriptor , buffer, max_len);
Example:
n = read(0, buff, 50);
Here,
这里,
file_descriptor is the file descriptor of the file.
file_descriptor是文件的文件描述符。
buffer is the name of the buffer where data is to be stored.
buffer是要在其中存储数据的缓冲区的名称。
max_len is the number specifying the maximum amount of that data can be read
max_len是一个数字,指定可以读取的最大数据量
Return value: If successful read returns the number of bytes actually read.
返回值:如果读取成功,则返回实际读取的字节数。
3.写() (3. write ())
write() system call is used to write the content to the file.
write()系统调用用于将内容写入文件。
Syntax:
句法:
length = write(file_descriptor , buffer, len);
Example:
n = write(fd, "Hello world!", 12);
Here,
这里,
file_descriptor is the file descriptor of the file.
file_descriptor是文件的文件描述符。
buffer is the name of the buffer to be stored.
buffer是要存储的缓冲区的名称。
len is the length of the data to be written.
len是要写入的数据的长度。
Return value: If successful write() returns the number of bytes actually written.
返回值:如果成功,write()返回实际写入的字节数。
4.关闭() (4. close ())
close() system call is used to close the opened file, it tells the operating system that you are done with the file and close the file.
close()系统调用用于关闭打开的文件,它告诉操作系统您已完成文件操作并关闭文件。
Syntax:
句法:
int close(int fd);
Here,
这里,
fd is the file descriptor of the file to be closed.
fd是要关闭的文件的文件描述符。
Return value: If file closed successfully it returns 0, else it returns -1.
返回值:如果文件成功关闭,则返回0,否则返回-1。
C代码演示系统调用示例 (C code to demonstrate example of system calls)
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
int main()
{
int n,fd;
char buff[50]; // declaring buffer
//message printing on the display
printf("Enter text to write in the file:\n");
//read from keyboard, specifying 0 as fd for std input device
//Here, n stores the number of characters
n= read(0, buff, 50);
// creating a new file using open.
fd=open("file",O_CREAT | O_RDWR, 0777);
//writting input data to file (fd)
write(fd, buff, n);
//Write to display (1 is standard fd for output device)
write(1, buff, n);
//closing the file
int close(int fd);
return 0;
}
Output
输出量
Enter text to write in the file:
Hello world, welcome @ IncludeHelp
Hello world, welcome @ IncludeHelp
翻译自: https://www.includehelp.com/c/file-management-system-calls.aspx
c编程是系统调用吗