Lab 3: Let there be a process!
Advanced Operating Systems

Zubair Nabi
zubair.nabi@itu.edu.pk

February 13, 2013
Turning on a PC

1

PC boots

2

Starts executing the BIOS (stored on the motherboard)

3

Loads code from the first 512 bytes of boot disk a.k.a. boot sector
• Contains the kernel boot loader
• Instructions to load the kernel into memory
• Loads the boot loader at address 0x7c00
• Jumps to it (sets the %ip)

4

The OS boot loader starts running
• Contained within bootasm.S and bootmain.c in case of xv6
Turning on a PC

1

PC boots

2

Starts executing the BIOS (stored on the motherboard)

3

Loads code from the first 512 bytes of boot disk a.k.a. boot sector
• Contains the kernel boot loader
• Instructions to load the kernel into memory
• Loads the boot loader at address 0x7c00
• Jumps to it (sets the %ip)

4

The OS boot loader starts running
• Contained within bootasm.S and bootmain.c in case of xv6
Turning on a PC

1

PC boots

2

Starts executing the BIOS (stored on the motherboard)

3

Loads code from the first 512 bytes of boot disk a.k.a. boot sector
• Contains the kernel boot loader
• Instructions to load the kernel into memory
• Loads the boot loader at address 0x7c00
• Jumps to it (sets the %ip)

4

The OS boot loader starts running
• Contained within bootasm.S and bootmain.c in case of xv6
Turning on a PC

1

PC boots

2

Starts executing the BIOS (stored on the motherboard)

3

Loads code from the first 512 bytes of boot disk a.k.a. boot sector
• Contains the kernel boot loader
• Instructions to load the kernel into memory
• Loads the boot loader at address 0x7c00
• Jumps to it (sets the %ip)

4

The OS boot loader starts running
• Contained within bootasm.S and bootmain.c in case of xv6
Process

• A sandboxed container for a program
• Illusion of an exclusive CPU;
• and a private address space

• Works on virtual addresses
• Need hardware support to make virtual to physical mapping
• OS maintains a page table for each process
Process

• A sandboxed container for a program
• Illusion of an exclusive CPU;
• and a private address space

• Works on virtual addresses
• Need hardware support to make virtual to physical mapping
• OS maintains a page table for each process
Process

• A sandboxed container for a program
• Illusion of an exclusive CPU;
• and a private address space

• Works on virtual addresses
• Need hardware support to make virtual to physical mapping
• OS maintains a page table for each process
Process

• A sandboxed container for a program
• Illusion of an exclusive CPU;
• and a private address space

• Works on virtual addresses
• Need hardware support to make virtual to physical mapping
• OS maintains a page table for each process
Process (2)

• The address space contains the process’s memory
• Instructions, data, stack, and heap

• Where does the kernel reside then?
• Kernel’s instructions and data is also mapped to high addresses
to each process’s address space
• Each system call executes in the kernel’s mapping of this address
space
• Can directly refer user memory
Process (2)

• The address space contains the process’s memory
• Instructions, data, stack, and heap

• Where does the kernel reside then?
• Kernel’s instructions and data is also mapped to high addresses
to each process’s address space
• Each system call executes in the kernel’s mapping of this address
space
• Can directly refer user memory
Process (2)

• The address space contains the process’s memory
• Instructions, data, stack, and heap

• Where does the kernel reside then?
• Kernel’s instructions and data is also mapped to high addresses
to each process’s address space
• Each system call executes in the kernel’s mapping of this address
space
• Can directly refer user memory
Process (3)

• Process state within the kernel resides within a struct proc
• Page table, kernel stack, and run state

• Each process has:
• A thread of execution, which can be suspended and resumed
• Two stacks:
1
2

User stack
Kernel stack

• During the execution of user code, the user stack is used and the
kernel stack is empty
Process (3)

• Process state within the kernel resides within a struct proc
• Page table, kernel stack, and run state

• Each process has:
• A thread of execution, which can be suspended and resumed
• Two stacks:
1
2

User stack
Kernel stack

• During the execution of user code, the user stack is used and the
kernel stack is empty
Process (3)

• Process state within the kernel resides within a struct proc
• Page table, kernel stack, and run state

• Each process has:
• A thread of execution, which can be suspended and resumed
• Two stacks:
1
2

User stack
Kernel stack

• During the execution of user code, the user stack is used and the
kernel stack is empty
Process (4)

• In case of an interrupt or a system call, the processor:
1

Switches to the kernel stack
• The user stack is still present

2

Raises the hardware privilege level
Starts executing kernel instructions

3

• Once the kernel is done, the processor:
Lowers the privilege level
Switches back to the user stack
3 Resumes executing user instructions
1
2
Process (4)

