Top Banner
Embedded Systems KUT 1-1 Embedded Systems Linux Structure, System Call, Process, Task, Process Control Block(PCB), Thread, Process State, Inter-Process Communication(IPC), Signal, Scheduler, Kernel Module Prof. Myung-Eui Lee (A-405) [email protected]
51

Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Jun 03, 2021

Download

Documents

dariahiddleston
Welcome message from author
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
Page 1: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-1

Embedded Systems

Linux Structure, System Call, Process, Task, Process Control Block(PCB), Thread, Process State, Inter-Process Communication(IPC), Signal, Scheduler, Kernel Module

Prof. Myung-Eui Lee (A-405)[email protected]

Page 2: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-2

Linux Structurel Linux Structure

System Call Interface

Buffer Cache

Memory Manager

Process ManagerFilesystem Manager

Device Manager Network Manager

Ext2fs proc devfs

minix nfs msdos

Console KBD SCSICD-ROM PCI network

Ipv6 ethernet

…..

Memory Management

Process ManagementSchedulerSignaling

Device Interface

proc1 proc2 proc3 proc4 proc5 procn

dev1 dev2 dev3 dev4 devn

char block

User Space

Kernel Space

Page 3: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-3

Linux Structurel Linux Structure

Computer Hardware

System Shared Libraries (System Call Interface)

Applications: Graphics UI, Compilers, Media player

API

UI

BIOS

Text UI = Shell: sh, csh, ksh, bash, tcsh, zsh

Compiler libraries (libc.a)

Device DrivesKernel

File management

Memory management

Process management

Page 4: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-4

System Call Interface

System CallAPI – System call – OS Relationship

Standard C Lib.C program invoking printf() library call,

which calls write() system callar –t /usr/lib/gcc/arm-linux-gnueabihf/libc/libc.aar –t /usr/lib/x86_64-linux-gnu/libc.a

./arch/arm/tools/syscall.tbl

./arch/x86/entry/syscalls/syscall_64.tbl

https://www.gnu.org/software/libc/manual/html_mono/libc.html#Function-Index

Page 5: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-5

System Calll System calls define the programmer interface to

Linux system » Interface between user-level processes and hardware

devices§ CPU, memory, disks etc.

» Make programming easier§ Let kernel take care of hardware-specific issues

» Increase system security§ Let kernel check requested service via system call

» Provide portability§ Maintain interface but change functional implementation

l Roughly five categories of system calls in Linux» Process control» File management» Device management» Information maintenance» Communication

Page 6: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-6

System Calll Invoked by executing int or swi instruction

» CPU switches to kernel mode & executes a kernel function

l #man syscall

x86 : movl %eax, 2 ; system call numberint $0x80

ARM : swi 2 ; system call number

Page 7: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-7

Reference

l GCC Library Manualhttps://www.gnu.org/software/libc/manual/html_mono/libc.html

https://www.gnu.org/software/libc/manual/html_mono/libc.html#Function-Index

https://www.gnu.org/software/libc/manual/pdf/libc.pdf

https://en.wikipedia.org/wiki/C_standard_library

l System Call Manualhttp://man7.org/linux/man-pages/man2/syscalls.2.html

https://en.wikipedia.org/wiki/System_call

Page 8: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-8

ARM System Calll ARM System Call Example

. .text

.global _start_start:

.func mainmain:

ldr r0, =FileName @ address of file namemov r1, #102 @ create R/Wmov r7, #5 @ open & createswi 0 @ or svc 0 mov r5, r0 @ save file descriptor

ldr r1, =FileMesg @ address of file message mov r2, #12 @ length mov r7, #4 @ writeswi 0 @ or svc 0

mov r0, r5 @ restore file descriptor

mov r7, #6 @ closeswi 0 @ or svc 0

mov r7, #1 @ exitswi 0 @ or svc 0

.dataFileName: .asciz "test.txt"FileMesg: .asciz "Hello World!"

Page 9: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-9

System Call

l #man 2 open » Flags : ./include/uapi/asm-generic/fcntl.h

» C Lib. : fd = open( "test.txt", O_WRONLY ¦ O_CREAT ¦ O_TRUNC) = creat()

