Thanks, Greg and Manish, What I am trying to understand is not only for "ls" itself. I just wanted to know how general command such like "ps", "top", or something like that works in linux. Also, I wanted to know the path from user-level application to how it accesses to kernel resources.
I guess I could understand roughly about the mechanism of an application in linux, but I feel to study more. If you know some resources I need to dig into and share of it, it would be great helpful to me. Thanks, Best Regards, Daniel (Youngwhan) Song On Wed, Dec 2, 2009 at 8:36 PM, Manish Katiyar <[email protected]> wrote: > On Thu, Dec 3, 2009 at 9:18 AM, Daniel (Youngwhan) Song > <[email protected]> wrote: > > Thanks, krushnaal and Greg, > > > > By looking at the Greg's strace, it gives me great insight of the > command. > > > > So, It looks like it calls a library which can be in glib(?), and the > > library actually calls kernel device drivers like console or something > like > > that, and they were communicating each other back and forth, and finally, > it > > outputs the result of "ls" onto the screen. Is my understanding correct? > > Actually what you have told is true for any command and is not limited > to "ls". If you want to know how ls works you will need to understand > a little about how files are stored on disk and how they are indexed. > And then you can try to read about readdir(), opendir(),stat() system > calls and finally implement your own "ls" :-) > > > > > > Best Regards, > > Daniel (Youngwhan) Song > > > > > > > > On Wed, Dec 2, 2009 at 7:41 PM, Greg Freemyer <[email protected]> > > wrote: > >> > >> On Wed, Dec 2, 2009 at 9:12 PM, Daniel (Youngwhan) Song > >> <[email protected]> wrote: > >> > Hi, > >> > > >> > Could somebody explain me how exactly "ls" command work in Unix/Linux? > >> > > >> > When we type "ls" command in linux shell, what does process/procedure > >> > work > >> > with linux library or linux kernel, and how exactly does it show > >> > directory > >> > information to the standard output (1)? > >> > > >> > Thanks in advance. > >> > > >> > Best Regards, > >> > Daniel Song > >> > > >> > >> read the source of course, but a very handy shortcut is: > >> > >> # strace ls > >> > >> The key part with a couple comments is: > >> > >> # Get the directory entries > >> open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3 > >> fcntl64(3, F_GETFD) = 0x1 (flags FD_CLOEXEC) > >> getdents64(3, /* 53 entries */, 32768) = 1744 > >> getdents64(3, /* 0 entries */, 32768) = 0 > >> close(3) = 0 > >> > >> # Verify stdout is a character device > >> fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 0), ...}) = 0 > >> > >> # map stdin into memory. (Not sure why, see the source) > >> mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, > >> 0) = 0xb73ff000 > >> > >> # write the directory entries to stdout and wrap-up > >> write(1, "bin Desktop Documents Downloa"..., 91bin Desktop > >> Documents Download Music Pictures Public public_html Templates > >> Videos > >> ) = 91 > >> close(1) = 0 > >> munmap(0xb73ff000, 4096) = 0 > >> close(2) = 0 > >> exit_group(0) = ? > >> > >> Greg > > > > > > > > -- > Thanks - > Manish > ================================== > [$\*.^ -- I miss being one of them > ================================== >
