Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use unix/Linux low-level IO function

Shulou Source: shulou.com Published: 2022-05-31 22:13:22 10月02日 Update

This article mainly introduces how to use the unix/Linux low-level IO function, which is very detailed and has a certain reference value. Friends who are interested must finish it!

The use of unix/Linux low-level IO functions, you must master to write module, the necessary application programming knowledge should have, otherwise, how to write applications to test your module or driver?

And the most important thing is to understand the interfaces called by the upper layer, and you will understand what the upper developers need and what they can achieve.

Put aside all the reading in your hand and get thoroughly familiar with the third chapter of the book on advanced programming in the Unix environment.

The contents include:

What exactly does open (), especially various common parameters, mean, such as creating an empty file: fd = open ("/ tmp/xx.txt", O_RDWR | O_CREAT | O_TRUNC)

Read a file, fd = open ("/ dev/hello", O_RDONLY)

If you want to write an existing file, fd = open ("/ dev/hello", O_WRONLY)

Specific parameters: man 2 open

Read ()

Write ()

Ioctl (), this is the most important.

Lseek (), the file pointer moves

For example, the following example:

Is to simply read a file:

# include

# include

# include

# include

Int main (int argc,char * * argv)

{

Int fd = 0

Int pid = 0

Char buffer [20] = {'\ 0'}

Char * read_buffer [20] = {'\ 0'}

/ / fd = open ("/ dev/hello", O_RDWR | O_CREAT | O_TRUNC)

Fd = open ("/ dev/hello", O_RDONLY); / / | O_NONBLOCK)

Printf ("fd=%d\ n", fd)

If (fd < 0) {

Perror ("/ dev/hello")

Return-1

}

Read (fd,read_buffer,sizeof (read_buffer)-1)

Printf ("read_buffer=%s\ n", read_buffer)

Close (fd)

Return 0

}

The following is a simple way to write a file

# include

# include

# include

# include

Int main (int argc,char * * argv)

{

Int fd = 0

Int pid = 0

Char buffer [20] = {'\ 0'}

Char write_buffer [20] = {'\ 0'}

Strcpy (write_buffer, "zhanglinbao")

Fd = open ("/ dev/hello", O_RDWR | O_CREAT | O_TRUNC)

/ / fd = open ("/ dev/hello", O_RDONLY)

Printf ("fd=%d\ n", fd)

If (fd < 0) {

Perror ("/ dev/hello")

Return-1

}

Write (fd,write_buffer,sizeof (write_buffer)-1)

Close (fd)

Return 0

}

These are all the contents of the article "how to use unix/Linux low-level IO functions". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Files low-level functions content that is upper layers parameters knowledge articles applications programming important advanced necessary things values examples interests including guys. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia vpn OPPO Reno Shulou Information