Ç°ÑÔ:
ÎÒÃÇÔÚÕâÒ»½Ú½«ÒªÌÖÂÛlinuxÏÂÎļþ²Ù×÷µÄ¸÷¸öº¯Êý.
ÎļþµÄ´´½¨ºÍ¶Áд
ÎļþµÄ¸÷¸öÊôÐÔ
Ŀ¼ÎļþµÄ²Ù×÷
¹ÜµÀÎļþ
--------------------------------------------------------------------------------
1¡£ÎļþµÄ´´½¨ºÍ¶Áд
ÎÒ¼ÙÉèÄãÒѾ֪µÀÁ˱ê×¼¼¶µÄÎļþ²Ù×÷µÄ¸÷¸öº¯Êý(fopen,fread,fwriteµÈµÈ).µ±È»Èç¹ûÄã²»Çå³þµÄ»°Ò²²»Òª×ż±.ÎÒÃÇÌÖÂÛµÄϵͳ¼¶µÄÎļþ²Ù×÷ʵ¼ÊÉÏÊÇΪ±ê×¼¼¶Îļþ²Ù×÷·þÎñµÄ.
µ±ÎÒÃÇÐèÒª´ò¿ªÒ»¸öÎļþ½øÐжÁд²Ù×÷µÄʱºò,ÎÒÃÇ¿ÉÒÔʹÓÃϵͳµ÷Óú¯Êýopen.ʹÓÃÍê³ÉÒÔºóÎÒÃǵ÷ÓÃÁíÍâÒ»¸öcloseº¯Êý½øÐйرղÙ×÷.
#include
#include
#include
#include
int open(const char *pathname,int flags);
int open(const char *pathname,int flags,mode_t mode);
int close(int fd);
openº¯ÊýÓÐÁ½¸öÐÎʽ.ÆäÖÐpathnameÊÇÎÒÃÇÒª´ò¿ªµÄÎļþÃû(°üº¬Â·¾¶Ãû³Æ,ȱʡÊÇÈÏΪÔÚµ±Ç°Â·¾¶ÏÂÃæ).flags¿ÉÒÔÈ¥ÏÂÃæµÄÒ»¸öÖµ»òÕßÊǼ¸¸öÖµµÄ×éºÏ.
O_RDONLY:ÒÔÖ»¶ÁµÄ·½Ê½´ò¿ªÎļþ.
O_WRONLY:ÒÔֻдµÄ·½Ê½´ò¿ªÎļþ.
O_RDWR:ÒÔ¶ÁдµÄ·½Ê½´ò¿ªÎļþ.
O_APPEND:ÒÔ×·¼ÓµÄ·½Ê½´ò¿ªÎļþ.
O_CREAT:´´½¨Ò»¸öÎļþ.
O_EXEC:Èç¹ûʹÓÃÁËO_CREAT¶øÇÒÎļþÒѾ´æÔÚ,¾Í»á·¢ÉúÒ»¸ö´íÎó.
O_NOBLOCK:ÒÔ·Ç×èÈûµÄ·½Ê½´ò¿ªÒ»¸öÎļþ.
O_TRUNC:Èç¹ûÎļþÒѾ´æÔÚ,Ôòɾ³ýÎļþµÄÄÚÈÝ.
Ç°ÃæÈý¸ö±êÖ¾Ö»ÄÜʹÓÃÈÎÒâµÄÒ»¸ö.Èç¹ûʹÓÃÁËO_CREATE±êÖ¾,ÄÇôÎÒÃÇҪʹÓÃopenµÄµÚ¶þÖÖÐÎʽ.»¹ÒªÖ¸¶¨mode±êÖ¾,ÓÃÀ´±íʾÎļþµÄ·ÃÎÊȨÏÞ.mode¿ÉÒÔÊÇÒÔÏÂÇé¿öµÄ×éºÏ.
-----------------------------------------------------------------
S_IRUSR Óû§¿ÉÒÔ¶Á S_IWUSR Óû§¿ÉÒÔд
S_IXUSR Óû§¿ÉÒÔÖ´ÐÐ S_IRWXU Óû§¿ÉÒÔ¶ÁдִÐÐ
-----------------------------------------------------------------
S_IRGRP ×é¿ÉÒÔ¶Á S_IWGRP ×é¿ÉÒÔд
S_IXGRP ×é¿ÉÒÔÖ´ÐÐ S_IRWXG ×é¿ÉÒÔ¶ÁдִÐÐ
-----------------------------------------------------------------
S_IROTH ÆäËûÈË¿ÉÒÔ¶Á S_IWOTH ÆäËûÈË¿ÉÒÔд
S_IXOTH ÆäËûÈË¿ÉÒÔÖ´ÐÐ S_IRWXO ÆäËûÈË¿ÉÒÔ¶ÁдִÐÐ
-----------------------------------------------------------------
S_ISUID ÉèÖÃÓû§Ö´ÐÐID S_ISGID ÉèÖÃ×éµÄÖ´ÐÐID
-----------------------------------------------------------------
ÎÒÃÇÒ²¿ÉÒÔÓÃÊý×ÖÀ´´ú±í¸÷¸öλµÄ±êÖ¾.Linux×ܹ²ÓÃ5¸öÊý×ÖÀ´±íʾÎļþµÄ¸÷ÖÖȨÏÞ.
00000.µÚһλ±íʾÉèÖÃÓû§ID.µÚ¶þλ±íʾÉèÖÃ×éID,µÚÈýλ±íʾÓû§×Ô¼ºµÄȨÏÞλ,µÚËÄλ±íʾ×éµÄȨÏÞ,×îºóһλ±íʾÆäËûÈ˵ÄȨÏÞ.
ÿ¸öÊý×Ö¿ÉÒÔÈ¡1(Ö´ÐÐȨÏÞ),2(дȨÏÞ),4(¶ÁȨÏÞ),0(ʲôҲûÓÐ)»òÕßÊÇÕ⼸¸öÖµµÄºÍ.
±ÈÈçÎÒÃÇÒª´´½¨Ò»¸öÓû§¶ÁдִÐÐ,×éûÓÐȨÏÞ,ÆäËûÈ˶ÁÖ´ÐеÄÎļþ.ÉèÖÃÓû§IDλÄÇôÎÒÃÇ¿ÉÒÔʹÓõÄģʽÊÇ--1(ÉèÖÃÓû§ID)0(×éûÓÐÉèÖÃ)7(1+2+4)0(ûÓÐȨÏÞ,ʹÓÃȱʡ)5(1+4)¼´10705:
open("temp",O_CREAT,10705);
Èç¹ûÎÒÃÇ´ò¿ªÎļþ³É¹¦,open»á·µ»ØÒ»¸öÎļþÃèÊö·û.ÎÒÃÇÒÔºó¶ÔÎļþµÄËùÓвÙ×÷¾Í¿ÉÒÔ¶ÔÕâ¸öÎļþÃèÊö·û½øÐвÙ×÷ÁË.
µ±ÎÒÃDzÙ×÷Íê³ÉÒÔºó,ÎÒÃÇÒª¹Ø±ÕÎļþÁË,Ö»Òªµ÷ÓÃclose¾Í¿ÉÒÔÁË,ÆäÖÐfdÊÇÎÒÃÇÒª¹Ø±ÕµÄÎļþÃèÊö·û.
Îļþ´ò¿ªÁËÒÔºó,ÎÒÃǾÍÒª¶ÔÎļþ½øÐжÁдÁË.ÎÒÃÇ¿ÉÒÔµ÷Óú¯ÊýreadºÍwrite½øÐÐÎļþµÄ¶Áд.
#include
ssize_t read(int fd, void *buffer,size_t count);
ssize_t write(int fd, const void *buffer,size_t count);
fdÊÇÎÒÃÇÒª½øÐжÁд²Ù×÷µÄÎļþÃèÊö·û,bufferÊÇÎÒÃÇҪдÈëÎļþÄÚÈÝ»ò¶Á³öÎļþÄÚÈݵÄÄÚ´æµØÖ·.countÊÇÎÒÃÇÒª¶ÁдµÄ×Ö½ÚÊý.
¶ÔÓÚÆÕͨµÄÎļþread´ÓÖ¸¶¨µÄÎļþ(fd)ÖжÁÈ¡count×Ö½Úµ½buffer»º³åÇøÖÐ(¼ÇסÎÒÃDZØÐëÌṩһ¸ö×ã¹»´óµÄ»º³åÇø),ͬʱ·µ»Øcount.
Èç¹ûread¶Áµ½ÁËÎļþµÄ½áβ»òÕß±»Ò»¸öÐźÅËùÖжÏ,·µ»ØÖµ»áСÓÚcount.Èç¹ûÊÇÓÉÐźÅÖжÏÒýÆð·µ»Ø,¶øÇÒûÓзµ»ØÊý¾Ý,read»á·µ»Ø-1,ÇÒÉèÖÃerrnoΪEINTR.µ±³ÌÐò¶Áµ½ÁËÎļþ½áβµÄʱºò,read»á·µ»Ø0.
write´ÓbufferÖÐдcount×Ö½Úµ½ÎļþfdÖÐ,³É¹¦Ê±·µ»Øʵ¼ÊËùдµÄ×Ö½ÚÊý.
ÏÂÃæÎÒÃÇѧϰһ¸öʵÀý,Õâ¸öʵÀýÓÃÀ´¿½±´Îļþ.
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
rt ÓÚ 2005-11-12 01:05:21·¢±í:
ÓÐһЩ¿´²»¶®£¬ÂýÂýѧϰ
yo ÓÚ 2005-11-07 12:57:14·¢±í:
exit(1);
}
if(fork()==0)
{
close(fd[0]);
printf("Child[%d] Write to pipe\n\a",getpid());
snprintf(buffer,BUFFER,"%s",argv[1]);
write(fd[1],buffer,strlen(buffer));
printf("Child[%d] Quit\n\a",getpid());
exit(0);
}
else
{
close(fd[1]);
printf("Parent[%d] Read from pipe\n\a",getpid());
memset(buffer,'\0',BUFFER+1);
read(fd[0],buffer,BUFFER);
printf("Parent[%d] Read:%s\n",getpid(),buffer);
exit(1);
}
}
ΪÁËʵÏÖÖض¨Ïò²Ù×÷,ÎÒÃÇÐèÒªµ÷ÓÃÁíÍâÒ»¸öº¯Êýdup2.
#include
int dup2(int oldfd,int newfd);
dup2½«ÓÃoldfdÎļþÃèÊö·ûÀ´´úÌænewfdÎļþÃèÊö·û,ͬʱ¹Ø±ÕnewfdÎļþÃèÊö·û.Ò²¾ÍÊÇ˵,
ËùÓÐÏònewfd²Ù×÷¶¼×ªµ½oldfdÉÏÃæ.ÏÂÃæÎÒÃÇѧϰһ¸öÀý×Ó,Õâ¸öÀý×Ó½«±ê×¼Êä³öÖض¨Ïòµ½Ò»¸öÎļþ.
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
int fd;
char buffer[BUFFER_SIZE];
if(argc!=2)
{
fprintf(stderr,"Usage:%s outfilename\n\a",argv[0]);
exit(1);
}
if((fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%s\n\a",argv[1],strerror(errno));
exit(1);
}
if(dup2(fd,STDOUT_FILENO)==-1)
{
fprintf(stderr,"Redirect Standard Out Error:%s\n\a",strerror(errno));
exit(1);
}
fprintf(stderr,"Now,please input string");
fprintf(stderr,"(To quit use CTRL+D)\n");
while(1)
{
fgets(buffer,BUFFER_SIZE,stdin);
if(feof(stdin))break;
write(STDOUT_FILENO,buffer,strlen(buffer));
}
exit(0);
}
ºÃÁË,ÎļþÒ»ÕÂÎÒÃǾÍÔÝʱÏÈÌÖÂÛµ½ÕâÀï,ѧϰºÃÁËÎļþµÄ²Ù×÷ÎÒÃÇÆäʵÒѾ¿ÉÒÔд³öһЩ±È½ÏÓÐÓõijÌÐòÁË.ÎÒÃÇ¿ÉÒÔ±àдһ¸öʵÏÖÀýÈçdir,mkdir,cp,mvµÈµÈ³£ÓõÄÎļþ²Ù×÷ÃüÁîÁË.
Ïë²»Ïë×Ô¼ºÐ´¼¸¸öÊÔÒ»ÊÔÄØ?
yo ÓÚ 2005-11-07 12:56:37·¢±í:
LinuxÌṩÁË´óÁ¿µÄĿ¼²Ù×÷º¯Êý,ÎÒÃÇѧϰ¼¸¸ö±È½Ï¼òµ¥ºÍ³£Óõĺ¯Êý.
#include
#include
#include
#include
#include
int mkdir(const char *path,mode_t mode);
DIR *opendir(const char *path);
struct dirent *readdir(DIR *dir);
void rewinddir(DIR *dir);
off_t telldir(DIR *dir);
void seekdir(DIR *dir,off_t off);
int closedir(DIR *dir);
struct dirent {
long d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[NAME_MAX+1]; /* ÎļþÃû³Æ */
mkdirºÜÈÝÒ×¾ÍÊÇÎÒÃÇ´´½¨Ò»¸öĿ¼,opendir´ò¿ªÒ»¸öĿ¼ΪÒÔºó¶Á×ö×¼±¸.readdir¶ÁÒ»¸ö´ò¿ªµÄĿ¼.rewinddirÊÇÓÃÀ´ÖضÁĿ¼µÄºÍÎÒÃÇѧµÄrewindº¯ÊýÒ»Ñù.closedirÊǹرÕÒ»¸öĿ¼.telldirºÍseekdirÀàËÆÓëfteeºÍfseekº¯Êý.
ÏÂÃæÎÒÃÇ¿ª·¢Ò»¸öС³ÌÐò,Õâ¸ö³ÌÐòÓÐÒ»¸ö²ÎÊý.Èç¹ûÕâ¸ö²ÎÊýÊÇÒ»¸öÎļþÃû,ÎÒÃÇÊä³öÕâ¸öÎļþµÄ´óСºÍ×îºóÐ޸ĵÄʱ¼ä,Èç¹ûÊÇÒ»¸öĿ¼ÎÒÃÇÊä³öÕâ¸öĿ¼ÏÂËùÓÐÎļþµÄ´óСºÍÐÞ¸Äʱ¼ä.
#include
#include
#include
#include
#include
#include
#include
static int get_file_size_time(const char *filename)
{
struct stat statbuf;
if(stat(filename,&statbuf)==-1)
{
printf("Get stat on %s Error:%s\n",
filename,strerror(errno));
return(-1);
}
if(S_ISDIR(statbuf.st_mode))return(1);
if(S_ISREG(statbuf.st_mode))
printf("%s size:%ld bytes\tmodified at %s",
filename,statbuf.st_size,ctime(&statbuf.st_mtime));
return(0);
}
int main(int argc,char **argv)
{
DIR *dirp;
struct dirent *direntp;
int stats;
if(argc!=2)
{
printf("Usage:%s filename\n\a",argv[0]);
exit(1);
}
if(((stats=get_file_size_time(argv[1]))==0)||(stats==-1))exit(1);
if((dirp=opendir(argv[1]))==NULL)
{
printf("Open Directory %s Error:%s\n",
argv[1],strerror(errno));
exit(1);
}
while((direntp=readdir(dirp))!=NULL)
if(get_file_size_time(direntp-
closedir(dirp);
exit(1);
}
4¡£¹ÜµÀÎļþ
LinuxÌṩÁËÐí¶àµÄ¹ýÂ˺ÍÖض¨Ïò³ÌÐò,±ÈÈçmore cat
µÈµÈ.»¹ÌṩÁË< > | <<µÈµÈÖض¨Ïò²Ù×÷·û.ÔÚÕâЩ¹ýÂ˺ÍÖØ ¶¨Ïò³ÌÐòµ±ÖÐ,¶¼Óõ½Á˹ܵÀÕâÖÖÌØÊâµÄÎļþ.ϵͳµ÷ÓÃpipe¿ÉÒÔ´´½¨Ò»¸ö¹ÜµÀ.
#include
int pipe(int fildes[2]);
pipeµ÷ÓÿÉÒÔ´´½¨Ò»¸ö¹ÜµÀ(ͨÐÅ»º³åÇø).µ±µ÷Óóɹ¦Ê±,ÎÒÃÇ¿ÉÒÔ·ÃÎÊÎļþÃèÊö·ûfildes[0],fildes[1].ÆäÖÐfildes[0]ÊÇÓÃÀ´¶ÁµÄÎļþÃèÊö·û,¶øfildes[1]ÊÇÓÃÀ´Ð´µÄÎļþÃèÊö·û.
ÔÚʵ¼ÊʹÓÃÖÐÎÒÃÇÊÇͨ¹ý´´½¨Ò»¸ö×Ó½ø³Ì,È»ºóÒ»¸ö½ø³Ìд,Ò»¸ö½ø³Ì¶ÁÀ´Ê¹ÓõÄ.
¹ØÓÚ½ø³ÌͨÐŵÄÏêϸÇé¿öÇë²é¿´½ø³ÌͨÐÅ
#include
#include
#include
#include
#include
#include
#include
#define BUFFER 255
int main(int argc,char **argv)
{
char buffer[BUFFER+1];
int fd[2];
if(argc!=2)
{
fprintf(stderr,"Usage:%s string\n\a",argv[0]);
exit(1);
}
if(pipe(fd)!=0)
{
fprintf(stderr,"Pipe Error:%s\n\a",strerror(errno));
yo ÓÚ 2005-11-07 12:53:16·¢±í:
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
if(argc!=3)
{
fprintf(stderr,"Usage:%s fromfile tofile\n\a",argv[0]);
exit(1);
}
/* ´ò¿ªÔ´Îļþ */
if((from_fd=open(argv[1],O_RDONLY))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[1],strerror(errno));
exit(1);
}
/* ´´½¨Ä¿µÄÎļþ */
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[2],strerror(errno));
exit(1);
}
/* ÒÔÏ´úÂëÊÇÒ»¸ö¾µäµÄ¿½±´ÎļþµÄ´úÂë */
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
/* Ò»¸öÖÂÃüµÄ´íÎó·¢ÉúÁË */
if((bytes_read==-1)&&(errno!=EINTR)) break;
else if(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
/* Ò»¸öÖÂÃü´íÎó·¢ÉúÁË */
if((bytes_write==-1)&&(errno!=EINTR))break;
/* дÍêÁËËùÓжÁµÄ×Ö½Ú */
else if(bytes_write==bytes_read) break;
/* ֻдÁËÒ»²¿·Ö,¼ÌÐøд */
else if(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
/* дµÄʱºò·¢ÉúµÄÖÂÃü´íÎó */
if(bytes_write==-1)break;
}
}
close(from_fd);
close(to_fd);
exit(0);
}
2¡£ÎļþµÄ¸÷¸öÊôÐÔ
Îļþ¾ßÓи÷ÖÖ¸÷ÑùµÄÊôÐÔ,³ýÁËÎÒÃÇÉÏÃæËùÖªµÀµÄÎļþȨÏÞÒÔÍâ,Îļþ»¹Óд´½¨Ê±¼ä,´óСµÈµÈÊôÐÔ.
ÓÐʱºîÎÒÃÇÒªÅжÏÎļþÊÇ·ñ¿ÉÒÔ½øÐÐijÖÖ²Ù×÷(¶Á,дµÈµÈ).Õâ¸öʱºòÎÒÃÇ¿ÉÒÔʹÓÃaccessº¯Êý.
#include
int access(const char *pathname,int mode);
pathname:ÊÇÎļþÃû³Æ,modeÊÇÎÒÃÇÒªÅжϵÄÊôÐÔ.¿ÉÒÔÈ¡ÒÔÏÂÖµ»òÕßÊÇËûÃǵÄ×éºÏ.
R_OKÎļþ¿ÉÒÔ¶Á,W_OKÎļþ¿ÉÒÔд,X_OKÎļþ¿ÉÒÔÖ´ÐÐ,F_OKÎļþ´æÔÚ.µ±ÎÒÃDzâÊԳɹ¦Ê±,º¯Êý·µ»Ø0,·ñÔòÈç¹ûÓÐÒ»¸öÌõ¼þ²»·ûʱ,·µ»Ø-1.
Èç¹ûÎÒÃÇÒª»ñµÃÎļþµÄÆäËûÊôÐÔ,ÎÒÃÇ¿ÉÒÔʹÓú¯Êýstat»òÕßfstat.
#include
#include
int stat(const char *file_name,struct stat *buf);
int fstat(int filedes,struct stat *buf);
struct stat {
dev_t st_dev; /* É豸 */
ino_t st_ino; /* ½Úµã */
mode_t st_mode; /* ģʽ */
nlink_t st_nlink; /* Ó²Á¬½Ó */
uid_t st_uid; /* Óû§ID */
gid_t st_gid; /* ×éID */
dev_t st_rdev; /* É豸ÀàÐÍ */
off_t st_off; /* Îļþ×Ö½ÚÊý */
unsigned long st_blksize; /* ¿é´óС */
unsigned long st_blocks; /* ¿éÊý */
time_t st_atime; /* ×îºóÒ»´Î·ÃÎÊʱ¼ä */
time_t st_mtime; /* ×îºóÒ»´ÎÐÞ¸Äʱ¼ä */
time_t st_ctime; /* ×îºóÒ»´Î¸Ä±äʱ¼ä(Ö¸ÊôÐÔ) */
};
statÓÃÀ´ÅжÏûÓдò¿ªµÄÎļþ,¶øfstatÓÃÀ´Åжϴò¿ªµÄÎļþ.ÎÒÃÇʹÓÃ×î¶àµÄÊôÐÔÊÇst_mode.ͨ¹ý×ÅÊôÐÔÎÒÃÇ¿ÉÒÔÅжϸø¶¨µÄÎļþÊÇÒ»¸öÆÕͨÎļþ»¹ÊÇÒ»¸öĿ¼,Á¬½ÓµÈµÈ.¿ÉÒÔʹÓÃÏÂÃ漸¸öºêÀ´ÅжÏ.
S_ISLNK(st_mode):ÊÇ·ñÊÇÒ»¸öÁ¬½Ó.S_ISREGÊÇ·ñÊÇÒ»¸ö³£¹æÎļþ.S_ISDIRÊÇ·ñÊÇÒ»¸öĿ¼S_ISCHRÊÇ·ñÊÇÒ»¸ö×Ö·ûÉ豸.S_ISBLKÊÇ·ñÊÇÒ»¸ö¿éÉ豸S_ISFIFOÊÇ·ñ ÊÇÒ»¸öFIFOÎļþ.S_ISSOCKÊÇ·ñÊÇÒ»¸öSOCKETÎļþ. ÎÒÃÇ»áÔÚÏÂÃæ˵Ã÷ÈçºÎʹÓÃÕ⼸¸öºêµÄ.
3¡£Ä¿Â¼ÎļþµÄ²Ù×÷
ÔÚÎÒÃDZàд³ÌÐòµÄʱºò£¬ÓÐʱºò»áÒªµÃµ½ÎÒÃǵ±Ç°µÄ¹¤×÷·¾¶¡£C¿âº¯ÊýÌṩÁËgetcwdÀ´½â¾öÕâ¸öÎÊÌâ¡£
#include
char *getcwd(char *buffer,size_t size);
ÎÒÃÇÌṩһ¸ösize´óСµÄbuffer,getcwd»á°ÑÎÒÃǵ±Ç°µÄ·¾¶¿¼µ½bufferÖÐ.Èç¹ûbuffer̫С,º¯Êý»á·µ»Ø-1ºÍÒ»¸ö´íÎóºÅ.