» #man creat

Page 10: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-10

System Call

l Arguments Passing : registershttps://w3challs.com/syscalls/?arch=arm_thumbhttps://w3challs.com/syscalls/?arch=x86_64

l Makefile #1 : at target# Makefileall: arm-syscall-examarm-syscall-exam: arm-syscall-exam.o

ld -o $@ $<arm-syscall-exam.o : arm-syscall-exam.asm

as -g -o $@ $<clean:

rm -vf arm-syscall-exam *.o test.txt

Page 11: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-11

System Call

l Makefile #2 : at host – cross compile

l #./arm-syscall-exam

# MakefileAS = arm-linux-gnueabihf-asLD = arm-linux-gnueabihf-ld

all: arm-syscall-examarm-syscall-exam: arm-syscall-exam.o

${LD} -o $@ $<arm-syscall-exam.o : arm-syscall-exam.asm

${AS} -g -o $@ $<clean:

rm -vf arm-syscall-exam *.o test.txt

Page 12: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-12

X86 System Calll System Call Handling

idt_tabledivide_error()

debug()

nmi()

….

0x0

system_call()

….

kerneluser task

main(){….fork()

}

libc.a

….fork(){….movl %eax, 2int $0x80….

}….

ENTRY(system_call) /* arch/i386/kernel/entry.S */

SAVE_ALL

….

call *SYMBOL_NAME(sys_call_table)(,%eax,4)

….

ret_from_sys_call (schedule, signal, bh_active, nested interrupt handling)

0x80

sys_call_table

sys_exit()

sys_fork()

sys_read ()

sys_write ()

….

1

2

3

4

sys_fork()

/* arch/i386/kernel/process.c */

do_fork()

/* kernel/fork.c */

Page 13: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-13

Processl Program

» Structured set of commands stored in an executable file on a file system

» Executed to create a processl Process

» Program running in memory and on the CPU (active program)

» Modern systems allow ‘multiprogramming’ (i.e., several processes exist concurrently)

» Each process requires an allocation of cpu time, in order to make forward progress

» The OS must do ‘scheduling’ of cpu time» Each process also needs the use of other system facilities

(memory, files, devices)» The terms job (batch system) and process is almost used

interchangeably

Page 14: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-14

Process

l A process includes refer to next slide» text section : program » data / bss section» stack» heap

l User process» Process begun by a user that runs on a terminal (tty)

l Daemon process» System process » Not associated with a terminal

l Process ID (PID) top, ps -el» Unique identifier assigned to every process as it begins» Processes are identified by their process identifier, an

integer

Page 15: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-15

Process in Memory Space

l Virtual Memory = User + Kernel

0xFFFFFFFF

text

data

bss

stack

kernel

0x00000000

kernel text

kernel data

kernel stack

kernel bss

User

0xBFFFFFFF

0xC0000000

kernelspace

user space

textdata

stack

textdata

stackmappings

3-GB

1-GB

physical memory virtual memory

Page 16: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-16

Process in Memory Space

physical memory virtual memory

Kernel mode

User mode

Page 17: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-17

Processl Child processes

» Refers to a process that was started by another process (parent process)

l Parent processes» Process that has started other processes (child processes)

l Parent Process ID (PPID) ps -el» The PID of the parent process that created the current

processl Processes communicate via pipes (IPC)

» Queues of bytes between two processes that are accessed by a file descriptor

» # man ipcl Remote Procedure Call (RPC)

» Remote procedure call abstracts procedure calls between processes on networked systems

» # man rpc

Page 18: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-18

Processl Process System Call

» Creation : fork system call creates new process§ the process that calls fork() is parent, the new process is child§ fork() is calling sys_fork() → do_fork() in /kernel/fork.c§ clone() : #man clone – create “thread”

» Execution : exec system call is used after a fork to execute the process

» Exit : exit terminates process and free all resources§ Calling exit() → sys_exit() → do_exit () explicit in /kernel/exit.c § The child is turned into a zombie process until the parent makes the

call to wait→ When a process finishes execution, it will have an exit status to

