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 call sys_close in linux system

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

Share

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

Today, I will talk to you about how to call sys_close in linux system. Many people may not know much about it. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope everyone can gain something according to this article.

1 According to the file descriptor, set the pointer array corresponding item to null.

2 If the file structure pointed to is not used by other processes, the file structure can be reused. But the inode node it points to needs to be written back to hard disk. See iput function for details.

//disassociate file descriptor->file structure->inode

int sys_close(unsigned int fd)

{

struct file * filp;

if (fd >= NR_OPEN)

return -EINVAL;

//Clear the close_on_exec flag, which indicates that the file is closed when fork+exec

current->close_on_exec &= ~(1filp[fd] = NULL;

if (filp->f_count == 0)

panic("Close: file count is 0");

// file structure references minus one, non-zero means that there are other processes or descriptors using the structure, so file and inode cannot be released yet

if (--filp->f_count)

return (0);

//if no process is used, release the inode or write it back to the hard disk

iput(filp->f_inode);

return (0);

}

//Release the inode, if it is not referenced, destroy it, otherwise subtract one from the number of references

void iput(struct m_inode * inode)

{

if (! inode)

return;

//if there is a process using this inode, it blocks

wait_on_inode(inode);

//no process references this inode

if (! inode->i_count)

panic("iput: trying to free free inode");

//pipeline inode

if (inode->i_pipe) {

//Wake up the waiting queue, because the pipeline may have to be destroyed, otherwise it will make the waiters wait indefinitely. This sentence can be put after if.

wake_up(&inode->i_wait);

//Reduce the number of references by one. If there are processes that are referenced, do not destroy them first.

if (--inode->i_count)

return;

//release the corresponding page size of the pipeline

free_page(inode->i_size);

//The inode can be reused because the inode points to an element of the inode_table

inode->i_count=0;

inode->i_dirt=0;

inode->i_pipe=0;

return;

}

//dev does not indicate that it is not the inode corresponding to the hard disk file. There is no need to write back the hard disk. The number of references can be reduced by one.

if (! inode->i_dev) {

inode->i_count--;

return;

}

if (S_ISBLK(inode->i_mode)) {

//block file, inode->i_zone[0] stores the device number, write back the dev device in buffer to hard disk

sync_dev(inode->i_zone[0]);

wait_on_inode(inode);

}

repeat:

//There are processes referencing this inode node, and the number of references is reduced by one and returned.

if (inode->i_count>1) {

inode->i_count--;

return;

}

//The inode is not referenced by any process, and the file corresponding to the inode is not referenced by any other directory entry. Delete the contents of the inode and release the inode.

if (! inode->i_nlinks) {

truncate(inode);

free_inode(inode);

return;

}

//Write back hard disk if you need to

if (inode->i_dirt) {

write_inode(inode); /* we can sleep - so do again */

wait_on_inode(inode);

goto repeat;

}

inode->i_count--;

return;

}

After reading the above, do you know more about how to call sys_close in linux? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report