Tag: linux

libevent:如果事件是由malloc创建的,它是否允许在其回调函数中释放一个事件

我需要使用malloc创建事件,但我不知道在哪里释放它们,我想知道它是否允许在其回调函数中释放一个事件,如: struct event *pkt_ev = (struct event *)malloc(sizeof(struct event)); evtimer_set(&pkt_ev, timer_cb, &pkt_ev); event_base_set(base, &pkt_ev); event_add(&pkt_ev, timeout); 回调函数timer_cb(): timer_cb(int fd, short ev, void* arg){ ……. free(arg); // here the arg is &pkt_ev } 我最初的想法是:在调用回调函数timer_cb() ,libevent将隐式调用event_del(&pkt_ev) 。 但是因为我在回调中释放了&pkt_ev ,所以在event_del(&pkt_ev)上会出现崩溃/exception。 这样对吗? 但是,如果event_del(&pkt_ev)不关心pkt_ev指向的内容,那可能不是问题吗? 此外,在这个function: event_add(struct event *ev, struct timeval *timeout); ev指出的内容应该关注很多,一般应该是一个全局变量,或者它的生命周期应该覆盖事件循环(即,当事件循环函数运行时,它将访问ev指向的内容)。 超时指向的内容怎么样? 超时指向的内容是否应覆盖事件循环?

gtk-get在liststore treeview中单击哪个项目

我想获取在树视图中单击的项目,这会导致“行激活”事件。 我在树视图中有一个列表存储。 单击其中的任何项目将显示另一个列表。 但是我怎么知道点击了哪个项目? 我怎么找到的? 码- GtkWidget * init_tree() { GtkListStore *liststore = gtk_list_store_new(1, G_TYPE_STRING); GtkTreeIter treeiter; gtk_list_store_append(liststore, &treeiter); gtk_list_store_set(liststore, &treeiter, 0, “Register”, -1); gtk_list_store_append(liststore, &treeiter); gtk_list_store_set(liststore, &treeiter, 0, “New Configuration”, -1); gtk_list_store_append(liststore, &treeiter); gtk_list_store_set(liststore, &treeiter, 0, “Edit Configuration”, -1); gtk_list_store_append(liststore, &treeiter); gtk_list_store_set(liststore, &treeiter, 0, “Delete Configuration”, -1); gtk_list_store_append(liststore, &treeiter); gtk_list_store_set(liststore, &treeiter, 0, “Add location […]

可以写入,但无法从linux C程序中读取串口ttyS0

我正在尝试学习如何使用C对Linux中的ttyS0串口进行编程。我的另一台机器连接到我的串口,每隔两秒钟发送5f和6f的交替hex值。 我已经与其他端口监控应用程序validation了这些值是否出现在端口上。 在我的代码中,我使用阻塞read()到10个字符长度的缓冲区中。 即使我的其他机器仍在发送数据, read()也会永久阻止。 如果我包括行fcntl(fd,F_SETFL,FNDELAY); 将read()设置为非阻塞read()始终返回值为-1,这意味着UART缓冲区中没有数据,而我的for循环代码只打印出缓冲区中的随机值。 所以简而言之,我的假设是我的代码不是读取ttyS0而我不知道为什么。 下面是我的代码,希望有人会看到导致我的问题的原因并让我直截了当。 顺便说一下,我使用的是Scientific Linux,我相信ttyS0是com端口1,就像在RedHat和Fedora中一样。 以下是我运行代码时的输出。 它似乎是写入COM端口没有问题,但对于读取它说它不可用。 另外很明显,我打印出来的缓冲区只是随机值,而不是已读入的数据。谢谢 控制台输出 hello world hi again write error: : Success wrote 4 bytes number of bytes read is -1 read error:: Resource temporarily unavailable 4 8 120 -99 -73 -65 41 -120 4 8 should of put something out 码 #include #include […]

munmap,mmap的function是什么?

当我尝试研究一些处理FPGA的代码时,我遇到了munmap,mmap。 我浏览了这里提供的手册。 我仍然不理解这个function的目的。 究竟是什么呢?

C语言中的进程和无限循环用于Linux

这是我想要做的: 编写一个带有整数命令行参数n的C程序,生成n个进程,每个进程生成-100到100之间的随机数,然后计算并打印出这些随机数的总和。 每个进程都需要打印出它生成的随机数。 这是我到目前为止: #include #include #include #include #include #include #include int main(int argc, char *argv[]){ int command,processCheck; // processCheck: to check if fork was successful or not and to char * strNumProcess = NULL;// check the status of child process while((command = getopt(argc, argv, “n:”))!=-1){ if(command == ‘n’){ strNumProcess = optarg; break; } } […]

链接到共享符号名称的库的程序运行错误

这有点难以理解,但我正在尽我所能。 在Red Hat 6.4上使用gcc 4.4.6和ld 2.20.51, 我从一个共享库(.so)和一个静态库(.a)链接到二进制可执行PROGRAM代码。 共享库公开由PROGRAM直接调用的API。 该共享库的实现是针对静态库静态库lib1编译和链接的。 静态库还公开了自己的API,它由PROGRAM直接调用。 它的一些实现是基于直接复制到其中的静态lib1文件的一个子集。 任何API(任一库)都没有实际公开数据类型或静态lib1实现的函数。 因为代码被复制,所以符号是相同的。 在运行时我看到这种行为: 如果链接库的顺序是共享库 , 静态库 ,那么调用共享库API并调用静态库* API都将使用** static lib1中的实现 。 如果链接器的库顺序是静态库 , 共享库 ,那么调用共享库API并调用静态库API都将使用静态库1中的实现– 来自lib1的代码修改 。 如何调用静态库API以在lib 1中修改代码中运行实现,而在共享库API中运行实现在静态lib1中 ?

如何从Linux framebuffer获取RGB像素值?

我希望使用Linux以最有效的方式获得屏幕像素的RGB值。 所以我决定使用C( fb.h )中的framebuffer库来访问framebuffer设备( / dev / fb0 )并直接读取它。 这是代码: #include #include #include #include #include #include #include #include int main() { int fb_fd; struct fb_fix_screeninfo finfo; struct fb_var_screeninfo vinfo; uint8_t *fb_p; /* Open the frame buffer device */ fb_fd = open(“/dev/fb0”, O_RDWR); if (fb_fd < 0) { perror("Can't open /dev/fb0\n"); exit(EXIT_FAILURE); } /* Get fixed […]

复制目录内容

我想将内容目录(tmp1)复制到另一个目录(tmp2)。 tmp1可能包含文件和其他目录。 我想使用C / C ++复制tmp1的内容(包括模式)。 如果tmp1包含一个目录树,我想以递归方式复制它们。 什么是最简单的解决方案? 我找到了一个解决方案来打开目录并读取每个条目并使用cp命令复制它。 更简单的解决方案?

读取savefiles时,linktype 1上不支持入站/出站

从pcap文件获取传入数据包。 我在pcap_compile()中设置了“入站”filter,这里是部分代码。 pcap = pcap_open_offline(“test.pcap”, errbuf); if (pcap == NULL) { fprintf(stderr, “error reading pcap file: %s\n”, errbuf); exit(1); } char filter_exp[] = “inbound”; struct bpf_program pgm; if (pcap_compile(pcap, &pgm, filter_exp, 0, PCAP_NETMASK_UNKNOWN) == -1) { printf(“Bad filter – %s\n”, pcap_geterr(pcap)); return 1; } if (pcap_setfilter(pcap, &pgm) == -1) { printf(“Error setting filter – %s\n”, […]

如何在linux内核模块中使用get_random_bytes()?

我已经制作了简单的内核模块。 void cb_funct(unsigned long arg) // callback function. { int rand; get_random_bytes(&rand, sizeof(rand)); rand%=250; seq_printf(m, “random number : %d\n”, rand); … } 我使用seq_printf函数打印出rand变量。 cb_funct函数被调用五次。 结果如下。 随机数:66 随机数:-5 随机数:135 随机数:178 随机数:-42 为什么打印出负变量? 如何在linux中使用get_random_bytes函数?