report to its parent process. Because of this last little bit of information, the process will remain in the operating system’s process table as a zombie process. indicating that it is not to be scheduled for further execution, but that it cannot be completely removed (and its process ID cannot be reused)

» Wait : A parent may wait for a child process to terminate, and return its termination status

Refer to Slide #31

Page 19: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-19

Processl Process termination

» Voluntary termination1. Returning to the main() function (implicit)2. Calling exit() (explicit)

» Involuntary termination1. An exception might be raised during its kernel mode execution2. The process might have received the SIGABRT or other termination

signal» Termination process state

1. the parent is alive : zombie process 2. the parent is dead : orphan process

An orphan process is a process that is still executing, they do not become zombie processes; instead, they are adopted(inherited) by init process (PID 1)

l Task» Another name for a process is a task, and Linux kernel

internally refers to processes as tasks» Generally refer to a process from the kernel’s point of view

Page 20: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-20

Task

» As seen by a process running under Linux

§ The kernel is a provider of services§ Individual processes exist

independently alongside each other and cannot affect each other directly

§ Each process’s own area of memory is protected against modification by other processes

» The internal viewpoint of a running Linux

§ Only one program (OS) is running on the computer, can access all the resource

§ The various tasks are carried out by co-routines

§ An error in the kernel programming can block the entire system

Process and Tasks

Page 21: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-21

Process Control Block (PCB)

l Information associated with each process l type sturcture task_struct ; line 593 (target)

» /usr/src/linux/include/linux/sched.hProcess stateUnique process identifierMemory-management information File system informationSignal informationProgram counterCPU registersCPU scheduling information (e.g., priority)Accounting informationI/O status informationPointers to other control blocks

Page 22: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-22

memory-map

open files

pidnext_taskprev_task

gid

processor register-values(EIP, ESP, EFLAGS, etc)

signal-handlers

timers

task-state task-priority

task-name

terminal

processexecution aspects

resourceownership aspects

Process Control Block (PCB)l task_struct

Page 23: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-23

Process Control Blockl task_struct

struct task_struct {volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */unsigned long flags; /* per process flags */mm_segment_t addr_limit; /* thread address space:

0-0xBFFFFFFF for user-thead0-0xFFFFFFFF for kernel-thread */

struct exec_domain *exec_domain;long need_resched;long counter;long priority;/* SMP and runqueue state */

struct task_struct *next_task, *prev_task;struct task_struct *next_run, *prev_run;...

/* task state *//* limits *//* file system info *//* ipc stuff *//* tss for this task */

…/* open file information *//* memory management info *//* signal handlers */

...};

mm_struct

signal_struct

task_struct

pid = 5

mm

slg

files

files_struct

Page 24: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-24

Threadl Thread of execution = Threadl The object of activity within the processl Each thread includes a unique program counter,

process stack, and set of processor registersl The kernel schedules individual threads, not

processl Linux does not differentiate between threads and

processes : a thread is just a special kind of process

l A process has two distinct aspects » Its ‘execution’ (forward progression of states)» Its ‘ownership’ (share of system’s resources) » The word ‘thread’ refers to the execution aspect of a

process (i.e., the entity which gets ‘scheduled’ by an Operating System for a share of the available cpu time)

Page 25: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-25

Thread

l Multi-threading» It is possible for an operating system to support a

concept of ‘process’ in which more than one execution-thread exists (with process-resources being shared)

l Advantages of ‘threads’» All the ‘threads’ that belong to a process can access the

same memory, the same files, the same terminal, the same timers and the same signal-handlers

» Thus the system ‘overhead’ involved in managing a multithreaded task is more efficient (less time-consuming to set up and keep track of) than if each of those threads had individualized ownerships

one process withone thread

one process withmultiple threads

Page 26: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-26

Threadl Thread communication

» Since the threads in a process share the same memory, no special mechanism is needed for them to communicate data

process

All these threads can read or write the same memory-locations

Page 27: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-27

User/Kernel Mode Transition

» 1. P1 in User Mode issues a system call

» 2. The process switches to Kernel Mode, and the system call is serviced

» 3. P1 then resumes execution in User Mode until a timer interrupt occurs

» 4. The scheduler is activated in Kernel Mode

