我正在设计一个二进制文件格式来存储字符串[不终止null以节省空间]和二进制数据.
一世.处理小/大端系统的最佳方法是什么?ia将所有内容转换为网络字节顺序并返回ntohl()/ htonl()工作?
II.在x86,x64和arm上,打包结构的大小是否相同?
III.这种方法有任何固有的弱点吗?
struct __attribute__((packed)) Header {
uint8_t magic;
uint8_t flags;
};
struct __attribute__((packed)) Record {
uint64_t length;
uint32_t crc;
uint16_t year;
uint8_t day;
uint8_t month;
uint8_t hour;
uint8_t minute;
uint8_t second;
uint8_t type;
};
Run Code Online (Sandbox Code Playgroud)
测试器代码我正在使用开发格式:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <strings.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
struct __attribute__((packed)) Header {
uint8_t magic;
uint8_t flags;
};
struct __attribute__((packed)) Record {
uint64_t length;
uint32_t crc;
uint16_t year;
uint8_t day;
uint8_t month;
uint8_t …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用原始克隆系统调用,以避免将pid 0代码重构为函数.Linux要求堆栈增加16个字节,此外,libc保留16位可能存储ptid和ctid.下面的代码创建一个对齐的堆栈,然后从子进程退出.在等待由libc的包装器克隆的子进程后,我使用相同的缓冲区进行原始系统调用,但每次程序在使用原始系统调用时都会发生段错误.附加是strace的输出,除非我忽略任何显示系统调用参数两次都相同.至少有一个问题Raw Clone系统调用 SO,其中OP似乎有类似的困难,不幸的是,接受的答案使用libc clone包装器而不是syscall.
#define _GNU_SOURCE
#include <sched.h>
#include <stdalign.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <syscall.h>
#include <signal.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
int test(void*c)
{
quick_exit(0);
}
int main(void)
{
alignas (16) unsigned char stack[4096] = {0};
printf("Top of stack %p\n", stack+sizeof(stack));
printf("Top of stack minus 16 %p\n", stack+sizeof(stack)-16);
pid_t pid = clone(test, stack+sizeof(stack), CLONE_VM|SIGCHLD, 0, 0, 0, 0);
wait(NULL);
memset(stack, 0, sizeof stack);
pid = syscall(SYS_clone, CLONE_VM|SIGCHLD, stack+sizeof(stack)-16);
if (pid == …
Run Code Online (Sandbox Code Playgroud)