• In case of an interrupt or a system call, the processor:
1

Switches to the kernel stack
• The user stack is still present

2

Raises the hardware privilege level
Starts executing kernel instructions

3

• Once the kernel is done, the processor:
Lowers the privilege level
Switches back to the user stack
3 Resumes executing user instructions
1
2
Process (4)

• In case of an interrupt or a system call, the processor:
1

Switches to the kernel stack
• The user stack is still present

2

Raises the hardware privilege level
Starts executing kernel instructions

3

• Once the kernel is done, the processor:
Lowers the privilege level
Switches back to the user stack
3 Resumes executing user instructions
1
2
Process (4)

• In case of an interrupt or a system call, the processor:
1

Switches to the kernel stack
• The user stack is still present

2

Raises the hardware privilege level
Starts executing kernel instructions

3

• Once the kernel is done, the processor:
Lowers the privilege level
Switches back to the user stack
3 Resumes executing user instructions
1
2
Process (4)

• In case of an interrupt or a system call, the processor:
1

Switches to the kernel stack
• The user stack is still present

2

Raises the hardware privilege level
Starts executing kernel instructions

3

• Once the kernel is done, the processor:
Lowers the privilege level
Switches back to the user stack
3 Resumes executing user instructions
1
2
Process (4)

• In case of an interrupt or a system call, the processor:
1

Switches to the kernel stack
• The user stack is still present

2

Raises the hardware privilege level
Starts executing kernel instructions

3

• Once the kernel is done, the processor:
Lowers the privilege level
Switches back to the user stack
3 Resumes executing user instructions
1

2
Process (4)

• In case of an interrupt or a system call, the processor:
1

Switches to the kernel stack
• The user stack is still present

2

Raises the hardware privilege level
Starts executing kernel instructions

3

• Once the kernel is done, the processor:
Lowers the privilege level
Switches back to the user stack
3 Resumes executing user instructions
1

2
Process (4)

• In case of an interrupt or a system call, the processor:
1

Switches to the kernel stack
• The user stack is still present

2

Raises the hardware privilege level
Starts executing kernel instructions

3

• Once the kernel is done, the processor:
Lowers the privilege level
Switches back to the user stack
3 Resumes executing user instructions
1

2
Process (5)

• Process states are present within p->state
•
•
•
•
•

Allocated
Ready to run
Running
Waiting for I/O
Exiting

• Page table exists at p->pgdir
• Used by the paging hardware
Process (5)

• Process states are present within p->state
•
•
•
•
•

Allocated
Ready to run
Running
Waiting for I/O
Exiting

• Page table exists at p->pgdir
• Used by the paging hardware
First page table

• When the kernel starts running, virtual addresses map directly to
physical ones
• Why?

• Assembly code within the kernel sets up the first page table
• Now the kernel main can run
First page table

• When the kernel starts running, virtual addresses map directly to
physical ones
• Why?

• Assembly code within the kernel sets up the first page table
• Now the kernel main can run
First page table

• When the kernel starts running, virtual addresses map directly to
physical ones
• Why?

• Assembly code within the kernel sets up the first page table
• Now the kernel main can run
First process

• main first initializes devices and subsystems
• The first process is then created via userinit() which in turn
calls allocproc()
• allocproc() is in charge of allocating a new struct proc
in the process table and setting up its kernel state
• userinit() is only called for the first process while
allocproc() is called for each process creation
First process

• main first initializes devices and subsystems
• The first process is then created via userinit() which in turn
calls allocproc()
• allocproc() is in charge of allocating a new struct proc
in the process table and setting up its kernel state
• userinit() is only called for the first process while
allocproc() is called for each process creation
First process

• main first initializes devices and subsystems
• The first process is then created via userinit() which in turn
calls allocproc()
• allocproc() is in charge of allocating a new struct proc
in the process table and setting up its kernel state
• userinit() is only called for the first process while
allocproc() is called for each process creation
First process

• main first initializes devices and subsystems
• The first process is then created via userinit() which in turn
calls allocproc()
• allocproc() is in charge of allocating a new struct proc
in the process table and setting up its kernel state
• userinit() is only called for the first process while
allocproc() is called for each process creation
First process (2)

• allocproc() (also called by fork()):
1 Scans the process table for a free slot (p->state ==
UNUSED)
2 When it finds one, sets p->state = EMBRYO
4

Assigns the process a pid
Allocates a kernel stack for the process
• In case of failure, sets p->state = UNUSED

5

Sets up a specially prepared kernel stack

3
First process (2)

