CS 390 Unix Programming Environment

19
CS390 - Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Memory Management Process Scheduling

description

CS 390 Unix Programming Environment. Topics to be covered: Memory Management Process Scheduling. 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 - PowerPoint PPT Presentation

Transcript of CS 390 Unix Programming Environment

Page 1: CS 390 Unix Programming Environment

CS390 - Unix Programming Environment

CS 390 Unix Programming Environment

Topics to be covered: Memory Management

Process Scheduling

Page 2: CS 390 Unix Programming Environment

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

Page 3: CS 390 Unix Programming Environment

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

Page 4: CS 390 Unix Programming Environment

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

Page 5: CS 390 Unix Programming Environment

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.

Page 6: CS 390 Unix Programming Environment

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

Page 7: CS 390 Unix Programming Environment

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

Page 8: CS 390 Unix Programming Environment

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

Page 9: CS 390 Unix Programming Environment

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

Page 10: CS 390 Unix Programming Environment

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

Page 11: CS 390 Unix Programming Environment

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

Page 12: CS 390 Unix Programming Environment

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

Page 13: CS 390 Unix Programming Environment

CS390 - Unix Programming Environment

Interrupt contd…Current Process Suspend resume

timeTerminal interrupt handler

Process interrupt

completedTerminal interrupt

Page 14: CS 390 Unix Programming Environment

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

Page 15: CS 390 Unix Programming Environment

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

Page 16: CS 390 Unix Programming Environment

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

Page 17: CS 390 Unix Programming Environment

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

Page 18: CS 390 Unix Programming Environment

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

Page 19: CS 390 Unix Programming Environment

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