我是Linux信号的新手,请帮忙.以下代码在Linux 2.6 gcc中运行时获得核心转储.
$ ./a.out
浮点异常(核心转储)
问题:
1.由于安装了过程信号掩码,第40行(z = x/y)生成的"SIGFPGE"是否应该被阻止?
2.如果没有阻塞,由于已经安装了信号处理程序,信号处理程序不应该捕获"SIGFPE"而不是核心转储?
3.如果我注释掉第40行(z = x/y),并使用第42行(加注(SIGFPE)),那么一切都按预期工作.这里x/0和提升SIGFPE有什么区别?
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void sig_handler(int signum)
{
printf("sig_handler() received signal %d\n", signum);
}
int main(int argc, char * argv[])
{
// setup signal mask, block all signals
sigset_t set;
sigfillset(&set);
if(sigprocmask(SIG_BLOCK, &set, NULL)<0)
{
perror("failed to set sigmask");
return -1;
}
// install signal handler for SIGFPE
struct sigaction act;
act.sa_handler = sig_handler;
act.sa_mask = set;
act.sa_flags = 0;
if(sigaction( SIGFPE, &act, …Run Code Online (Sandbox Code Playgroud) 以下代码适用于gcc 4.4.
但是gcc 4.7会让断言失败.
#include <assert.h>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string input("abcdefg");
stringstream iss(input);
ostringstream oss;
oss << iss.rdbuf();
assert (!iss.eof());
(void) iss.peek();
assert (iss.eof());
// the following assertion will fail with gcc 4.7
assert( streamoff(iss.tellg()) ==
streamoff(input.length()) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在gcc 4.7中,如果istream已达到EOF,则tellg()将返回-1.不会调用pubseekoff()和seekoff()在gcc 4.4中它不是问题.
应该是哪种行为,gcc 4.4还是gcc 4.7?为什么?
boost::asio TCP 套接字接受/读/写都提供异步版本,但不关闭。
在我的代码中,我只是调用了 socket.close(),并且大部分时间它都可以正常工作。它触发了正常的 TCP 关闭。
但有时,close() 只是在没有关闭 TCP 的情况下关闭套接字。因此,我必须改为调用 shutdown()。但我不想阻止我的代码。在 boost:asio 中,shutdown() 是否阻塞?关闭()怎么样?close() 是否阻塞?
我仍然对CTORs感到困惑:
问题1:
为什么第15行呼叫A:A(int)而不是A:A(double&)?
问题2:
为什么18号线没有打电话A:A(B&)?
#include <iostream>
using namespace std;
class B{};
class A{
public:
A(int ) {cout<<"A::A(int)"<<endl;}
A(double&){cout<<"A::A(double&)"<<endl;} // it will work if it is A(double), without the &
A(B&){cout<<"A::A(B&)"<<endl;}
};
int main()
{
/*line 15*/ A obj((double)2.1); // this will call A(int), why?
B obj2;
A obj3(obj2);
/*line 18*/ A obj4(B); // this did not trigger any output why?
}
Run Code Online (Sandbox Code Playgroud) c++ ×2
asynchronous ×1
boost ×1
boost-asio ×1
c ×1
constructor ×1
istream ×1
linux ×1
shutdown ×1
sigfpe ×1
signals ×1
tcp ×1