Skip to content

Latest commit

 

History

History
130 lines (114 loc) · 4.13 KB

W2_process.md

File metadata and controls

130 lines (114 loc) · 4.13 KB

CS 2 Process

Element of a process

  • Threads: abstract the CPU
  • address space: memory
  • files: disks
  • sockets: network

Multiprogramming

  • The Process supports the ability to run multiple programs simultaneously on a single CPU
  • CPS only runs one program at a time: switches from process to process quickly

benefits:

  • resource utilization (CPU, memory, I/O) can be improved
  • allow processes to use the CPU when it would otherwise become idle
  • enables the concurrent execution of a programs on a single processors
  • higher job throughout
  • lower response times when running multiple interactive programs

Process creation

four main events that cause processes to be created:

  1. system initialization
  2. execution of a process creation system call by a running process
  3. a user request to create
  4. initiation of a batch job
    • batch job is not interactive, java scheduler that runs without user order

Process creation: example

  • System call in UNIX: fork()
    • create a clone of the calling process
    • different address spaces: two process always have different spaces
    • same memory image
    • same values for the PC and general-purpose registers, same open file handlers
      • recall that pointer-counter stores the address of the next instruction
int main()
{
    fork();
    printf("Hello world!\n");
    return 0;
}

this will result two "Hello world!", as the pointer-counter stores the same instruction, 'printf'

int main()
{
    if(fork() == 0)
        printf("child!\n");
    else 
        printf("parent!\n");
    return 0;
}

the next instruction is actually compare return value with 0

exec()

fork() is usually followed by an invocation to a function in the exec() family

  • execve() cause the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and data segments

Process termination

typical conditions:

  1. Normal exit (voluntary)
  2. Error exit (voluntary)
    • error with the program
  3. Fatal error (involuntary)
    • the operating system kills the program (by sending a signal to the process), maybe caused by stackoverflow...
  4. Killed by another process (involuntary)

Process States

mey be in:

  1. Running: using CPU
  2. Ready: runnable, but temporarily stopped
  3. Blocked: unable to run until some external event happens

Process Control Block

OS maintains information about a process in a data structure PCB

  • process table: list of PCBs (one PCB for each process)
    • Process ID, process state, parent process, ...

Threads

  • provides an execution context to a process
    • execution context: program counter, stack pointer, ...
    • sequential execution of a set of instructions
    • has a PC, SP, registers, stack
  • to run, lod execution context to CPU's registers
    • PC, SP, general purpose registers with intermediate values for computations
  • when not running, the context of a thread is stored in memory, and the processor's registers hold the context for some other thread
    • PC: instruction of some other thread
    • SP: stack of... ![[multi_thread_mamory.png]]

environment

  • threads execute the environment defined by a process
  • a process has one or more threads

address space

  • all threads of a process share the same address space
  • code, data and heap memory are shared between threads
  • each thread has its own stack

Why threads?

  • parallelism
    • has potential to use multiple processors
  • enable overlap of I/O with other actives
    • avoid blocking progress while waiting for I/O operations

Summary

  • Threads
    • share an address space
      • sharing data easy nad fast
      • do not provide isolation, one thread bring the entire process down
    • created faster
    • incur in less overhead when switching between threads
  • Processes
    • enable applications to be paralleled
    • provide isolated execution environments
    • incur in interprocess communication overhead

Pthreads

a POSIX standard API for thread creation and synchronisation

  • functions start with pthread
  • include pthread.h
  • threads have an id of type pthread_t