在C中,我需要知道结构的大小,其中包含函数指针.我可以保证在所有平台和架构上:
我认为所有这些答案都是肯定的,但我想确定.对于上下文,我正在呼唤sizeof(struct mystruct),仅此而已.
在阅读了这篇回答和Robert Love的"Linux内核开发"之后,随后,在clone()系统调用中,我发现Linux中的进程和线程(几乎)与内核无法区分.他们之间有一些调整(在引用的SO问题中被讨论为"更多共享"或"更少共享"),但我仍然有一些问题尚未得到解答.
我最近参与了一个涉及几个POSIX线程的程序,并决定在这个前提下进行实验.在创建两个线程的进程中,所有线程当然都会获得一个返回的唯一值pthread_self(),但不是getpid().
我创建的示例程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
void* threadMethod(void* arg)
{
int intArg = (int) *((int*) arg);
int32_t pid = getpid();
uint64_t pti = pthread_self();
printf("[Thread %d] getpid() = %d\n", intArg, pid);
printf("[Thread %d] pthread_self() = %lu\n", intArg, pti);
}
int main()
{
pthread_t threads[2];
int thread1 = 1;
if ((pthread_create(&threads[0], NULL, threadMethod, (void*) &thread1))
!= 0)
{
fprintf(stderr, "pthread_create: error\n");
exit(EXIT_FAILURE);
} …Run Code Online (Sandbox Code Playgroud) 谁能解释一下linux中list_for_each_entry和... entry_safe循环的工作原理.它像是
list_for_each_entry(type *cursor, struct list_head *list, member)
list_for_each_entry_safe(type *cursor, type *next, struct list_head *list,member)
所有这些参数的作用是什么以及它们如何用于遍历列表.
提前致谢
所以我知道如何通过简单地包含linux/sched.h和使用以下代码来创建内核并线性迭代这些进程:
struct task_struct *task;
for_each_process(task)
{
printk("Name: %s PID: [%d]\n", task->comm, task->pid);
}
Run Code Online (Sandbox Code Playgroud)
如何使用深度优先搜索打印这些任务?我希望我的输出类似于其中一个ps -eLf.
以下代码补丁已供参考:
struct task_struct *task;
struct list_head *list;
list_for_each(list, &init_task->children) {
task = list_entry(list, struct task_struct, sibling);
/* task points to the next child in the list */
}
Run Code Online (Sandbox Code Playgroud)
我知道它task->comm返回名称并task->pid返回该任务的PID.
什么命令用于返回状态和父pid?
我正在尝试了解Linux内核链表API.
根据Linux内核链接列表我应该初始化列表,INIT_LIST_HEAD但在这里(Linux内核程序) 建议使用它LIST_HEAD_INIT.
这是我写的一个工作代码,但我不确定我是否以正确的方式做到了.有人可以验证它没问题吗?
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
typedef struct edge_attr {
int d;
struct list_head list;
} edge_attributes_t;
typedef struct edge {
int id;
edge_attributes_t *attributes;
} edge_t;
int main () {
int i;
struct list_head *pos;
edge_attributes_t *elem;
edge_t *a = (edge_t*)malloc(sizeof(edge_t));
a->id = 12;
a->attributes = (edge_attributes_t*) malloc(sizeof(edge_attributes_t));
INIT_LIST_HEAD(&a->attributes->list);
for (i=0; i<5; ++i) {
elem = (edge_attributes_t*)malloc(sizeof(edge_attributes_t));
elem->d = i;
list_add(&elem->list, &a->attributes->list);
}
list_for_each(pos, &(a->attributes->list)) {
elem = …Run Code Online (Sandbox Code Playgroud) 在Linux Kernel Development,第3版中,这段代码用于遍历当前进程的子代.
list_for_each(list, ¤t->children) {
task = list_entry(list, struct task_struct, sibling);
/* task now points to one of current’s children */
}
Run Code Online (Sandbox Code Playgroud)
这个成语中的"兄弟姐妹"看起来不合时宜.它的目的是什么?
我想到一个进程的子任务(进程)列表,这里是代码:
void myFunc()
{
struct task_struct* current_task;
struct task_struct* child_task;
struct list_head children_list;
current_task = current;
children_list = current_task->children;
child_task = list_entry(&children_list,struct task_struct,tasks);
printk("KERN_INFO I am parent: %d, my child is: %d \n",
current_task->pid,child_task->pid);
}
Run Code Online (Sandbox Code Playgroud)
当前的pid是正确的,但是孩子的pid不正确.我究竟做错了什么?
我是模块编程的新手,我需要进行系统调用以检索系统进程并显示他们消耗了多少CPU.
我该如何打电话?
对于冗长的帖子道歉,我在以较短的方式制定它时遇到了麻烦.此外,这可能更适合Unix和Linux Stack Exchange,但我会先在SO处尝试,因为有一个ftrace标签.
无论如何 - 我想观察用户程序的机器指令是否在完全function_graph捕获的上下文中执行ftrace.一个问题是我需要这个旧内核:
$ uname -a
Linux mypc 2.6.38-16-generic #67-Ubuntu SMP Thu Sep 6 18:00:43 UTC 2012 i686 i686 i386 GNU/Linux
Run Code Online (Sandbox Code Playgroud)
......在这个版本中,没有UPROBES- 正如3.5 [LWN.net]中的Uprobes应该能够做到这一点.(只要我不需要修补原始内核,我就会愿意尝试使用树构建的内核模块,因为User-Space Probes(Uprobes)[chunghwan.com]似乎可以证明;但是至于我可以从0看到:基于Inode的uprobes [LWN.net],2.6可能需要一个完整的补丁)
但是,在这个版本上,有一个/sys/kernel/debug/kprobes,和/sys/kernel/debug/tracing/kprobe_events; 和Documentation/trace/kprobetrace.txt意味着可以直接在地址上设置kprobe; 即使我无法在任何地方找到如何使用它的例子.
在任何情况下,我仍然不确定使用什么地址 - 作为一个小例子,假设我想跟踪程序main功能的开始wtest.c(包括在下面).我可以这样做来编译并获得一个机器指令汇编列表:
$ gcc -g -O0 wtest.c -o wtest
$ objdump -S wtest | less
...
08048474 <main>:
int …Run Code Online (Sandbox Code Playgroud) 我试图遍历linux内核中的task_struct的子节点并从子节点获取信息.我遇到了所有信息的问题,所以让我们保持简单的pid.
这是我的代码的相关部分.
struct list_head * p;
struct task_struct ts, *tsk;
pid_t tmp_pid;
INIT_LIST_HEAD(&ts.children);
current = tsk;
list_for_each(p, &(tsk->children)){
ts = *list_entry(p, struct task_struct, children);
tmp_pid = ts.pid;
printk("the pid is %d\n", tmp_pid);
}
Run Code Online (Sandbox Code Playgroud)
我认为问题在于list_entry但我不知道如何解决它,我能找到的所有例子似乎都是以同样的方式调用它.
这应该打印出所有子PID,而不是我总是得到相同的数字-17 ....它是10 ^ 9或10 ^ 11的数量级.
有人可以帮我从这里出去吗?编译大约需要30分钟,所以尝试记录不同的东西并不是一个真正的选择.
我需要从内核向用户空间函数发送一个字符串,而不需要特别从用户空间请求它,有点通过内核中的某个事件触发用户空间中的函数或应用程序。到目前为止,我已经尝试了一个 Ioctl,它从用户空间中的 init 开始,然后休眠并继续阅读有关 netlink 的内容,但找不到一个很好的工作示例。任何建议或示例将非常感谢。
linux-kernel ×9
c ×5
linked-list ×3
linux ×2
task ×2
debugging ×1
ftrace ×1
list ×1
pointers ×1
sizeof ×1
system-calls ×1