» 5. P2 starts its execution in User Mode until a hardware device raises an interrupt

» 6. As a consequence of the interrupt, P2 switches to Kernel Mode and services the interrupt

Transitions between User and Kernel Mode

P1.aRequest system call service

P1.bWait for timerinterrupt

P2.aWait for device H/W interrupt

P2.bDevice H/Winterrupt service

Scheduler

P1.a P1.b P2.a P2.b

System call handler Interrupt handler

Page 28: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-28

Process State

l TASK_RUNNING : 1. running / 2. ready» The process is runnable

§ 1. currently running - running§ 2. on a run queue waiting to run - ready

l TASK_INTERRUPTIBLE : waiting/sleeping/blocked» Waiting on a condition: interrupts, or signals » Becomes runnable if it receives a signal

l TASK_UNINTERRUPTIBLE» Waiting process cannot be woken by a signal » Cannot become runnable if it receives a signal» This is used in situations where the process must wait

without interruptionl TASK_STOPPED : temporarily stopped

» Stopped process - e.g., by a debugger» This occurs if the process receives SIGSTOP signal

Page 29: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-29

Process State

l TASK_ZOMBIE : zombie, defunct, immortal process» Process finished, but parent has not released PID » Process has completed execution but still has an entry in the

process table. This entry is still needed to allow the parent process that started the (now zombie) process to read its exit status

» When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes. However, the process's entry in the process table remains. The parent can read the child's exit status by executing the wait system call, at which stage the zombie is removed

» The process has terminated, but its parent has not yet issued a wait system call

» wait() provides the process id of a terminated child so that the parent can tell which child terminated

Page 30: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-30

Process State

l Kernel 4.19 Main Task State

» TASK_RUNNING» TASK_INTERRUPTIBLE» TASK_UNINTERRUPTIBLE» TASK_STOPPED» TASK_ZOMBIE» TASK_DEAD : currently not used» TASK_TRACED : being traced by a debugger

/usr/src/linux/include/linux/sched.h Line # 70

Page 31: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-31

Diagram of Process State

/kernel/sched/core.c

Page 32: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-32

Processl Context Switch

» When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process

» Context-switch time is overhead; the system does no useful work while switching

» Time dependent on hardware support

Page 33: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-33

Processl Viewing Processes

» ps –f command : user process§ User identifier (UID), PID, PPID, start time, CPU utilization

» ps –l command : user process – refer to next slide§ More complete information§ Process state can be seen § R = running, S = sleeping, D = uninterruptible sleep,

T = stopped or being traced, Z = zombie» top or ps –efl command : system/user process

§ Process priority (PRI) : priority→ Higher value means lower priority→ 0 (high priority) to 139 (low priority)

§ Nice value (NI) : time slice→ Indirectly represents priority→ The priority value may be in the range -20 (800ms) to 19(5ms). → Higher value means lower priority

§ NICE_TO_PRIO(nice), PRIO_TO_NICE(prio): macro /include/linux/sched/prio.h – line 33

Page 34: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-34

Process

l Process State Codes : #man ps

Page 35: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-35

InterProcess Communication(IPC)

l Mechanism for processes to communicate and to synchronize their actions

l Message system – processes communicate with each other without resorting to shared variables

l IPC facility provides two operations:» send(message) – message size fixed or variable » receive(message)

l If P and Q wish to communicate, they need to:» Establish a communication link between them» Exchange messages via send/receive

l Implementation of communication link» Physical : shared memory, hardware bus» Logical : logical properties(protocol, layer)

Page 36: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-36

Signal

l Introduced in UNIX systems to simplify IPCl Used by the kernel to notify processes of

system eventsl A signal is a short message sent to a process, or

group of processes, containing the number identifying the signal

l Linux supports 31 non-RT signals » /usr/src/linux/include/uapi/asm-generic/signal.h

l POSIX standard defines a range of values for RT signals

Page 37: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-37

l A signal is sent due to occurrence of corresponding event (kernel/signal.c – line 1526)» send_sig_info(int sig, struct siginfo *info, struct task_struct *t);

» sig is signal number» info is either:

§ 0, if user mode process is signal sender§ 1, if kernel is signal sender

