小编cam*_*ino的帖子

STDIN_FILENO 和 STDOUT_FILENO 的非阻塞 I/O 行为很奇怪

我有以下代码:

void
set_fl(int fd, int flags) /* flags are file status flags to turn on */
{
    int val;

    if ((val = fcntl(fd, F_GETFL, 0)) < 0)
        err_sys("fcntl F_GETFL error");

    val |= flags;       /* turn on flags */

    if (fcntl(fd, F_SETFL, val) < 0)
        err_sys("fcntl F_SETFL error");
}

int
main(void)
{
    char buf[BUFSIZ];
    set_fl(STDOUT_FILENO, O_NONBLOCK);  //set STDOUT_FILENO to nonblock
    if(read(STDIN_FILENO, buf, BUFSIZ)==-1) { //read from STDIN_FILENO
        printf("something went wrong with read()! %s\n", strerror(errno));
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我设置STDOUT_FILENO为非阻塞模式,但读取操作似乎STDIN_FILENO立即完成。为什么? …

linux nonblocking

5
推荐指数
1
解决办法
2633
查看次数

为什么在成为孤立进程组时没有收到SIGHUP信号

在关于孤立进程组的 GNU libc手册中,它提到:

“process groups that continue running even after the session leader 
has terminated are marked as orphaned process groups. 

When a process group becomes an orphan, its processes are sent a SIGHUP 
signal. Ordinarily, this causes the processes to terminate. However, 
if a program ignores this signal or establishes a handler for it 
(see Signal Handling), it can continue running as  in the orphan process
 group even after its controlling process terminates; but it still 
cannot access …
Run Code Online (Sandbox Code Playgroud)

c linux jobs process

5
推荐指数
1
解决办法
770
查看次数

如何强制子类重写虚函数

有时我想强制子类覆盖函数,例如:

class A
{
    virtual void foo() = 0;
};

class B : public A
{
    virtual void foo() {...}
};

class C : public B
{
    //people may forget to override function foo
};
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
2332
查看次数

如何确保类的每个数据成员都已复制到其复制构造函数中?

当一个类有许多数据成员时,很难弄清楚数据成员是否在其复制构造函数中被复制.

有解决方案吗?

c++

5
推荐指数
1
解决办法
94
查看次数

constexpr上下文中constexpr函数内的所有函数都必须是constexpr函数吗?

在ubuntu gcc 8.0中:

void bar(){}

constexpr int foo(int a)
{
    if (a <=0 )
        bar();

    return 1;
}

int main()
{
    int a1[foo(-1)]; //will give a compile error, which is expected, 
                     //since non-constexpr function is not allowd in constexpr context
}
Run Code Online (Sandbox Code Playgroud)

但在以下测试中:

int main()
{
    int a2[foo(1)];  //no compile error
}
Run Code Online (Sandbox Code Playgroud)

这里,bar是非constexpr功能.我想知道为什么在constexpr上下文中允许非constexpr函数,尽管在这个测试中它不会被调用.

c++

5
推荐指数
1
解决办法
201
查看次数

为什么/lib32/libc.so.6中有两个"fopen"符号?

nm -D /lib32/libc.so.6 | grep '\<fopen\>'
0005d0c0 T fopen
00109750 T fopen

readelf -s  /lib32/libc.so.6 | egrep '0005d0c0|00109750'
181: 0005d0c0    50 FUNC    GLOBAL DEFAULT   12 fopen@@GLIBC_2.1
182: 00109750   136 FUNC    GLOBAL DEFAULT   12 fopen@GLIBC_2.0
679: 0005d0c0    50 FUNC    GLOBAL DEFAULT   12 _IO_fopen@@GLIBC_2.1
680: 00109750   136 FUNC    GLOBAL DEFAULT   12 _IO_fopen@GLIBC_2.0
Run Code Online (Sandbox Code Playgroud)

这是我的问题:

  1. 为什么/lib32/libc.so.6中有两个fopen符号?应禁止同一目标文件中的相同符号,对吗?

  2. 为什么readelf -s转出fopen @@ GLIBC_2.1和fopen@GLIBC_2.0而不是fopen?

谢谢

linux readelf nm

4
推荐指数
1
解决办法
817
查看次数

是否可以为元素指定字符集?

在html中,我们可以使用以下命令为整个文档指定charset:

<head>
<meta charset="utf-8" /> 
</head>
Run Code Online (Sandbox Code Playgroud)

但有时候,不可能改变整个页面的字符集,例如,使用注入.

我想声明一个指定元素的charset,就像这样

<span charset="utf-8">....</span>
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用.

有任何想法吗?

css html5

4
推荐指数
1
解决办法
920
查看次数

每个lambda函数都是匿名类吗?

auto a = [](){};
auto b = [](){};
vector<decltype(a)> v;
v.push_back(a); //ok
v.push_back(b); //compiler error
Run Code Online (Sandbox Code Playgroud)

a和b有不同的类型.

我想知道每个lambda函数是否真的是一种匿名类,每当我们创建一个lambda函数时,我们创建一个具有随机名称的新类,只对编译器可见?

c++ c++11

4
推荐指数
1
解决办法
326
查看次数

tcp堆栈如何区分关闭和关闭?

我们知道:

////////////////////////////////////////////////// ///////

close()将终止tcp连接上的两个方向

shutdown()可以阻止一个或两个方向的通信

////////////////////////////////////////////////// ///////

在这里,让我困惑的是tcp stack如何区分它们?

我写了一个示例程序:

首先我使用:

....
connect(192.168.1.100) //there is a server process running in 192.168.1.100
....
close(socketfd);
sleep(1000);
Run Code Online (Sandbox Code Playgroud)

然后我使用wireshark来转储数据包:

01 -->syn

02 <--syn,ack

03 -->ack

04 -->fin,ack

05 <--ack
Run Code Online (Sandbox Code Playgroud)

netstat -an | grep 192.168.1.100

我已经运行了大约5分钟,它打印:

首先是"tcp 0 0 ... FIN_WAIT2",然后大约2分钟后没有输出,似乎连接已被破坏.

然后,我使用:

....
connect(192.168.1.100)
....
shutdown(socketfd,SHUT_WR);
sleep(1000);
Run Code Online (Sandbox Code Playgroud)

使用wireshark转储数据包:

01 - >同义词

02 < - syn,ack

03 - >确认

04 - > fin,ack

05 < - ack

...

netstat -an | grep 192.168.1.100

运行大约10分钟,它总是打印:"tcp 0 0 …

tcp

3
推荐指数
1
解决办法
4755
查看次数

为什么auto i = same_const_variable无法推断出"const"?

const int ci = 10;
auto i = ci;  // i will be "int" instead of "const int"
i = 20;
Run Code Online (Sandbox Code Playgroud)

我想知道为什么汽车是为这种行为而设计的?

为什么类型i是"int"而不是"const int"?

这里有什么问题?

我想明白为什么会帮助我们记住它

c++ auto c++11 type-deduction

3
推荐指数
2
解决办法
665
查看次数

标签 统计

c++ ×5

linux ×3

c++11 ×2

auto ×1

c ×1

css ×1

html5 ×1

jobs ×1

nm ×1

nonblocking ×1

process ×1

readelf ×1

tcp ×1

type-deduction ×1