Objectives. Introduction to process management of an operating system Explain step-by-step how processes are created in TOS

Similar documents
Objectives. Explain non-preemptive scheduling Explain step-by-step how a context switch works in TOS

Objectives. Making TOS preemptive Avoiding race conditions

Objectives. External declarations Pointers Dynamic data structures using pointers Function pointers

Processes (Intro) Yannis Smaragdakis, U. Athens

4. The Abstraction: The Process

3. Process Management in xv6

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

CS 318 Principles of Operating Systems

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

CS 318 Principles of Operating Systems

Computer Systems II. First Two Major Computer System Evolution Steps

CSC369 Lecture 2. Larry Zhang

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Assembly Language: Function Calls

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS 537 Lecture 2 - Processes

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Assembly Language: Function Calls" Goals of this Lecture"

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Assembly Language: Function Calls" Goals of this Lecture"

Objectives. Learn how to read and write from memory using C-pointers Implement the very first TOS functions

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Chapter 3: Process Concept

Chapter 3: Process Concept

CSCI 8530 Advanced Operating Systems. Part 7 Low-level Memory Management

Project 2: Signals. Consult the submit server for deadline date and time

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

What You Need to Know for Project Three. Dave Eckhardt Steve Muckle

Chapter 3: Process Concept

W4118: PC Hardware and x86. Junfeng Yang

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

Lecture 5: Concurrency and Threads

Pebbles Kernel Specification September 26, 2004

Processes and Threads

Threads and Concurrency

Threads and Concurrency

SE350: Operating Systems. Lecture 3: Concurrency

U23 - Binary Exploitation

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

1 Programs and Processes

ArdOS The Arduino Operating System Reference Guide Contents

Chap 4, 5: Process. Dongkun Shin, SKKU

Practical Malware Analysis

Lecture 4: Process Management

Section 4: Threads and Context Switching

X86 Stack Calling Function POV

x86 Assembly Tutorial COS 318: Fall 2017

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

Operating System. Hanyang University. Hyunmin Yoon Operating System Hanyang University

Operating Systems Process description and control

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

pthreads CS449 Fall 2017

Processes. CS3026 Operating Systems Lecture 05

x86 assembly CS449 Fall 2017

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

CPEG421/621 Tutorial

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Project 2 Non- preemptive Kernel. COS 318 Fall 2016

x86 architecture et similia

What is a Process? Processes and Process Management Details for running a program

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

System Call. Preview. System Call. System Call. System Call 9/7/2018

Mon Sep 17, 2007 Lecture 3: Process Management

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

ò Paper reading assigned for next Tuesday ò Understand low-level building blocks of a scheduler User Kernel ò Understand competing policy goals

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1


Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Program Exploitation Intro

W4118 Operating Systems. Junfeng Yang

Process Concept. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Assembly Language. Lecture 2 x86 Processor Architecture

Register Allocation, iii. Bringing in functions & using spilling & coalescing

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Chapter 3: Processes

SMD149 - Operating Systems

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

Processes. Today. Next Time. Process concept Process model Implementing processes Multiprocessing once again. Scheduling processes

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

Review: Program Execution. Memory program code program data program stack containing procedure activation records

CMSC 412 Project #3 Threads & Synchronization Due March 17 th, 2017, at 5:00pm

Digital Forensics Lecture 3 - Reverse Engineering

Major Requirements of an OS

Processes. Dr. Yingwu Zhu

Lecture 4 CIS 341: COMPILERS

Computer Systems Organization V Fall 2009

Processes, PCB, Context Switch

Computer Systems Assignment 2: Fork and Threads Package

Protection and System Calls. Otto J. Anshus

Function Call Convention

Transcription:

TOS Arno Puder 1

Objectives Introduction to process management of an operating system Explain step-by-step how processes are created in TOS 2

Introduction to Processes What is a process? A process consists of the program code, the data, the heap and the stack A process image is created out of a program with the intention to be executed (e.g., *.exe files under Windows) Single tasking There is only one process. The CPU dedicates complete processing time to one process 3

From Single to Multitasking Two identical computers each run a process independently. Each computer has its own RAM and CPU, and therefore its own registers EIP, ESP, etc. Note that each process has its own code, heap and stack space. Idea: move both processes into one computer. Problem: then we only have one CPU and one set of registers but two processes! Solution: multitasking (time sharing; work a bit on one process and then work a bit on the other process) Process 1 Stack Heap Code 1 MB 0 Process 2 Stack Heap Code 1 MB Computer 1 Computer 2 0 4

Multitasking Being able to run more than one process at the same time Several processes time-share one CPU Each process has its own program code, memory and stack as shown in the picture Process 2 Process 1 Stack Heap Code Stack Heap Code 1 MB 0 5

Sharing the CPU Time Process 1 Process 2 Context Switch 6

Types of Multitasking Cooperative multitasking running process voluntarily relinquishes control Preemptive multitasking running process is suspended involuntary (e.g., because of a hardware interrupt) and control given to another process In both cases, program scheduler decides next process to run Deciding which process to run next is called Scheduling 7

