Chinaunix首页 | 论坛 | 博客
  • 博客访问: 768009
  • 博文数量: 247
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 501
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-12 21:53
个人简介

系统未建立

文章分类

全部博文(247)

文章存档

2021年(1)

2020年(3)

2019年(5)

2018年(3)

2017年(44)

2016年(75)

2015年(52)

2014年(63)

2013年(1)

我的朋友

分类: LINUX

2016-12-30 14:00:25

今天调试lxc,发现不能工作,问题追踪到tmpfile调用失败,No Such File or Directory????好奇怪
构造测试程序: 
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
int main(void)
{
FILE*tempfp;
#ifdef __UCLIBC_HAS_LFS__
printf("#ifdef __UCLIBC_HAS_LFS__\n");
#endif
tempfp=tmpfile();
if(tempfp)
printf("Temporary file created\n");
else
{
printf("Unable to create temporary file,err:%d\n",errno);
exit(1);
}
return 0;
}
确实在某些环境OK,某些环境失败,很是奇怪.
找tmpfile在C库里面的实现,发现了原因:
tmpfile创建的临时文件默认建立在/tmp目录,而根文件系统里面没有/tmp。
在根文件系统里面添加/tmp目录后,可以成功创建临时文件


另外1个好像问题,临时文件在哪里
#include
#include
int main(void)
{
char s[100];
FILE*tempfp;
#ifdef __UCLIBC_HAS_LFS__
printf("#ifdef __UCLIBC_HAS_LFS__\n");
#endif
tempfp=tmpfile();
if(tempfp)
printf("Temporary file created\n");
else
{
printf("Unable to create temporary file\n");
exit(1);
}
//sleep(10);
sprintf(s, "ls -l /proc/%d/fd", getpid());
system(s);
sleep(10);
return 0;
}
xxx:~/test$ gcc tmpfileT.c -o tmpfileT
xxx:~/test$ ./tmpfileT &
[1] 1523
xxx:~/test$ Temporary file created
total 0
lrwx------ 1 wangshixin wangshixin 64 12月 30 13:34 0 -> /dev/pts/3
lrwx------ 1 wangshixin wangshixin 64 12月 30 13:34 1 -> /dev/pts/3
lrwx------ 1 wangshixin wangshixin 64 12月 30 13:34 2 -> /dev/pts/3
lrwx------ 1 wangshixin wangshixin 64 12月 30 13:34 3 -> /tmp/tmpfaAOphK (deleted)
ls -l /tmp/
total 8
drwx------ 2 wangshixin wangshixin 4096 12月 29 14:17 ssh-cColJwaJzP
drwx------ 2 wangshixin wangshixin 4096 12月 29 16:57 ssh-IRqFCfsiNe
xxx:~/test$ 
[1]+  Done                    ./tmpfileT
3 -> /tmp/tmpfaAOphK (deleted)
给了个解释: 
一般来说,tmpfile是根据时间算出的唯一的文件名,一般在/tmp下,open系统调用创建之后,马上unlink系统调用,当最终close之后,文件就从文件系统中移除了


阅读(1062) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~