社区
Linux/Unix社区
帖子详情
为什么以下代码不能调用find_task_by_id
kes2000
2007-08-26 10:17:03
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
void execname() {
struct task_struct *my;
my = find_task_by_id(getpid());
printf("%s",my->comm);
}
int main() {
execname();
return 0;
}
...全文
139
1
打赏
收藏
为什么以下代码不能调用find_task_by_id
#include #include #include void execname() { struct task_struct *my; my = find_task_by_id(getpid()); printf("%s",my->comm); } int main() { execname(); return 0; }
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
1 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
cceczjxy
2007-08-26
打赏
举报
回复
find_task_by_id
是个内核函数,你在用户空间没法调用.你不能这样直接使用它.
下边是别人写的,可以看看
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/proc_fs.h>
#include<linux/init.h>
#include<linux/sched.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("scutan");
static int find_read(char *buffer, char **buffer_location, off_t offset, int count, int *eof, void *data)
{
struct task_struct *p;
int pid;
pid = current->pid;
p = find_task_by_pid(pid);
sprintf(buffer, "%d\t%s\n", p->pid, p->comm);
return strlen(buffer);
}
static int __init find_init(void)
{
struct proc_dir_entry *entry;
entry = create_proc_entry("findpid", 0644, NULL);
if (entry == 0)
{
printk(KERN_ERR "creat_proc_entry failed\n");
return -1;
}
entry->mode = S_IFREG | S_IRUGO;
entry->size = 100;
entry->owner = THIS_MODULE;
entry->uid = 0;
entry->gid = 0;
entry->read_proc = find_read;
return 0;
}
void __exit find_exit(void)
{
remove_proc_entry("findpid", &proc_root);
}
module_init(find_init);
module_exit(find_exit);
Makefile
代码:
obj-m:=find.o
KERNELDIR:=/usr/src/linux
default:
make -C $(KERNELDIR) M=$(shell pwd) modules
install:
insmod find.ko
uninstall:
rmmod find.ko
clean:
rm -rf *.o *.mod.c *.ko
另外还有一个测试程序get.c
代码:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
int fd;
char buf[50];
fd = open("/proc/findpid", O_RDONLY);
if (fd < 0)
{
printf("error\n");
exit(1);
}
memset(buf, 0, sizeof(buf));
if (read(fd, buf, 50) < 0)
{
printf("read error\n");
exit(1);
}
printf("buf = %s\n", buf);
printf("getpid = %d\n", getpid());
return 0;
}
find_
task
_by_p
id
() / get_
task
_struct()/p
id
_
task
()
本文深入解析Linux内核(特别是5.10+版本)中基于P
ID
查找进程结构体的核心机制,重点阐述find_
task
_by_p
id
()(或其替代接口p
id
_
task
())、get_
task
_struct()及put_
task
_struct()的协同工作原理。详细说明P
ID
哈希表加速查找、
task
_struct引用计数(usage字段)的原子增减逻辑、新版P
ID
命名空间重构下的struct p
id
与
task
_struct解耦关系,并强调‘查得即锁定’的安全范式:先p
id
_
task
()获取指针,再get_
task
_struct()保活,使用后务必put_
task
_struct()释放,以防野指针和内存泄漏。
进程管理API之find_
task
_by_vp
id
本文介绍了find_
task
_by_vp
id
函数的功能及使用方法,该函数用于根据虚拟进程
ID
(vp
id
)在当前任务的命名空间中查找对应的
task
_struct。文中通过move_pages系统
调用
示例展示了如何使用此函数,并解释了其内部实现原理。
find_
task
_by_vp
id
undefined 问题
在Linux内核版本2.6.34中,由于find_
task
_by_vp
id
函数未被导出,导致内核模块无法正确加载。文章详细介绍了这一问题的背景、原因以及解决方法,即使用p
id
_
task
和find_vp
id
进行替代,以确保模块在不同内核版本下的兼容性和正确性。
进程管理API之find_
task
_by_p
id
_ns
本文解析了find_
task
_by_p
id
_ns函数的工作原理,该函数用于根据进程
ID
和命名空间查找对应的
task
_struct。文章深入介绍了其内部实现过程,包括RCU锁的使用、struct p
id
结构的解析及hlist_entry的应用。
rails之动态find_by方法 转摘
本文介绍Rails框架中如何使用动态查询方法,如find_all_by_complete及find_by_columnName等,并解析其内部实现原理,同时展示了如何利用_and_连接符添加更多查询条件。
Linux/Unix社区
23,223
社区成员
74,536
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章