close与shutdown系统调用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了close与shutdown系统调用相关的知识,希望对你有一定的参考价值。
使用多线程时,pthread_create的参数flag有CLONE_FILES, 最终调用do_fork(),并且会根据CLONE_FILES标志来调用copy_files()来共享父进程中的文件描述符(这里包括socketfd)。
使用多进程时,fork的参数flag没有CLONE_FILES, 最终调用do_fork(),子进程会把文件描述符的引用计数加一,即把struct files_struct成员f_count++。
在单进程、单线程、多线程时,close()会调用tcp_close();
在多进程时,只是减少f_count引用计数,在f_count为0时才会调用tcp_close()。
而shutdown()与进程线程类型无关,都是直接调用tcp_shutdown()。
NAME
shutdown - shut down part of a full-duplex connection
SYNOPSIS
#include <sys/socket.h>
int shutdown(int sockfd, int how);
DESCRIPTION
The shutdown() call causes all or part of a full-duplex connection on the socket associated with sockfd
to be shut down. If how is SHUT_RD, further receptions will be disallowed. If how is SHUT_WR, further
transmissions will be disallowed. If how is SHUT_RDWR, further receptions and transmissions will be
disallowed.
NAME
close - close a file descriptor
SYNOPSIS
#include <unistd.h>
int close(int fd);
DESCRIPTION
close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record
locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed
(regardless of the file descriptor that was used to obtain the lock).
If fd is the last file descriptor referring to the underlying open file description (see open(2)), the
resources associated with the open file description are freed; if the descriptor was the last reference
to a file which has been removed using unlink(2) the file is deleted.
以上是关于close与shutdown系统调用的主要内容,如果未能解决你的问题,请参考以下文章
socket链接的关闭连接与close和shutdown的区别
linux网络编程----close()与 shutdown() 函数详解