Solaris User Contexts (and some other stuff) - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Solaris User Contexts (and some other stuff)

Description:

A user context (ucontext) defines the context of a thread of ... In our system, each of these dots represents a timer tick. Our program receives a SIGALRM. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 18
Provided by: robert450
Learn more at: http://www.cis.upenn.edu
Category:
Tags: contexts | solaris | stuff | tick | user

less

Transcript and Presenter's Notes

Title: Solaris User Contexts (and some other stuff)


1
Solaris User Contexts(and some other stuff)
  • Cse 380
  • Tuesday, october 10th
  • Robert spier

2
User Contexts
  • A user context (ucontext) defines the context of
    a thread of control within an executing process.
  • These are not the same as threads.
  • Threads are built on top of ucontexts.

3
Saving State
  • The idea of a context is to save the state of the
    machine/program/process in such a way that it can
    be restored later.

4
Saved State
  • Signal Mask
  • Stack
  • Machine Context
  • (link)

5
Signal Mask
  • The signal mask is stored to allow different
    components to be interrupted by different
    signals.
  • Only one is live at a time.
  • Automatic Restoration

6
Stack
  • Imagine the confusion if multiple processes
    were using the same stack.
  • Saving the stack (and having an independent one
    for each ucontext) allows for function calls from
    the start function of a context.

7
Machine State (Context)
  • CPU Stores State in Registers
  • Integer Registers
  • Floating Point
  • mcontext_t stores them.

8
Using Contexts
  • getcontext( uc ) / initialize with current
    state of machine /
  • stack (ulong) malloc(SS) / create new stack
    /
  • / configure fields /
  • / stacks work backwards on SPARC /
  • uc-gtuc_stack.ss_sp (void )(stack SS - 32)
  • uc-gtuc_stack.ss_size SS
  • uc-gtuc_stack.ss_flags 0
  • sigemptyset( uc-gtuc_sigmask ) /configure
    sigmask/
  • makecontext( uc, function, 1 ) / set initial
    function, and pass arguments /

9
Passing Arguments
  • void f_add (int x, int y) output (xy)
  • makecontext( uc, f_add, 3, 7, 20 )

Result? Output -gt 27
10
Functions
  • int getcontext(ucontext_t ucp)
  • initializes the structure pointed to by ucp to
    the current user context of the calling process
  • int setcontext(ucontext_t ucp)
  • restores the user context pointed to by ucp.
    (does not return)

11
Functions
  • void makecontext(ucontext_t ucp, void(func)(),
    int argc, ...)
  • when this context (ucp) is resumed (swap or set)
    execution continues by calling the function func,
    passing it the arguments that follow argc.
  • swapcontext(ucontext_t oucp, ucontext_t ucp)
  • saves the current context in the context
    structure pointed to by oucp and sets the
    context to the context structure pointed to by
    ucp.

12
Signals
  • include ltsignal.hgt
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • void signal_handler( int s)
  • printf("I just received signal d\n",s)
  • void main()
  • sigset(SIGUSR1, signal_handler)
  • sigset(SIGALRM, signal_handler)
  • printf("My pid is d\n", getpid())
  • while(1) pause()

13
Signals (2)
kill -USR1 4437 kill -USR1 4437 kill
-USR1 4437 kill -ALRM 4437
  • ./a.out
  • My pid is 4437
  • I just received signal 16
  • I just received signal 16
  • I just received signal 16
  • I just received signal 14

(You dont need to implement generic signals for
the project.)
14
Context Switching
Round Robin
Time
Contexts
C1
C1
C2
C2
C3
C3
15
Context Demo (detail)
Quantas
C1
Scheduler
C2
Signal Context
C3
Time
16
Timing the Switch
  • In our system, each of these dots represents a
    timer tick.
  • Our program receives a SIGALRM.
  • We use an Interval Alarm (itimer) for this.
  • Itimers are created with setitimer(2)
  • The scheduler is called from the signal handler
    routine.

17
References
  • ucontext(5)
  • Portable Multithreading The Signal Stack Trick
    For User Space Thread Creation, Ralf S.
    Engelschall
  • Modern Operating Systems, Andrew S. Tannenbaum
Write a Comment
User Comments (0)
About PowerShow.com