Course Chapters

Shared Memory Computing

Understanding Basics of Shared Memory Computing with OpenMP

Processes

  • Independent execution units with separate memory spaces
  • Each process has its own resources and address space
  • Higher overhead for creation and context switching
  • Better isolation, security, and stability
#include <iostream>
#include <thread>
#include <unistd.h>

// Process example
int main() {
    pid_t pid = fork();
    
    if (pid == 0) {
        // Child process
        std::cout << "Child process with PID: " << getpid() << std::endl;
    } else {
        // Parent process
        std::cout << "Parent process with PID: " << getpid() << std::endl;
    }
    return 0;
}

Threads

  • Lightweight execution units within a single process
  • Share memory space, resources, and state
  • Lower overhead for creation and context switching
  • Efficient communication, but requires synchronization for safe shared access
#include <iostream>
#include <thread>

void threadFunction(int id) {
    std::cout << "Thread " << id << " running" << std::endl;
}

int main() {
    std::thread t1(threadFunction, 1);
    std::thread t2(threadFunction, 2);
    
    t1.join();
    t2.join();
    return 0;
}

Note: This example does not include synchronization. Without synchronization, threads may interleave outputs.