﻿# Operating Systems

> System programming in C on Linux: files and pointers, processes with fork and exec, signals, pipes and message queues, shared memory and semaphores – and simulation tasks that combine them.

Source: https://laszlonemes.com/university/operating-systems · Author: László Nemes

Operációs rendszerek

In the practical sessions we write C programs that talk to the operating system directly: they create processes, send each other signals and exchange data through pipes, message queues and shared memory.

Every session explains the system calls with diagrams of what happens between the processes, and shows the few lines that matter. The last part of the semester puts everything together in tasks where a given situation has to be simulated.

`C` `Linux` `Processes` `Signals` `IPC` `Semaphores`

## Requirements

What it takes to complete the practice part. Dates and deadlines are on Canvas.

Assignments

**Two take-home assignments that build on each other**

The second assignment continues and extends the first one.

Theory test

**15 questions in 15 minutes, on Canvas**

8 correct answers pass. 0–7: fail (1) · 8–9: pass (2) · 10–11: satisfactory (3) · 12–13: good (4) · 14–15: excellent (5)

Practical test

**A 90-minute programming task, submitted on Canvas**

The basic task is worth a pass (2); every further task block that is correct and complete adds one grade.

Course server

**ssh <neptun-code>@opsys.inf.elte.hu**

Your Neptun code in lowercase, with your faculty (IIG) password. Compile and test your programs there – see the guide below.

## Sessions

Open a session here, or on its own page in a new tab.

- Session 1
  
  **[C basics: pointers, strings and files](https://laszlonemes.com/university/operating-systems/c-basics-files)**
  
  The C under everything else: argc/argv, pointers, strings that end in a 0 byte, and the two ways to a file – buffered fopen/fgets and raw open/read/write – with the loop conditions that keep them correct.
- Session 2
  
  **[Processes and fork](https://laszlonemes.com/university/operating-systems/processes-fork)**
  
  fork() copies the calling process and returns twice. What the child inherits, why variables are not shared, how several forks form a tree, why output can appear twice, and what zombies and orphans are.
- Session 3
  
  **[exec, system and waiting for children](https://laszlonemes.com/university/operating-systems/exec-wait)**
  
  waitpid and the status macros, exec that puts a new program into the same process, the fork + exec + wait pattern every shell uses, what survives exec, and system() as the shortcut through /bin/sh.
- Session 4
  
  **[Signals](https://laszlonemes.com/university/operating-systems/signals)**
  
  What a signal is and where it comes from, handlers with sigaction and what is safe inside them, blocking and sigsuspend instead of a racy pause, the parent–child “ready” handshake, and reaping children with waitpid, WNOHANG and SIGCHLD.
- Session 5
  
  **[Pipes, named pipes and message queues](https://laszlonemes.com/university/operating-systems/pipes-message-queues)**
  
  Moving data between processes: pipes and why every process closes the ends it does not use, sending ints, strings and structs with the right sizes, named pipes with mkfifo, and System V message queues with typed messages.
- Session 6
  
  **[Shared memory and semaphores](https://laszlonemes.com/university/operating-systems/shared-memory-semaphores)**
  
  The life of a shared memory segment from shmget to IPC_RMID, why two processes reading and writing it race, System V semaphores as kernel counters with P and V, the difference between a semaphore that signals (0) and one that locks (1), cleanup with ipcs and ipcrm – and the solutions of the three pipe tasks.

## Guides

Stand-alone pages: references, background reading and fixes for common problems.

- Guide
  
  **[Compiling and running C on the course server](https://laszlonemes.com/university/operating-systems/compiling-and-running)**
  
  Logging in with ssh and copying with scp, gcc with -o, -Wall, -Wextra and -Werror, reading a compiler message, choosing between man 2 and man 3, and cleaning up the stray processes and IPC objects a broken run leaves behind.
- Guide
  
  **[Records in a file: a CRUD skeleton for the assignment](https://laszlonemes.com/university/operating-systems/crud-skeleton)**
  
  How a record keeper over a text file is built: a struct and its TAB-separated line, reading records with fscanf, unique ids, line-based input that cannot overflow, and modify and delete through a temporary file and rename – the base of the first take-home assignment.
- Practice
  
  **[Simulation tasks: everything together](https://laszlonemes.com/university/operating-systems/simulation-tasks)**
  
  Four practice tasks in the style of the practical test – a vaccination day, a thesis registration, a press conference and a masked quiz – each a story whose characters are processes that signal, talk through pipes and message queues and share memory under a semaphore.