# Signal Name Default Action Comment1 SIGHUP Abort Hangup terminal or process2 SIGINT Abort Keyboard interrupt (usually Ctrl-C)

…9 SIGKILL Abort Forced process termination

10 SIGUSR1 Abort Process specific11 SIGSEGV Dump Invalid memory reference

Signal

/usr/src/linux/include/uapi/asm-generic/signal.h

Page 38: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-38

l Sending a signal» Kernel sends (delivers) a signal to a destination process

to update some state in the context of the destination process

l Kernel sends a signal for one of the following reasons:» Generated internally:

§ Kernel has detected a system event such as divide-by-zero (SIGFPE) or the termination of a child process (SIGCHLD)

» Generated externally:§ Another process has invoked the kill system call to explicitly request

the kernel to send a signal to the destination process

Signal

Page 39: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-39

l Receiving a signal» A destination process receives a signal when it is forced

by the kernel to react in some way to the delivery of the signal

l Three possible ways to react:» Ignore the signal (do nothing)» Terminate the process» Catch the signal by executing a user-level function call(a

signal handler)§ Similar to a hardware exception handler being called in response to

an asynchronous interrupt

Signal

Page 40: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-40

Schedulerl Scheduling

» Non-preemptive scheduling: the process keeps the CPUuntil the process terminates or it switches to waitingstate (simple to implement)

» Preemptive scheduling: the process can be interruptedand must release the CPU

l Scheduling Policies :/include/uapi/linux/sched.h Line # 36» SCHED_NORMAL : non-RT / classic Unix

§ Interactive : make the system appear fast to user§ RT have higher priorities than any non-RT tasks

» SCHED_FIFO : soft-RT§ If no other higher-priority RT process is runnable, the process will

continue to use the CPU as long as it wishes» SCHED_RR : soft-RT

§ Assign CPU time to all SCHED_RR processes that have the samepriority (fixed time slice)

§ Interrupted when its time slice has expired* Scheduler schedule() function : /kernel/sched/core.c- line 986

Page 41: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-41

Scheduler

l Long-term scheduler (or job scheduler) » Selects which processes should be brought into the

ready queue» Invoked very infrequently (seconds, minutes) Þ (may

be slow)l Short-term scheduler (or CPU scheduler)

» Selects which process should be executed next and allocates CPU

» Invoked very frequently (milliseconds) Þ (must be fast)

l Processes can be described as either:» I/O-bound process – spends more time doing I/O than

computations, many short CPU bursts» CPU-bound process – spends more time doing

computations, few very long CPU burstsLinux implicitly favors I/O-bound processes over CPU-bound ones

Page 42: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-42

Modulel Linux is a monolithic kernel

» Trivial modifications require kernel to be recompiled» Kernel is increasing in size by adding new features» Many modules occupy permanent space in memory

though they are used rarelyl Module: steps toward micro-kernelized Linux

» Small and compact kernel» Clean kernel» Rapid kernel» Components-based Linux

l Easier way to extend Linux kernel functionsl Modules allow to plug a new functionality into the

kernel at runtime» A module is loaded only when it is needed» It is unloaded when it is not needed anymore

Page 43: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-43

Module

l LKM (Linux Kernel Module) = Kernel Module = Module

l Linux Kernel = Base Kernel (Kernel Image) + LKMl Kernel does not need to be rebuilt so often

» Reduce the likeliness of errorsl Bugs in code do not affect the kernel boot

» Easier debugging: you know where to look» Faster debugging: no need to reboot

l Modules can be compiled and dynamically linked into kernel address space» Useful for device drivers that need not always be resident

until needed.§ Keeps core kernel “footprint” small

Page 44: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-44

Module versus Application

l Modules» 1. runs in kernel space

(no main function)» 2. just registers itself in

order to serve future requests, and its initialization function, then terminates immediately(event-driven program)

» 3. no libraries to link (linked only to the kernel): the only functions it can call are the ones exported by the kernel

printk() : kernel function

l Applications» 1. runs in user space

(main function)» 2. performs a single task

from beginning to end(not all applications are event-

driven) » 3. can call functions it

doesn’t define: the linking stage resolves external references using the appropriate library of functions (libc)