Process Context What is the process context? State of a process consisting of all the x86 registers, heap and stack. Where is process context used? Each process needs its own EIP and ESP registers. Since there is only one copy of those registers, they must be shared by all processes. EIP and ESP are set for the currently running process. If another process is scheduled to run, the values of EIP and ESP are saved and re-loaded for the next process to run. This is called a context switch. 8

Introducing TOS Processes TOS can have up to 20 processes. A process is a C-function (remember pointers to functions?) All TOS processes share the global variables, but each process has its own stack The context of each process is stored in a PCB (Process Control Block) Each process has a priority. Priorities are used to decide which process to run next Priorities must be between 0 (lowest) and 7 (highest) All runnable processes are added to a ready queue Relevant API: create_process(): Creates a new process add_ready_queue(): Add a process to the ready queue remove_ready_queue(): Remove a process from the ready queue become_zombie(): Turn the calling process into a zombie. dispatcher(): Select a runnable process resign(): Voluntarily relinquish control of the CPU 9

Process Control Block (PCB) Each TOS process has a PCB record One PCB record describes the context of exactly one TOS process Since there are a maximum of 20 processes, the PCB is an array with 20 PCB records. TOS PCB Array name state esp PCB TOS PCB Record 0 1 2 19 priority PCB record of one TOS process 10

TOS PCB: struct PCB This is the definition of the PCB structure in TOS as defined in the common header ~/tos/kernel/kernel.h PROCESS is a C-pointer to a PCB entry Many of the fields will become clear later Where are the x86 registers saved? On the stack! typedef struct _PCB { unsigned magic; unsigned used; unsigned short priority; unsigned short state; MEM_ADDR esp; PROCESS param_proc; void* param_data; PORT first_port; PROCESS next_blocked; PROCESS next; PROCESS prev; char* name; } PCB; typedef PCB* PROCESS; 11

TOS Process States Each process has a state that gives a high-level indication on its current activity. A TOS process can be in exactly one state. For now, TOS supports two states (defined in include/kernel.h): STATE_READY: the process is ready to run and is willing to be scheduled CPU cycles. STATE_ZOMBIE: a process has reached the end of its lifespan. It is removed from the ready queue permanently and should no longer be scheduled CPU cycles. Note: TOS does not have a kill_process() function (the reason for this will become apparent later). Being a zombie is the closest thing for a process to be dead. 12

TOS Ready Queue TOS Ready Queue maintains list of runnable processes ready_queue is an 8 elementsized array ordered by process priority (0 == lowest; 7 == highest) Priority is required to react quickly to interrupts Processes with same priority form circular double-linked list within that level Ready queue used to select next process to run Global variable active_proc points to process that the CPU is currently executing PCB.next and PCB.prev are used to implement the double linked list. 7 4 13 6 5 2 1 0 ready_queue 5 10 0 8 13

Maintaining the Ready Queue void add_ready_queue (PROCESS p) Changes the state of process p to ready (p->state = STATE_READY) Process p is added to the ready queue. Process p is added at the tail of the double linked list for the appropriate priority level. p->priority determines to which priority level the process is added in the ready queue automatically maintains the double-linked list. void remove_ready_queue (PROCESS p) Process p is removed from the ready queue. After the removal of the process, the ready queue should be a double-linked list again with process p removed. The caller of remove_ready_queue() should change the state of process p to an appropriate state. Right now, the only state is STATE_ZOMBIE. become_zombie() Turns the caller (active_proc) into a zombie (active_proc->state = STATE_ZOMBIE). Then it should do nothing : while(1); Will be revisited later once TOS supports context switches. 14

CPU Scheduling Round Robin Scheduling: Tasks just take turns, rotate through all tasks Priority Scheduling: Higher-priority tasks are scheduled before lower-priority tasks (e.g., interactive applications get favored over background computation) Real-Time Scheduling: OS makes specific guarantees about when a task will be scheduled 15

Scheduling PROCESS dispatcher() returns the next to be executed process. Only processes that are on the ready queue are eligible for selection. The assumption is that there is always at least one process on the ready queue. Global variable active_proc of type PROCESS always points to the process (i.e., PCB slot) that currently owns the CPU. The next process is selected based on active_proc: If there is a process with a higher priority than active_proc->priority, then that process will be chosen. Otherwise, active_proc->next will be chosen (Round-Robin within the same priority level). 16

TOS Process Creation TOS is one executable file that gets created by the linker The name of this executable is tos.img During booting, this file gets loaded to address 4000 into RAM. A process in TOS is a C-function with the following signature: void process_a (PROCESS, PARAM); All TOS processes share the same code and heap space but still need different stack space. 1 MB 640 KB 4000 0 } } 16 KB stack frame for process 0 16 KB stack frame for process 1 TOS code (tos.img) 17