• allocproc() (also called by fork()):
1 Scans the process table for a free slot (p->state ==
UNUSED)
2 When it finds one, sets p->state = EMBRYO
4

Assigns the process a pid
Allocates a kernel stack for the process
• In case of failure, sets p->state = UNUSED

5

Sets up a specially prepared kernel stack

3
First process (2)

• allocproc() (also called by fork()):
1 Scans the process table for a free slot (p->state ==
UNUSED)
2 When it finds one, sets p->state = EMBRYO
4

Assigns the process a pid
Allocates a kernel stack for the process
• In case of failure, sets p->state = UNUSED

5

Sets up a specially prepared kernel stack

3
First process (2)

• allocproc() (also called by fork()):
1 Scans the process table for a free slot (p->state ==
UNUSED)
2 When it finds one, sets p->state = EMBRYO
4

Assigns the process a pid
Allocates a kernel stack for the process
• In case of failure, sets p->state = UNUSED

5

Sets up a specially prepared kernel stack

3
First process (2)

• allocproc() (also called by fork()):
1 Scans the process table for a free slot (p->state ==
UNUSED)
2 When it finds one, sets p->state = EMBRYO
4

Assigns the process a pid
Allocates a kernel stack for the process
• In case of failure, sets p->state = UNUSED

5

Sets up a specially prepared kernel stack

3
First process (3)

• userinit():
2

Sets up a page table for the process via setupkvm()
Copies the binary for initcode.S (the first process) via

3

inituvm()
Sets p->state = RUNNABLE

1
First process (3)

• userinit():
2

Sets up a page table for the process via setupkvm()
Copies the binary for initcode.S (the first process) via

3

inituvm()
Sets p->state = RUNNABLE

1
First process (3)

• userinit():
2

Sets up a page table for the process via setupkvm()
Copies the binary for initcode.S (the first process) via

3

inituvm()
Sets p->state = RUNNABLE

1
Running the first process

1

After userinit(), main() calls mpmain() which in turn
calls scheduler()

2

scheduler() looks for a process with p->state ==
RUNNABLE

3

It then sets the per-cpu proc variable to the target process

4

Calls switchuvm() to enable the hardware to use p->pgdir

5

Sets p->state = RUNNING

6

Calls swtch() to force a context switch and load p->context
Running the first process

1

After userinit(), main() calls mpmain() which in turn
calls scheduler()

2

scheduler() looks for a process with p->state ==
RUNNABLE

3

It then sets the per-cpu proc variable to the target process

4

Calls switchuvm() to enable the hardware to use p->pgdir

5

Sets p->state = RUNNING

6

Calls swtch() to force a context switch and load p->context
Running the first process

1

After userinit(), main() calls mpmain() which in turn
calls scheduler()

2

scheduler() looks for a process with p->state ==
RUNNABLE

3

It then sets the per-cpu proc variable to the target process

4

Calls switchuvm() to enable the hardware to use p->pgdir

5

Sets p->state = RUNNING

6

Calls swtch() to force a context switch and load p->context
Running the first process

1

After userinit(), main() calls mpmain() which in turn
calls scheduler()

2

scheduler() looks for a process with p->state ==
RUNNABLE

3

It then sets the per-cpu proc variable to the target process

4

Calls switchuvm() to enable the hardware to use p->pgdir

5

Sets p->state = RUNNING

6

Calls swtch() to force a context switch and load p->context
Running the first process

1

After userinit(), main() calls mpmain() which in turn
calls scheduler()

2

scheduler() looks for a process with p->state ==
RUNNABLE

3

It then sets the per-cpu proc variable to the target process

4

Calls switchuvm() to enable the hardware to use p->pgdir

5

Sets p->state = RUNNING

6

Calls swtch() to force a context switch and load p->context
Running the first process

1

After userinit(), main() calls mpmain() which in turn
calls scheduler()

2

scheduler() looks for a process with p->state ==
RUNNABLE

3

It then sets the per-cpu proc variable to the target process

4

Calls switchuvm() to enable the hardware to use p->pgdir

5

Sets p->state = RUNNING

6

Calls swtch() to force a context switch and load p->context
First system call

1

Once initcode.S starts, it makes a call to exec() asking it
to load /init

2

/init creates a new console file and opens it as file descriptors
0, 1, and 2

3

Starts looping

4

Creates a console shell

5

Once the shell exits, it handles zombies
First system call

1

Once initcode.S starts, it makes a call to exec() asking it
to load /init

2

/init creates a new console file and opens it as file descriptors
0, 1, and 2

3

Starts looping

4

Creates a console shell

5

Once the shell exits, it handles zombies
First system call

1

Once initcode.S starts, it makes a call to exec() asking it
to load /init

2