printf()

Page 45: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-45

Module

l Every module consist of two basic functions (minimum)int init_module(void) /*used for all initialization*/ { ... } void cleanup_module(void) /*used for exit */ { ... }

l Loading a module - by issuing the following command: » insmod module.o

l Removing a module - by issuing the following command: » rmmod module

Page 46: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-46

Module

» Basic two Interfaces§ module_init()§ module_exit()

» Function units which can be implemented as modules§ File system : register_filesystem(), unregister_filesystem(),

read_super function (mount), put_super function (unmount)§ Block device drivers : register_blkdev(), unregister_blkdev(), open

function, release function § Character device drivers : register_chrdev(), unregister_chrdev(),

open function, release function§ Network device drivers : register_netdev(), unregister_netdev(), open

function, close function

kernel

module

init_module()

module_init()

cleanup_module()

module_exit()

register

system call

unregister

system call

insmod

rmmod

Page 47: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-47

Module

l Instruction» insmod : current inserted module lists(/proc/modules)

§ Insert an module into the kernel» lsmod = /proc/modules (refer to next slide)

§ List currently loaded modules» rmmod

§ Remove an module from the kernel» depmod : /lib/modules/x.x.xx/modules.dep

§ Determine interdependencies between modules» modprobe : /etc/modules.conf (module config for alias & options)

§ Insert module and resolve module dependency (as described by modules.dep). For example, if you must load A before loading B, modprobe will automatically load A when you tell it to load B

» modinfo : refer to next slide § Display module information (.modinfo section in module object file)

Page 48: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-48

Module

l # modinfo hello.ko » MODULE_AUTHOR(“MELEE”);» MODULE_DESCRIPTION(“Hello Module”);» MODULE_LICENCE(“GPL”);

l /proc/modules » Holds information about the loaded modules» column : module name / size / use count / dependency

Page 49: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-49

Modulel kernel 4.19 : target

#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/delay.h>#include <asm/io.h>

/* define GPIO_BASE for BCM2837*/#define GPIO_BASE 0x3F200000#define BLOCK_SIZE 4096

volatile unsigned int * gpio_addr;

#define GPIO_LED 26 // 26

/* main function */static void main_func(void){

int i;

for(i=0; i<5 ; i++){/* 1C h : 28 dec = 4 * 7 */ *(gpio_addr + 7)|= 1 <<(GPIO_LED); //GPSET0 : 3F20 001C h mdelay(300);

/* 28 h : 40 dec = 4 * 10 */*(gpio_addr + 10)|= 1 <<(GPIO_LED); //GPCLR0 : 3F20 0028 h mdelay(300);}

}

Page 50: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-50

Modulel kernel 4.19 : target

static int hello_init(void){

printk("* insmod : Hello,GPIO LED Module Test Program!\n");

gpio_addr = ioremap(GPIO_BASE,BLOCK_SIZE);

/* GPFSEL2 = gpio_addr + 2 */ // *(gpio_addr+ 2) &= ~(7<<(((GPIO_LED)%10)*3)); // bit 20~18 = 000 for input

*(gpio_addr+ 2) |= (1<<(((GPIO_LED)%10)*3)); // bit 20~18 = 001 for output

main_func();

return 0;}

static void hello_exit(void){

iounmap(gpio_addr);printk("* rmmod : Hello, GPIO LED Module Test Program! Good bye!\n");

}module_init(hello_init);module_exit(hello_exit);

MODULE_LICENSE("GPL");MODULE_AUTHOR("Embedded System Labs.");MODULE_DESCRIPTION("GPIO LED Module Test Program");MODULE_VERSION("0.1");

Page 51: Embedded Systems - KOREATECHmicrocom.koreatech.ac.kr/lectures/IFC415/ES09-rpi.pdf · 2019. 11. 29. · Embedded Systems 1-1 KUT Embedded Systems Linux Structure, System Call, Process,

Embedded Systems KUT1-51

Modulel Make module : sample modules (microcom homepage)l Load the module

» insmod hello-module.ko» dmesg

l Check that the module is loaded» lsmod = cat /proc/modules

l Remove the module» rmmod hello-module» dmesg