Advanced Programming in the Unix Environment

Week 5: Debugging your code

Next in our discussion of the Unix _development_ environment, we start a short series on the use of a debugger by example of gdb(1). First, we illustrate just why exactly we might want to use a separate tool instead of sprinkling "printf" statements all over our code...

https://youtu.be/61YwbTQy2G0

#apue #unix #programming #gdb

Advanced Programming in the UNIX Environment: Week 05, Segment 08 - Debugging your code

YouTube

Advanced Programming in the Unix Environment

Week 5: Using gdb(1)

Alright, let's look at gdb(1)! We begin by using it to troubleshoot our failing programs and immediately identify the location of our program where it segfaults. We also see how to inspect variables and call functions from within the debugger.

https://youtu.be/Miw0XLzHCws

#apue #unix #programming #gdb

Advanced Programming in the UNIX Environment: Week 05, Segment 09 - Using gdb(1)

YouTube

Advanced Programming in the Unix Environment

Week 5: Using gdb(1), part II

In this video lecture, we continue to explore the capabilities of the debugger. We show how you can set a breakpoint to pause the program and how to step through the program while watching our code.

https://youtu.be/fOp4Q4mnTD4

#apue #unix #programming #gdb

Advanced Programming in the UNIX Environment: Week 05, Segment 10 - Using gdb(1), part II

YouTube

Advanced Programming in the Unix Environment

Week 5: Using gdb(1), part III

In this video lecture, we'll use our newly learned debugging skills to analyze and fix yet another flawed program. In the process, we learn to debug code from multiple source files and find out how to change the value of variables while the program is running.

https://youtu.be/hgcj7iAxhhU

#apue #unix #programming #gdb

Advanced Programming in the UNIX Environment: Week 05, Segment 11 - Using gdb(1), part III

YouTube

Advanced Programming in the Unix Environment

Week 5: Using gdb(1), part IV

In our last segment on gdb(1), we use the debugger to examine memory locations in a running program and illustrate how pointers and arrays work in the C programming language.

https://youtu.be/hr-rn0yUhAw

#apue #unix #programming #gdb

Advanced Programming in the UNIX Environment: Week 05, Segment 12 - Using gdb to understand pointers

YouTube

Advanced Programming in the Unix Environment

Tool Tip: ed(1) is the standard text editor

As part of our discussion of unix development tools, we take a short detour to provide a quick walkthrough of ed(1), the standard text editor. This is not done merely to up your geek creds, but understanding ed(1) will also helps us better understand other tools like vi(1), sed(1), and ultimately even things like diff(1) and patch(1) better.

https://youtu.be/mRZsV7aMK0I

#apue #unix #programming #ed

Advanced Programming in the UNIX Environment: Tool Tip: ed(1) is the standard text editor

YouTube

Advanced Programming in the Unix Environment

Week 6: Memory Layout of a Process

Let's talk about the details of a process. We start with a visual representation of a process in memory and create a program that prints out the addresses of different elements of a program and observe the alignment of the text, data, and bss segments as well as the placement of the heap and stack. We also illustrate what a stack overflow looks like.

https://youtu.be/aHmcowhoOzU

#apue #unix #programming

Advanced Programming in the Unix Environment

Week 6: Program Startup

In this video lecture, we learn how a program starts. "What, you write 'main()' and off you go!" you say? Not so fast! Let's look at the program entry point '_start' and see if we can change it, and in the process find out that the function prototype of 'main' may not be what you think it is. Use your debugger to better understand how the process begins and ends its life.

https://youtu.be/Yul4d-BDdx8

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 06, Segment 2 - Program Startup

YouTube

Advanced Programming in the Unix Environment

Week 6: Process Termination

After we talked about program startup in our previous video, we now look at how programs terminate. We observe the return status and how exit handlers are registered.

https://youtu.be/xNeEw1f_js8

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 06, Segment 3 - Program Termination

YouTube

Advanced Programming in the Unix Environment

Week 6: Process Termination

Relevant to our discussion of process termination, using the debugger to understand program behavior, our little pop quiz "What does `int main() { printf("Hello World!\n"); }` return?" provides for a short detour into the realm of nasal daemons:

https://www.netmeister.org/blog/return-printf.html

#apue #unix #programming

There is no 'printf'.

In C, 'main' returns an int. What happens when you don't explicitly return a value, though? C99 provides a return value of 0, but C89... well, that's undefined, my favorite kind of behavior.

Advanced Programming in the Unix Environment

Week 6: The Environment

