SlideShare a Scribd company logo
1 of 54
Download to read offline
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

What's hot

AOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyondAOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyond
Zubair Nabi
 
Windows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersWindows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel Developers
Kernel TLV
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!
Zubair Nabi
 
Linux internal
Linux internalLinux internal
Linux internal
mcganesh
 

What's hot (20)

AOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyondAOS Lab 10: File system -- Inodes and beyond
AOS Lab 10: File system -- Inodes and beyond
 
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecksKernel Recipes 2015: Solving the Linux storage scalability bottlenecks
Kernel Recipes 2015: Solving the Linux storage scalability bottlenecks
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
High Performance Storage Devices in the Linux Kernel
High Performance Storage Devices in the Linux KernelHigh Performance Storage Devices in the Linux Kernel
High Performance Storage Devices in the Linux Kernel
 
Linux Kernel I/O Schedulers
Linux Kernel I/O SchedulersLinux Kernel I/O Schedulers
Linux Kernel I/O Schedulers
 
Basic Linux kernel
Basic Linux kernelBasic Linux kernel
Basic Linux kernel
 
Introduction to UNIX
Introduction to UNIXIntroduction to UNIX
Introduction to UNIX
 
Device Drivers and Running Modules
Device Drivers and Running ModulesDevice Drivers and Running Modules
Device Drivers and Running Modules
 
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
 
Hardware Probing in the Linux Kernel
Hardware Probing in the Linux KernelHardware Probing in the Linux Kernel
Hardware Probing in the Linux Kernel
 
Windows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersWindows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel Developers
 
Kernel Recipes 2015: Kernel packet capture technologies
Kernel Recipes 2015: Kernel packet capture technologiesKernel Recipes 2015: Kernel packet capture technologies
Kernel Recipes 2015: Kernel packet capture technologies
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!
 
Linux introduction
Linux introductionLinux introduction
Linux introduction
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
Linux internal
Linux internalLinux internal
Linux internal
 
The Linux Scheduler: a Decade of Wasted Cores
The Linux Scheduler: a Decade of Wasted CoresThe Linux Scheduler: a Decade of Wasted Cores
The Linux Scheduler: a Decade of Wasted Cores
 

Viewers also liked (9)

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

Similar to AOS Lab 1: Hello, Linux!

Operating system enhancements to prevent misuse of systems
Operating system enhancements to prevent misuse of systemsOperating system enhancements to prevent misuse of systems
Operating system enhancements to prevent misuse of systems
Dayal Dilli
 
IT241 - Full Summary.pdf
IT241 - Full Summary.pdfIT241 - Full Summary.pdf
IT241 - Full Summary.pdf
SHEHABALYAMANI
 

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

Os lectures
Os lecturesOs lectures
Os lectures
 
Os introduction
Os introductionOs introduction
Os introduction
 
Os introduction
Os introductionOs introduction
Os introduction
 
Kernal
KernalKernal
Kernal
 
Chapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux KernelChapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux Kernel
 
Process scheduling & time
Process scheduling & timeProcess scheduling & time
Process scheduling & time
 
Operating system enhancements to prevent misuse of systems
Operating system enhancements to prevent misuse of systemsOperating system enhancements to prevent misuse of systems
Operating system enhancements to prevent misuse of systems
 
Unit 5 ppt
Unit 5 pptUnit 5 ppt
Unit 5 ppt
 
Embedded Fest 2019. Руслан Биловол. Linux Boot: The Big Bang theory
Embedded Fest 2019. Руслан Биловол. Linux Boot: The Big Bang theoryEmbedded Fest 2019. Руслан Биловол. Linux Boot: The Big Bang theory
Embedded Fest 2019. Руслан Биловол. Linux Boot: The Big Bang theory
 
Kernal
KernalKernal
Kernal
 
02-OS-review.pptx
02-OS-review.pptx02-OS-review.pptx
02-OS-review.pptx
 
Operating systems
Operating systemsOperating systems
Operating systems
 
IT241 - Full Summary.pdf
IT241 - Full Summary.pdfIT241 - Full Summary.pdf
IT241 - Full Summary.pdf
 
Ali.ppt
Ali.pptAli.ppt
Ali.ppt
 
Chapter-01.ppt
Chapter-01.pptChapter-01.ppt
Chapter-01.ppt
 
Chapter 01
Chapter 01Chapter 01
Chapter 01
 
Lec 3
Lec 3 Lec 3
Lec 3
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdf
 
Windows kernel
Windows kernelWindows kernel
Windows kernel
 
Linux architecture
Linux architectureLinux architecture
Linux architecture
 

More from Zubair Nabi (11)

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

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

AOS Lab 1: Hello, Linux!

  • 1. Lab 3: Let there be a process! Advanced Operating Systems Zubair Nabi zubair.nabi@itu.edu.pk 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”