/init creates a new console file and opens it as file descriptors
0, 1, and 2

3

Starts looping

4

Creates a console shell

5

Once the shell exits, it handles zombies
First system call

1

Once initcode.S starts, it makes a call to exec() asking it
to load /init

2

/init creates a new console file and opens it as file descriptors
0, 1, and 2

3

Starts looping

4

Creates a console shell

5

Once the shell exits, it handles zombies
First system call

1

Once initcode.S starts, it makes a call to exec() asking it
to load /init

2

/init creates a new console file and opens it as file descriptors
0, 1, and 2

3

Starts looping

4

Creates a console shell

5

Once the shell exits, it handles zombies
xv6

Your operating system is up and running now!
Today’s Task

• Write C code that implements a grep with two optional flags: -f
and -v and a wc with three optional flags: -b, -l, and -w
• Make two different source files: grep.c and wc.c
• Should be callable via ./grep -v -f foo for instance
• Boilerplate code:

void main(int argc, char *argv[]) {
// argc: number of arguments
// argv[]: array of string arguments
// your code goes here
}
Reading

Appendix A and B and Chapter 1 from “xv6: a simple, Unix-like
teaching operating system”

More Related Content

PDF
AOS Lab 11: Virtualization
PDF
AOS Lab 4: If you liked it, then you should have put a “lock” on it
PDF
AOS Lab 5: System calls
PDF
AOS Lab 12: Network Communication
PDF
AOS Lab 6: Scheduling
PDF
AOS Lab 2: Hello, xv6!
PDF
AOS Lab 8: Interrupts and Device Drivers
PDF
AOS Lab 7: Page tables
AOS Lab 11: Virtualization
AOS Lab 4: If you liked it, then you should have put a “lock” on it
AOS Lab 5: System calls
AOS Lab 12: Network Communication
AOS Lab 6: Scheduling
AOS Lab 2: Hello, xv6!
AOS Lab 8: Interrupts and Device Drivers
AOS Lab 7: Page tables

What's hot (20)

PDF
AOS Lab 10: File system -- Inodes and beyond
PDF
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
PDF
Making Linux do Hard Real-time
PPT
Basic Linux Internals
PDF
High Performance Storage Devices in the Linux Kernel
PPTX
Linux Kernel I/O Schedulers
PPT
Basic Linux kernel
PPTX
Device Drivers and Running Modules
PDF
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
PPTX
Hardware Probing in the Linux Kernel
PPTX
Windows Internals for Linux Kernel Developers
PDF
Kernel Recipes 2015: Kernel packet capture technologies
PDF
Embedded linux network device driver development
PPTX
The TCP/IP Stack in the Linux Kernel
PDF
AOS Lab 1: Hello, Linux!
PPT
Linux introduction
PDF
ODP
Linux internal
PPTX
The Linux Scheduler: a Decade of Wasted Cores
AOS Lab 10: File system -- Inodes and beyond
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Making Linux do Hard Real-time
Basic Linux Internals
High Performance Storage Devices in the Linux Kernel
Linux Kernel I/O Schedulers
Basic Linux kernel
Device Drivers and Running Modules
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Hardware Probing in the Linux Kernel
Windows Internals for Linux Kernel Developers
Kernel Recipes 2015: Kernel packet capture technologies
Embedded linux network device driver development
The TCP/IP Stack in the Linux Kernel
AOS Lab 1: Hello, Linux!
Linux introduction
Linux internal
The Linux Scheduler: a Decade of Wasted Cores
Ad

Viewers also liked (9)

PDF
Topic 13: Cloud Stacks
PDF
AOS Lab 9: File system -- Of buffers, logs, and blocks
PDF
Topic 14: Operating Systems and Virtualization
PDF
Topic 15: Datacenter Design and Networking
PDF
The Anatomy of Web Censorship in Pakistan
PDF
MapReduce and DBMS Hybrids
PDF
Raabta: Low-cost Video Conferencing for the Developing World
PDF
MapReduce Application Scripting
PPTX
The Big Data Stack
Topic 13: Cloud Stacks
AOS Lab 9: File system -- Of buffers, logs, and blocks
Topic 14: Operating Systems and Virtualization
Topic 15: Datacenter Design and Networking
The Anatomy of Web Censorship in Pakistan
MapReduce and DBMS Hybrids
Raabta: Low-cost Video Conferencing for the Developing World
MapReduce Application Scripting
The Big Data Stack
Ad

Similar to AOS Lab 1: Hello, Linux! (20)

