Assignment 1: Linux system calls and API
Files
Lab materials
A Linux PC (or virtual machine). You can use a VirtualBox guest
machine that can be downloaded from the course book's website, or your
own Linux machine, or one of the Linux machines in T002 (the same
kernel as in the 10th Edition of the book).
Some of these programming assignments are described in chapter 2 and 3
in the course book.
Task 1
In your Linux system, write a simple program that copies a file to
another. The file names should be provided as command-line arguments.
Use the standard C stdio interface with fopen, getc, putc and fclose.
Compile with gcc.
(Possibly useful command: "gcc -Wall -Wextra -std=c11 -O3 my-c-cp.c -o my-c-cp")
With the following Makefile you can just type "make my-c-cp":
CC = gcc
CFLAGS += -Wall -Wextra -std=c11 -O3
Look at the manual page for the strace command, using the command "man
strace". Use strace to see the system calls.
Questions:
Why don't you see the calls to fopen, getc, putc and fclose? Which
calls do you see? What do they do? Are there more calls than you would
have expected?
Here is one way to create a file called "1M" with 1 mebibyte of random data:
dd if=/dev/urandom of=1M bs=1k count=1024
Question: Why do you need such a large file?
Task 2
In the course "Datorteknik" you have already used the Linux API with
open, read, write and close. Make a new version of your program above
that uses that to copy a file.
Questions:
What results do you get? Which are the times you have measured?
Is this version faster or slower than the stdio version? Why?
Use the program "flush-cache.c" to empty OS buffers.
Question: Why do you need to do that? When do you need to do that?
Task 3
Do the "simple shell" assignment from the book, but with my "skalman"
skeleton program ("skalman-skeleton.c"), and implement execution of
programs using fork, execvp and wait to create a new process, start a
program (with command-line arguments), and wait for the new process to
terminate.
Task 4 (optional, counts toward a higher grade than 3)
For the grade 4, do at least three of the optional tasks in
assignment 1-3.
You can hand in the mandatory tasks first,
and the optional tasks later, if you have time.
|
Add output to a file using the syntax "program > file".
Add input from a file using the syntax "program < file".
Task 5 (optional, counts toward a higher grade than 3)
Compare stdio, read and mmap. Create a big file, for exampel like
this:
dd if=/dev/urandom of=1G bs=1M count=1024
Compare the time it takes to read the file and count the number of 'x'
in it, using: getc, fread, read, mmap. You can use the timing code in
the attached file timing-example.c.
A useful tip: read using a larger buffer, such as 1 megabyte.
Report
Write down and hand in a short summary (1-2 pages) of what you have
done.
Don't forget to answer the questions above.
Mail (as a pdf) to the
teacher. All source code for your programs must be attached. It is
good if you can also demonstrate your results at one of the scheduled
lab times.
Thomas Padron-McCarthy
(thomas.padron-mccarthy@oru.se),
15 maj 2020