免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1976 | 回复: 0
打印 上一主题 下一主题

/proc/self/exe与/proc/pid号/exe的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-29 15:05 |只看该作者 |倒序浏览
向各位请教在开发中碰到一个很奇怪的问题。有一个hello.c程序,还有一个test.c程序中使用readlink(2)通过/proc/self/exe来获得当前运行程序的,通过gcc test.c -o test -static编译,在ia64,x86_64和i386上都可以正常运行,但是当我在编译hello.c时通过gcc -Wl,--dynamic-linker test hello.c -o hello将test作为linker的时候,在i386和x86_64上用readlink(2)通过/proc/self/exe来获得当前运行程序失败,但在ia64上会成功:
[root@hpc-rm ld]# ./hello
pid is 22226
string is /proc/22226/exe
readlink: No such file or directory
[root@hpc-rm ld]# ./test
pid is 22239
string is /proc/22239/exe
real file is /home/baiwd/ld/test
     修改成通过/proc/pid号/exe来读也是同样的效果。而且通过ll /proc/22239/看是有exe这个文件的,但通过ll /proc/22239/exe来看却报No such file or directory。
附test.c文件:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
   char elf_file[65536];
   pid_t pid;
   char string[100];

   pid = getpid();
   printf("pid is %ld\n", pid);

   sprintf(string, "/proc/%ld/exe", pid);
   printf("string is %s\n", string);
   sleep(20);
   if (readlink(string, elf_file, 65536) == -1) {
        perror("readlink");
        return -1;
   }
   printf("real file is %s\n", elf_file);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP