By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

are the functions in <stdio.h> system calls provided by operation system?

P: n/a
hi,guy
i have a question. are the functions in <stdio.h> system calls
provided by operation system?
if so, i want to know how C implements that we can call system calls by
using the functions in <stdio.h>.
i need your help, thanks a lot.

Apr 20 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hello

stdio.h is a library, and so those call are _not_ system calls, they
are _library_ calls. Of course library calls are built on top of system
calls. Try this:

Step 1: save this as foo.c and compile with gcc foo.c

#include <stdio.h>

int
main ()
{
printf ("Hello comp.lang.c!\n");

return 0;
}

Step 2: run a.out and see this ($ is your prompt):
$ Hello comp.lang.c!
$

Step 3: run this command, and see:
$ strace a.out
execve("./a.out", ["a.out"], [/* 27 vars */]) = 0
uname({sys="Linux", node="linuxpoa3", ...}) = 0
brk(0) = 0x966e000
open("/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or
directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=72077, ...}) = 0
old_mmap(NULL, 72077, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb75e7000
close(3) = 0
open("/lib/tls/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\2 00X\1"...,
512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1571692, ...}) = 0
old_mmap(NULL, 1275340, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) =
0x1f5000
old_mmap(0x327000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED,
3, 0x132000) = 0x327000
old_mmap(0x32a000, 9676, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x32a000
close(3) = 0
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0xb75e6000
set_thread_area({entry_number:-1 -> 6, base_addr:0xb75e64e0,
limit:1048575, seg_32bit:1, contents:0, read_exec_only:0,
limit_in_pages:1, seg_not_present:0, useable:1}) = 0
munmap(0xb75e7000, 72077) = 0
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb75f8000
write(1, "Hello comp.lang.c!\n", 19Hello comp.lang.c!
) = 19
munmap(0xb75f8000, 4096) = 0
exit_group(0) = ?

The above output is strace's output, or simply system call trace. Those
functions are system calls. As you can see, the actual action (print
the string) is performed by a system call:

write(1, "Hello comp.lang.c!\n", 19Hello comp.lang.c!) = 19

where 19 is the string length plus the '\0' on the end.

---mauricio

Apr 20 '06 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pragma wrote:
Hello

stdio.h is a library,


Nope. Technically, stdio.h is the name of a standard include, which
/may/ be a real file containing C preprocessor and language constructs,
or may not. stdio.h is not a library, although the functions declared in
stdio.h /may/ be implemented in a library.

[snip]

- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFER+XwagVFX4UWr64RAny+AJ4kqMGX54v6WjuT11TcrQ 89HjKrjQCfTT4m
01MiZsre3OaazmXxHC+tAbA=
=Ebb5
-----END PGP SIGNATURE-----
Apr 20 '06 #3

P: n/a
"talk" <ji*******@gmail.com> writes:
i have a question. are the functions in <stdio.h> system calls
provided by operation system?
Maybe. The C standard says nothing about "system calls". The
functions declared in <stdio.h> can be implemented in any way the
implementation chooses.
if so, i want to know how C implements that we can call system calls by
using the functions in <stdio.h>.


The compiler generates code to implement the call. The details of how
this code works are entirely up to the implementer, as long as it
works as specified.

Why do you care?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 20 '06 #4

P: n/a
pragma wrote:
Hello

stdio.h is a library, and so those call are _not_ system calls, they
are _library_ calls. Of course library calls are built on top of system
calls. Try this:


There are systems that doesn't have "system calls".

C doesn't care wether what's specified in stdio.h ends up
in a library/system/something else call.
Apr 20 '06 #5

P: n/a
pragma wrote:
Hello
Please provide context when replying. Follow the link in my sig and read
the section especially for Google users.
stdio.h is a library, and so those call are _not_ system calls, they
are _library_ calls.
Do you know every single C implementation and OS in existence? How about
the ones that will be written next year? There is nothing in the C
standard that says an OS can't provide the C functions as a native part
of the OS in which can calling them will from a C program will be making
system calls.
Of course library calls are built on top of system
calls. Try this:

Step 1: save this as foo.c and compile with gcc foo.c

#include <stdio.h>

int
main ()
{
printf ("Hello comp.lang.c!\n");

return 0;
}

Step 2: run a.out and see this ($ is your prompt):
$ Hello comp.lang.c!
$

Step 3: run this command, and see:
$ strace a.out


There are systems with strace. The OP did not say which OS was being
used, so it could easily be one with no strace available.

<snip highly system specific stuff.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Apr 20 '06 #6

P: n/a

"talk" <ji*******@gmail.com> wrote in message
news:11********************@i39g2000cwa.googlegrou ps.com...
hi,guy
i have a question. are the functions in <stdio.h> system calls
provided by operation system?
depends on the OS...
if so, i want to know how C implements that we can call system calls by
using the functions in <stdio.h>.
tis the other way round. The routines in stdio.h convert or map the standard
"C" interfaces onto the OS interfaces. For UNIX or LINUX they probably map
(almost) straight to system calls. For machines like the IBM.370 that have
record orientation system calls they have a lot of work to do. See if your
college has a copy of "The Standard C Library" by Plauger. Lots of info in
that....
i need your help, thanks a lot.

I think you need to do some research your self...
Apr 21 '06 #7

P: n/a
talk wrote:
i have a question. are the functions in <stdio.h> system calls
provided by operation system?


They are provide by the compiler vendor. The compiler vendor will
typically write library functions which implement these standard
compliant functions (plus possible extensions) on top of available OS
functionality. The contents of FILE, for example, is not dictated by
the OS, but rather the compiler vendor.

Your compiler will typically link the C runtime library, which contains
these functions, to your program when you build it. Your compiler may
or may not provide source code for this C runtime library, but if it
does this, looking through these sources might be very insightful for
you.

--
Paul Hsieh
http://www.pobox.com/~qed
http://bstring.sf.net/

Apr 21 '06 #8

P: n/a
we******@gmail.com writes:
talk wrote:
i have a question. are the functions in <stdio.h> system calls
provided by operation system?


They are provide by the compiler vendor.

[snip]

Not necessarily. They're provided as part of the C implementation,
but it's entirely possible for the compiler and library to be provided
by two different vendors. For example, gcc typically uses whatever
library is provided by the underlying system (though I think it does
something funky with headers).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 21 '06 #9

P: n/a
hi,
thank you for your advice.

i just want to know in DOS or in WindowsXp how a C program including
<stdio.h> outputs. for example, how do the function "printf()" in
<stdio.h> print a string in console in DOS or in WindowsXp.
can you give me some advices?
thanks.

Apr 21 '06 #10

P: n/a
talk wrote:
hi,
thank you for your advice.

i just want to know in DOS or in WindowsXp how a C program including
<stdio.h> outputs. for example, how do the function "printf()" in
<stdio.h> print a string in console in DOS or in WindowsXp.
By interpreting the format specifier and making whatever magic
incantations DOS or Windows XP require. How this occurs will be
different if you compile using Cygwin to how Visual Studio does it.
can you give me some advices?


My main advice is that you don't need to know how it works under the
hood. All you need to know is that it does what the C standard and the
implementations documentation specifies.

If you really do want to pursue this then go to a DOS or Windows group.
We don't deal with the specifics of implementations here since there are
so many implementations that one question about each once a month would
flood the group and make it unusable.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 21 '06 #11

P: n/a

"talk" <ji*******@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
hi,
thank you for your advice.

i just want to know in DOS or in WindowsXp how a C program including
<stdio.h> outputs. for example, how do the function "printf()" in
<stdio.h> print a string in console in DOS or in WindowsXp.
can you give me some advices?
thanks.

As others have said that is implementation dependant. To write to a console
window it might call the "old" DOS I/O primitives, or it might use the
Windows I/O calls. Its been a long time since I looked but I think windows
provides both character mode and record/line mode functions. The implementor
of printf() is free to choose either..... You can download the source for
GCC and WATCOM. Why don't you do that and see how they work....
Apr 21 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.