Tag: linux

用C ++执行脚本

我想通过c ++程序执行脚本并获取其输出。 现在我在做 system(“./script.sh > out.txt”); 但是我需要一个命令来将输出转换为字符串,例如: out = system(“./script.sh”); printf(out); 执行脚本后我无法读取文件out.txt,因为我没有权限。 我在其他框架(boinc)上部署了我的c ++程序,但没有给我这个权限。 有人有提示吗? 提前致谢! 费利佩

SDL保存窗口为BMP

我正在用SDL和C编写程序,我希望能够将窗口保存为图像。 这是我的代码: screen = SDL_GetWindowSurface(win); SDL_SaveBMP(screen,”screen”); 但是当我执行它时,我得到: Segmentation Fault 从其他来源我收集它的指针和内存访问。 有帮助吗?

在vfork()/ clone()中调用execv()之前的setuid()

我需要从服务器派生一个exec。 由于我的服务器内存占用量很大,我打算使用vfork() / linux clone() 。 我还需要为stdin / stdout / stderr打开管道。 这是clone() / vfork()允许的吗?

像树命令一样的打印线

我正在尝试为工作中的任务学习C编程,我为自己设置了一个小项目,其中包括读取包含所有子目录的文件树,以获取有关每个文件的信息。 我无法解决的问题是如何在打印所有目录时创建行,就像真正的树命令一样。 这是我的示例代码: enum { doSkip, isFile, isDir } testDir(char *path, char *name) { struct stat st_buf; if (strcmp(name, “.”) == 0 || strcmp(name, “..”) == 0) { return doSkip; } stat(path, &st_buf); if (S_ISDIR(st_buf.st_mode)) return isDir; return isFile; } void list(const char *path, int indentlevel) { DIR *dirp = opendir(path); struct dirent *dentry; char […]

从Assembly调用C函数(printf)时的Segfault

我在linux上使用NASM编写一个基本的汇编程序,它从C库(printf)调用一个函数。 不幸的是,我这样做会导致分段错误。 注释掉对printf的调用允许程序无错误地运行。 ; Build using these commands: ; nasm -f elf64 -g -F stabs .asm ; gcc .o -o ; SECTION .bss ; Section containing uninitialized data SECTION .data ; Section containing initialized data text db “hello world”,10 ; SECTION .text ; Section containing code global main extern printf ;————- ;MAIN PROGRAM BEGINS HERE […]

如何在Linux中使用c的`getch`function?

我在Linux mint中安装了ncurses库,但我仍然不能在c中使用getch函数。 我正在使用Linux mint 18.2。 这是我的计划: #include #include int main() { char k; printf(“how are you”); k = getch(); printf(“%c”,k); } 这是输出: ram@ram$ gcc-7 test.c -lcurses ram@ram$ ./a.out how are you ram@ram$ 它不会等我按任何键并快速终止。 我不想为Linux安装conio.h 。 如何在Linux中使用getch和getche函数? 请不要告诉我自己的function。 我还是个菜鸟。 或者必须有替代品。

如何编译这个C程序?

当我输入gcc gprof-helper.c来编译程序时,我得到以下错误: gprof-helper.c: In function `wooinit’: gprof-helper.c:38: error: `RTLD_NEXT’ undeclared (first use in this function) gprof-helper.c:38: error: (Each undeclared identifier is reported only once gprof-helper.c:38: error: for each function it appears in.) 这是程序文件: #define _GNU_SOURCE #include #include #include #include #include static void * wrapper_routine(void *); /* Original pthread function */ static int (*pthread_create_orig)(pthread_t *__restrict, __const […]

`read()`可以直接跟在`read()`和`read()`之间的`read()`吗?

在C标准库中,输出不能跟随输入,反之亦然。 对于Linux API, read()可以通过read()直接后跟write()和write() read()吗? 如果是,为什么Linux API和C库IO API之间存在这样的差异? 谢谢。

使用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 […]

Linux c / c ++如何从ThreadID中查找PID

我目前正在编写一个应该实现基于进程的访问权限的FUSE应用程序。 我现在偶然发现,FUSE只提供ThreadID,而不是ProcessID。 现在我需要找到不同进程的给定线程ID的PID(或线程组ID)。 我注意到proc fs提供了基于线程的信息(即使ls / proc没有显示任何PID TID,它们仍然可以通过/ proc / /访问)这样我可以问/ proc / / status关于Tgid,但由于我的FUSE应用程序每秒会提供数百个请求,我觉得这可能不是最好的方法。 有没有人知道这里使用的formsgettgid(tid)的系统调用或函数?