Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux system functions open, close, read, writ

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

System call

System calls are a set of "special interfaces" provided by the operating system to users. System calls do not deal directly with programmers or system administrators, but submit requests to the kernel through soft interrupts to obtain the service entry (system call table) of kernel functions. The system call allows the system to run from user space into kernel space, and then returns the results to the application (kernel state-> user space).

The difference between system call and system API

System API

It is mainly realized through the C library libc. Programmers often interact with the kernel in this way, and these API are implemented through system calls.

System command

The system administrator uses system commands to interact with the kernel, which is an executable file, which is realized through system API and system calls.

Shell program

A program composed of a series of system commands and SHELL scripts.

Function library call and system call

File descriptor

-there is a file descriptor pointer in each process PCB structure, which points to the file descriptor table of files_struct and records the list of files opened by each process.

-the system kernel does not allow applications to access the file descriptor table of the process, but only returns the index of these structures, that is, the file descriptor ID (File Description) to the application.

-in Linux systems, applications use these file descriptors to enable the kernel to access files.

-the file descriptors that each process can access are limited, which can be viewed through # ulimit-n

Special file descriptor

Standard input STDIN_FILENO

Standard output STDOUT_FILENO

Standard error STDERR_FILENO

After each process is loaded, the three file descriptors, 0J. 1J. 2, are turned on by default.

Open

-function prototype

Int open (const char * path, int flags,mode_t mode)

-Parameter

Path: the name of the file, which can contain (absolute and relative) paths

Flags: file open mode

Mode: used to specify the access rights to the owner of the file, the user group of the file, and other users in the system, the file permission is: mode& (~ umask)

-return value

Open it successfully and return the file descriptor

Failed to open, return-1

Open mode

Access permission

O_CREATE will generate special permissions, and access permissions need to be set:

S_IRWXU is equivalent to S_IRUSR | S_IWUSR | S_IXUSR (file owner)

S_IRWXG is equivalent to S_IRGRP | S_IWGRP | S_IXGRP (file user group)

S_IRWXO is equivalent to S_IROTH | S_IWOTH | S_IXOTH (other users of the file)

Close

Close the file close (release the file description table structure corresponding to the fd in the process):

Function prototype: int close (int fd); / / return-1 if an error occurs; 0 if the call succeeds

Read

-function prototype:

Int read (int fd, void * buf, size_t nbytes)

-Parameter

Fd: the file descriptor of the file you want to read

Buf: a pointer to a block of memory in which bytes read from a file are placed

Nbytes: the number of bytes copied from this file into buf

-return value

If an error occurs, return-1

Returns the number of bytes copied from the file to the specified buffer!

Write

-function prototype: int write (int fd,void * buf,size_t nbytes)

-function parameters:

Fd: the file descriptor of the file to write

Buf: a pointer to a block of memory from which data is read and written to a file

Nbytes: the number of bytes to write to the file

-return value

If an error occurs, return-1

If the write is successful, the number of bytes written to the file is returned

Exercise: read and write the serial communication configuration file by means of file system call

The configuration file serial.cfg is as follows:

Requirements: 1. Program running command format:

Serialchat [- options]

[options]:

P-output prints serial.cfg configuration item information. Format: parameters-parameter values

S-perform menus (save and exit) for users to set

F-specify configuration item settings, output each option setting file name, and output each configuration item information

Other options prompt the program for help.

2. Serialchat is not supported to run twice

3. If the configuration file cannot be found, the operation fails. The reason: the configuration file cannot be found.

The code is as follows:

# include

< stdio.h>

# include

< string.h>

# include

< sys/types.h>

# include

< sys/stat.h>

# include

< unistd.h>

# include

< fcntl.h>

Int icount = 0while / blank'\ 0'void rm_space (char * pStr) {char * pos = pStr; pos = strchr (pStr,''); while (pos! = NULL) {strcpy (pos,pos+1); pos = strchr (pos,'') }} / / go to comment on'#'\ nfolk 'and Chinese, leaving the data void rm_annotation (char * pStr) {icount++; char * pos = pStr; char * end = NULL; if (icount = = 1) / / the first line may have a top box {pos = strchr (pStr,'#');} else {pos = strstr (pStr, "\ n #") } while (pos! = NULL) {end = strchr (pos+1,'\ n'); strcpy (pos,end); pos = strstr (pStr, "\ n #");}} / / output configuration item information void printMsg (char * buf) {char key [20] = ""; char value [20] = "; char line [50] ="; char * pos = buf Char * end = NULL; int flag = 0; printf ("configuration item information is as follows:\ n"); pos = strchr (buf,'\ n'); end = strchr (pos,'='); while (end! = NULL) {memset (key,0,sizeof (key)); memset (value,0,sizeof (value)); memcpy (key,pos+1,end-(pos+1); pos = end) End = strchr (pos,'\ r'); if (end = = NULL) / / if the final data {flag = 1; break;} memcpy (value,pos+1,end-(pos+1)); sprintf (line, "% s% s", key,value); printf ("% s\ n", line); pos = end + 1 End = strchr (pos,'=');} if (flag) {end = strchr (pos,'\ 0'); memcpy (value,pos+1,end-(pos+1)); sprintf (line, "% s% s", key,value); printf ("% s\ n", line) }} / / modify the configuration file (incoming buf is unprocessed) void updateFile (int fd,char * buf) {char * pos = buf; char * end = NULL; char key [20] = ""; char newvalue [20] = ""; int lse; int count; printf ("Please enter configuration parameters:"); scanf ("% s", key); printf ("Please enter the value of configuration parameters:") Scanf ("% s", newvalue); strcat (key, "="); pos = strstr (buf,key); if (pos = = NULL) {printf ("input parameter does not exist!") ; return;} pos = pos + strlen (key); while (* pos = =') / handle'= 'cases with spaces {pos = pos + 1;} end = strchr (pos,'\ r'); if (end = = NULL) / / if the final data {end = strchr (pos,'\ 0') } / / modify the file lse = lseek (fd,pos-buf,SEEK_SET); write (fd,newvalue,end-pos); / / pay attention to the length of the old and new values printf ("modified successfully! \ n ");} / *-main function-* / int main (int argc,char * argv []) {int fd; int readByte = 0; char buf [512] ="; char option [3] ="; char filename [20] ="; int index; int s_flag = 0 Intf _ flag = 0; struct flock lock = {0}; / / determine the number of parameters if (argc! = 2 & & argc! = 3) {printf ("incorrect input format! \ n "); return 1;} / / Open the configuration file strcpy (option,argv [1]); if (strcmp (option,"-f ") = = 0) / / Open the specified file {if (argc! = 3) {printf (" incorrect input format! \ n "); return 1;} f_flag = 1; strcpy (filename,argv [2]); fd = open (filename,O_RDWR,S_IRWXU | S_IRGRP);} else {fd = open (" serial.cfg ", Olympiad DWR Magazine Shearing IRWXU | S_IRGRP) } if (fd =-1) {printf ("failed to run and could not find the configuration file! \ n "); return 1;} / / read configuration file readByte = read (fd,buf,512); if (readByte)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report