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

How to use unix/Linux low-level IO function

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

Share

Shulou(Shulou.com)05/31 Report--

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!

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