state

Document Sample
state Powered By Docstoc
					         CS 454

Process State Transitions and
         System Calls
Why use getpid() instead of
 currpid?
• Portability of Code: It is not always possible to
  access global variables, such as currpid, from
  any process in some operating systems.


What is wrong with kill()?
• It does not follow the C calling convention. The
  return address is not right above the
  arguments in the stack; the process ID is. The
  arguments are therefore not at the correct
  offset with respect to the base pointer. It will
  try to take the PID as argument 0.
Process State Transitions
         WAKE U P                                          SLEEP
                            SLEEPING




                SEN D                             RE CEIV E
                           RE CEIV ING




                SIGNAL                            WAIT
                             WAITING

                               RE SCHE D
       RE ADY                                               CU RRE NT
                               RE SCHE D

           SU SPEND
                                                     SU SPEND
                          SU SPEND ED
       RE SUM E



                             CR EATE

                  Tr ansfor mati ons for the ' waiting ' state
 Process State Transitions
Suspend (suspend.c)
• check pid
• check for current or ready
• if ready then
  – dequeue the process from the
    ready queue
  – put it in suspended state
 Process State Transitions
Suspend (suspend.c)
• if current then
  – put it in suspended state
  – call resched(), which calls ctxsw()
NOTE:
• system calls: suspend, resume, kill,
  resched
• utility functions: getprio, getpid,
  chprio
          System Calls
• The precautions, that system calls
  take to verify that an operation is
  illegal, make the general purpose
  subroutines that can be invoked by
  any process at any time.
• System calls stand between a naive
  user program and the rest of the
  operating system, which must
  protect the internal system from
          System Calls
• System calls define the exterior of
  the OS by providing an interface
  through which the user accesses all
  system services.
• Difference between library and
  system calls
  – When a program calls a
    subroutine, the code is always
    part of the final object program,
    even if it was linked in from a
          System Calls
• Difference between library and
  system calls
  – With a system call, the major part
    of the code excecuted is actually
    part of the kernel itself and not the
    calling program.
• In other word, the calling program is
  making direct use of the facilities
  provided by the kernel.
Chapter 6: Process Coordination

• Look at Semaphores and their functions
  –   wait(sem)
  –   signal(sem)
  –   screate(count)
  –   newsem()
  –   sdelete(sem)
  –   sreset(sem, count)
When are semaphores used?
• For the collaboration and
  coordination of processes
  – When processes used shared
    resources
    • i.e. the producer and consumer type
      of process
  – Protects the shared resource
 What other ways can we
protect shared resources?
• Disable Interrupts
  – Not Good
    • Miss interrupts from I/O devices or
      other things that don’t effect the
      current process
    • Performance degradation
    • Not all processes may need to access
      the shared resource
      XINU Semaphores
• In XINU, each semaphore has 4
  fields
  – struct sentry {
          state /* used or free */
          semcount
          sqhead
          sqtail
    }
    Semaphore Invariant
• A non-negative semaphore count
  means that the queue is empty. A
  semaphore count of negative n
  means that the queue contains n
  waiting processes
How many semaphores are
       needed?
• For a producer consumer problem
  with one buffer
• Similar to ex5.c: page 13-14 of text
  – 2 for producer/consumer
  – What are they initialized to
    • if both 0 : neither can run, both wait
    • if both 1 : numbers may be produced
      twice
  • What if change which is initialized to
    1 and which is initialized to 0?
     – if consumed = 0
          produced = 1
          then ex5.c will print 0 - 1999
     – if consumed = 1
          produced = 0
          then ex5.c will print 1 - 2000
– if you just want mutual exclusion
  to the shared resource, just need 1
  semaphore (see ex6.c on pages
  14-15 of the text)