<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2022F_Lecture_6</id>
	<title>Operating Systems 2022F Lecture 6 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2022F_Lecture_6"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2022F_Lecture_6&amp;action=history"/>
	<updated>2026-04-05T22:41:58Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2022F_Lecture_6&amp;diff=24080&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  Video from the lecture given on September 27, 2022 is now available: * [https://homeostasis.scs.carleton.ca/~soma/os-2022f/lectures/comp3000-2022f-lec06-20220927.m4v video] * [https://homeostasis.scs.carleton.ca/~soma/os-2022f/lectures/comp3000-2022f-lec06-20220927.cc.vtt auto-generated captions] Video is also available through Brightspace (Resources-&gt;Zoom meeting-&gt;Cloud Recordings tab)  ==Notes==  &lt;pre&gt; Lecture 6 ---------   - validator not working: read the...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2022F_Lecture_6&amp;diff=24080&amp;oldid=prev"/>
		<updated>2022-09-28T03:56:09Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  Video from the lecture given on September 27, 2022 is now available: * [https://homeostasis.scs.carleton.ca/~soma/os-2022f/lectures/comp3000-2022f-lec06-20220927.m4v video] * [https://homeostasis.scs.carleton.ca/~soma/os-2022f/lectures/comp3000-2022f-lec06-20220927.cc.vtt auto-generated captions] Video is also available through Brightspace (Resources-&amp;gt;Zoom meeting-&amp;gt;Cloud Recordings tab)  ==Notes==  &amp;lt;pre&amp;gt; Lecture 6 ---------   - validator not working: read the...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
Video from the lecture given on September 27, 2022 is now available:&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/os-2022f/lectures/comp3000-2022f-lec06-20220927.m4v video]&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/os-2022f/lectures/comp3000-2022f-lec06-20220927.cc.vtt auto-generated captions]&lt;br /&gt;
Video is also available through Brightspace (Resources-&amp;gt;Zoom meeting-&amp;gt;Cloud Recordings tab)&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 6&lt;br /&gt;
---------&lt;br /&gt;
&lt;br /&gt;
 - validator not working: read the error messages, if they don&amp;#039;t make sense&lt;br /&gt;
   please PM me&lt;br /&gt;
&lt;br /&gt;
signals&lt;br /&gt;
-------&lt;br /&gt;
&lt;br /&gt;
Mental model first&lt;br /&gt;
&lt;br /&gt;
What is a signal?&lt;br /&gt;
 - a short message sent to a process by the kernel&lt;br /&gt;
 - only a set number of them, and the signal number is the only&lt;br /&gt;
   data sent&lt;br /&gt;
 - but really, the data isn&amp;#039;t exactly sent&lt;br /&gt;
 - instead, the kernel causes the designated &amp;quot;signal handler&amp;quot; to be run&lt;br /&gt;
&lt;br /&gt;
signals cause an immediate change in process flow of control&lt;br /&gt;
  - it is like jumping to a function, but without any explicit call&lt;br /&gt;
  - more like an exception in a higher level language&lt;br /&gt;
&lt;br /&gt;
So you&amp;#039;re process is executing its code, one instruction at a time&lt;br /&gt;
 - then a signal is received&lt;br /&gt;
 - the kernel causes the process to stop running the code it was running,&lt;br /&gt;
   instead making it jump to a signal handler&lt;br /&gt;
 - when signal handler returns (its a function), execution continues&lt;br /&gt;
   from where the process was before the interruption&lt;br /&gt;
&lt;br /&gt;
ALMOST all signals have signal handlers&lt;br /&gt;
 - two don&amp;#039;t: SIGKILL and SIGSTOP&lt;br /&gt;
 - the process can&amp;#039;t do anything about these, the kernel does it all&lt;br /&gt;
   (either terminate the process or pause its execution)&lt;br /&gt;
&lt;br /&gt;
Every other signal has a handler that is called&lt;br /&gt;
&lt;br /&gt;
The C library defines default signal handlers&lt;br /&gt;
   (functions to be run when a process receives a given signal)&lt;br /&gt;
 - these mostly just terminate the process&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signals are an interrupt-like mechanism&lt;br /&gt;
 - but interrupts are normally generated by hardware and are handled by the kernel&lt;br /&gt;
&lt;br /&gt;
So how does a signal handler return to previously-executing code?&lt;br /&gt;
 - the current address is saved on the stack before the signal handler is invoked&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Signals are used to control the execution of a process and to handle certain kinds of errors.&lt;br /&gt;
&lt;br /&gt;
Note that a signal is kind of the opposite of a system call&lt;br /&gt;
  - kernel calling userspace (process) code,&lt;br /&gt;
    rather than a process calling kernel code&lt;br /&gt;
  - note that a signal handler can make system calls...&lt;br /&gt;
    but it has to be VERY CAREFUL about what it does&lt;br /&gt;
      - only certain ones are safe&lt;br /&gt;
&lt;br /&gt;
In &amp;quot;man 7 signal&amp;quot; man page there is a table listing all the signals&lt;br /&gt;
 - the Action column means the following&lt;br /&gt;
    - Term: terminate the process&lt;br /&gt;
    - Core: &amp;quot;dump core&amp;quot; and terminate the process&lt;br /&gt;
            (core was an old kind of RAM, so dump the contents of RAM to disk)&lt;br /&gt;
    - Ign: ignore&lt;br /&gt;
    - Stop: stop (pause) the process)&lt;br /&gt;