PPTX
Os lectures
PPT
Os introduction
PPT
Os introduction
PPTX
Operating Systems Process Management.pptx
PPTX
Chapter 1: Introduction to Unix / Linux Kernel
PPTX
Process scheduling & time
PPTX
Operating system enhancements to prevent misuse of systems
PPTX
Unit 5 ppt
PDF
Embedded Fest 2019. Руслан Биловол. Linux Boot: The Big Bang theory
PPT
Kernal
PDF
Unit 1.1.pdfOperating_SystemOperating_System
PPTX
02-OS-review.pptx
PPTX
Operating systems
PPT
Processes this has stuff about processes and deifntions.ppt
PPT
Chapter-01.ppt
PPT
Chapter 01
PPTX
operating system process management with example
PDF
Lec 3
Os lectures
Os introduction
Os introduction
Operating Systems Process Management.pptx
Chapter 1: Introduction to Unix / Linux Kernel
Process scheduling & time
Operating system enhancements to prevent misuse of systems
Unit 5 ppt
Embedded Fest 2019. Руслан Биловол. Linux Boot: The Big Bang theory
Kernal
Unit 1.1.pdfOperating_SystemOperating_System
02-OS-review.pptx
Operating systems
Processes this has stuff about processes and deifntions.ppt
Chapter-01.ppt
Chapter 01
operating system process management with example
Lec 3

More from Zubair Nabi (11)

PDF
Lab 5: Interconnecting a Datacenter using Mininet
PDF
Topic 12: NoSQL in Action
PDF
Lab 4: Interfacing with Cassandra
PDF
Topic 10: Taxonomy of Data and Storage
PDF
Topic 11: Google Filesystem
PDF
Lab 3: Writing a Naiad Application
PDF
Topic 9: MR+
PDF
Topic 8: Enhancements and Alternative Architectures
PDF
Topic 7: Shortcomings in the MapReduce Paradigm
PDF
Lab 1: Introduction to Amazon EC2 and MPI
PDF
Topic 6: MapReduce Applications
Lab 5: Interconnecting a Datacenter using Mininet
Topic 12: NoSQL in Action
Lab 4: Interfacing with Cassandra
Topic 10: Taxonomy of Data and Storage
Topic 11: Google Filesystem
Lab 3: Writing a Naiad Application
Topic 9: MR+
Topic 8: Enhancements and Alternative Architectures
Topic 7: Shortcomings in the MapReduce Paradigm
Lab 1: Introduction to Amazon EC2 and MPI
Topic 6: MapReduce Applications

Recently uploaded (20)

PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Architecture types and enterprise applications.pdf
PDF
August Patch Tuesday
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Five Habits of High-Impact Board Members
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPT
What is a Computer? Input Devices /output devices
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Taming the Chaos: How to Turn Unstructured Data into Decisions
Univ-Connecticut-ChatGPT-Presentaion.pdf
Hybrid model detection and classification of lung cancer
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
observCloud-Native Containerability and monitoring.pptx
A review of recent deep learning applications in wood surface defect identifi...
Architecture types and enterprise applications.pdf
August Patch Tuesday
Web Crawler for Trend Tracking Gen Z Insights.pptx
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
1 - Historical Antecedents, Social Consideration.pdf
DP Operators-handbook-extract for the Mautical Institute
Five Habits of High-Impact Board Members
Enhancing emotion recognition model for a student engagement use case through...
What is a Computer? Input Devices /output devices
Final SEM Unit 1 for mit wpu at pune .pptx
sustainability-14-14877-v2.pddhzftheheeeee
How ambidextrous entrepreneurial leaders react to the artificial intelligence...

