CS460 Notes on Multitasking 3.1. Multitasking In general, multitasking refers to the ability of performing several independent activities at the same time. For example, we often see people talking on their cell phones while driving. In a sense, these people are doing multitasking, although a very bad kind. In computing, multitasking refers to the execution of several independent tasks at the same time. In a uniprocessor (single CPU) system, only one task can execute at a time. Multitasking is achieved by multiplexing the CPU's execution time among different tasks, i.e. by switching the CPU’s execution from one task to another. The mechanism of switching executions among different tasks is called context switching, which changes the execution environment of one task to that of another task. If the switch is fast enough, it gives the illusion that all the tasks are executing simultaneously. This logical parallelism is called concurrency. In multiprocessor systems with many CPUs or processor cores, tasks can execute on different CPUs in parallel in real time. In addition, each processor may also do multitasking by executing different tasks concurrently. Multitasking is the basis of all operating systems. It is also the basis of concurrent programming in general. 3.2. The Process Concept An operating system is a multitasking system. In an operating system, tasks are also called processes. For all practical purposes, the terms task and process can be used interchangeably. In Chapter 2, we defined an execution image as a memory area containing the execution’s code, data and stack. Formally, we define process as A process is the execution of an image. It is a sequence of executions regarded by the OS kernel as a single entity for using system resources. System resources include memory space, I/O devices and, most importantly, CPU time. In an OS kernel, each process is represented by a unique data structure, called the Process Control Block (PCB) or Task Control Block (TCB), etc. In this book, we shall simply call it the PROC structure. Like a personal record, which records all the information of a person, a PROC structure contains all the information of a process. In a real OS, the PROC structure may contain many fields and quite large. To begin with, we shall define a very simple PROC structure to represent processes. typedef struct proc{ struct proc *next; // next proc pointer int *ksp; // saved sp: at byte offset 4 int pid; // process ID int ppid; // parent process pid int status; // PROC status=FREE|READY, etc. int priority; // scheduling priority int kstack[1024]; // process stack }PROC; In the PROC structure, next is a pointer to the next PROC structure. It is used to maintain the PROCs in various dynamic data structures, such as link lists and queues. The field ksp
15
Embed
CS460 Notes on Multitasking 3.1. Multitaskingcs460/460mtx.pdf · CS460 Notes on Multitasking 3.1. Multitasking In general, multitasking refers to the ability of performing several
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CS460 Notes on Multitasking
3.1. Multitasking
In general, multitasking refers to the ability of performing several independent
activities at the same time. For example, we often see people talking on their cell phones
while driving. In a sense, these people are doing multitasking, although a very bad kind.
In computing, multitasking refers to the execution of several independent tasks at the
same time. In a uniprocessor (single CPU) system, only one task can execute at a time.
Multitasking is achieved by multiplexing the CPU's execution time among different tasks,
i.e. by switching the CPU’s execution from one task to another. The mechanism of
switching executions among different tasks is called context switching, which changes
the execution environment of one task to that of another task. If the switch is fast enough,
it gives the illusion that all the tasks are executing simultaneously. This logical
parallelism is called concurrency. In multiprocessor systems with many CPUs or
processor cores, tasks can execute on different CPUs in parallel in real time. In addition,
each processor may also do multitasking by executing different tasks concurrently.
Multitasking is the basis of all operating systems. It is also the basis of concurrent
programming in general.
3.2. The Process Concept
An operating system is a multitasking system. In an operating system, tasks are also
called processes. For all practical purposes, the terms task and process can be used
interchangeably. In Chapter 2, we defined an execution image as a memory area
containing the execution’s code, data and stack. Formally, we define process as
A process is the execution of an image.
It is a sequence of executions regarded by the OS kernel as a single entity for using
system resources. System resources include memory space, I/O devices and, most
importantly, CPU time. In an OS kernel, each process is represented by a unique data
structure, called the Process Control Block (PCB) or Task Control Block (TCB), etc. In
this book, we shall simply call it the PROC structure. Like a personal record, which
records all the information of a person, a PROC structure contains all the information of a
process. In a real OS, the PROC structure may contain many fields and quite large. To
begin with, we shall define a very simple PROC structure to represent processes.
typedef struct proc{
struct proc *next; // next proc pointer
int *ksp; // saved sp: at byte offset 4
int pid; // process ID
int ppid; // parent process pid
int status; // PROC status=FREE|READY, etc.
int priority; // scheduling priority
int kstack[1024]; // process stack
}PROC;
In the PROC structure, next is a pointer to the next PROC structure. It is used to maintain
the PROCs in various dynamic data structures, such as link lists and queues. The field ksp
is the saved stack pointer when a process is not executing. It points to the stack top when
the process gives up the CPU. It is defined as the second field in the PROC structure,
which is at the byte offset 4 to access PROC.ksp in assembly code. Among the other
fields of the PROC structure, pid is the process ID number that identifies a process, ppid
is the parent process ID number, status is the current status of the process, priority is the
process scheduling priority and kstack is the process stack when it is executing. An OS
kernel usually defines a finite number of PROC structures in its data area, denoted by
PROC proc[NPROC]; // NPROC a constant, e.g. 64
which are used to represent processes in the system. In a single CPU system, only one
process can be executing at a time. The OS kernel usually uses a global PROC pointer,
running or current, to point at the PROC that is currently executing. In a multiprocessor
OS with multiple CPUs, many processes may be executing on different CPUs in parallel
in real time. Accordingly, in a MP system running[NCPU] may be an array of pointers,
each points at a PROC running on a specific CPU. For simplicity, we shall only consider
single CPU systems.
3.3. A Multitasking System
In order to let the reader have a better understanding of the concept of processes, we
shall begin the study by a concrete programming example, which is designed to illustrate
the principles of multitasking and the programming techniques of context switching. The
example program implements a multitasking environment, which simulates the kernel
mode operations of an operating system. The multitasking system, denoted by MT,
consists of the following components.
3.3.1. type.h file: The type.h file defines system constants and a simple PROC structure to
represent processes.
/*********** type.h file ************/
#define NPROC 9 // number of PROCs
#define SSIZE 1024 // stack size = 4KB
// PROC status
#define FREE 0
#define READY 1
#define SLEEP 2
#define ZOMBIE 3
typedef struct proc{
struct proc *next; // next proc pointer
int *ksp; // saved stack pointer
int pid; // pid = 0 to NPROC-1
int ppid; // parent pid
int status; // PROC status
int priority; // scheduling priority
int kstack[SSIZE]; // process stack
}PROC;
As we expand the MT system, we shall add more fields to the PROC structure later.
3.3.2. The ts.s file: The ts.s file implements process context switching in 32-bit GCC
assembly code, which will be explained in later sections.