Download Linux - Operating System - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
1
Linux: Ch. 5.6.3; Ch 21.1-21.
2
Overview
- Linux system
- Kernel, system, distribution
- Characteristics & goals
- Kernel modules
- Ch 21.4: Process management
- Ch 21.5: CPU Scheduling, SMP, Interrupt
handling
- Ch. 21.9: Interprocess Communication
- Ch 21.6: Memory management
- Ch 21.7: File Systems
- Ch 21.8: Input and Output
4
Components of Linux
Figure 21.
General Characteristics & Goals
- Multi-user, UNIX system
- Kernel
- Monolithic-- single address space containing all kernel functionality (not a microkernel)
- Modules
- Multi-tasking
- Preemptive: Processes can be preempted while running in kernel mode
- Goals
- Speed, efficiency
- E.g., install on a system with relatively small amount of RAM & disk
- Compatibility & standardization (e.g., POSIX)
- “Even when the same system calls are present on two different UNIX systems, they do not necessarily behave in exactly the same way” (p.
- Docsity.com
Explicit & Implicit
Module Loading/Unloading
- Explicit
- System can be configured so particular drivers are loaded upon system startup
- Implicit
- Can be loaded on demand and unloaded when not in use
- E.g., A CD-ROM driver might be loaded when a CD is mounted, and unloaded from memory when CD dismounted from file system
- Module support under Linux
- Module management : Allows modules to be loaded into memory and to talk to rest of kernel - Module is dynamic linked into running kernel; kernel has a dynamic symbol table to allow module access to kernel symbols
- Driver registration : Allows modules to tell rest of kernel that a new driver has become available
- Conflict-resolution mechanism : Allows different device drivers to reserve hardware resources
8
Process Management
- Kernel support for both heavy-weight processes
and threads
- PCB (process descriptor; p. 82, Linux Kernel Primer)
- Process identity
- PID, credentials (user & group ID), personality (emulation libraries)
- Process environment
- Command line arguments; shell variables
- Process context (p. 750 text & p. 82, Linux Kernel Primer)
- Scheduling context (e.g., registers), including kernel stack
- Virtual memory context (region descriptions, page table)
- Open file table
- File system context
- Signal handler table
- Resource limits
- Same PCB structure for all process types
- Thread shares some of data structures of parent
- Each PCB is just a series of pointers into kernel tables
10
Process Management - 2
- System calls
- Linux doesn’t distinguish between processes and
threads
- Uses the general term task
- When a clone is invoked, it is passed a set of flags
that determine how much sharing is to take place
between parent and child tasks (see Table, p. 750)
- Fork is just a special case of clone in which none
of the process data is shared
11
CPU Scheduling (Ch. 5.6.3, 21.5)
- Discussion is partly from 2.6.8.1 kernel CPU
Scheduler (Aas, 2005)
- Preemptive, priority-based algorithm
- Multiple CPU process scheduling algorithms
- Time-sharing (“nice” scheduling)
- Real-time (RT)
- Absolute priorities are more important than fairness SCHED_FIFO, SCHED_RR
Linux Real-time Scheduling
• SCHED_FIFO, SCHED_RR
- Soft real-time scheduling
- Priority-based, not deadline-based
- Each process has a priority & a scheduling class
- Scheduling classes:
- FCFS (first-come first-served)
- RR (round-robin)
- Always runs highest priority process
- Among equal priority processes, runs one that has been waiting the longest
- “FCFS” or “RR”
- “The … difference between FCFS and round-robin scheduling is that FCFS processes continue to run until they either exit or block, whereas a round-robin process will be preempted after [a time slice] and will be moved to the end of the scheduling queue” (p. 753, text)
- SCHED_FIFO (FCFS) processes do not have timeslices
14
Time-Sharing Scheduler
• SCHED_NORMAL
- Tasks initially given a time quanta according to priority
- Runnable tasks
- Those that have time remaining in their quanta
- When a task exhausts its time quanta it has expired ; otherwise it is active - Expired tasks are not scheduled again until all other tasks have exhausted their individual time quanta’s
- Two arrays of tasks in the runqueue
- Expired & active tasks
- Each have linked lists within priority number
- When active array empty, swap with expired
- Tasks time quanta’s replenished
Ch 21.9 IPC: Interprocess Communication
- Semaphore
- Can be used between heavy weight processes
- Pipe
- Communication channel from parent to child
- Sockets
- Shared memory
- Between light weight processes (in same HWP)
- Between heavy weight processes
- Message queues
- Signals
- Asynchronous events (not data); used to inform a process that an event has occurred
- Sent by one user process to another or from kernel to a user process (e.g., to inform when a child dies)
17
Signals
- Some signals can be handled (caught) by the
process
- Handling takes the form of registering a signal handler
- Signal handlers are functions that get called when the signal occurs
- Signal handling can be used to make a program
more robust to certain conditions
- E.g., if you want to make sure to do some processing before terminating a process, handling a signal generated by a ^C is a good idea
- Some signals cannot (e.g., SIGKILL, SIGSTOP)
be handled by the process
19
Some Signal System Calls
int sigaction(int signum, const struct sigaction *act,
struct sigaction *oldact);
// Examine and change a signal action
sighandler_t signal(int signum, sighandler_t handler);
// install a new signal handler (deprecated)
int pause(void); // wait for a signal int kill(pid_t pid, int sig); // send a signal unsigned int alarm(unsigned int seconds);
// arranges for a SIGALRM signal to be delivered
20
Signal Example:
Receiving Process
/* signal.c starts */ #include <signal.h> #include <stdio.h> #include <sys/types.h> #include <unistd.h> void handleSIGINT(int sig) { printf("received SIGINT (sig= %d)\n", sig); fflush(stdout); } void handleSIGQUIT(int sig) { printf("received SIGQUIT (sig= %d)\n", sig); fflush(stdout); } void handleSIGUSR1(int sig) { printf("received SIGUSR1 (sig= %d)\n", sig); fflush(stdout); } void handleSIGTSTP(int sig) { printf("received SIGTSTP (sig= %d)\n", sig); fflush(stdout); } void handleSIGFPE(int sig) { printf("received SIGFPE (sig= %d)\n", sig); fflush(stdout); }
int main() { printf("pid of handler process is: %d \n", getpid()); signal(SIGINT, handleSIGINT); signal(SIGQUIT, handleSIGQUIT); signal(SIGTSTP, handleSIGTSTP); signal(SIGUSR1, handleSIGUSR1); signal(SIGFPE, handleSIGFPE); for (;;) { printf("Waiting for signals... \n"); printf ("Enter 't' to test FPE exception; 'e' to exit \n"); int c = getchar(); getchar(); // consume newline if (c == 'e') break; // No FPE is caused here float x = 100.0; float y = x/0.0; printf("y= %f \n", y); // FPE i s caused h ere-- also causes a n infinite l oop int i=1/0; } }