COS 318 - Operating System: Assignment 4 Inter-Process Communication and Process Management
COS 318 - Operating System: Assignment 4 Inter-Process Communication and Process Management
Assignment 4
Inter-Process Communication and
Process management
Fall 2004
Main tasks
Inter-Process Communication
– Implement Mailboxes
– Keyboard Input
Minimizing interrupt disabling
Process Management
– Be able to load a program from disk
– Extra credit options
Mailbox - Bounded Buffer
Multiple producers
– Put data into the buffer
Multiple consumers
– Remove data from the
buffer
Buffer Blocking operations
– Has fixed size – Sender blocks if not
– Is a FIFO enough space
– Variable size – Receiver blocks if no
message message
Mailbox - Implementation
Buffer management
– Circular buffer: head and tail pointers
Bounded buffer problem
– Use locks and condition variables to solve
this problem as shown in class
– 2 condition variables: moreData and
moreSpace
– See mbox.h and mbox.c
Keyboard - Overview
How the keyboard interacts with OS
– An hardware interrupt (IRQ1) is generated
when a key is pressed or released
– Interrupt handler talks to the hardware and
gets the scan code back.
– If it is SHIFT/CTRL/ALT, some internal
states are changed.
– Otherwise the handler converts the scan
code into an ASCII character depending on
the states of SHIFT/CTRL/ALT.
Keyboard - Overview
How the keyboard interacts with OS
– An hardware interrupt (IRQ1) is generated
when a key is pressed or released
– init_idt() in kernel.c sets handler to
irq1_entry in entry.S
– irq1_entry calls keyboard_interrupt in
keyboard.c
Keyboard - Overview
keyboard_handler talks to the hardware
and gets the scan code back.
• key = inb(0x60);
• Call key specific handler
Keyboard - Overview
If it is SHIFT/CTRL/ALT, some internal
states are changed.
Otherwise normal_handler converts the
scan code into an ASCII character.
normal_handler calls putchar() to add
character to keyboard buffer
You need to implement putchar()
Also getchar() which is called by the
shell
Keyboard - Implementation
It’s a bounded buffer problem
– So, use mailbox.
But, there are some variations
– Single producer (IRQ1 handler)
– Multiple consumers (more than one
processes could use keyboard)
– Producer can't block - discard character if
buffer is full.
Keyboard - Subtle points
Producer shouldn’t be blocked
– Solution: check and send message only if
mailbox is not full, otherwise discard it.
– Make use of mbox_stat() function
Is that all ?
– What if a process being interrupted by
IRQ1 is currently calling getchar()?
– Address how to fix this issue in design
review
Reducing interrupt disabling
Disable interrupt only when necessary.
Motivation
– Otherwise, could lose hardware events
• For instance, keyboard or timer events
Where to reduce
– Very little we can do with scheduler.c
• Switching stacks, manipulating ready queue
– Thread.c
• Locks, condition variables
Reducing interrupt disabling
Alternative to interrupt disabling
– Use spinlock to guarantee atomicity
spinlock_acquire( int *l) { while ( !TAS(l)); }
spinlock_release( int *l) { *l = 0; }
see thread.c
One spinlock per lock/condition variable
typedef struct {
int spinlock;
struct pcb *waiting;
int status;
} lock;
see thread.h
Using spinlock - An example
Code from project 3 Using spinlock
void lock_acquire (lock_t *l){ void lock_acquire(lock_t *l) {
CRITICAL_SECTION_BEGIN; use spinlocks to achieve same thing
if (l->status == UNLOCKED) { (part of design review)
l->status = LOCKED; }
} else {
block(&l->waiting);
NOTE: block now takes any extra
} argument - spinlock
CRITICAL_SECTION_END; – the spinlock is released in block()
}
Process Management
So far, we only handle processes booted
along with the OS.
To support dynamic loading, we must have
the followings:
– Separate address space for each process
– A simple file system format describing how
processes reside on a disk
– A memory manager
Read shell.c to find out the commands it
supports
Separate Address Space
Each process has its own CS and DS
segment selector, and the program
always starts at address 0x1000000
(16MB mark).
Basic paging only -- no paging to disk
yet.
This is done for you
Paging in x86
MEM_START
Process 1 code/data process’s
physical
available_mem Process 1 user stack space
Memory pool
MEM_END
user access
MEM_START
Process 1 code/data process’s
physical
available_mem Process 1 user stack space
Memory pool
MEM_END
user access
MEM_START
Process 1 code/data process’s
identity-mapped physical
available_mem Process 1 user stack
memory with space
Memory pool
kernel privilege MEM_END
access
MEM_START
Process 1 code/data process’s
physical
available_mem Process 1 user stack space
Memory pool
MEM_END
Kernel code/data
is allocated in STACK_MIN kernel’s
0xB8000
Kernel stack is Video mem
allocated in MEM_START
Process 1 code/data
kernel’s available_mem Process 1 user stack
process’s
space