In this video lecture, we're going to take a look at the process environment. We'll use what we learned about the process layout in memory to understand how the environment variables are stored and, if necessary, moved around. We'll also get a quick look at what malloc(3) does.

https://youtu.be/8DEPA6nJXNY

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 06, Segment 4 - The Environment

YouTube

Advanced Programming in the Unix Environment

Week 6: Process Limits and Identifiers

In the last few videos, we've looked at the details of the process layout in memory, and how a process starts and is terminated. In this video lecture, we we will spend a few short minutes on two additional aspects of every process: its resource limits and its process ID.

https://youtu.be/bnki8QKjSfQ

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 06, Segment 5 - Process Limits and Identifiers

YouTube

Advanced Programming in the Unix Environment

Week 6: Process Control

In this video lecture, we will look at process control: how new processes are started from an executable, and what happens when they terminate. In particular, we will look at the fork(2), exec(3), and wait(2) system calls. Be warned, though: there will be zombies, so limber up.

https://youtu.be/KJq5nTCFsIg

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 06, Segment 6 - Process Control

YouTube

Advanced Programming in the Unix Environment

Week 7: The Login Process

We'll begin our discussion of process relationships, including process groups, sessions, and our first, asynchronous type of inter process communication in the form of signals. To get us started, we look at how processes created during the normal Unix boot process relate to one another.

(This does not include systemd. I said "the normal Unix boot process". init or gtfo)

https://youtu.be/eNYTJbmYzH8

#apue #unix #programming

Advanced Programming in the Unix Environment

Week 7: Job Control

In this video lecture, we illustrate the concept of job control in the shell, first introduced in the C shell, and allowing you to run multiple tasks from within the same terminal, switching back and forth between them by placing them into the background, suspending them, or bringing them to the foreground. It's one of my all-time favorite productivity hacks - Ctrl+Z FTW!

https://youtu.be/l6-663i8bwQ

#apue #unix #programming

Advanced Programming in the Unix Environment

Week 7: Signals

In this video lecture, we look at Unix signals, simple, asynchronous event notifications. We run through a number of examples to illustrate how signals are delivered, can be ignored, caught, handled, or blocked.

https://youtu.be/Vh7rBGj0Ty4

#apue #unix #programming

Advanced Programming in the Unix Environment

Week 7: Reentrant and Interrupted Functions

With this video lecture, we will conclude our coverage of Unix signals, by looking at what happens when we call unsafe functions from within the signal handler as well as what happens when we are interrupted while blocking on certain I/O.

https://youtu.be/0GRLhpMao3I

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 07, Segment 5 - Reentrant & interrupted functions

YouTube

Advanced Programming in the Unix Environment

Week 8: Interprocess Communications Intro

This week, we begin our discussion of Interprocess Communication. We start by providing a quick overview of the different properties of IPC mechanisms as well as which mechanisms we will cover.

https://youtu.be/JHV2dsGZUzk

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 08, Segment 1 - Interprocess Communication Intro

YouTube

Advanced Programming in the Unix Environment

Week 8: System V IPC

In this video lecture, we cover traditional, primarily asynchronous System V Inter Process Communication: semaphores, shared memory, and message queues. We then also take a brief look at the POSIX message queues implementation.

https://youtu.be/hTUquvhWysA

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 08, Segment 2 - System V IPC

YouTube

Advanced Programming in the Unix Environment

Week 8: Pipes and FIFOs

With this video lecture, we continue our discussion of Interprocess Communication and dive into two of the oldest and most ubiquitous forms of Unix IPC: pipes and FIFOs. We also discuss popen(3) and the dangers of shelling out to unverified commands.

https://youtu.be/mq5G33Sbc_o

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 08, Segment 3 - Pipes and FIFOs

YouTube

Advanced Programming in the Unix Environment

Did you know that #NetBSD comes with a number of historical Unix research papers and supplementary documentation? Here, we find papers by Marshall Kirk McKusick on the Fast File System, by Robert Morris and Ken Thompson on Password Security, a shell tutorial by Stephen R. Bourne, a guide to using vi(1) by Bill Joy, and the well known BSD IPC Tutorials! Take a tour through /usr/share/doc...

https://youtu.be/XqhOUqi4fc0

#apue #unix #programming

Advanced Programming in the UNIX Environment: Tool Tip - /usr/share/doc

YouTube

Advanced Programming in the Unix Environment

Week 9: socketpair(2)

Let's continue with the above mentioned IPC tutorial and discuss the socketpair(2) system call and compare its functionality with that of the pipe(2) call. This introduces the concept of sockets, domains, and protocols, which we'll dive into in the next few videos.

