Tag: posix

POSIX对C中指针类型的限制

背景 POSIX标准为C语言添加了许多库函数和其他标识符。 在dlsym()函数的描述中,它说(我强调): 概要 #include void *dlsym(void *restrict handle, const char *restrict name); 描述 dlsym ()函数应获取符号的地址( 函数标识符或数据对象标识符)… C标准不保证函数指针可以转换为void * ,甚至不能保证指针的大小相同。 这有效地增加了对C类型系统的额外限制。 题 我的问题是: 对于C类型系统的这种限制是否存在规范性参考,或者它是否只能从某些库函数的描述中推导出来? POSIX甚至可以在sizeof ( function pointer ) > sizeof (void *)吗? 参考 C11标准(最终公开草案): n1570 The Open Group的POSIX标准: POSIX.1-2008 POSIX dlsym()函数

推荐的信号要赶上?

目前,我抓住SIGSEGV,给自己发一封电子邮件,然后abort(),这样我就可以得到一个核心文件并调试我的程序。 (如果我没有抓到,我将无法知道我的特定程序是否已被发现。我的程序是在与我自己的服务器不同的服务器上运行的。) 是否有任何其他信号我应该抓住调试或我应该知道的原因?

阅读未初始化的内存空间总是不明智吗?

我正在重新创建整个标准C库,我正在为strle n实现一个我希望成为所有其他str函数的基础。 我目前的实施如下: int ft_strlen(char const *str) { int length; length = 0; while(str[length] != ‘\0’ || str[length + 1] == ‘\0’) length++; return length; } 我的问题是,当我通过一个str : char str[6] = “hi!”; 正如所料,内存读取: [‘h’][‘i’][‘!’][‘\0’][‘\0’][‘\0’][‘\0′] 如果你看看我的实现,你可以期望我得到6的回报 – 而不是3(我以前的方法),这样我就可以检查strlen可能包括额外分配的内存。 这里的问题是,我将不得不在初始化内存之外读取1个字节,以便在最后的null终结符处失败我的最后一个循环条件 – 这是我想要的行为。 然而,这通常被认为是不好的做法,并且有些是自动错误。 即使您非常具体地想要读入垃圾值(以确保它不包含’\ 0’),在初始化值之外读取也是一个坏主意吗? 如果是这样,为什么? 我明白那个: “buffer overruns are a favorite avenue for attacking secure programs” […]

使用switch语句分叉两个进程

我正在参加C课程的介绍,我对第一次任务感到有点难过。 我们的任务是创建父进程和两个子进程。 到目前为止,文本向我们展示的所有示例都涉及具有一个父项和一个子项的switch语句。 我对如何将其转换为一个父进程和两个子进程感到困惑。 这是我到目前为止: #include int main() { int i, pid, status; pid = fork(); switch(pid) { case -1: /* An error has occurred */ printf(“Fork Error”); break; case 0: /* This code is executed by the first parent */ printf(“First child process is born, my pid is %d\n”, getpid()); printf(“First child parent process […]

shmat返回分段falut与errno = 13(EACCES)

我只想测试shmget()和shmat(),但似乎有些不对劲。 🙁 shmget()运行良好,但shmat()导致分段错误。 这是代码: #include #include #include #include #include int main(void) { key_t key=98;/* yes, just 98 for test */ int shid; char *str=NULL; shid = shmget(key, 4096, IPC_CREAT); printf(“shid:%d\n”,shid); str=(char*)shmat(shid,NULL,0); printf(“str:%d\n”,(int)str); printf(“errno:%d\n”, errno); str[0] = ‘h’; str[1] = ‘\0’; return 0; } 这是输出: shid:28246036 str:-1 errno:13 zsh: segmentation fault ./t1 thx:D

C pluginsystem:符号查找错误

我正在编写一个插件系统,它与其他3个模块分开: plugin_system.c – 系统的核心 list.c – 包含插件存储的链接列表实现 plugin_interface.h – 包含插件所需的声明,没有与之关联的源文件 plugin_interface.h只包含类型和函数: extern int plugin_register(PluginManager *plug_manager, const char *name, Plugin *plug); 这是在plugin_system.c中定义的 加载插件时,插件系统会查找函数init_plugname()并调用它,该函数必须调用plugin_register来注册插件。 该程序使用复杂的递归Makefile编译(不是最好的主意),但我尝试实现的是: 我在主程序文件夹中编译插件系统对象,然后将其与主程序链接。 从make执行: gcc -Wall -O2 -std=gnu99 -D DEBUG -g -fPIC -c -o /home/kowa/code/reseaux/projet/ringo/c/bin/list.o list.c gcc -Wall -O2 -std=gnu99 -D DEBUG -g -fPIC -c -o /home/kowa/code/reseaux/projet/ringo/c/bin/plugin_system.o plugin_system.c 插件使用gcc -fPIC -c -o plugname.o plugname.c […]

是否存在可用于在POSIX程序中计算TCP段校验和的预先存在的函数或代码

我正在写一个小POSIX程序,我需要计算TCP段的校验和,我想使用现有的函数,以避免自己写一个。 像(伪代码)的东西: char *data = …. u16_integer = computeChecksum(data); 我在网上搜索但我找不到正确的答案,有什么建议吗?

POSIX信号量和pthread问题

我正在使用POSIX库练习信号量。 我试图通过一个信号量(代表一个服务器)传递线程(代表客户),这个信号量位于两个表(每个由sempahores控制)下的8个人。 我认为罪魁祸首是解锁和锁定多个信号量的顺序,但我似乎无法针对非法指令(核心转储)错误的来源。 EDITED – 互斥初始化的反向顺序和创建线程循环 – 添加返回NULL到eat()的结尾: #include #include #include #include sem_t server_sem; int server_pshared; int server_ret; int server_count = 10; sem_t tablea_sem; int tablea_pshared; int tablea_ret; int tablea_count = 4; sem_t tableb_sem; int tableb_pshared; int tableb_ret; int tableb_count = 4; //server_ret = serm_open(“serverSem”, O_CREAT | O_EXCL, 0644, server_count); int customer_count = 10; pthread_t […]

struct itimerspec as timer_create的参数无效参数

我正在尝试使用POSIX信号处理POSIX定时器。 当我尝试执行你可以找到的代码时,我得到: Errore timer_settime:参数无效 在GAPIL上,这是基于高级Linux编程和Unix网络编程的,我读到这可能发生在new_value.value里面你指定了一个负时间值或者高于999999999的纳秒数。但我认为我使用过的参数没关系…… #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void termination_handler(int signum) { printf(“Timer scaduto\n”); } int main() { timer_t timer1; struct sigevent sigeventStruct; sigeventStruct.sigev_notify = SIGEV_SIGNAL; sigeventStruct.sigev_signo = 10; if(timer_create(_POSIX_MONOTONIC_CLOCK, &sigeventStruct, &timer1) == -1) { printf( “Errore timer_create: %s\n”, strerror( errno ) […]

什么时候pthread_attr_t不是NULL?

除了pthread_attr_t之外,来自POSIX线程的pthread_create的所有参数都非常简单易懂。 pthread_attr_t是什么,如何以及何时不应该被NULL初始化? 我浏览了Linux 手册页 。 我发现有关pthread_attr_t的描述是: 句法: int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void*),void *arg); 说明: The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new thread; this structure is initialized using pthread_attr_init(3) and related functions. If attr is NULL, then […]