In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to call the Linux operating file system". Many people will encounter such a dilemma in the operation of the actual case, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Header files to be introduced:
# inlcude1. Open a file
Open an existing file
Int open (const char * pathname, int flags)
Create a new file and create permissions
Int open (const char * pathname, int flags, mode_t mode); introduction to parameters
Pathname: the path and name of the file to be opened
Flags: open Fla
Logo introduction:
The argument flags must include one of the following access modes:O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-only, write-only, or read/write, respectively.
O_RDONLY read-only open
O_RDWR read and write open
Created if the O_CREAT file does not exist
Append at the end of O_APPEND file
O_TRUNC empties the file and rewrites the mode
The following symbolic constants are provided for mode:S_IRWXU 00700 user (file owner) has read, write, and execute permissionS_ IRUSR 00400 user has read permissionS_IWUSR 00200 user has write permissionS_IXUSR 00100 user has execute permissionS_IRWXG 00070 group has read, write, and execute permissionS_IRGRP 00040 group has read permissionS_IWGRP 00020 group has write permissionS_IXGRP 00010 group has execute permissionS_IRWXO 00007 others have read, write, and execute permissionS_IROTH 00004 others have read permissionS_IWOTH 00002 others have write permissionS_IXOTH 00001 others have execute permission
Return value: file descriptor
two。 Read the file ssize_t read (int fd, void * buf, size_t count)
Parameter introduction
Fd: corresponding to the open file descriptor buf: space for storing data count: number of bytes of data to be read from the file at a time. Returned value: number of bytes actually read.
3. Write the file ssize_t write (int fd, const void * buf, size_t count)
Parameter description:
Fd: corresponding to the open file descriptor buf: stores the data to be written count: how much data is planned to be written to the file at a time
4. Turn off int close (int fd)
Fd: the corresponding file descriptor
Analytical problem
If the parent process opens a file first, can the child processes share it after fork?
File content
Code
# include#include#include#include#includeint main () {char buff [128C] = {0}; int fd = open ("myfile.txt", O_RDONLY); pid_t pid = fork (); assert (pid! =-1); if (pid = = 0) {read (fd, buff, 1); printf ("child buff =% s\ n", buff); sleep (1); read (fd, buff, 1) Printf ("child buff =% s\ n", buff);} else {read (fd, buff, 1); printf ("parent buff =% s\ n", buff); sleep (1); read (fd, buff, 1); printf ("parent buff =% s\ n", buff);} close (fd); exit (0);}
Running result:
Conclusion:
Because the PCB of the child process created by fork is copied from the parent process, and the pointer to the open file in the PCB table of the child process only copies the value in the parent process PCB, the parent and child processes share all the file descriptors opened before the parent process fork.
Exercises
Finish copying a file (similar command: cp)
The content of the original file is:
Code:
# include#include#include#include#includeint main (void) {char buff [128A] = {0}; int fdr = open ("myfile.txt", O_RDONLY); assert (fdr! =-1); int fdw = open ("newfile.txt", O_WRONLY | O_CREAT, 0600); assert (fdw! =-1); int n = 0; while (n = read (fdr, buff, 128) > 0) {write (fdw, buff, n) } close (fdr); close (fdw); exit (0);}
Run the example:
You can see that the newfile.txt was created successfully
The difference between system calls and library functions
Difference: the implementation of the system call is in the kernel and belongs to the kernel space, while the implementation of the library function belongs to the user space in the function library.
System call execution procedure:
This is the end of the content of "how to call the Linux operating file system". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.