Skip to main content

Homework Code

This page is a generated reference surface for selective reading. It exists to keep the learner apps guide-first while still preserving source access.

Learning objectives

  • Explain the main ideas and vocabulary in Homework Code.
  • Work through the source examples for Homework Code without depending on raw chunk order.
  • Use Homework Code as selective reference when learner modules point back to Ostep.

Prerequisites

  • None curated yet.

Module targets

  • module-01-processes-scheduling

AI companion modes

  • Explain simply
  • Socratic tutor
  • Quiz me
  • Challenge my understanding
  • Diagnose my confusion
  • Generate extra practice
  • Revision mode
  • Connect forward / backward

Source-of-truth note

This unit is anchored to Ostep and the source chapter "Homework Code". Use external resources only to clarify, extend, or modernize details without replacing the chapter's conceptual spine.

External enrichment

No chapter-specific enrichment resources are curated yet. Add them in the unit manifest when a source clearly improves learning.

Source provenance

  • Primary source: Ostep
  • Source chapter: Homework Code
  • Raw source file: 028-homework-code.md

Merged source

Homework Code

Homework (Code)

ASIDE: CODINGHOMEWORKS

Coding homeworks are small exercises where you write code to run on a real machine to get some experience with some of the basic APIs that modern operating systems have to offer. After all, you are (probably) a computer scientist, and therefore should like to code, right? Of course, to truly become an expert, you have to spend more than a little time hacking away at the machine; indeed, find every excuse you can to write some code and see how it works. Spend the time, and become the wise master you know you can be.

In this homework, you are to gain some familiarity with the process management APIs about which you just read. Don't worry - it's even more fun than it sounds! You'll in general be much better off if you find as much time as you can to write some code5, so why not start now?

Questions

  1. Write a program that callsfork(). Before callingfork(), have the

main process access a variable (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process?

What happens to the variable when both the child and parent change the value ofx?

2. Write a program that opens a file (with the open()system call)
and then callsfork()to create a new process. Can both the child
and parent access the file descriptor returned by open()? What

happens when they are writing to the file concurrently, i.e., at the same time?

  1. Write another program usingfork(). The child process should

print "hello"; the parent process should print "goodbye". You should try to ensure that the child process always prints first; can you do thiswithoutcallingwait()in the parent?

  1. Write a program that calls fork()and then calls some form of

exec()to run the program/bin/ls.See if you can try all of the variants ofexec(), includingexecl(), execle(), execlp(), execv(), execvp(),andexecvP(). Why do you think there are so many variants of the same basic call?

  1. Now write a program that useswait()to wait for the child process

to finish in the parent. What doeswait()return? What happens if you usewait()in the child?

5If you don't like to code, but want to become a computer scientist, this means you need to either (a) become really good at the theory of computer science, or (b) perhaps rethink this whole "computer science" thing you've been telling everyone about.

  1. Write a slight modification of the previous program, this time us-

ingwaitpid()instead ofwait(). When wouldwaitpid()be useful?

  1. Write a program that creates a child process, and then in the child

closes standard output (STDOUTFILENO). What happens if the child callsprintf()to print some output after closing the descriptor?

  1. Write a program that creates two children, and connects the stan-

dard output of one to the standard input of the other, using the pipe()system call.