[cs631apue] Several Questions

kthompso kthompso at stevens.edu
Thu Sep 15 19:13:49 EDT 2016


I'm not sure the sparse file one, but I might be able to help with a 
few.

Responses inline.

On 09/15/2016 5:12 PM, bzhang41 wrote:
> Hi all, I have several questions:
> 
> 1. On the textbook, section 3.14, I don’t understand the test cases:
> $ ./a.out 2 2>>temp.foo
> write only, append
> $ ./a.out 5 5<>temp.foo
> read write
> 
> What does 2>>temp.foo and 5<>temp.foo mean here ?

2>>temp.foo means open temp.foo on file descriptor 2 (stderr) for
writing in append mode.

5<>temp.foo means open temp.foo for reading and writing on file
descriptor 5.

The commands cause the shell to open these files on these file
descriptors for the program.  And then the program checks the
flags set on the file descriptor provided as an argument.  It's
making a point about which flags the shell uses when it opens
files with the redirection operators.

   >  - write only
   <  - read only
   >> - write, append
   <> - read write


> 2. When I test the hole.c on the lab, the result I got is
> bzhang41 at smurf:~/ReadAndTest$ cc -Wall hole.c
> bzhang41 at smurf:~/ReadAndTest$ ./a.out
> bzhang41 at smurf:~/ReadAndTest$ ls -l file.hole
> -rw-------+ 1 bzhang41 student 10240020 Sep 15 11:15 file.hole
> bzhang41 at smurf:~/ReadAndTest$ hexdump -c file.hole
> 0000000   a   b   c   d   e   f   g   h   i   j  \0  \0  \0  \0  \0  \0
> 0000010  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
> *
> 09c4000  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0   A   B   C   D   E   F
> 09c4010   G   H   I   J
> 09c4014
> bzhang41 at smurf:~/ReadAndTest$ cat file.hole > file.nohole
> bzhang41 at smurf:~/ReadAndTest$ ls -ls file.*
> 259 -rw-------+ 1 bzhang41 student 10240020 Sep 15 11:15 file.hole
>   1 -rw-r--r--+ 1 bzhang41 student 10240020 Sep 15 11:18 file.nohole
> 
> why the file.nohole has large block size than the file.hole ? When
> Prof shows the result, it also happened. It’s the problem of different
> OS ?

No clue.

> 3. How can I run the I/O efficiency example in the slides ?
> I can not "make tempfiles" shows in the slides. Can Prof. send us the
> makefile ? so we can try the example.

I think the shell commands being shown after the "make" commands
are actually just copied out of their respective make sections.  For
example the tempfiles section of the makefile would contain:

   for n in $(seq 10); do
     dd if=/dev/urandom of=tmp/file$n count=204800
   done

This makes a lot of sense considering the stat command had been
commented out and changed.  If you were just running these commands
from a shell prompt you probably wouldn't include the comment.

Basically you should have everything you need to run that example right
there.  Just combine all those commands into a shell script, minus the
make commands (or you could do a makefile... seems like an odd use of
make to me though).

> Actually, I also don’t understand
> what does "dd if=/dev/urandom of=tmp/file$n count=204800" mean ? I
> know the purpose is creating several files. I don’t understand the
> grammar here.
> Is there any material I can catch up to understand this grammar ?

Read the man pages.  From dd(1):

dd - convert and copy a file

...

count=N
     copy only N input blocks
if=FILE
     read from FILE instead of stdin
of=FILE
     write to FILE instead of stdout

> And `cc -Wall -DBUFFSIZE=$n simple-cat.c`  I know this is compile the
> program, but what does the "-DBUFFSIZE=$n" do here ?

Again, check out man pages for flags. -D means #define, it's setting the
macro BUFFSIZE to whatever is in variable n before compiling the 
program.

 From cc(1):

-Dmacro
     Define macro macro with the string `1' as its definition.

-Dmacro=defn
     Define macro macro as defn.    All instances of `-D' on the com-
     mand line are processed before any `-U' options.


This is (one reason) why we have the #ifndef lines, it only sets
BUFFSIZE if it wasn't already set.  From simple-cat.c:

   #ifndef BUFFSIZE        # Check if the macro BUFFSIZE is _not_ 
defined.
   #define BUFFSIZE 32768  # If it's not - define it to a default value, 
32768.
   #endif


> Finally, `stat -f "%k" tmp/file1 # stat -c "%o" tmp/file1`,  I checked
> the stat in man, but I didn’t find %k option, and I also don’t
> understand what the symbol `#` does here ?

This one is confusing.  First we need to know that '#' is the beginning
of a comment in a shell.  So that line had previously used `-c "%o"` and
then someone commented that out to use `-f "%k"`.  If you look at 
stat(1),
-c means use a format string and %o refers to the I/O block size.  
Furthermore,
k is not a valid format specifier on GNU linux.  Strange.

So, this line was changed to something that clearly doesn't work on 
Linux.
A little google-fu brings up the FreeBSD stat(1)* page, which does 
accept a -f
flag (does the same thing as -c) and also a %k format specifier 
(likewise the
same as %o on linux).  So this command prints out the I/O block size of 
the
file -- on FreeBSD.

*https://www.freebsd.org/cgi/man.cgi?query=stat&sektion=1

> _______________________________________________
> cs631apue mailing list
> cs631apue at lists.stevens.edu
> https://lists.stevens.edu/mailman/listinfo/cs631apue



More information about the cs631apue mailing list