小编Wil*_*ill的帖子

x86指令缓存是如何同步的?

我喜欢这个例子,所以我在c中写了一些自修改代码...

#include <stdio.h>
#include <sys/mman.h> // linux

int main(void) {
    unsigned char *c = mmap(NULL, 7, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|
                            MAP_ANONYMOUS, -1, 0); // get executable memory
    c[0] = 0b11000111; // mov (x86_64), immediate mode, full-sized (32 bits)
    c[1] = 0b11000000; // to register rax (000) which holds the return value
                       // according to linux x86_64 calling convention 
    c[6] = 0b11000011; // return
    for (c[2] = 0; c[2] < 30; c[2]++) { // incr immediate data after every run
        // rest of …
Run Code Online (Sandbox Code Playgroud)

c assembly instructions self-modifying cpu-cache

24
推荐指数
3
解决办法
5151
查看次数

如何防止GtkAspectFrame浪费空间?

<interface>
 <object class='GtkWindow' id='window'>
  <child>
   <object class='GtkBox' id='box'>
    <property name='orientation'>horizontal</property>
    <child>
     <object class='GtkAspectFrame' id='aspect_frame'>
      <property name='xalign'>0.0</property>
      <property name='yalign'>0.0</property>
      <child>
       <object class='GtkDrawingArea' id='drawing_area_A'>
        <property name='expand'>TRUE</property>
       </object>
      </child>
     </object>
    </child>
    <child>
     <!-- widget B goes here -->
    </child>
   </object>
  </child>
 </object>
</interface>
Run Code Online (Sandbox Code Playgroud)

上述GtkBuilder UI定义创建一个正方形绘图区.我希望它在窗口中尽可能大,所以我将Aexpand属性设置为.但是,当窗口本身的宽度大于高度时,我想要一个小部件B来填充绘图区域未使用的剩余宽度.基本上,我想要这个:TRUE

+--------------------+
|+-----------++-----+|
||           ||     ||
||     A     ||  B  ||
||           ||     ||
||           ||     ||
|+-----------++-----+|
+--------------------+
Run Code Online (Sandbox Code Playgroud)

但是,我得到这个代替(如expand设置FALSE):

+--------------------+
|+-----------+    +-+| …
Run Code Online (Sandbox Code Playgroud)

gtk gtk3

9
推荐指数
1
解决办法
360
查看次数

如何在C中打印errno的符号名称?

我可以使用perror()strerror()打印属于a的"人类可读"错误消息errno,但是如果我还要打印符号名称(例如" EAGAIN")的话errno呢?

任何方便的功能或宏来做到这一点?

更新:附上我最终编写的代码,基于下面接受的答案及其评论的想法:

#include <ctype.h>
#include <errno.h>
#include <stdio.h>

int get_errno_name(char *buf, int buf_size) {
    // Using the linux-only gawk instead of awk, because of the convenient
    // match() functionality. For Posix portability, use a different recipe...
    char cmd[] = "e=       && " // errno to be inserted here (max digits = 6)
                 "echo '#include <errno.h>' | "
                 "gcc -dM -E - | " // optionally, use …
Run Code Online (Sandbox Code Playgroud)

c printf errno strerror

8
推荐指数
2
解决办法
4697
查看次数

我可以部分/选择性地内联函数吗?

void run_hot(void) {
    // I am called very often!
    serve();
    // <more code here>
}

void run_cold(void) {
    // I am called only occasionally!
    serve();
    // <more code here>
}

???inline??? void serve(void) {
    // I only want to be called inline from hot functions!
    // <more code here>
}
Run Code Online (Sandbox Code Playgroud)

有没有什么办法,明确内联函数一个函数中,同时明确在函数内联相同功能的Ç?还是我完全受编译器的支配?

c inline c99

6
推荐指数
1
解决办法
163
查看次数

给定任何epoll TCP套接字事件,如果EPOLLRDHUP = 0且EPOLLIN = 1; 是对read()/ recv()的后续调用,保证返回不等于0的读取大小?

从epoll_ctl的手册:

EPOLLRDHUP(自Linux 2.6.17起)

流套接字对等关闭连接,或关闭写入一半的连接.(此标志对于编写简单代码以在使用边缘触发监视时检测对等关闭特别有用.)

从recv手册:

如果没有可用的消息被接收并且对等体已经执行了有序关闭,则recv()将返回0.

在我看来,那么,以上的盖的同一场景,只要我抓住EPOLLRDHUP事件首先,我不应该收到read()或长度为零的recv()(因此不需要费心检查这样的).但这是否保证是真的?

c sockets linux epoll tcp

6
推荐指数
1
解决办法
2846
查看次数

期望在Linux中,fd <打开文件描述符的最大数量是否合理?

我正在编写一个需要处理许多开放套接字的服务器,所以我setrlimit()用来设置打开文件描述符的最大数量(以root身份,在删除权限之前),如下所示:

#include <sys/resource.h>
#define MAX_FD_C 9001

if (setrlimit(
      RLIMIT_NOFILE, &(struct rlimit){.rlim_cur = MAX_FD_C, .rlim_max = MAX_FD_C}
    ) == -1) {
    perror("Failed to set the maximum number of open file descriptors");
    return EXIT_FAILURE;
}
Run Code Online (Sandbox Code Playgroud)

现在,我意识到可能没有任何保证,并且我受Linux内核用于实现文件描述符表的任何方法的支配; 但实际上,假设这个程序从Linux内核收到的任何fd的值都小于我在上面设置的MAX_FD_C,这是否合理?

我想每个插槽的数据,以保持尽可能的紧凑它可以简单地使用像数组是指static struct client clients[MAX_FD_C] = {{0}};与使用FD作为索引到客户端结构(这基本上是我自己的版本的FDT的).

c linux file-descriptor linux-kernel setrlimit

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

联盟规模大于预期.如何在这里进行类型对齐?

#include <stdio.h>

union u1 {
    struct {
        int *i;
    } s1;
    struct {
        int i, j;
    } s2;
};

union u2 {
    struct {
        int *i, j;
    } s1;
    struct {
        int i, j;
    } s2;
};

int main(void) {
    printf("        size of int: %zu\n", sizeof(int));
    printf("size of int pointer: %zu\n", sizeof(int *));
    printf("   size of union u1: %zu\n", sizeof(union u1));
    printf("   size of union u2: %zu\n", sizeof(union u2));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果是:

$ gcc -O -Wall -Wextra -pedantic -std=c99 …
Run Code Online (Sandbox Code Playgroud)

c pointers sizeof alignment unions

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