ECE/CS 250 – Summer 2020 – Prof. Bletsch Recitation #1 – Unix
CS 390 Unix Programming Environment
-
Upload
charlton-alberto -
Category
Documents
-
view
18 -
download
2
description
Transcript of CS 390 Unix Programming Environment
CS390 - Unix Programming Environment
CS 390 Unix Programming Environment
Topics to be covered: Memory Management
Process Scheduling
CS390 - Unix Programming Environment
Memory Management Memory is an important resource that has to be
managed with the help of memory manager Memory Manager is a component of the OS
responsible for managing memory• Allocate and deallocate processes to and from the
memory• Allocate memory to processes that are currently
being used• Deallocate memory from processes that have ended
Unlike PC’s, UNIX OS use very sophisticated memory management algorithms to make efficient use of memory resources
CS390 - Unix Programming Environment
Types of Memory Main Memory
• The physical RAM, also called Real memory• processes can only be run when both the program
code, and program data resides in main memory
Cache Memory• The cache is a small amount of high-speed memory,
usually with a memory cycle time comparable to the time required by the CPU to fetch one instruction.
• The cache is usually filled from main memory when instructions or data are fetched into the CPU.
Swap space• Disk memory used to hold data that is not in Real or
File System
CS390 - Unix Programming Environment
Virtual Memory (VM) Used to enlarge the available address
space beyond the physical address space Total Memory = Virtual Memory + Main
Memory It is the responsibility of the Memory
manager to keep track of processes, whether they are in Main or Virtual Memory
All current running processes must be in the Main Memory
CS390 - Unix Programming Environment
Virtual Memory Contd. Most modern computers have special hardware
called a memory management unit (MMU). This unit sits between the CPU and the memory
unit If CPU wants to access memory (whether it is to
load an instruction or load or store data), it sends the desired memory address to the MMU, which translates it to another address
The address generated by the CPU is called a virtual address, and the address it gets translated to by the MMU is called a physical address.
CS390 - Unix Programming Environment
Memory Manager Issues Often the number of processes that an OS needs
to manage have memory requirements that exceed the amount of available memory
Move processes to and from disk to the main memory
Disk access is much slower than main memory Needs efficient algorithms to effectively manage
all system processes Common techniques
• Swapping• Demand Paging
CS390 - Unix Programming Environment
Swapping When the number of processes have memory
requirements that exceed the amount of main memory, some processes need to be moved to the disk
Swapping systems require that the entire process has to be in main memory in order to run the process
Consists of three parts• Managing space in the swap device (secondary memory)• Swapping process out of main memory• Swapping process back into the main memory
CS390 - Unix Programming Environment
Demand Paging Virtual address space is divided into equally
sized units called pages• Pages typically range from 1K to 8k
Now the entire process need not to be in the main memory to execute
The act of moving back and forth the pages from the Main memory to the virtual memory is called Paging
If a process accesses a page and that page is not in Main memory a page fault occurs
• Move the page from the virtual memory to the main memory
CS390 - Unix Programming Environment
Processes A task in operation is called as a Process A process is created by fork system call at
the lowest level One can check what processes are
running using the ps command We also know how to terminate a process
using the kill command Lets now see how processes can be
scheduled
CS390 - Unix Programming Environment
Process scheduling As a time sharing system, the UNIX
system kernel directly controls how processes are scheduled
The Kernel suspends a process when its quantum time expires
If a process wants to get the attention of the kernel, it uses Interrupts
CS390 - Unix Programming Environment
Interrupts When an Interrupt occurs, the current
process is suspended and the kernel examines the cause and services the interrupt
After the interrupt has been handled, the suspended process either resumes execution or preempted
Pre-emption is a process of taking away the control
CS390 - Unix Programming Environment
Process state Diagram
Zombie
Preempted
Ready to runAsleepin memory
Created using fork
Kernel Running
Sleep Swapped Ready to run, swapped
User RunningSystem call,interrupt
return
Preempt
Swap in, Swap out
Not enough memory
Enough memory
sleep
Swap out
Wakeup
Interrupt,Interrupt return
Return to user
Wakeup
exit
Rescheduleprocess
CS390 - Unix Programming Environment
Interrupt contd…Current Process Suspend resume
timeTerminal interrupt handler
Process interrupt
completedTerminal interrupt
CS390 - Unix Programming Environment
Process Scheduling contd… A user can specify when a process
should run using user-level commands
• at command• batch command• nice command• sleep command• wait command• nohup command
CS390 - Unix Programming Environment
at command One can specify when the commands
should run using the at command$ at 0500 Jan 18echo “Don’t forget the meeting” | mail youCNTL-D The above command will mail you a reminder
at 5:00 am on Jan 18 about the meeting -f option: run a sequence of commands
contained in a file$ at –f scriptfile 6 am Monday This will run scriptfile at 6 A.M. on Monday
CS390 - Unix Programming Environment
at command contd… You can see the listing of all at jobs
you have scheduled, using the –l option, which returns ID number and scheduled time for each of your at jobs
$ at –l To remove a job from the queue
use –r option
CS390 - Unix Programming Environment
batch command The batch command lets the user
defer the execution, but does not give control as to when it is supposed to run
$ batch <<!
cat mybook | lp
CS390 - Unix Programming Environment
Process Priorities
The kernel assigns the CPU to a process for a time slice
When the time of a process is elapsed it is placed in one of the several priority queues.
All the processes have some priority associated with them
CS390 - Unix Programming Environment
The nice command The nice command allows a user to
execute a command with a lower-than-normal priority
Priority = priority index + nice value
You can decrease the priority of a command by using nice. It will run slower and takes less CPU time.
Advantage: you are being nice to your neighbors$ nice –19 profit