Total Pageviews

Saturday, April 7, 2012

mmap to implement copy command

/*
Program to Implement copy using mmap

*/

#include
#include
#include
#include
#include
#include
#include
#include
struct stat statbuf;

int main(int argc, char *argv[]) {

int fdin, fdout, ret;
char *src, *dest;
if(argc < 3) {
printf("\n Check number of arguments\n");
exit(EXIT_FAILURE);
}
fdin = open(argv[1], O_RDONLY);
if(fdin < 0) {
printf("\n Cant open source file \n");
exit(EXIT_FAILURE);
}

ret=fstat(fdin,&statbuf);
if(ret < 0 ) {
printf("\nfstat system call failed ");
exit(EXIT_FAILURE);
}
printf("\nInpute File size is - %lu ",statbuf.st_size);
src=(char*)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin,0);
fdout = open(argv[2],O_RDWR | O_CREAT | O_TRUNC,S_IRWXU | S_IRWXO);
if(fdout < 0) {
printf("\n Cant open source file");
exit(EXIT_FAILURE);
}
ret=lseek(fdout,statbuf.st_size-1,SEEK_SET);
if(ret < 0)
printf("\n lseek failed ");
ret=write(fdout,"",1);
if(ret < 0)
printf("\n write failed ");
dest=(char *)mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout,0);
dest=memcpy(dest,src,statbuf.st_size);
if(!dest)
printf("\n memcpy failed ");
// msync((void *)dest,statbuf.st_size,MS_ASYNC);
printf("\nFile copied succefully\n");
printf("\n");
}

Thursday, April 5, 2012

Description of fget_light and fput_light

Hi friends 
Description of fget_light, which is currently incorrect
about needing a prior refcnt (judging by the way it is actually used).




/*
- * Lightweight file lookup - no refcnt increment if fd table isn't shared.
- * You can use this only if it is guranteed that the current task already
- * holds a refcnt to that file. That check has to be done at fget() only
- * and a flag is returned to be passed to the corresponding fput_light().
- * There must not be a cloning between an fget_light/fput_light pair.
+ * Lightweight file lookup - no refcnt increment if fd table isn't shared.
+ *
+ * You can use this instead of fget if you satisfy all of the following
+ * conditions:
+ * 1) You must call fput_light before exiting the syscall and returning control
+ * to userspace (i.e. you cannot remember the returned struct file * after
+ * returning to userspace).
+ * 2) You must not call filp_close on the returned struct file * in between
+ * calls to fget_light and fput_light.
+ * 3) You must not clone the current task in between the calls to fget_light
+ * and fput_light.
+ *
+ * The fput_needed flag returned by fget_light should be passed to the
+ * corresponding fput_light.