01 Basics
01 Basics
Jan Schaumann
[email protected]
https://stevens.netmeister.org/631/
CS631 - Advanced Programming in the UNIX Environment
2
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
Standards
• IEEE POSIX (1003.1-2008) / SUSv4
3
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
man printf
man 3 printf
man write
man 3 write
man 2 write
man fprintf
4
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
https://pubs.opengroup.org/onlinepubs/9799919799/
5
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
Error handling:
• meaningful return values
• errno variable
• lookup of constant error values via two convenient functions:
strerror(3) and perror(3)
6
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
$ ftp https://stevens.netmeister.org/631/apue-code.tar.gz
$ tar zxvf apue-code.tar.gz
$ cd apue-code/01
$ cc welcome.c
$ ./a.out
Welcome to CS631 Advanced Programming in the UNIX Environment, jschauma!
$
7
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
See also:
https://kristerw.blogspot.com/2017/09/useful-gcc-warning-options-not-enabled.html
8
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
9
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
$ cd 01
$ more simple-shell.c
$ cc -Wall -Werror -Wextra simple-shell.c
$ ./a.out
$$ ls
$$ ls -l
$$ exit
$$ ^D
$
10
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
Program Design
11
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
Program Design
https://en.wikipedia.org/wiki/Unix_philosophy
UNIX programs...
• ...are simple
• ...follow the element of least surprise
• ...accept input from stdin
• ...generate output to stdout
• ...generate meaningful error messages to stderr
• ...have meaningful exit codes
• ...have a manual page
12
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
13
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
What is the longest word found on the ten most frequently retrieved English Wikipedia pages?
$ URL=“https://dumps.wikimedia.org/other/pageviews/2024/2024-09/pageviews-20240901-000000.gz"
for f in $(curl -L ${URL} | zgrep -i "^en " | sort -k3 -n | tail -10 |
sed -e 's/en \(.*\) [0-9]* [0-9]*/\1/'); do
links -dump https://en.wikipedia.org/wiki/${f};
done |
tr '[:punct:]' ' ' |
tr '[:space:]' '\n' |
tr '[:upper:]' '[:lower:]' | egrep '^[a-z]+$' |
awk '{ print length() " " $0; }' | sort |
uniq |
sort -n |
tail -1
14
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
The UNIX lesystem is a tree structure, with all partitions mounted under the root (/).
File names may consist of any character except / and NUL as pathnames are a
sequence of zero or more lenames separated by /’s.
Directories are special ” les” that contain mappings between inodes and lenames,
called directory entries.
All processes have a current working directory from which all relative paths are
speci ed. (Absolute paths begin with a slash, relative paths do not.)
15
Jan Schaumann 2024-09-09
fi
fi
fi
fi
fi
CS631 - Advanced Programming in the UNIX Environment
$ cd 01
$ more simple-ls.c
$ cc -Wall -Werror -Wextra simple-ls.c
$ ./a.out .
$
16
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
User Identification
User IDs and group IDs are numeric values used to identify users on the system and
grant permissions appropriate to them.
$ whoami
$ id -un
$ groups
$ id [-Gn]
17
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
Calendar time: measured in seconds since the UNIX epoch (Jan 1, 00:00:00, 1970,
GMT). Stored in a variable of type time_t.
$ date +%s
1598535631
https://www.xkcd.com/376/
18
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
https://en.wikipedia.org/wiki/Year_2038_problem
19
Jan Schaumann 2024-09-09
CS631 - Advanced Programming in the UNIX Environment
• clock time
• user CPU time
• system CPU time
20
Jan Schaumann 2024-09-09
fi
fi
CS631 - Advanced Programming in the UNIX Environment
Standard I/O
• le descriptors: Small, non-negative integers which identify a le to the kernel. The shell
can redirect any le descriptor.
• kernel provides unbu ered I/O through e.g. open(2), read(2), write(2), lseek(2),
close(2)
• kernel provides bu ered I/O through e.g. fopen(3), fread(3), fwrite(3), getc(3),
putc(3)
$ cc simple-cat.c
$ a.out
$ a.out simple-cat.c
$ a.out <simple-cat.c >/tmp/copy
$ diff -bu simple-cat.c simple-cat2.c
21
Jan Schaumann 2024-09-09
fi
fi
ff
ff
fi
CS631 - Advanced Programming in the UNIX Environment
Processes
$ proctree
$ echo $$
$ cc -Wall -Werror -Wextra pid.c
$ ./a.out
22
Jan Schaumann 2024-09-09
fi
CS631 - Advanced Programming in the UNIX Environment
Signals
Signals notify a process that a condition has occurred. Signals may be:
• allowed to cause the default action
• intentionally and explicitly ignored
• caught and control transferred to a user-de ned function
23
Jan Schaumann 2024-09-09
fi
CS631 - Advanced Programming in the UNIX Environment
Homework
24
Jan Schaumann 2024-09-09