AOS Lab 1: Hello, Linux!

  • 1. Lab 3: Let there be a process! Advanced Operating Systems Zubair Nabi [email protected] February 13, 2013
  • 2. Turning on a PC 1 PC boots 2 Starts executing the BIOS (stored on the motherboard) 3 Loads code from the first 512 bytes of boot disk a.k.a. boot sector • Contains the kernel boot loader • Instructions to load the kernel into memory • Loads the boot loader at address 0x7c00 • Jumps to it (sets the %ip) 4 The OS boot loader starts running • Contained within bootasm.S and bootmain.c in case of xv6
  • 3. Turning on a PC 1 PC boots 2 Starts executing the BIOS (stored on the motherboard) 3 Loads code from the first 512 bytes of boot disk a.k.a. boot sector • Contains the kernel boot loader • Instructions to load the kernel into memory • Loads the boot loader at address 0x7c00 • Jumps to it (sets the %ip) 4 The OS boot loader starts running • Contained within bootasm.S and bootmain.c in case of xv6
  • 4. Turning on a PC 1 PC boots 2 Starts executing the BIOS (stored on the motherboard) 3 Loads code from the first 512 bytes of boot disk a.k.a. boot sector • Contains the kernel boot loader • Instructions to load the kernel into memory • Loads the boot loader at address 0x7c00 • Jumps to it (sets the %ip) 4 The OS boot loader starts running • Contained within bootasm.S and bootmain.c in case of xv6
  • 5. Turning on a PC 1 PC boots 2 Starts executing the BIOS (stored on the motherboard) 3 Loads code from the first 512 bytes of boot disk a.k.a. boot sector • Contains the kernel boot loader • Instructions to load the kernel into memory • Loads the boot loader at address 0x7c00 • Jumps to it (sets the %ip) 4 The OS boot loader starts running • Contained within bootasm.S and bootmain.c in case of xv6
  • 6. Process • A sandboxed container for a program • Illusion of an exclusive CPU; • and a private address space • Works on virtual addresses • Need hardware support to make virtual to physical mapping • OS maintains a page table for each process
  • 7. Process • A sandboxed container for a program • Illusion of an exclusive CPU; • and a private address space • Works on virtual addresses • Need hardware support to make virtual to physical mapping • OS maintains a page table for each process
  • 8. Process • A sandboxed container for a program • Illusion of an exclusive CPU; • and a private address space • Works on virtual addresses • Need hardware support to make virtual to physical mapping • OS maintains a page table for each process
  • 9. Process • A sandboxed container for a program • Illusion of an exclusive CPU; • and a private address space • Works on virtual addresses • Need hardware support to make virtual to physical mapping • OS maintains a page table for each process
  • 10. Process (2) • The address space contains the process’s memory • Instructions, data, stack, and heap • Where does the kernel reside then? • Kernel’s instructions and data is also mapped to high addresses to each process’s address space • Each system call executes in the kernel’s mapping of this address space • Can directly refer user memory
  • 11. Process (2) • The address space contains the process’s memory • Instructions, data, stack, and heap • Where does the kernel reside then? • Kernel’s instructions and data is also mapped to high addresses to each process’s address space • Each system call executes in the kernel’s mapping of this address space • Can directly refer user memory
  • 12. Process (2) • The address space contains the process’s memory • Instructions, data, stack, and heap • Where does the kernel reside then? • Kernel’s instructions and data is also mapped to high addresses to each process’s address space • Each system call executes in the kernel’s mapping of this address space • Can directly refer user memory
  • 13. Process (3) • Process state within the kernel resides within a struct proc • Page table, kernel stack, and run state • Each process has: • A thread of execution, which can be suspended and resumed • Two stacks: 1 2 User stack Kernel stack • During the execution of user code, the user stack is used and the kernel stack is empty
  • 14. Process (3) • Process state within the kernel resides within a struct proc • Page table, kernel stack, and run state • Each process has: • A thread of execution, which can be suspended and resumed • Two stacks: 1 2 User stack Kernel stack • During the execution of user code, the user stack is used and the kernel stack is empty
  • 15. Process (3) • Process state within the kernel resides within a struct proc • Page table, kernel stack, and run state • Each process has: • A thread of execution, which can be suspended and resumed • Two stacks: 1 2 User stack Kernel stack • During the execution of user code, the user stack is used and the kernel stack is empty
  • 16. Process (4) • In case of an interrupt or a system call, the processor: 1 Switches to the kernel stack • The user stack is still present 2 Raises the hardware privilege level Starts executing kernel instructions 3 • Once the kernel is done, the processor: Lowers the privilege level Switches back to the user stack 3 Resumes executing user instructions 1 2
  • 17. Process (4) • In case of an interrupt or a system call, the processor: 1 Switches to the kernel stack • The user stack is still present 2 Raises the hardware privilege level Starts executing kernel instructions 3 • Once the kernel is done, the processor: Lowers the privilege level Switches back to the user stack 3 Resumes executing user instructions 1 2
  • 18. Process (4) • In case of an interrupt or a system call, the processor: 1 Switches to the kernel stack • The user stack is still present 2 Raises the hardware privilege level Starts executing kernel instructions 3 • Once the kernel is done, the processor: Lowers the privilege level Switches back to the user stack 3 Resumes executing user instructions 1 2
  • 19. Process (4) • In case of an interrupt or a system call, the processor: 1 Switches to the kernel stack • The user stack is still present 2 Raises the hardware privilege level Starts executing kernel instructions 3 • Once the kernel is done, the processor: Lowers the privilege level Switches back to the user stack 3 Resumes executing user instructions 1 2
  • 20. Process (4) • In case of an interrupt or a system call, the processor: 1 Switches to the kernel stack • The user stack is still present 2 Raises the hardware privilege level Starts executing kernel instructions 3 • Once the kernel is done, the processor: Lowers the privilege level Switches back to the user stack 3 Resumes executing user instructions 1 2
  • 21. Process (4) • In case of an interrupt or a system call, the processor: 1 Switches to the kernel stack • The user stack is still present 2 Raises the hardware privilege level Starts executing kernel instructions 3 • Once the kernel is done, the processor: Lowers the privilege level Switches back to the user stack 3 Resumes executing user instructions 1 2
  • 22. Process (4) • In case of an interrupt or a system call, the processor: 1 Switches to the kernel stack • The user stack is still present 2 Raises the hardware privilege level Starts executing kernel instructions 3 • Once the kernel is done, the processor: Lowers the privilege level Switches back to the user stack 3 Resumes executing user instructions 1 2
  • 23. Process (4) • In case of an interrupt or a system call, the processor: 1 Switches to the kernel stack • The user stack is still present 2 Raises the hardware privilege level Starts executing kernel instructions 3 • Once the kernel is done, the processor: Lowers the privilege level Switches back to the user stack 3 Resumes executing user instructions 1 2
  • 24. Process (5) • Process states are present within p->state • • • • • Allocated Ready to run Running Waiting for I/O Exiting • Page table exists at p->pgdir • Used by the paging hardware
  • 25. Process (5) • Process states are present within p->state • • • • • Allocated Ready to run Running Waiting for I/O Exiting • Page table exists at p->pgdir • Used by the paging hardware
  • 26. First page table • When the kernel starts running, virtual addresses map directly to physical ones • Why? • Assembly code within the kernel sets up the first page table • Now the kernel main can run
  • 27. First page table • When the kernel starts running, virtual addresses map directly to physical ones • Why? • Assembly code within the kernel sets up the first page table • Now the kernel main can run
  • 28. First page table • When the kernel starts running, virtual addresses map directly to physical ones • Why? • Assembly code within the kernel sets up the first page table • Now the kernel main can run
  • 29. First process • main first initializes devices and subsystems • The first process is then created via userinit() which in turn calls allocproc() • allocproc() is in charge of allocating a new struct proc in the process table and setting up its kernel state • userinit() is only called for the first process while allocproc() is called for each process creation
  • 30. First process • main first initializes devices and subsystems • The first process is then created via userinit() which in turn calls allocproc() • allocproc() is in charge of allocating a new struct proc in the process table and setting up its kernel state • userinit() is only called for the first process while allocproc() is called for each process creation
  • 31. First process • main first initializes devices and subsystems • The first process is then created via userinit() which in turn calls allocproc() • allocproc() is in charge of allocating a new struct proc in the process table and setting up its kernel state • userinit() is only called for the first process while allocproc() is called for each process creation
  • 32. First process • main first initializes devices and subsystems • The first process is then created via userinit() which in turn calls allocproc() • allocproc() is in charge of allocating a new struct proc in the process table and setting up its kernel state • userinit() is only called for the first process while allocproc() is called for each process creation
  • 33. First process (2) • allocproc() (also called by fork()): 1 Scans the process table for a free slot (p->state == UNUSED) 2 When it finds one, sets p->state = EMBRYO 4 Assigns the process a pid Allocates a kernel stack for the process • In case of failure, sets p->state = UNUSED 5 Sets up a specially prepared kernel stack 3
  • 34. First process (2) • allocproc() (also called by fork()): 1 Scans the process table for a free slot (p->state == UNUSED) 2 When it finds one, sets p->state = EMBRYO 4 Assigns the process a pid Allocates a kernel stack for the process • In case of failure, sets p->state = UNUSED 5 Sets up a specially prepared kernel stack 3
  • 35. First process (2) • allocproc() (also called by fork()): 1 Scans the process table for a free slot (p->state == UNUSED) 2 When it finds one, sets p->state = EMBRYO 4 Assigns the process a pid Allocates a kernel stack for the process • In case of failure, sets p->state = UNUSED 5 Sets up a specially prepared kernel stack 3
  • 36. First process (2) • allocproc() (also called by fork()): 1 Scans the process table for a free slot (p->state == UNUSED) 2 When it finds one, sets p->state = EMBRYO 4 Assigns the process a pid Allocates a kernel stack for the process • In case of failure, sets p->state = UNUSED 5 Sets up a specially prepared kernel stack 3
  • 37. First process (2) • allocproc() (also called by fork()): 1 Scans the process table for a free slot (p->state == UNUSED) 2 When it finds one, sets p->state = EMBRYO 4 Assigns the process a pid Allocates a kernel stack for the process • In case of failure, sets p->state = UNUSED 5 Sets up a specially prepared kernel stack 3
  • 38. First process (3) • userinit(): 2 Sets up a page table for the process via setupkvm() Copies the binary for initcode.S (the first process) via 3 inituvm() Sets p->state = RUNNABLE 1
  • 39. First process (3) • userinit(): 2 Sets up a page table for the process via setupkvm() Copies the binary for initcode.S (the first process) via 3 inituvm() Sets p->state = RUNNABLE 1
  • 40. First process (3) • userinit(): 2 Sets up a page table for the process via setupkvm() Copies the binary for initcode.S (the first process) via 3 inituvm() Sets p->state = RUNNABLE 1
  • 41. Running the first process 1 After userinit(), main() calls mpmain() which in turn calls scheduler() 2 scheduler() looks for a process with p->state == RUNNABLE 3 It then sets the per-cpu proc variable to the target process 4 Calls switchuvm() to enable the hardware to use p->pgdir 5 Sets p->state = RUNNING 6 Calls swtch() to force a context switch and load p->context
  • 42. Running the first process 1 After userinit(), main() calls mpmain() which in turn calls scheduler() 2 scheduler() looks for a process with p->state == RUNNABLE 3 It then sets the per-cpu proc variable to the target process 4 Calls switchuvm() to enable the hardware to use p->pgdir 5 Sets p->state = RUNNING 6 Calls swtch() to force a context switch and load p->context
  • 43. Running the first process 1 After userinit(), main() calls mpmain() which in turn calls scheduler() 2 scheduler() looks for a process with p->state == RUNNABLE 3 It then sets the per-cpu proc variable to the target process 4 Calls switchuvm() to enable the hardware to use p->pgdir 5 Sets p->state = RUNNING 6 Calls swtch() to force a context switch and load p->context
  • 44. Running the first process 1 After userinit(), main() calls mpmain() which in turn calls scheduler() 2 scheduler() looks for a process with p->state == RUNNABLE 3 It then sets the per-cpu proc variable to the target process 4 Calls switchuvm() to enable the hardware to use p->pgdir 5 Sets p->state = RUNNING 6 Calls swtch() to force a context switch and load p->context
  • 45. Running the first process 1 After userinit(), main() calls mpmain() which in turn calls scheduler() 2 scheduler() looks for a process with p->state == RUNNABLE 3 It then sets the per-cpu proc variable to the target process 4 Calls switchuvm() to enable the hardware to use p->pgdir 5 Sets p->state = RUNNING 6 Calls swtch() to force a context switch and load p->context
  • 46. Running the first process 1 After userinit(), main() calls mpmain() which in turn calls scheduler() 2 scheduler() looks for a process with p->state == RUNNABLE 3 It then sets the per-cpu proc variable to the target process 4 Calls switchuvm() to enable the hardware to use p->pgdir 5 Sets p->state = RUNNING 6 Calls swtch() to force a context switch and load p->context
  • 47. First system call 1 Once initcode.S starts, it makes a call to exec() asking it to load /init 2 /init creates a new console file and opens it as file descriptors 0, 1, and 2 3 Starts looping 4 Creates a console shell 5 Once the shell exits, it handles zombies
  • 48. First system call 1 Once initcode.S starts, it makes a call to exec() asking it to load /init 2 /init creates a new console file and opens it as file descriptors 0, 1, and 2 3 Starts looping 4 Creates a console shell 5 Once the shell exits, it handles zombies
  • 49. First system call 1 Once initcode.S starts, it makes a call to exec() asking it to load /init 2 /init creates a new console file and opens it as file descriptors 0, 1, and 2 3 Starts looping 4 Creates a console shell 5 Once the shell exits, it handles zombies
  • 50. First system call 1 Once initcode.S starts, it makes a call to exec() asking it to load /init 2 /init creates a new console file and opens it as file descriptors 0, 1, and 2 3 Starts looping 4 Creates a console shell 5 Once the shell exits, it handles zombies
  • 51. First system call 1 Once initcode.S starts, it makes a call to exec() asking it to load /init 2 /init creates a new console file and opens it as file descriptors 0, 1, and 2 3 Starts looping 4 Creates a console shell 5 Once the shell exits, it handles zombies
  • 52. xv6 Your operating system is up and running now!
  • 53. Today’s Task • Write C code that implements a grep with two optional flags: -f and -v and a wc with three optional flags: -b, -l, and -w • Make two different source files: grep.c and wc.c • Should be callable via ./grep -v -f foo for instance • Boilerplate code: void main(int argc, char *argv[]) { // argc: number of arguments // argv[]: array of string arguments // your code goes here }
  • 54. Reading Appendix A and B and Chapter 1 from “xv6: a simple, Unix-like teaching operating system”