https://youtu.be/w6Au54pVt-8

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 09, Segment 1 - socketpair(2)

YouTube

Advanced Programming in the Unix Environment

Week 9: socket(PF_LOCAL, SOCK_DGRAM, 0)

Following up on socketpairs, we're continuing our discussion of the sockets API by an example of datagram connections in the PF_LOCAL (formerly AF_UNIX) domain.

https://youtu.be/7LoLt49oB9A

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 09, Segment 2 - socket(2)s in the LOCAL domain

YouTube

Advanced Programming in the Unix Environment

Week 9: socket(PF_INET, SOCK_DGRAM, 0)

We're finally moving on to network communications, and show how to communicate between hosts across the internet using datagram sockets in the PF_INET domain, that is, via UDP. We also observe the packets as they are sent by using the tcpdump(8) utility.

https://youtu.be/MMQ50PADnrY

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 09, Segment 3 - DGRAM Sockets in the INET Domain

YouTube

Advanced Programming in the Unix Environment

Week 9: socket(PF_INET6, SOCK_STREAM, 0)

In this video, we demonstrate sockets using TCP to establish a sequenced, reliable, two-way byte stream over an #IPv6 network. Our good friend tcpdump(8) helps us inspect the packets on the wire to observe the 3-way handshake, data being exchanged, and connection tear down or observing a reset (RST) packet from the server if it's not listening on the port.

https://youtu.be/qKMXw76Dk1o

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 9, Segment 4 - STREAM sockets in the INET6 Domain

YouTube

Advanced Programming in the Unix Environment

Week 9: I/O Multiplexing

With this video, we complete our discussion of interprocess communication and move from one-to-one communications towards a more typical client-server model with a server side process capable of handling multiple simultaneous clients. We accomplish this by using the select(2) syscall to handle synchronous I/O multiplexing.

https://youtu.be/Y5PiHboUctw

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 09, Segment 5 - I/O Multiplexing

YouTube

Advanced Programming in the Unix Environment

Week 10: Dæmon Processes

In this video lecture, we take a quick look at processes intended to run continuously and in the background: daemon processes. We'll also brush upon system start scripts and daemon/service conventions.

https://youtu.be/YbYQqVMv7b8

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 10, Segment 1 - Daemon processes

YouTube

Advanced Programming in the Unix Environment

Week 11: The Executable and Linkable Format

This week, we begin our exploration of shared libraries. We start with a look at the Executable and Linkable Format (ELF) for binary files such as executables, object files, core files and shared libraries. We'll use the hexdump(1) and readelf(1) utilities to better understand the format.

https://youtu.be/i1UDF05iZPU

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 11, Segment 1 - The Executable & Linkable Format

YouTube

Advanced Programming in the Unix Environment

Week 11: Of Linkers and Loaders

Picking up where we left off when we talked about the compilation and linking process in Week 05, we now take a look at how a relocatable object file is turned into an executable by the dynamic linker (ld) and how an executable is loaded into memory by the run-time link-editor (ld.so or ld.elf_so).

https://youtu.be/8KWuz7gLycc

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 11, Segment 2 - Of Linkers and Loaders

YouTube

Advanced Programming in the Unix Environment

I often see students confuse the "sizeof" operator and the "strlen" library function, especially when mixing fixed-sized buffers vs. pointers. So as a brief intermezzo, let's explain the difference between the two.

https://youtu.be/FyPqII2rewA

Based on that, I eventually ended up writing a manual page for 'sizeof', which is now included in #FreeBSD:

https://man.freebsd.org/cgi/man.cgi?query=sizeof

#apue #unix #programming

Advanced Programming in the UNIX Environment: Tool Tip: sizeof != strlen

YouTube

Advanced Programming in the Unix Environment

Week 12: syslog(3)

In this video lecture, we take a look at the standard Unix central logging facility, syslogd(8), and the syslog(3) library function. We show examples of system logs and how to separate messages by priority and level.

https://youtu.be/YfdAJ8rhG-I

Below is an accurate capture of syslogd(8) in action:

#apue #unix #programming

Advanced Programming in the Unix Environment

Week 12: Non-blocking I/O

Continuing our grab bag of miscellaneous topics, we briefly cover non-blocking I/O, whereby a system call that would normally block, waiting for I/O, returns immediately with an errno of EAGAIN or EWOULDBLOCK. We use the examples of I/O into a pipe (restricted to PIPE_BUF bytes at a time) or over the network using TCP with its buffer.

