Assignment 1: Linux system calls and API
Send each assignment to the teacher when you have finished it.
Do not wait until the end of the course and then
hand in all of the labs at the same time.
You can hand in the mandatory tasks first,
and then the optional tasks later if you have time.
|
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
www.os-book.com,
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,
the same way as the standard "cp" command works.
(If you don't remember how to get the command-line arguments in C,
you can look at this example: showargs.c.)
Use the standard C stdio interface with the library functions
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
system 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 "1G" with 1 gibibyte of random data:
dd if=/dev/urandom of=1G bs=1M 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
the system calls
open, read, write and close. Make a new version of your program above
that uses the Linux API to copy a file. Compare the speeds of the two versions.
Questions:
What results do you get? Which are the times you have measured?
Is the Linux API 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 the system calls fork, execvp and wait to create a new process, start a
program (with command-line arguments), and wait for the new process to
terminate.
Don't forget what you have learned in your introductory programming courses: Test your program to try to find the bugs!
For example, can it handle simple commands such as the following:
- ls
- ls -al /
- sleep 3
- echo Heigh-ho Heigh-ho It's home from work we go
- exit
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.
|
Modify the "skalman" shell so you can send output to a file using the syntax "program > file",
as in the normal shell,
and also 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. A pdf would be good.
Don't forget to answer the questions above.
Hand in using Blackboard.
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),
30 juni 2023