From: Glynn Clements <glynn.clements@virgin.net>
To: "A. Murat Eren" <meren@comu.edu.tr>
Cc: Holger Kiehl <Holger.Kiehl@dwd.de>, linux-c-programming@vger.kernel.org
Subject: Re: [Fwd: Re: Implementing a file counter (like "ls | wc")]
Date: Thu, 8 Apr 2004 05:39:23 +0100 [thread overview]
Message-ID: <16500.55163.50294.74986@cerise.nosuchdomain.co.uk> (raw)
In-Reply-To: <200404090200.36787.meren@comu.edu.tr>
A. Murat Eren wrote:
> I've read all of the mailing thread about this subject and i'd like to ask
> some questions..
>
> > But most important always try to avoid system calls!
>
> I guess eveybody is accepting that the syscalls is slowing down the process
> when invoking them from the critical parts of the code (i hope i understand
> properly).. I'm trying to write a basic shell for me (just for fun, nothing
> serious), i'm setting up my user's shell in passwd file to my basic shell's
> compiled binary, and when i'm logging in from the console, normally i'm
> falling to a prompt which is written by me.
>
> I'm not using the well-known programs such as 'ls', 'cd' etc.
"cd" isn't a program; it's a built-in shell command. It has to be; a
process can't change the current directory of another process, so a
"cd" program just wouldn't work.
>. i have my own
> envoriment variables and my own basic programs those are invoking from my
> prompt with fork and execvp. If i do not want to use the syscalls such as
> readdir, opendir what i must do?
In most of the situations where you use syscalls, you can't avoid
using them; the point is not to use them excessively.
E.g. if you're reading a file (or any other descriptor), calling
read() a few times and reading a large amount of data each time is
more efficient than calling read() lots of times and reading a small
amount of data each time.
Or, for an "ls" replacement, only call stat() (or lstat()) if you
actually need the additional data (e.g. for "ls -l", "ls -F" etc)
rather than always calling it "just in case".
> With readdir, i can read everything from the every type of file system which
> supported by kernel *transparently*, for example when i'm working on a disk
> partition which is formatted with ext3 file system i don't have to try to
> read the superblocks and inode's adresses from disk because VFS takes care
> all of the low level operations via readdir (and kernel's modules for that
> file system) for me, isn't it? If i want to do all of them by my self (sure,
> this is not necessary but i just want to try and learn) which is the right
> way i have to go? I thought that it would be a very simple and absolute way
> looking into the glibc for implementations of the functions like readdir, but
> nearly i'm sure that it would be impossible to undrstand (and this is another
> problem, i can't understand the source code usually :( it would be great to
> hear your advices about how i improve my vision).
There is seldom any point in bypassing libc. For a syscall, the libc
function will usually consist of the syscall itself, plus setting
errno if the syscall returns an error status, so there wouldn't be
anything to gain from making the syscall directly.
BTW, on Linux, glibc's readdir() function isn't implemented using the
readdir syscall but using the getdents syscall instead. The latter
reads multiple directory entries per call, so is more efficient.
You can't realistically bypass the syscalls themselves; not only would
trying to read files by accessing the device directly require
sufficient privilege, it would also be error-prone, as a user-space
process can't hold kernel locks and can't prevent itself being
preempted at an inconvenient point.
--
Glynn Clements <glynn.clements@virgin.net>
next prev parent reply other threads:[~2004-04-08 4:39 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-04-07 16:50 [Fwd: Re: Implementing a file counter (like "ls | wc")] Luciano Moreira - igLnx
2004-04-07 16:54 ` John T. Williams
2004-04-07 22:29 ` Holger Kiehl
2004-04-08 0:06 ` A. Murat Eren
2004-04-08 1:01 ` John T. Williams
2004-04-08 4:39 ` Glynn Clements [this message]
2004-04-08 8:05 ` A. Murat Eren
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=16500.55163.50294.74986@cerise.nosuchdomain.co.uk \
--to=glynn.clements@virgin.net \
--cc=Holger.Kiehl@dwd.de \
--cc=linux-c-programming@vger.kernel.org \
--cc=meren@comu.edu.tr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).