Tag: linux

为什么不能触发子进程中的信号处理程序?

我的目的是为fgets创造1秒的时间。 如果在1秒内未收到任何输入,则程序终止。 我想出的设计是:父级为SIGALRM注册一个信号处理程序。 然后它会分叉一个触发SIGALRM的孩子,然后继续调用fgets。 SIGALRM将触发杀死父进程的处理程序。 但是当我在64位的ubuntu 14.04上执行此操作时,处理程序不会被触发,程序只是等待用户永远输入fgets。 为什么会发生这种情况,我该如何解决这个问题? #include “csapp.h” void handler() { printf(“lazy man\n”); kill(SIGKILL, getppid()); exit(0); } int main() { char buf[100]; signal(SIGALRM, handler); pid_t pid; if ((pid = fork()) == 0) { alarm(1); } else { fgets(buf, 100, stdin); printf(“%s”, buf); } return 0; } 〜

c中的简单shell:waitpid系统调用无法正常工作

我必须创建简单的shell来读取命令并按顺序执行它们。 condition不会改变main函数的forms,而execute函数应该是递归的。 主要问题是,似乎waitpid不起作用。 但我知道,我的代码中存在很多问题。 请让我知道我应该从哪里开始.. #include #include #include #include #include #include #define MAX 10 char cmmd[MAX][256]; int sp; char *argv[10]; int size; void ClearLineFromReadBuffer(void){ while(getchar() != ‘\n’); } void printCommands(){ size = sp+1; //print by moving stack pointer while(1){ if (sp==-1) break; printf(“Command line : %s\n”, cmmd[sp]); sp–; } printf(“print end\n”); } void readCommandLines(){ int […]

在这种情况下使用perror?

我写了一个小程序(带有来自SO的代码),它做了printenv | sort | less printenv | sort | less 现在我想用perror添加error handling并检查返回值。 我之前从未这样做过,但我认为它类似于exception处理。 我需要检查execvp,fork,pipe和dup2的错误。 我有这个代码 #include #include #include #include #include #include struct command { const char **argv; }; /* Helper function that spawns processes */ int spawn_proc (int in, int out, struct command *cmd) { pid_t pid; if ((pid = fork ()) == 0) { […]

奇怪的printf()行为

我附上这个问题的代码应该得到设备的mac地址并打印出来。 当我将结构ifreq中的数据复制到uint8_t数组时,它可以工作,但是当我没有,我得到奇怪的结果。 查看第二行和输出的第二个字节。 #include #include #include #include #include #include #include #include int main() { uint8_t tab[6]; int i; struct ifreq one; int sd = socket(AF_PACKET, SOCK_RAW, 0); memset(&one, 0, sizeof(struct ifreq)); strcpy(one.ifr_name, “eth0”); int v = ioctl(sd, SIOCGIFHWADDR, &one); memcpy(tab, one.ifr_hwaddr.sa_data, sizeof(uint8_t) * 6); for(i=0;i<6;i++) printf("%02x:", tab[i]); printf("\n"); for(i=0;i<6;i++) printf("%02x:", one.ifr_hwaddr.sa_data[i]); printf("\n"); } OUTPUT: 74:86:7A:0A:2C:6D: […]

如何用ptrace跟踪程序执行?

我一直在尝试使用系统调用“ptrace”(使用PTRACE_SINGLESTEP宏)来跟踪简单应用程序的执行。 在记录程序的执行时,我想跳过阅读中无用的部分,只能从我的应用程序的“主要”开始。 因为每当我启动我的跟踪器时,我都会获得大约100k的执行步骤。 Cordialy

POSIX系统(3)会立即调用异步shell命令吗?

例如, system(“sh /mydir/some-script.sh &”)

民意调查如何处理封闭管道

我想知道: pipe关闭时,将为管道的文件描述符设置什么状态poll ? 我尝试下面的代码,在子进程关闭所有文件描述符后,轮询只是认为所有文件描述符都可以读取! 是对的吗? 或者我在这段代码中犯了一些错误? 我使用SUSE和gcc。 #include #include #include “../../../myInclude/apue.h”// this is ok #include int main(int argc, char **argv) { int fd1[2]; int fd2[2]; int fd3[2]; pid_t pid; if(pipe(fd1)<0 ||pipe(fd2)<0 ||pipe(fd3) <0) err_sys("pipe error");//this is a error deal function .it will exit the program and print error message. if((pid = fork()) 0) { printf(“now […]

perf记录如何获取目标进程的指令的虚拟内存地址以及用于存储它的数据结构

我正在阅读perf的源代码,并尝试理解perf record如何获取触发perf计数的目标进程的指令的虚拟内存地址(例如, call test的虚拟地址,即汇编代码监视通过perf report的注释)。 什么数据结构用于存储使perf事件发生并触发计数器的指令的虚拟地址?

如何在C中获得Linux中的绝对鼠标位置

据我所知,获取鼠标位置的两种方法是使用libgpm或读取/dev/input/mice文件。 但后者只是从最后一个位置返回一个相对位置。 所以我的问题是如何通过阅读/dev/input/mice或其他方式获得绝对鼠标位置 。 我想用C或C++实现这个function。 任何信息将不胜感激。

触发服务从服务本身重新启动

我开发了一个应用程序mysvc ,它通过/etc/init.d/mysvc-service作为(Peta)Linux服务运行(名称应该不同,因为它们在petalinux / yocto词汇表中是不同的“应用程序”)。 /usr/bin/mysvc通过start-stop-daemon : # start() start-stop-daemon -S -o –background -x /usr/bin/mysvc # stop() start-stop-daemon -K -x /usr/bin/mysvc 它嵌入了一个简单的HTTP服务器,允许盒重启/关闭(工作),我想添加一个只运行/etc/init.d/mysvc-service restart的Restart按钮(从命令行运行正常)。 当我想从程序本身使用Linux /etc/init.d/系统(构建命令行参数等)时,我检查了另一个重新运行程序本身的问题 (即响应HTTP请求)由我的服务器处理)所以我尝试了以下方法: daemon() 将调用daemon()基本上fork()和exit()父进程。 子进程实际上只是运行/etc/init.d/mysvc-service start : if (daemon(1,1) == 0) { // Forks and exit() the parent. We are the child system(“/etc/init.d/mysvc-service start”); // “start” and not “restart” because the parent […]