CPS 356 & 444/544 Lecture notes: Processes

(identification, creation, & termination)



Coverage: [UPE] §7.4 (pp. 220-225), and [USP] Chapter 2 (pp. 21-26, 51-53) and §§3.1-3.3 (pp. 59-70)


Overview

  • processes (state, creation, termination)
  • (logical) layout of the program image
  • memory allocation/deallocation (malloc family of memory allocation functions)


Process

  • a process is a program in execution
  • executable module of program resides in a program image in main memory
  • each process id (do a ps in UNIX) and a process state (ready, running, blocked, and so on)


Logical layout of program image



(modified version of [USP] Fig. 2.1, p. 24; image courtesy Robbins & Robbins' [USP] Chapter
2 webpage)


Activation record



(modified version of [USP] Fig. 1.1, p. 16)

A stack of these activation records at run-time is called the execution stack


Process control block

For the duration of its existence, each process is represented by a process control block (PCB) which contains information such as:

  • state of the process (ready, running, blocked)
  • register values
  • priorities and scheduling information
  • memory management information
  • accounting information such as open files
  • allocated resources

Specifics of a PCB depend on the system and techniques used.



(ref. [OSIDP] Fig. 3.1 on p. 110; image courtesy [OSIDP] webpage)


Process life cycle

A process moves through various states during its life:

  • new,
  • ready,
  • running,
  • blocked, and
  • done states, and
  • transitions between them

(regenerated from [USP] Fig. 3.1, p. 62)

Many processes may be ready or waiting at the same time, but only one can be running at a time in a uniprocessor system.

Process and process lifecycle nomenclature


System calls cause context switches



(ref. [OSIDP] Fig. 1.5 on p. 16; image courtesy [OSIDP] webpage)


Process identification

  • output of ps command provides many of these details, including process state; experiment with the -a, -A, -l, and -o options
  • getuid, getgid, geteuid, getegid
  • process id, parent process id
  • getpid and getppid
  • real vs. effective user and group ids
  • cast results of these functions to long
  • top and htop (F2 turns threads on; different colors)


Process creation

  • an initial OS process begins running at boot time, which spawns (creates) other processes
  • a process hierarchy (tree) evolves with parent-child relationships


fork

  • system call used for process creation in UNIX
  • caller becomes parent and newly created process is child
  • processes are the same except for the process id
  • child inherits many attributes of its parent (e.g., environment, privileges, open files and devices)
  • returns 0 to the child and the child pid to the parent
  • execution proceeds after the call to fork in each process image
  • parent code and child code
  • effect of concurrency
  • sleep function, takes seconds to block as an int (e.g., sleep(30))


Graphical depiction of fork

(ref. [ATT] 6-11)




Process termination

  • returned to OS (in shell variable $? in UNIX system)
  • why 0 for success?
    • does not seem to follow C boolean convention
    • many ways to fail; see bottom of manpage for meanings of particular exit status
  • echo is the UNIX print command
  • for instance, echo hello world, echo $?
  • exit vs. return
    • in main they are the same
    • outside of main
      • exit can be used to exit the program (from any function)
      • return will transfer control to the caller
  • absence of return or exit in main has the same effect as exit(0)
  • to install an exit handler use atexit
  • how can a process terminate abnormally? external event (e.g., a <ctrl-c> (an asynchronous event)) or an internal error (e.g., illegal memory access (a synchronous event))


NULL pointer

  • it is illegal to read or write to address 0
  • a null pointer
    • any pointer with value 0
    • returned by pointer-returning functions to signify failure
  • NULL is defined in stdio.h as
    • #define NULL 0, or
    • #define NULL (void*) 0 in the new ANSI standard
  • not to be confused with the null character \0


Memory allocation/deallocation

malloc, calloc, realloc, and free
  • a general-purpose memory allocation package
  • prototyped in stdlib.h
  • malloc family allocates storage from the heap
  • malloc returns a pointer to a block of at least size bytes suitably aligned for any use
  • malloc returns a pointer of type void*; cast to appropriate type
  • to declare and array of 10 ints: int* int_ptr = malloc (sizeof(*int_ptr)*10);
  • PCB* process_str_ptr = malloc (sizeof(*process_str_ptr));
  • the argument to free is a pointer to a block previously allocated by malloc
  • after free is executed, this space is made available for further allocation by the application, though not returned to the system; memory is returned to the system only upon termination of the process (there is more to the story than this)
  • when passed NULL, recalloc acts like malloc
  • when passed a non-NULL pointer and a size of 0, recalloc acts like free


References

    [C] C Language for Experienced Programmers, Version 2.0.0, AT&T, 1988.
    [CPL] B.W. Kernighan and D.M. Ritchie. The C Programming Language. Prentice Hall, Upper Saddle River, NJ, Second edition, 1988.
    [OSIDP] W. Stallings. Operating Systems: Internals and Design Principles. Prentice Hall, Upper Saddle River, NJ, Sixth edition, 2009.
    [UPE] B.W. Kernighan and R. Pike. The UNIX Programming Environment. Prentice Hall, Upper Saddle River, NJ, Second edition, 1984.
    [USP] K.A. Robbins and S. Robbins. UNIX Systems Programming: Concurrency, Communication, and Threads. Prentice Hall, Upper Saddle River, NJ, Second edition, 2003

Return Home