"It's a dangerous business, Frodo, going out of your door. You step into the road, and if you don't keep your feet, there is no knowing where you might be swept off to."
—Bilbo, The Fellowship of the Ring, Book 1, Chapter 3
Welcome to Operating Systems! Let's get started with two simple "Hello, world!" programs that demonstrate the difference between doing standard output with the C standard library versus Linux system calls.
In this studio, you will:
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers in a text file. When finished, submit your code and text file to the git respository under the appropriate folder.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
As the answer to the first exercise, list the names of the people who worked together on this studio.
fprintf()
in the C standard library. To do so, pull up the function's
documentation by typing "man fprintf
" in the Linux terminal. This
page tells you everything you need to know in order to use
fprintf()
in a program. As the answer to this exercise, give the
#include
header file(s) needed for this function (look right
beneath the "SYNOPSIS" heading).
fprintf()
and
the printf()
. What is the difference? (Signature is a name
for the argument list.)
printf()
and
fprintf()
functions?
// Your name
// The date
// A short description of your program
// Include file goes here
int main( int argc, char* argv[] ){
//Make a call to fprintf() here
return 0;
}
The first argument of fprintf()
must specify an I/O stream. In
this case you should use "stdout
". This is a special stream that
prints to the Linux console, but this function works for a variety of
stream-oriented operations, such as writing to a file or sending data over
a network. The second argument should be a string literal
that gives your message, and should be terminated with "\n
"
(the newline character). For example, your string literal might look like:
"Hello, world!\n"
You can compile your program with the command:
gcc -o hello_fprintf hello_fprintf.c
Run your program by typing "./hello_fprintf
".
If your program runs correctly then copy and paste
your terminal output as the answer to this exercise.
write()
. However, if you give the command
"man write
" you won't arrive at the correct documentation because
there are multiple pages that might apply to such a common term. Instead,
give the command "man man
" to pull up the documentation for the
manual system.
As the answer to this exercise, give the section number for system calls.
write()
system call. The syntax you should use is:
man <section number> write
For example, the command "man 3 fprintf
" would take you to the
documentation page used previously, but explicitly states to look in the
standard library section of the manual.
As the answer to this exercise, give the header file
which must be included to use the write()
system call (again, look
beneath the "SYNOPSIS" statement).
hello_write.c
. The Linux
terminal command to copy a file is cp
. In this case, you should
execute the command:
cp hello_fprintf.c hello_write.c
Leave the answer for this exercise blank.
fprintf()
function doesn't actually implement the code
that allows your program to print to the terminal, it relies on the operating
system to do that. The OS provides a system call called
write()
that gives this functionality, and fprintf()
calls this function on your behalf. However, you can call write()
directly, which is what we will do now.
Modify your program to use write()
instead of
fprintf()
. There are two differences between the functions. The
first is that you need to use a file descriptor instead of a file stream.
In other words, replace the built-in variable "stdout
" with the
built-in variable "STDOUT_FILENO
". Both variables refer to the
same "thing" but in different ways.
The second difference is that write()
operates on a character
buffer (a vector of characters) rather than a string literal. You can declare
a character buffer as such:
char buffer[] = "Hello, world!\n";
You will also need to tell write()
how large the character
buffer is. When counting how many characters is in your buffer, don't forget
the newline character '\n'
. Note that the whole newline character
escape sequence ('\n'
) counts as one character.
Once you are finished, copy and paste your program output
as the answer to this exercise.
write()
to be much
larger than your character buffer (say, 100 or 1000). What happens? Why
do you think this is?
fprintf()
is provided by the C standard library
and is guaranteed to exist for any standards-compliant C language
implementation- even on other operating systems. The write()
system call is not guaranteed to exist as it is provided by the operating
system itself. Many system calls (but not all) have C library versions.
Thinking as a software developer, speculate a situation when you would want to use a C library function and another situation when you would want to use an OS system call.