&lt;br /&gt;
These are default actions&lt;br /&gt;
&lt;br /&gt;
Why dump core?&lt;br /&gt;
 - so you can find out what happened!  (for debugging)&lt;br /&gt;
 &lt;br /&gt;
So note that by default, processes are not multithreaded&lt;br /&gt;
&lt;br /&gt;
A process is:&lt;br /&gt;
 - address space plus one or more execution contexts&lt;br /&gt;
&lt;br /&gt;
A thread is:&lt;br /&gt;
 - an execution context inside of an address space&lt;br /&gt;
&lt;br /&gt;
&amp;quot;execution context&amp;quot;&lt;br /&gt;
 - the state of the CPU (its registers)&lt;br /&gt;
&lt;br /&gt;
When you switch to the kernel from a process or switch between processes&lt;br /&gt;
 - the CPU state has to be saved and later restored&lt;br /&gt;
 - processes don&amp;#039;t get their own registers, they have to take turns&lt;br /&gt;
&lt;br /&gt;
Traditional UNIX is single threaded&lt;br /&gt;
 - and signals were designed for single-threaded processes&lt;br /&gt;
&lt;br /&gt;
Literally, in a multithreaded process you have multiple &amp;quot;currently executing&amp;quot; instructions and multiple stacks&lt;br /&gt;
  - one for each running thread&lt;br /&gt;
  - and they have to be very careful not to interfere with each other&lt;br /&gt;
&lt;br /&gt;
Multithreaded programming is how one process can make use of multiple cores&lt;br /&gt;
  - otherwise you need separate processes to run on each core,&lt;br /&gt;
    each with their own memory&lt;br /&gt;
&lt;br /&gt;
In a traditional (single threaded) UNIX process, it is either running regular application code OR running signal handler code, never both at the same time.&lt;br /&gt;
&lt;br /&gt;
Signals are a mess on multithreaded processes on UNIX&lt;br /&gt;
 - really, best to avoid as much as possible&lt;br /&gt;
 - we really need a better mechanism&lt;br /&gt;
&lt;br /&gt;
We&amp;#039;ve developed multithreaded processes to make efficient use of multiple cores&lt;br /&gt;
 - but I think it was a VERY BAD IDEA&lt;br /&gt;
 - if you need to share memory, just share the memory that you need&lt;br /&gt;
   (we can do that pretty easily)&lt;br /&gt;
 - with threads, you SHARE EVERYTHING, which is a recipe for chaos&lt;br /&gt;
   (you never need to share everything)&lt;br /&gt;
   - we will demonstrate these issues later in the term&lt;br /&gt;
   - I&amp;#039;m just mentioning it now to distinguish it from signal processing&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When we create a child process with fork, the parent process gets SIGCHLD when the child process terminates&lt;br /&gt;
 - it is important to handle the death of your children, otherwise you get zombies&lt;br /&gt;
&lt;br /&gt;
A zombie process is a process that has terminated (and so is dead) but is still listed as a process&lt;br /&gt;
 - zombies are unkillable because they are already dead&lt;br /&gt;
 - but if you kill their parent process they die&lt;br /&gt;
    - because the system becomes their parent&lt;br /&gt;
&lt;br /&gt;
Zombies exist because all processes have a return value&lt;br /&gt;
 - somebody must get this return value&lt;br /&gt;
 - if nobody gets it, the system has to hold onto the value until someone does&lt;br /&gt;
   (that&amp;#039;s all a zombie is, it is a placeholder for the return value&lt;br /&gt;
    of a process)&lt;br /&gt;
&lt;br /&gt;
The return value of a process is the return value of main() or the argument given to an exit() system call.&lt;br /&gt;
&lt;br /&gt;
The parent gets a child&amp;#039;s return value by calling wait()&lt;br /&gt;
&lt;br /&gt;
So how are dead child processes handled if the parent won&amp;#039;t call wait/waitpid?&lt;br /&gt;
 - they will stick around for the lifetime of the parent process&lt;br /&gt;
 - when the parent dies, we have (terminated) child processes with no parent&lt;br /&gt;
 - this is a problem, because every process has to fit into the tree of processes somehow&lt;br /&gt;
    - so we &amp;quot;re-parent&amp;quot; the child proceses (a new parent adopts them), there is&lt;br /&gt;
      always a designated process to handle such orphans&lt;br /&gt;
    - (by default it is process #1, but it varies on modern systems)&lt;br /&gt;
    - the new parent will normally call wait and so the dead children&amp;#039;s&lt;br /&gt;
      return values will be received and they can be put to rest&lt;br /&gt;
&lt;br /&gt;
Each wait only works for one child process, so if you have multiple children you&amp;#039;ll have to call wait multiple times&lt;br /&gt;
 - the process should get a SIGCHLD for each&lt;br /&gt;
&lt;br /&gt;
Note that 3000shell does one thing that is kind of bad&lt;br /&gt;
 - you shouldn&amp;#039;t be doing printf&amp;#039;s in a signal handler, not so safe&lt;br /&gt;
   - you want to mess with process state as little as possible while&lt;br /&gt;
     in a handler because the process isn&amp;#039;t necessarily prepared for what&lt;br /&gt;
     the handler will do, as it can happen at any point&lt;br /&gt;
&lt;br /&gt;
Make sure your tutorials 1 &amp;amp; 2 have been marked by the end of tomorrow!&lt;br /&gt;
Assignment 1 is due tomorrow night!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>