Process Manager Ext2fs Proc Devfs Process Management Memory Manager Scheduler Minix Nfs Msdos Memory Management Signaling
Total Page:16
File Type:pdf, Size:1020Kb
Embedded Systems Prof. Myung-Eui Lee (A-405) [email protected] Embedded Systems 1-1 KUT Linux Structure l Linux Structure proc1 proc2 proc3 proc4 proc5 procn User Space System Call Interface Filesystem Manager Process Manager Ext2fs proc devfs Process Management Memory Manager Scheduler minix nfs msdos Memory Management Signaling Buffer Cache Kernel Space Device Manager Network Manager char block Ipv6 ethernet Console KBD SCSI CD-ROM PCI network ….. Device Interface dev1 dev2 dev3 dev4 devn Embedded Systems 1-2 KUT Linux Structure l Linux Structure Applications: Graphics UI, Compilers, Media player UI Text UI = Shell: sh, csh, ksh, bash, tcsh, zsh Compiler libraries (libc.a) API System Shared Libraries (System Call Interface) Memory File Process management management management Kernel Device Drives BIOS Computer Hardware Embedded Systems 1-3 KUT System Call Interface Standard C Lib. System Call C program invoking printf() library call, API – System call – OS Relationship which calls write() system call ar –t /usr/lib/libc.a | grep printf /usr/src/linux-2.4/arch/i386/kernel/entry.S Embedded Systems 1-4 KUT System Call l System calls define the programmer interface to Linux system » Interface between user-level processes and hardware devices u CPU, memory, disks etc. » Make programming easier u Let kernel take care of hardware-specific issues » Increase system security u Let kernel check requested service via system call » Provide portability u Maintain interface but change functional implementation l Roughly five categories of system calls in Linux » Process control » File management * 288 system calls » Device management refer to slide #6 » Information maintenance » Communication Embedded Systems 1-5 KUT System Call l Invoked by executing int or swi instruction » CPU switches to kernel mode & executes a kernel function x86 : movl %eax, 2 ; system call number int $0x80 ARM : swi 2 ; system call number l Linux files relating to system call » /usr/src/linux-2.4/arch/i386/kernel/entry.S x86 host #408 /pxa270/kernel/linux-2.6…/arch/arm/kernel/calls.S ARM target #17 u System call and low-level fault handling routines u ENTRY(sys_call_table) » /usr/src/linux-2.4/include/asm-i386/unistd.h x86 host /pxa270/kernel/linux-2.6…/include/asm-arm/unistd.h ARM target u System call numbers and macros Embedded Systems 1-6 KUT ARM System Call l ARM System Call Example An example to write a short file on disk SWI_Exit EQU 0x01 SWI_Write EQU 0x04 SWI_Open EQU 0x05 SWI_Close EQU 0x06 write_only EQU 4 ; mode 4 = open to write ENTRY ; mark first instruction to execute start ADR r0, filename ; r0 points to string MOV r1, #write_only ; file attribute for write only SWI SWI_Open ; open a file for writing MOV r5, r0 ; save file-handle in r5 ADR r1, string ; point to a string MOV r2, #14 ; 14 characters long SWI SWI_Write ; write to file MOV r0, r5 ; restore file-handle SWI SWI_Close ; close the file SWI SWI_Exit filename = "test.txt",0 string = "Hello World!",&0a,&0d END Embedded Systems 1-7 KUT System Call l System Call Handling kernel user task ENTRY(system_call) /* arch/i386/kernel/entry.S */ main() SAVE_ALL { . …. … fork() idt_table call *SYMBOL_NAME(sys_call_table)(,%eax,2) } 0x0 divide_error() …. debug() ret_from_sys_call (schedule, signal, bh_active, nmi() nested interrupt handling) libc.a . sys_call_table … …. fork() { …. 1 sys_exit() movl %eax, 2 0x80 int $0x80 system_call() 2 sys_fork() sys_fork() …. …. sys_read () } 3 /* arch/i386/kernel/process.c */ . … 4 sys_write () /* kernel/fork.c */ …. Embedded Systems 1-8 KUT Process l Program » Structured set of commands stored in an executable file on a file system » Executed to create a process l 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 Embedded Systems 1-9 KUT Process l A process includes Process in memory » text section : program » stack » data section » 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 Embedded Systems 1-10 KUT Process l 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 process l Processes communicate via pipes (IPC) » Queues of bytes between two processes that are accessed by a file descriptor Embedded Systems 1-11 KUT Process l Process System Call » Creation : fork system call creates new process u the process that calls fork() is parent, the new process is child u fork() is calling sys_fork() → do_fork() in /kernel/fork.c u “cloning flags” : man clone /usr/src/linux-2.4/include/linux/sched.h #39 » Execution : exec system call is used after a fork to execute the process » Exit : exit terminates process and free all resources u Calling exit() → sys_exit() → do_exit () explicit kernel/exit.c u The child is turned into a zombie process until the parent makes the call to wait Refer to → When a process finishes execution, it will have an exit status to Slide #26 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 Embedded Systems 1-12 KUT Process l Process termination » Voluntary termination 1. Returning to the main() function (implicit) 2. Calling exit() (explicit) » Involuntary termination 1. An exception might be raised during its kernel mode execution 2. 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 Embedded Systems 1-13 KUT Task » As seen by a process running under Linux Process and Tasks u The kernel is a provider of services u Individual processes exist independently alongside each other and cannot affect each other directly u Each process’s own area of memory is protected against modification by other processes » The internal viewpoint of a running Linux u Only one program (OS) is running on the computer, can access all the resource u The various tasks are carried out by co-routines u An error in the kernel programming can block the entire system Embedded Systems 1-14 KUT Process Control Block (PCB) l Information associated with each process l type sturcture task_struct ; line 382 (target:528) » /usr/src/linux-2.4/include/linux/sched.h Process state Unique process identifier Memory-management information File system information Signal information Program counter CPU registers CPU scheduling information (e.g., priority) Accounting information I/O status information Pointers to other control blocks Embedded Systems 1-15 KUT Process Control Block (PCB) l task_struct prev_task next_task pid gid task-name process task-state task-priority execution aspects processor register-values (EIP, ESP, EFLAGS, etc) memory-map open files resource ownership terminal aspects timers signal-handlers Embedded Systems 1-16 KUT Process Control Block l 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-thead 0-0xFFFFFFFF for kernel-thread */ mm_struct struct exec_domain *exec_domain; task_struct long need_resched; pid = 5 long counter; long priority; mm /* SMP and runqueue state */ slg signal_struct struct task_struct *next_task, *prev_task; struct task_struct *next_run, *prev_run; files ... /* task state */ /* limits */ files_struct /* file system info */ /* ipc stuff */ /* tss for this task */ … /* open file information */ /* memory management info */ /* signal handlers */ ... }; Embedded Systems 1-17 KUT Thread l Thread of execution = Thread l The object of activity within the process l Each thread includes a unique program counter, process stack, and set of processor registers l The kernel schedules individual threads, not process l 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) Embedded Systems 1-18 KUT Thread l Multi-threading » It is possible