TOS Process Entry Point A C-function designates a process in TOS. The process needs to be explicitly created (next slide). If void process_a (PROCESS, PARAM) is a TOS process, then process_a defines the entry point of this process, i.e. the new process will start executing from process_a(). The new process is given the input parameters of type PROCESS and PARAM. The latter is defined as an unsigned long in ~/tos/include/kernel.h The first parameter points to the PCB entry for the newly created process and the second parameter is an application-specific parameter passed from parent to child process. Since a TOS process is implemented by a C-function, special care must be taken to ensure that that function is never exited (there is no caller of that function in the traditional sense). By convention, at the end of the function become_zombie() should be called. 18

Creating a TOS process New TOS process can be created via create_process() This function is located in file ~/tos/kernel/process.c Signature: PORT create_process(void (*func) (PROCESS, PARAM), int prio, PARAM param, char* name) Input: func: function pointer that defines the entry point of the process to be created. param: a parameter that the parent process can pass to the child process. prio: Priority of the process. 0 prio 7 name: Clear text name for the process (e.g. Boot process ) Output: For now, this function simply returns a NULL pointer. The meaning of data type PORT will be explained later. 19

Example: create_process() void test_process(process self, PARAM param) { // assert(k_memcmp(self->name, Test process, k_strlen( Test process )+1) == 0); // assert(param == 42) //... become_zombie(); } void kernel_main() { // create_process(test_process, 5, 42, "Test process"); } 20

create_process() What does create_process (func, prio, param, name) do? -Allocates an available PCB entry -Initializes the elements of this PCB entry PCB.magic = MAGIC_PCB PCB.used = TRUE PCB.state = STATE_READY PCB.priority = prio PCB.first_port = NULL PCB.name = name - Allocates an available 16KB stack frame for this process - Initializes an initial stack frame (see next slide) - Saves the stack pointer to PCB.esp - Adds the new process to the ready queue - Returns a NULL pointer 21

Stack of the new process 1 MB 16 K stack frame for process 0 16 K stack frame for process 1 640 KB { { param self func PARAM actual parameter PROCESS actual parameter Return address (dummy argument) Address of new process (EIP) EAX ECX EDX EBX EBP ESI EDI Address TOS code 0 Address will be saved in PCB.esp 22

Printing the PCB It is useful to get a list of all processes. Similar to ps command in Unix This is done via functions print_process() and print_all_processes() located in file ~/tos/kernel/process.c TOS functions: void print_process (WINDOW* wnd, PROCESS proc) print the details of process proc to window wnd void print_all_processes (WINDOW* wnd) print the details of all processes currently existing in TOS to window wnd The following process information should be displayed: Name of the process (PCB.name) State of the process (PCB.state) Priority of the process (PCB.priority) The process that is currently active (I.e., the process where active_proc is pointing to) should also be marked by a flag The following screenshot show how the output could look like. You don t necessarily have to follow the exact same layout. The screenshot is taken from test_create_process_3. 23

Sample Layout for print_all_processes() 24

Note on Initializing There are some global variables in TOS that need to be initialized at startup. Those variables are initialized in C-functions called init_*(): init_dispatcher(): initialize the global variables associated with the ready queue init_process(): initialize the global variables associated with process creation Those init-functions need to be called from kernel_main() in order to initialize everything correctly. The test functions included in TOS call these functions for you. If you run a test program there is no need to call the init-functions explicitly 25

~/tos/kernel/main.c #include <kernel.h> void kernel_main() { init_process(); init_dispatcher(); init_ipc(); init_interrupts(); init_null_process(); init_timer(); init_com(); init_keyb(); init_shell(); become_zombie(); } 26

init_process() When initializing the process sub-system, the first (i.e., current) process becomes the boot process. Use PCB[0] for the boot process. Use the following parameters to initialize the PCB entry for the boot process: PCB[0].magic = MAGIC_PCB PCB[0].used = TRUE PCB[0].state = STATE_READY PCB[0].priority = 1 PCB[0].first_port = NULL PCB[0].name = Boot process Note: PCB[0].esp does not need to be initialized (why?) 27

Assignment 3 Implement the functions located in ~/tos/kernel/dispatch.c: add_ready_queue(), remove_ready_queue(), dispatcher(), init_dispatcher() Implement the functions located in ~/tos/kernel/process.c: create_process(), print_process(), print_all_processes(), init_process() Implementation for become_zombie() is already provided. Test cases: test_create_process_1 test_create_process_2 test_create_process_3 test_dispatcher_1 test_dispatcher_2 test_dispatcher_3 Hint: no inline assembly necessary for this assignment! 28

PacMan (1) Remember: The purpose of the PacMan application is a simple game as a showcase for some of the TOS API. You are encouraged to implement it in order to gain a deeper understanding of the TOS API. The next stage of PacMan can be implemented when assignment 3 is completed. In this next stage, a new TOS process is created for each ghost. 29

PacMan (2) Here is how to define the ghost process: void ghost_proc(process self, PARAM param) { create_new_ghost(); } Note that the signature of ghost_proc follows the conventions of a TOS process. Now you can create several ghost processes in init_pacman() via: int i; for (i = 0; i < num_ghosts; i++) create_process(ghost_proc, 3, 0, "Ghost"); Note: since we have not yet implemented a context switch, creating a ghost will not do anything! Ghost processes will be created, but not yet scheduled. This will only be possible after the next assignment! 30