https://youtu.be/_hOLO_27L7U

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 12, Segment 2 - Non-blocking I/O

YouTube

Advanced Programming in the Unix Environment

Week 12: Resource Locking

More miscellaneous "advanced I/O" - this time we discuss resource- and record locking using flock(2), lockf(3), and fcntl(2).

https://youtu.be/017DtsMRHjg

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 12, Segment 3 - Resource Locking

YouTube

Advanced Programming in the Unix Environment

Week 12: Asynchronous and Memory Mapped I/O

With this video, we conclude our short series on advanced I/O topics with a quick summary of asynchronous (aio(7)) and memory mapped I/O (mmap(2)).

https://youtu.be/_R_t0d5BzEk

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 12, Segment 4 - Asynchronous & Memory Mapped I/O

YouTube

Advanced Programming in the Unix Environment

Week 12: Cryptographic Basics

A segment on where and how cryptography can help solve certain security issues, including a few code examples for how to handle secrets/passwords (and how they linger in memory!), how to hash passwords, and the impact of poor pseudo-random number generators (with a blast from the past via the 2008 Debian weak keys).

Lecture slides:
https://stevens.netmeister.org/631/12-cryptobasics.pdf

#apue #unix #programming

Advanced Programming in the Unix Environment

Week 13: Restricting Processes: POSIX ACLs

This week we're diving deeper into ways to restrict processes. We begin with a review of standard Unix file permissions and then show how POSIX.1e Access Control Lists (ACLs) can be used for more fine-grained control.

https://youtu.be/lCACl3NE058

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 13, Segment 1 - POSIX ACLs

YouTube

Advanced Programming in the Unix Environment

Week 13: Restricting Processes: eUIDs, file flags, mount options, securelevels

In this video lecture, we look at new ways to restrict processes and even root itself. We revisit how we change effective UIDs using setuid, su(1) and sudo(8), and then cover file flags, certain mount options, and BSD securelevels.

https://youtu.be/WBm5j-XAyVk

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 13, Segment 2 - chflags, mount -o, securelevels

YouTube

Advanced Programming in the Unix Environment

Week 13: Restricting Processes: Restricted shells, Chroots, Jails

In this video lecture, we cover restricted shells, the chroot(2) syscall, and FreeBSD jails as ways to contain users and processes beyond file access controls.

https://youtu.be/UF7gNYaZuG0

#apue #unix #programming #freebsd

Advanced Programming in the UNIX Environment: Week 13, Segment 3 - Restricted Shells, Chroots, Jails

YouTube

Advanced Programming in the Unix Environment

Week 13: Restricting Processes: Process Priorities

In this video lecture, we revisit resource utilization by processes and how to set their CPU scheduling priority or "niceness". We discuss dynamic priority scheduling algorithms and the use of the nice(1) and renice(8) utilities.

https://youtu.be/icjvv-RFm1I

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 13, Segment 4 - Process Priorities

YouTube

Advanced Programming in the Unix Environment

Week 13: Restricting Processes: Processor Affinity and CPU Sets

In this video lecture, we discuss how we can restrict a process to a specific CPU or set of CPUs by way of "CPU pinning" or processor affinity and via CPU sets.

https://youtu.be/sy8121p4pjw

This lecture also exists as a blog post version as well:
https://www.netmeister.org/blog/cpu-pinning-and-sets.html

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 13, Segment 5 - Processor Affinity and CPU Sets

YouTube

Advanced Programming in the Unix Environment

Week 13: Restricting Processes: Capabilities, Control Groups, Containers

In this video lecture, we're completing our discussion on how to restrict processes by looking at POSIX Capabilities, Linux Namespaces and Control Groups or "cgroups", and how these and the various other methods we've discussed in the last few videos allow us build containers like Docker or LXC.

https://youtu.be/ozzyh5wjjdw

#apue #unix #programming

Advanced Programming in the UNIX Environment: Week 13, Segment 6 - Capabilities, cgroups, containers

YouTube

Advanced Programming in the Unix Environment

The entire semester in one 157 slides deck: https://stevens.netmeister.org/631/f25.pdf

Full playlist on youtube of all 72 video lectures: https://www.youtube.com/watch?v=BsB9Cg6yJc4&list=PL0qfF8MrJ-jxMfirAdxDs9zIiBg2Wug0z

#apue #unix #programming

@jschauma
It is astonishing how much of my 1980s teaching of UNIX systems programming is still relevant. i would say that I covered at least 70% of that material then in my trainings (System V was current then)
@jschauma Thanks for sharing! I appreciate you posting these throughout the semester, and now making the slides available