我已经定义了以下结构来表示IPv4标头(直到选项字段):
struct IPv4Header
{
// First row in diagram
u_int32 Version:4;
u_int32 InternetHeaderLength:4; // Header length is expressed in units of 32 bits.
u_int32 TypeOfService:8;
u_int32 TotalLength:16;
// Second row in diagram
u_int32 Identification:16;
u_int32 Flags:3;
u_int32 FragmentOffset:13;
// Third row in diagram
u_int32 TTL:8;
u_int32 Protocol:8;
u_int32 HeaderChecksum:16;
// Fourth row in diagram
u_int32 SourceAddress:32;
// Fifth row in diagram
u_int32 DestinationAddress:32;
};
Run Code Online (Sandbox Code Playgroud)
我现在还使用Wireshark捕获了一个IP帧.作为数组文字,它看起来像这样:
// Captured with Wireshark
const u_int8 cIPHeaderSample[] = {
0x45, 0x00, 0x05, 0x17,
0xA7, 0xE0, …Run Code Online (Sandbox Code Playgroud) 有没有人知道通过扫描源文件的目录生成makefile的工具?
这可能是天真的:
在寻求关于良好编程实践的建议时,典型的答案是敏捷软件开发,测试驱动开发或设计模式的变体.然而,据我所知,这些都没有使用科学方法证明(如果我错了,那么随意纠正我).
我想知道,基于证据的发展实践这个主题有什么好的资源吗?
使用gcc 4.7(在OS X上使用MacPorts构建的g ++ - mp-4.7(GCC)4.7.0)编译以下代码时,我得到看似矛盾的结果.
当我试图重新阐释和非关联化的一个部分编译器不抱怨std::array作为uint32_t,但它使用C数组时一样.
示例代码:
#include <array>
#include <cstdint>
int main() {
std::array<uint8_t, 6> stdarr;
*reinterpret_cast<uint32_t*>(&stdarr[0]) = 0; // OK
uint8_t arr[6];
*reinterpret_cast<uint32_t*>(&arr[0]) = 0;
// ^ error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
}
Run Code Online (Sandbox Code Playgroud)
编译器命令是:
$ g++ -o test -std=c++0x -Wall -Wextra -Werror main.cpp
Run Code Online (Sandbox Code Playgroud)
他们为什么被区别对待?
对于使用GCC 4.7 MacPorts构建编译的程序,我似乎无法获得可读的调试输出.
我尝试过使用GDB 6.3和GDB 7.3,每个都有自己的问题.
使用GDB 7.3,我在启动时获得以下输出:
$ ggdb ./test
GNU gdb (GDB) 7.3
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin11.4.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
BFD: unable to read …Run Code Online (Sandbox Code Playgroud) 从二进制数据读取整数时,我注意到memcpy比铸造解决方案慢.
版本1:reinterpret_cast,由于潜在的对齐问题而臭,但也更快(?)
int get_int_v1(const char * data) { return *reinterpret_cast<const int*>(data); }
Run Code Online (Sandbox Code Playgroud)
版本2:memcpy,正确且稍慢:
int get_int_v2(const char * data) { int result; memcpy(&result, data, sizeof(result)); return result; }
Run Code Online (Sandbox Code Playgroud)
供将来参考,代码是:
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <vector>
#include <sys/time.h>
double get_current_time()
{
timeval tv;
gettimeofday(&tv, NULL);
return double (tv.tv_sec) + 0.000001 * tv.tv_usec;
}
int get_int_v1(const char * data) { return *reinterpret_cast<const int*>(data); }
int get_int_v2(const char * data) { …Run Code Online (Sandbox Code Playgroud) 我正在开发一个类似Ideone的系统,其中不受信任的用户代码必须以沙盒模式运行.
为此,我一直在寻找ptrace第一层保护的可能性.然而,经过几次实验后,似乎:
我想拦截某些系统调用并返回一个虚假的结果代码而不会实际调用.有没有办法实现这个?
我想实现一种方法来安排稍后执行的任务.界面类似于JavaScript setTimeout(function, milliseconds).
在我的应用程序中,某些资源由一个线程拥有.为了避免竞争条件,必须始终从同一个线程访问它们.如果其他线程想要访问资源,则必须将任务对象分派给资源线程.
所以我需要解决的两个问题是:
通过使用无消耗队列快速修复第一个问题,该队列在消费端具有资源线程.(我使用TBB的concurrent_bounded_queue.)然而,第二个问题对我来说并不那么明显.我可以想到两个策略:
我已经尝试了这两种方法,我倾向于支持第一种,因为它简单可靠,而第二种往往更容易出现微妙的错误.第一种方法将此委托给OS线程调度程序.
但是,第一个解决方案确实会创建大量短期线程,而我通常会听到重用线程的建议.
根据Antony Williams的书" C++ Concurrency in Action",一个spinlock可以实现如下:
class spinlock_mutex {
std::atomic_flag flag;
public:
spinlock_mutex() : flag(ATOMIC_FLAG_INIT) {}
void lock() {
while (flag.test_and_set(std::memory_order_acquire)) ;
}
void unlock() {
flag.clear(std::memory_order_release);
}
};
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,memory_order_acquire标记可确保使用该memory_order_release标记的最新商店操作的可见性.("释放操作与获取操作同步.")
由于test_and_set也是一个存储操作,我希望memory_order_acq_rel在lock()方法中需要一个标记,以确保在尝试锁定互斥锁的其他线程中锁定状态的可见性.
为什么memory_order_acquire足够?
我的应用程序从网络接收数据包并将它们发送到一个或多个"处理器".(每个数据包属于预定义的"流",可以通过查看数据包数据来识别.)
目前有一个线程可以完成所有工作:
以每秒2000万个数据包的速率接收传入数据(10个字节的60字节数据包).
然而,该解决方案仅能够跟上非常少量的流和处理器.例如,在10个流的情况下,已经有大约10-20%的分组丢失.
由于步骤(3)是最昂贵的,我计划将该工作委托给工作线程池.
但是,我必须小心,因为处理器本身不是线程安全的.因此,只有一个工作线程可以同时将数据包分派到同一个处理器.
这似乎是基于任务的编程的一个很好的用例.但我无法轻易地将TBB文档中解释的设计模式与我的问题相匹配.
所以我的问题是:我如何组织我的消费者线程,以便他们将数据包均匀地分发到单线程处理器?
我不期待一个完全成熟的解决方案,但我会很高兴你的建议或随机的想法:)
c++ ×9
c ×2
agile ×1
arrays ×1
atomic ×1
concurrency ×1
gcc ×1
gdb ×1
macos ×1
makefile ×1
performance ×1
ptrace ×1
tdd ×1
type-punning ×1