Advance Innovation Centre
  • AIC Knowledge @ EEC for All
  • 😎Logical Thinking
    • Karel Robot
    • Code to Flowchart
    • Play with Docker
    • CNX Software
  • MCU & Interfacing with Infineon PSOCâ„Ē
    • Basic MCU Interfacing
      • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
      • Development Environment Preparation
      • PSoCâ„Ē 6S2 Peripherals Interfacing (GPIO)
        • Hello World and LED Blinking
        • GPIO Principles
        • PSoCâ„Ē 6S2 GPIO-HAL LED Blink Lab
        • PSoCâ„Ē 6S2 GPIO-PDL LED Blink Lab
        • Button "Bounce" Principles
          • Push/Pull Button to Turn ON/OFF LED via HAL
          • Push/Pull Button to Turn ON/OFF LED via PDL
          • GPIO Button Interrupt via HAL
          • GPIO Button Interrupt via PDL
        • GPIO variables & functions
      • PSoCâ„Ē 6S2 Peripherals Interfacing (ADC, PWM)
        • PSoCâ„Ē 6S2 SAR ADC
          • ADC Principles
          • PSoCâ„Ē 6S2 with ADC Labs
            • Reading potentiometer sensor value via an ADC HAL
            • Reading potentiometer sensor value via an ADC PDL
        • PSoCâ„Ē 6S2 PWM & TCPWM
          • PWM Principles
          • PSoCâ„Ē 6S2 for PMW Function Labs
            • LED Brightness using PWM via HAL
            • LED Brightness using PWM via PDL
    • Sensor Interfacing and HMI
      • OLED Display
        • OLED Display Principles
        • Calling BDH’s OLED functions
        • Display ADC via Potentiometer on OLED
      • BDH Shell
        • Shell Principles
        • LED Blinking and CAPSENSE via BDH Shell
        • Adding "History" command
        • Adding "Reboot" command
        • CAPSENSE Button and Slider
          • CAPSENSE Button and Slider with Capsense Tuner
          • CAPSENSE Button and Slider using FreeRTOS
    • Serial Communication & Visualization
      • UART, I2C, SPI Communication via Infineon PSoCâ„Ē6
      • BMX160 Sensor Communication via Infineon PSoCâ„Ē6
        • Reading ADC via HAL with Potentiometer and Displaying GUI on Serial Studio
        • Reading XENSIV-DPS-3XX Pressure Sensor and Displaying GUI on Serial Studio
        • Motion Sensors GUI Integration via Serial Studio
    • IoT Connectivity & Data Analytics via Node-Red
      • Node-Red Installation
      • Setting MQTTS to MQTT Broker
      • Sending PSoC6’s sensor to MQTT (node-red)
    • Edge AI on PSoCâ„Ē
      • Machine Learning on PSoCâ„Ē6 via Edge-Impulse
    • Infineon PSoCâ„Ē Troubleshooting
  • IoT Development with Infineon PSOCâ„Ē & BDH Platform
    • PSoCâ„Ē IoT Development Kit
      • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
      • Development Environment Preparation
        • Hello World and LED Blinking
    • IoT Connectivity
      • Node-Red Installation
      • Controlling PSoCâ„Ē LED using MQTT
      • Setting MQTTS to MQTT Broker
      • Sending PSoC6’s sensor to MQTT (node-red)
    • BDH IoT Connectivity
    • WireLinXâ„Ē IoT PLC
    • BDH X-Brain Data Analytics
      • PSoC6 Data Collection to CSV log file
    • Data Visualization
      • āļŠāļĢāđ‰āļēāļ‡ Dashboard āļ”āđ‰āļ§āļĒ Looker Studio
  • ðŸ–ĨïļOperation Systems
    • Prerequisites
      • Guideline from Ubuntu
        • Ubuntu and VSCode on WSL2
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡ WSL 2
      • Run Ubuntu on VirtualBox7
    • Zero to Linux Hero
      • Computer OS Architecture
      • Anatomy of Linux System
        • UNIX/Linux History
        • UNIX/Linux Evolution
        • GNU Project
        • Linux OS Architecture
        • Command Line Interface (CLI)
          • Basic Commands
          • 😎Level up your Linux Shell
          • File & Dir. Commands
          • Searching Commands
          • 😎ChatGPT-based Terminal
          • SysAdmin Commands
          • Network Commands
          • Hacker Commands
        • Busybox
        • Shell Script
          • Awk Script
          • Bash Shell Script
            • Bash Snippets
            • Bash Useful Examples
      • Anatomy of Linux Kernel
        • Linux Kernel Principles
        • Linux Environment for Developer
      • Anatomy of Embedded Linux
        • Embedded Linux
        • Host & Target
        • Cross Toolchains
        • Bootloader
        • Building Embedded Linux
    • Linux OS Dev. Engineer
      • Process Management
        • Process Basic
        • Process State
        • Basic Process Mgmt. Commands
        • Advance Process Mgmt. Commands
        • Process API Programming
      • IPC
        • IPC Anatomy
        • Signal Programming
        • Pipe Programming
        • FIFO Programming
        • Msg. Queue Programming
          • System V
        • Share Memory Programming
          • System V
        • Socket Programming
      • POSIX Threads
        • Multi-tasking Basic
        • POSIX Thread Anatomy
        • Threading Programming
      • Applied IPC
        • Remote Commander
        • Multi-Remote Commanders
      • Process Synchronization
        • Mutex Programming
        • Semaphore Programming
      • Applied IPC with Semaphore
  • ⌚Embedded Systems Development
    • Introduction to ESD
      • Why's ESD?
      • What it use for?
      • How it works?
    • Enbedded System Development via PSoC6
      • Basic MCU Interfacing
        • Introduction to CY8CKIT-062S2-43012 Pioneer Kit
        • Development Environment Preparation
        • PSoCâ„Ē 6S2 Peripherals Interfacing (GPIO)
          • Hello World and LED Blinking
          • GPIO Principles
          • PSoCâ„Ē 6S2 GPIO-HAL LED Blink Lab
          • PSoCâ„Ē 6S2 GPIO-PDL LED Blink Lab
          • Button "Bounce" Principles
            • Push/Pull Button to Turn ON/OFF LED via HAL
            • Push/Pull Button to Turn ON/OFF LED via PDL
            • GPIO Button Interrupt via HAL
            • GPIO Button Interrupt via PDL
          • GPIO variables & functions
        • PSoCâ„Ē 6S2 Peripherals Interfacing (ADC, PWM)
          • PSoCâ„Ē 6S2 SAR ADC
            • ADC Principles
            • PSoCâ„Ē 6S2 with ADC Labs
              • Reading potentiometer sensor value via an ADC HAL
              • Reading potentiometer sensor value via an ADC PDL
          • PSoCâ„Ē 6S2 PWM & TCPWM
            • PWM Principles
            • PSoCâ„Ē 6S2 for PMW Function Labs
              • LED Brightness using PWM via HAL
              • LED Brightness using PWM via PDL
      • Sensor Interfacing and HMI
        • OLED Display
          • OLED Display Principles
          • Calling BDH’s OLED functions
          • Display ADC via Potentiometer on OLED
        • BDH Shell
          • Shell Principles
          • LED Blinking and CAPSENSE via BDH Shell
          • Adding "History" command
          • Adding "Reboot" command
          • CAPSENSE Button and Slider
            • CAPSENSE Button and Slider with Capsense Tuner
            • CAPSENSE Button and Slider using FreeRTOS
      • Serial Communication & Visualization
        • UART, I2C, SPI Communication via Infineon PSoCâ„Ē6
        • BMX160 Sensor Communication via Infineon PSoCâ„Ē6
          • Reading ADC via HAL with Potentiometer and Displaying GUI on Serial Studio
          • Reading XENSIV-DPS-3XX Pressure Sensor and Displaying GUI on Serial Studio
          • Motion Sensors GUI Integration via Serial Studio
    • Edge Computing and IoT Connectivity
    • Cloud-Based Data Analytics and Digital Twin
    • Edge Vision AI
    • Resources
      • Basic Hardware and Firmware
        • Environment Preparation
          • āļāļēāļĢāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄ Arduino IDE
            • āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™āđƒāļŠāđ‰āļ‡āļēāļ™ Arduino IDE
          • āļāļēāļĢāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄāļŠāļģāļŦāļĢāļąāļšāđƒāļŠāđ‰āļ‡āļēāļ™āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļ§āļąāļ” NI MyDAQ
            • āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļ•āļąāđ‰āļ‡āļ„āđˆāļēāđƒāļŠāđ‰ Digital Multimeter -NI ELVISmx
            • āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļāļēāļĢāļ•āļąāđ‰āļ‡āļ„āđˆāļēāđƒāļŠāđ‰ Oscilloscope-NI ELVISmx
          • āļ•āļīāļ”āļ•āļąāđ‰āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄ KingstVIS
        • Basic measurement
          • Basic Digital and Analog I/O
            • LAB: Basic Digital Input/Output
            • LAB: Basic Analog Input/Output
          • Waveform
            • LAB: Oscilloscope
            • LAB: Oscilloscope and Function Generator
            • LAB: Pulse Width Modulation (PWM)
              • Homework
        • Interfacing and Communication
          • LAB: UART, RS485, RS232 Protocol
          • LAB: I2C Protocol
            • HOMEWORK
          • LAB: SPI Protocol
      • IoT Connectivity
        • Example: IoT with MQTT on Node-red
        • Data logger
        • LAB: Data Visualization
  • 🛠ïļC/C++ for Embedded Programming
    • Development Environment Preparation
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡ WSL 2
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡ Ubuntu environment
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄ Visual Studio Code
      • āļāļēāļĢāđ€āļŠāļ·āđˆāļ­āļĄāļ•āđˆāļ­ Virtual studio code āđ€āļ‚āđ‰āļēāļāļąāļš WSL
      • āļ•āļīāļ”āļ•āļąāđ‰āļ‡ docker on WSL
    • Principle C/C++ Programming
      • Get started with C++
      • Makefile
        • Makefile Examples
      • Compiling and running
        • How to create a program that you can enter inputs.
          • Lab 1 Exercise
      • Arguments
        • Command line arguments in C and C++
      • signed and unsigned data types
      • Variable and Operator
      • If and If else
      • Loop, Infinite loop, and flag
        • Loop and Flag exercise
      • Array
        • Get to know with arrays
        • Implement example
      • Vector
    • Object Oriented Programming (OOP) in C++
      • Class and Object
      • Encapsulation and Abstraction
      • Polymorphism and Inheritance
    • C/C++ Preprocessing
      • Macro
        • Quiz Macro
      • File Inclusion
      • Conditional Compilation
      • Pragma directive
        • Quiz Pragma
    • String in C++
      • Concatenation
      • Split
    • Type conversions for C/C++
      • Conversion using Cast operator
    • Error handling
    • Data logger
      • āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āđ„āļŸāļĨāđŒāđāļĨāļ°āđ€āļ‚āļĩāļĒāļ™āđ„āļŸāļĨāđŒ
      • āļāļēāļĢāļ­āđˆāļēāļ™āđ„āļŸāļĨāđŒ
      • āļāļēāļĢāđ€āļāđ‡āļšāļ‚āđ‰āļ­āļĄāļđāļĨāļāļąāļšTime stamp
    • High performance programing
      • Multi-task and Multi-thread
        • Multi-threading example
      • Mutex
      • Queue
      • OpenCV
    • C/C++ Techniques
      • Makefile in action
      • Object Oriented Programming (OOP) in C++
        • Class and Object
        • Encapsulation and Abstraction
        • Polymorphism and Inheritance
      • C/C++ Preprocessing
        • Macro
          • Quiz Macro
        • File Inclusion
        • Conditional Compilation
        • Pragma directive
          • Quiz Pragma
      • Binary, Octal and Hexadecimal Numbers
      • Array and properties of an array
        • Get to know with arrays
        • Implement example
      • What's next?
  • ðŸĪ–Artificial Intelligence (AI)
    • VAMStack Design House, BUU
    • Data Analytics
      • Data cleansing
      • Data analytics
      • Data analytic exercise
    • Machine Learning
      • Neural Network Layers
      • Machine learning type
      • Dataset
      • Using Edge Impulse for AI Model
    • Basic Image Processing
      • Computer Vision using Python Language
        • Installation
        • Computer Vision Basics
          • Pixel and Color
          • Draw image
          • Basic Image processing
          • Morphology Transformations
          • Gaussian blur
          • Simple Thresholding
          • Contour
          • Canny edge detection
        • Case Study
          • Coin counting
          • Color detection & tracking
        • VAM_CV SDK
  • ⚙ïļFPGA Design and Development
    • Verilog HDL via Vivado IDE
      • LAB1: Setting Environment and Create Project
        • Create Vivado Project
      • LAB2: Hardware Description Language Work Flow
        • Simulation code
      • LAB3: Design HDL Project
        • Top Level Design
        • Top-level Simulation
      • LAB4: Asynchronous VS Synchronous Circuit
        • Simulation Synchronous counter
    • C/C++ Programming on Ultra96v2 FPGA Board
      • Application C/C++ on Ultra96v2 Part 1
        • Design Overview
        • Step 1 - Burn the image to SD card
        • Step 2 - Bring up Ultra96v2
        • Step 3 - Installing the Vitis-AI runtime packages
      • Application C/C++ on Ultra96v2 Part 2
        • STEP 1 : Setting auto boot Wifi
        • STEP 2 : How to working on Embedded
        • STEP 3 : How to run the test code
  • ðŸĪ–Robotics
    • Dobot Magician
      • Instruction of Dobot
      • Software Download
      • Basically of Program
        • Teaching and Playback
        • Write and Draw
        • LaserEngraving
        • 3D Printer
    • Robotino
      • Software Download
        • Robotino View
        • Robotino SIM
      • Charging
      • Connecting
      • Follow Line example
        • Basic block in Follow Line
    • RaspBlock
      • Get Started with Raspblock
  • ðŸšĐSpecial Topics
    • Node-Red
      • Set up Raspberry Pi
      • Install node red in Raspberry Pi
      • Get started with Node Red
        • Open node-red
        • Turn off node red
        • Install Dashboard on Node-red
        • Use node red to show message
        • Using Ultrasonic sensor with node-red
    • IoT Cloud
      • Overview
        • How do they work?
          • Basic Knowlege
      • Installations
        • Install Docker
        • Install Mosquitto Broker
        • Install InfluxDB
        • Install Telegraf
        • Install Grafana
      • Get Sensor Value and Send to MQTT
        • Connect ESP3266 to sensor
        • Connect ESP3266 to MQTT
      • Integration
    • Senses IoT
      • SENSES IoT Platform
      • LAB8: MCU send data to IoT platform
    • CrowPi Dev Kit
      • Raspberry Pi with CrowPi
      • Remote to Raspberry Pi
      • Cross-Compile
        • Lab 1: Programming and cross complier
      • Hardware and Interfaces Usage CLI
        • LAB: Usage GPIO via CLI
        • LAB: Scan I2C bus via CLI
      • Python library for Crow Pi
      • wiringPi library (C) for CrowPi
        • Lab2: Crowpi and sensors
    • LVGL Development
      • LVGL - Light and Versatile Embedded Graphics Library
        • Setting program for LVGL Simulator
        • Get started with LVGL simulator
        • Example Library of LVGL
        • Create your own screen
          • Exercise
        • Style
          • Exercise
        • Event
    • Docker OS
      • Docker OS Part 1
        • Part 1 : Installation
        • Part 2 : Basic Docker OS and Linux CLI
      • Docker OS Part 2
        • Part 1 : Docker communication
        • Part 2 : Docker compose
      • Application Gstreamer on devcontainer
        • STEP 1 : Setting gstreamer environment
        • STEP 2 : Create the Gstreamer element on template
        • STEP 3 : Testing and application on your gst element
  • ðŸĪŸRecommended by AIC
    • Skill Roadmap
      • Embedded Engineer
      • Developer
    • Hardware Programming
    • Embedded Programming
    • General-propose Programming
    • Algorithmica
    • Thai Expert Knowledge
    • RT-Thread University Program
      • Infineon PSoC6
      • Kernel
        • Kernel Basics
        • Thread Management
        • Clock Management
        • Inter-thread synchronization
        • Inter-thread communication
        • Memory Management
        • Interrupt Management
        • Kernel porting
        • Atomic Operations
        • RT-Thread SMP
        • Kernel API Changelog
      • Tools
      • Devices & Drivers
        • SENSOR Devices
        • Touch Equipment
        • CRYPTO Devices
        • AUDIO Devices
        • Pulse Encoder Devices
      • Components
        • C Library (libc)
        • ISO/ANSI C Standard
        • POSIX Standard
          • FILE (File IO)
          • Pthread
          • Timer
          • IPC Semaphore
          • IPC Message Queues
          • Dynamic Modules
        • Network Components
          • FinSH Console
          • FAL: Flash Abstraction Layer
          • Virtual File System
          • tmpfs: temporary file system
          • ulog log
          • utest testing framework
          • Power Management
          • RT-Link
        • Software Packages
          • Internet of Things
            • MQTT-umqtt
            • Telnet
          • Tools
            • SystemView
            • SEGGER_RTT
          • LVGL Manual
            • Touch Screen Driver
      • Demo
        • Infineon Gateway
        • Handwriting Recognition (MNIST)
        • Object Detection (Darknet)
        • ROS using RT-Thread
        • Control the car using RT-Thread
        • LiDAR via RT-Thread
        • Detection via RT-Thread and ROS
        • Sensor Driver Development Guide
Powered by GitBook

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University

On this page
  • POSIX threads
  • āđ€āļ›āļĢāļĩāļĒāļšāđ€āļ—āļĩāļĒāļšāļ›āļĢāļ°āļŠāļīāļ—āļ˜āļīāļ āļēāļžāļĢāļ°āļŦāļ§āđˆāļēāļ‡ fork() āđāļĨāļ° pthread_create()

Was this helpful?

  1. Operation Systems
  2. Linux OS Dev. Engineer
  3. POSIX Threads

POSIX Thread Anatomy

PreviousMulti-tasking BasicNextThreading Programming

Last updated 1 year ago

Was this helpful?

āļ āļēāļĒāđƒāļ™āļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢāļĨāļĩāļ™āļļāļāļ‹āđŒāļ™āļąāđ‰āļ™āļˆāļ°āļĢāļ­āļ‡āļĢāļąāļšāļāļēāļĢāļ—āļģāļ‡āļēāļ™āđāļšāļšāļŦāļĨāļēāļĒāļ‡āļēāļ™āļžāļĢāđ‰āļ­āļĄāļāļąāļ™ (multitasking support) āđ‚āļ”āļĒāļ•āļąāļ§āđ‚āļ›āļĢ-āđ€āļ‹āļŠāđ€āļ­āļ‡āļˆāļ°āļŠāļēāļĄāļēāļĢāļ–āļŠāļĢāđ‰āļēāļ‡āļ‡āļēāļ™āļĒāđˆāļ­āļĒāđ†āđ„āļ”āđ‰āđ€āļĢāļĩāļĒāļāļŠāđˆāļ§āļ™āļ™āļĩāđ‰āļ§āđˆāļē āđ€āļ—āļĢāļ” (thread) āļŦāļĢāļ·āļ­āļ­āļĩāļāļŠāļ·āđˆāļ­āļŦāļ™āļķāđˆāļ‡āļ§āđˆāļē “lightweight processes” āļ‚āđ‰āļ­āļ”āļĩāļ‚āļ­āļ‡āļāļēāļĢāđƒāļŠāđ‰ thread āļ„āļ·āļ­āļˆāļ°āļŠāļēāļĄāļēāļĢāļ–āļ—āļģāđƒāļŦāđ‰āļ™āļąāļāļžāļąāļ’āļ™āļēāđ‚āļ›āļĢāđāļāļĢāļĄāļ—āļģāļāļēāļĢāļˆāļąāļ”āļāļēāļĢāļāļąāļšāđ€āļŦāļ•āļļāļāļēāļĢāļ“āđŒāļ—āļĩāđˆāđ€āļāļīāļ”āļ‚āļķāđ‰āļ™āđƒāļ™āđ€āļ§āļĨāļēāļ—āļĩāđˆāđ„āļĄāđˆāđāļ™āđˆāļ™āļ­āļ™ (asynchronous events) āđ„āļ”āđ‰āļ­āļĒāđˆāļēāļ‡āļ—āļąāļ™āļ—āļĩāđāļĨāļ°āļĄāļĩāļ›āļĢāļ°āļŠāļīāļ—āļ˜āļīāļ āļēāļž āļ™āļ­āļāļˆāļēāļāļ™āļąāđ‰āļ™āļŠāļēāļĄāļēāļĢāļ–āđƒāļŠāđ‰āđƒāļ™āļāļēāļĢāļ›āļĢāļ°āļĄāļ§āļĨāđƒāļ™āļĨāļąāļāļĐāļ“āļ°āļ‚āļ™āļēāļ™ (parallel computing) āļšāļ™āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļ—āļĩāđˆāļĄāļĩāļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāđāļšāļšāļŦāļĨāļēāļĒāļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨ (multi-core CPU) āļ—āļĩāđˆāđƒāļŠāđ‰āļŦāļ™āđˆāļ§āļĒāļ„āļ§āļēāļĄāļˆāļģāļĢāđˆāļ§āļĄāļāļąāļ™āđ„āļ”āđ‰āļ”āļĩ (shared-memory multiprocessor)

āđ‚āļ”āļĒāļ—āļąāđˆāļ§āđ„āļ›āđāļĨāđ‰āļ§āđ‚āļ›āļĢāđ€āļ‹āļŠāļ āļēāļĒāđƒāļ™āļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢāļĨāļĩāļ™āļļāļāļ‹āđŒāļˆāļ°āļ›āļĢāļ°āļāļ­āļšāļ”āđ‰āļ§āļĒāļŠāļ–āļēāļ™āļ°āļ‚āļ­āļ‡āļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨ (āđ€āļŠāđˆāļ™āļ„āđˆāļēāļ āļēāļĒāđƒāļ™āļ•āļąāļ§āļĢāļĩāļˆāļīāļŠāđ€āļ•āļ­āļĢāđŒ AX,BX,DX) āļĢāļēāļĒāļĨāļ°āđ€āļ­āļĩāļĒāļ”āļ‚āļ­āļ‡āļāļēāļĢāļˆāļ­āļ‡āļŦāļ™āđˆāļ§āļĒāļ„āļ§āļēāļĄāļˆāļģ (āļŠāļģāļŦāļĢāļąāļšāđ€āļāđ‡āļš code, globals, heap āđāļĨāļ° stack) āđāļĨāļ°āļĢāļēāļĒāļĨāļ°āđ€āļ­āļĩāļĒāļ”āļ—āļĩāđˆāđ€āļāļĩāđˆāļĒāļ§āļ‚āđ‰āļ­āļ‡āļāļąāļšāļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢ (āđ€āļŠāđˆāļ™ āļāļēāļĢāđ€āļ›āļīāļ”āđ„āļŸāļĨāđŒ, āļŦāļĄāļēāļĒāđ€āļĨāļ‚āđ‚āļ›āļĢāđ€āļ‹āļŠ) āļ‹āļķāđˆāļ‡āđ€āļ—āļĢāļ”āļāđ‡āļˆāļ°āļ„āļĨāđ‰āļēāļĒāļāļąāļ™ āđāļ•āđˆāļˆāļ°āļ•āđˆāļēāļ‡āļāļąāļ™āļ•āļĢāļ‡āļ—āļĩāđˆāđ‚āļ›āļĢāđ€āļ‹āļŠāđāļ•āđˆāļĨāļ°āļ•āļąāļ§āļˆāļ°āđāļĒāļāļāļēāļĢāđ€āļāđ‡āļšāļŠāļ–āļēāļ™āļ°āļāļąāļ™āļ­āļĒāđˆāļēāļ‡āļŠāļąāļ”āđ€āļˆāļ™ āđƒāļ™āļ‚āļ“āļ°āļ—āļĩāđˆāđāļ•āđˆāļĨāļ°āđ€āļ—āļĢāļ”āļˆāļ°āļĄāļĩāļāļēāļĢāđƒāļŠāđ‰ code, globals āđāļĨāļ° heap āļĢāđˆāļ§āļĄāļāļąāļ™

āļ‹āļķāđˆāļ‡āđ€āļ—āļĢāļ”āļˆāļ°āļŠāļēāļĄāļēāļĢāļ–āļ—āļģāļ‡āļēāļ™āđ„āļ”āđ‰āļ”āļĩāđƒāļ™āļāļĢāļ“āļĩāļ—āļĩāđˆāđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļ„āļ­āļĄāļžāļīāļ§āđ€āļ•āļ­āļĢāđŒāļ™āļąāđ‰āļ™āļĄāļĩāļĄāļēāļāļāļ§āđˆāļēāļŦāļ™āļķāđˆāļ‡āļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨ āđ‚āļ”āļĒāđƒāļ™āļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡āđāļŠāļ”āļ‡āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āđ€āļ—āļĢāļ”āđāļ•āđˆāļĨāļ°āļ•āļąāļ§ (āđ„āļ”āđ‰āđāļāđˆ main(), function1() āđāļĨāļ° function2()) āļ‹āļķāđˆāļ‡āļ–āđ‰āļēāđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļĩāļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāđ€āļžāļĩāļĒāļ‡āļ•āļąāļ§āđ€āļ”āļĩāļĒāļ§āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āđ‚āļ›āļĢāđāļāļĢāļĄāļ™āļĩāđ‰āļāđ‡āļˆāļ°āļ—āļģāļ•āļēāļĄāļĨāļģāļ”āļąāļšāļ‚āļ­āļ‡āļ„āļģāļŠāļąāđˆāļ‡āđāļ•āđˆāļĨāļ°āļšāļĢāļĢāļ—āļąāļ” (program counter) āļ‹āļķāđˆāļ‡āļ­āļēāļˆāļˆāļ°āđƒāļŠāđ‰āđ€āļ§āļĨāļēāļ—āļąāđ‰āļ‡āļŠāļīāđ‰āļ™ 2 āļ™āļēāļ—āļĩ āđāļ•āđˆāļ–āđ‰āļēāđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļĩāļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāļˆāļģāļ™āļ§āļ™ 3 āļ•āļąāļ§āļ—āļąāđ‰āļ‡āļŠāļēāļĄāļŠāđˆāļ§āļ™āļāđ‡āļˆāļ°āđāļĒāļāļāļąāļ™āļ—āļģāļ‡āļēāļ™āđ„āļ›āđāļ•āđˆāļĨāļ°āļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāļ‹āļķāđˆāļ‡āļ­āļēāļˆāļˆāļ°āđƒāļŠāđ‰āđ€āļ§āļĨāļēāļ—āļąāđ‰āļ‡āļŠāļīāđ‰āļ™āđ€āļžāļĩāļĒāļ‡ 50 āļ§āļīāļ™āļēāļ—āļĩāđ€āļ—āđˆāļēāļ™āļąāđ‰āļ™

āļ•āļąāļ§āđ€āļ—āļĢāļ”āđāļ•āđˆāļĨāļ°āļ•āļąāļ§āļ āļēāļĒāđƒāļ™āđ‚āļ›āļĢāđ€āļ‹āļŠāļˆāļ°āđƒāļŠāđ‰āļ—āļĢāļąāļžāļĒāļēāļāļēāļĢāđ€āļ”āļĩāļĒāļ§āļāļąāļšāđ‚āļ›āļĢāđ€āļ‹āļŠ āđāļ•āđˆāļ­āļĒāđˆāļēāļ‡āđ„āļĢāļāđ‡āļ•āļēāļĄāļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢāļāđ‡āļŠāļēāļĄāļēāļĢāļ–āļˆāļąāļ”āļ•āļēāļĢāļēāļ‡āđƒāļŦāđ‰āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āđāļ•āđˆ thread āđ„āļ”āđ‰āļ­āļĒāđˆāļēāļ‡āļ­āļīāļŠāļĢāļ° āđ€āļ™āļ·āđˆāļ­āļ‡āļˆāļēāļāļžāļ§āļāļĄāļąāļ™āđ€āļžāļĩāļĒāļ‡āđāļ„āđˆāļ„āļąāļ”āļĨāļ­āļāđ€āļ­āļēāļ—āļĢāļąāļžāļĒāļēāļāļĢāđ„āļ›āđ€āļžāļĩāļĒāļ‡āđ€āļĨāđ‡āļāļ™āđ‰āļ­āļĒāđ€āļ—āđˆāļēāļ™āļąāđ‰āļ™āđ€āļžāļ·āđˆāļ­āđāļĒāļāđ„āļ›āđ€āļ›āđ‡āļ™āđ‚āļ›āļĢāđāļāļĢāļĄāđ€āļĨāđ‡āļāļ­āļĩāļāļ•āļąāļ§āļ”āļąāļ‡āđāļŠāļ”āļ‡āđƒāļ™āļĢāļđāļ›āļ‚āđ‰āļēāļ‡āļšāļ™

  • āļāļēāļĢāļŠāļ·āđˆāļ­āļŠāļēāļĢāļāļąāļ™āļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ€āļ—āļĢāļ”āļˆāļ°āļŠāļēāļĄāļēāļĢāļ–āļ•āļīāļ”āļ•āđˆāļ­āļœāđˆāļēāļ™āļāļąāļ™āđ„āļ”āđ‰āļ—āļēāļ‡āļŦāļ™āđˆāļ§āļĒāļ„āļ§āļēāļĄāļˆāļģāļ—āļĩāđˆāđƒāļŠāđ‰āļ‡āļēāļ™āļĢāđˆāļ§āļĄāļāļąāļ™āļ­āļĒāļđāđˆāđ„āļ”āđ‰āļ—āļąāļ™āļ—āļĩ

āđƒāļ™āļ‚āļ“āļ°āļ—āļĩāđˆ

  • āļāļēāļĢāļŠāļ·āđˆāļ­āļŠāļēāļĢāļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļˆāļ°āļŠāļēāļĄāļēāļĢāļ–āļ•āļīāļ”āļ•āđˆāļ­āļœāđˆāļēāļ™āļāļąāļ™āđ„āļ”āđ‰āļ•āđ‰āļ­āļ‡āļœāđˆāļēāļ™āļ—āļēāļ‡āļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢāđ€āļ—āđˆāļēāļ™āļąāđ‰āļ™ āđ€āļŠāđˆāļ™āļœāđˆāļēāļ™āđ„āļŸāļĨāđŒ, āļœāđˆāļēāļ™ pipe āļŦāļĢāļ·āļ­āļœāđˆāļēāļ™ socket āđ€āļ›āđ‡āļ™āļ•āđ‰āļ™

POSIX threads

āđ„āļĨāļšāļĢāļēāļĢāļĩ POSIX thread āļ–āļ·āļ­āļ§āđˆāļēāđ€āļ›āđ‡āļ™āļ•āļąāļ§āļĄāļēāļ•āļĢāļēāļāļēāļ™āļŦāļĨāļąāļāđƒāļ™āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āđ€āļ—āļĢāļ”āļŠāļģāļŦāļĢāļąāļšāđ‚āļ›āļĢāđāļāļĢāļĄāļ āļēāļĐāļē C/C++ āļ‹āļķāđˆāļ‡āđ€āļ›āđ‡āļ™āđ„āļĨāļšāļĢāļēāļĢāļĩāļ—āļĩāđˆāļˆāļąāļ”āđ€āļ•āļĢāļĩāļĒāļĄāļŸāļąāļ‡āļāđŒāļŠāļąāļ™āļ•āđˆāļēāļ‡āđ†āđ€āļāļĩāđˆāļĒāļ§āļāļąāļšāļāļēāļĢāļˆāļąāļ”āļāļēāļĢāđ€āļ—āļĢāļ”āđāļĨāļ°āļˆāļ°āļ—āļģāļ‡āļēāļ™āđ„āļ”āđ‰āļĄāļĩāļ›āļĢāļ°āļŠāļīāļ—āļ˜āļīāļ āļēāļžāļĄāļēāļāļŠāļģāļŦāļĢāļąāļšāļĢāļ°āļšāļšāļ„āļ­āļĄāļžāļīāļ§āđ€āļ•āļ­āļĢāđŒāļ—āļĩāđˆāļĄāļĩāļŦāļĨāļēāļĒāļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāļāļĨāļēāļ‡ (multi-processor āļŦāļĢāļ·āļ­ multi-core systems) āđ€āļ™āļ·āđˆāļ­āļ‡āļˆāļēāļāļĄāļĩāļŸāļąāļ‡āļāđŒāļŠāļąāļ™āđƒāļ™āļāļēāļĢāļˆāļąāļ”āļ•āļēāļĢāļēāļ‡āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāļšāļ™āđāļ•āđˆāļĨāļ°āļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāļāļĨāļēāļ‡āļ—āļĩāđˆāļ­āļĒāļđāđˆāļ āļēāļĒāđƒāļ™āļĢāļ°āļšāļšāļ„āļ­āļĄāļžāļīāļ§āđ€āļ•āļ­āļĢāđŒāļ•āļąāļ§āđ€āļ”āļĩāļĒāļ§āļāļąāļ™āđāļĨāļ°āđ€āļ—āļĢāļ”āļ—āļļāļāļ•āļąāļ§āļ—āļĩāđˆāļ­āļĒāļđāđˆāļ āļēāļĒāđƒāļ™āđ‚āļ›āļĢāđ€āļ‹āļŠāđ€āļ”āļĩāļĒāļ§āļāļąāļ™āļ™āļąāđ‰āļ™āļāđ‡āļˆāļ°āđƒāļŠāđ‰āļžāļ·āđ‰āļ™āļ—āļĩāđˆāđƒāļ™āļŦāļ™āđˆāļ§āļĒāļ„āļ§āļēāļĄāļˆāļģāđ€āļ”āļĩāļĒāļ§āļāļąāļ™ (address space) āļ‹āļķāđˆāļ‡āđāļ•āļāļ•āđˆāļēāļ‡āļˆāļēāļāđ€āļ—āļ„āđ‚āļ™āđ‚āļĨāļĒāļĩāđƒāļ™āļāļēāļĢāđ€āļ‚āļĩāļĒāļ™āđ‚āļ›āļĢāđāļāļĢāļĄāđāļšāļšāļ‚āļ™āļēāļ™ (Parallel programming) āđ€āļŠāđˆāļ™ MPI āđāļĨāļ° PVM āļ—āļĩāđˆāļ–āļđāļāđƒāļŠāđ‰āđƒāļ™āļŠāļ āļēāļžāđāļ§āļ”āļĨāđ‰āļ­āļĄāļāļēāļĢāļ„āļģāļ™āļ§āļ“āđāļšāļšāļāļĢāļ°āļˆāļēāļĒ (distributed computing) āđ„āļ›āļĒāļąāļ‡āļĢāļ°āļšāļšāļ„āļ­āļĄāļžāļīāļ§āđ€āļ•āļ­āļĢāđŒāđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļ­āļ·āđˆāļ™āđ† āļ—āļĩāđˆāļ­āļĒāļđāđˆāđ„āļāļĨāļ­āļ­āļāđ„āļ›āļŦāļĢāļ·āļ­āļ•āđˆāļēāļ‡āļŠāļ–āļēāļ™āļ—āļĩāđˆāļāļąāļ™

āļŸāļąāļ‡āļāđŒāļŠāļąāļ™āđāļĨāļ°āļ•āļąāļ§āđāļ›āļĢāļ—āļĩāđˆāđ€āļāļĩāđˆāļĒāļ§āļ‚āđ‰āļ­āļ‡

  • āđ„āļĨāļšāļĢāļēāļĢāļĩāļ—āļĩāđˆāđ€āļāļĩāđˆāļĒāļ§āļ‚āđ‰āļ­āļ‡āļ„āļ·āļ­ pthread.h

  • āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ int pthread_create(pthread_t *new_thread_ID, const pthread_attr_t *attr, void * (*start_func)(void *), void *arg);

    • āļŠāļģāļŦāļĢāļąāļšāļŠāļĢāđ‰āļēāļ‡ thread āļ•āļąāļ§āđƒāļŦāļĄāđˆ (new_thread_ID) āđāļĨāļ°āļāļģāļŦāļ™āļ”āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ (*start_func) āļ—āļĩāđˆāļˆāļ°āđƒāļŦāđ‰āļ—āļģāļ‡āļēāļ™āđ€āļĄāļ·āđˆāļ­ thread āļ–āļđāļāđ€āļĢāļĩāļĒāļāļ‚āļķāđ‰āļ™āļĄāļē āđ‚āļ”āļĒāļŠāļēāļĄāļēāļĢāļ–āļĢāļ°āļšāļļāļžāļēāļĢāļēāļĄāļīāđ€āļ•āļ­āļĢāđŒāļ—āļĩāđˆāļˆāļ°āļŠāđˆāļ‡āđ„āļ›āļĒāļąāļ‡āļŸāļąāļ‡āļāđŒāļŠāļąāļ™āļ‚āļ­āļ‡ thread āđ„āļ”āđ‰ (arg)

  • āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ int pthread_join(pthread_t target_thread, void **status);

    • āļŠāļģāļŦāļĢāļąāļšāđ€āļĢāļĩāļĒāļāđƒāļŦāđ‰ thread āļ™āļąāđ‰āļ™ (target_thread) āļ—āļģāļ‡āļēāļ™āļ‚āļķāđ‰āļ™āļĄāļē

  • āļŸāļąāļ‡āļāđŒāļŠāļąāļ™ void pthread_exit(void *retval);

    • āļŠāļģāļŦāļĢāļąāļšāļŠāļąāđˆāļ‡āđƒāļŦāđ‰ thread āļŠāļīāđ‰āļ™āļŠāļļāļ”āļāļēāļĢāļ—āļģāļ‡āļēāļ™

āļ‚āļšāļ§āļ™āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āđ€āļ—āļĢāļ”āļ™āļąāđ‰āļ™āļˆāļ°āļ›āļĢāļ°āļāļ­āļšāđ„āļ›āļ”āđ‰āļ§āļĒ āļŠāļĢāđ‰āļēāļ‡āđ€āļ—āļĢāļ” (thread creation), āļŠāļīāđ‰āļ™āļŠāļļāļ”āļāļēāļĢāļ—āļģāļ‡āļēāļ™ (termination), āļ—āļģāļ‡āļēāļ™āļ•āļēāļĄāļˆāļąāļ‡āļŦāļ§āļ° (thread synchronization āļ”āđ‰āļ§āļĒāļ§āļīāļ˜āļĩāļāļēāļĢāđāļšāļš joins, blocking āđ€āļ›āđ‡āļ™āļ•āđ‰āļ™), āļāļēāļĢāļˆāļąāļ”āļĨāļģāļ”āļąāļšāļāļēāļĢāļ—āļģāļ‡āļēāļ™ (scheduling), āļāļēāļĢāļˆāļąāļ”āļāļēāļĢāļ‚āđ‰āļ­āļĄāļđāļĨ (data management) āđāļĨāļ° āļāļēāļĢāļ•āļīāļ”āļ•āđˆāļ­āļāļąāļ™āļĢāļ°āļŦāļ§āđˆāļēāļ‡āļāļąāļ™ (process interaction)

āļžāļ·āđ‰āļ™āļ—āļĩāđˆāļ—āļĩāđˆāđƒāļŠāđ‰āļ‡āļēāļ™āļĢāđˆāļ§āļĄāļāļąāļ™āļ‚āļ­āļ‡āđ€āļ—āļĢāļ”āļ—āļąāđ‰āļ‡āļŦāļĄāļ”āļ āļēāļĒāđƒāļ™āđ‚āļ›āļĢāđ€āļ‹āļŠāļ›āļĢāļ°āļāļ­āļšāđ„āļ›āļ”āđ‰āļ§āļĒ

  • āļŠāļļāļ”āļ„āļģāļŠāļąāđˆāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠ (Process instructions)

  • āļ„āđˆāļē files descriptors āļ—āļĩāđˆāļĄāļĩāļāļēāļĢāđ€āļ›āļīāļ”āđ„āļ§āđ‰

  • āļŠāļąāļāļāļēāļ“ (signals) āđāļĨāļ°āļ•āļąāļ§āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢ (signal handlers)

  • āđ„āļ”āđ€āļĢāļ”āļ—āļ­āļĢāļĩāđˆāļ›āļąāļˆāļˆāļļāļšāļąāļ™ (current working directory)

  • āļŦāļĄāļēāļĒāđ€āļĨāļ‚ User āđāļĨāļ° Group

āđ‚āļ”āļĒāđāļ•āđˆāļĨāļ°āđ€āļ—āļĢāļ”āļˆāļ°āļĄāļĩ:

  • āļŦāļĄāļēāļĒāđ€āļĨāļ‚āđ€āļ—āļĢāļ” (Thread ID)

  • āļāļĨāļļāđˆāļĄāļ•āļąāļ§āđāļ›āļĢāļĢāļĩāļˆāļīāļŠāđ€āļ•āļ­āļĢāđŒ (set of registers) āđāļĨāļ° stack pointer

  • āļŠāđāļ•āđ‡āļ„āļŠāļģāļŦāļĢāļąāļšāđ€āļāđ‡āļšāļ„āđˆāļēāļ•āļąāļ§āđāļ›āļĢ (local variables)

  • signal mask

  • āļ„āđˆāļē priority

  • āļ„āđˆāļēāļŠāļ–āļēāļ™āļ°āļ—āļĩāđˆāļŠāđˆāļ‡āļāļĨāļąāļš: errno

āđāļĨāļ°āđ€āļĄāļ·āđˆāļ­āļ•āđ‰āļ­āļ‡āļāļēāļĢāļ„āļ­āļĄāđ„āļžāļĨāđŒāđ‚āļ›āļĢāđāļāļĢāļĄāļˆāļ°āļ•āđ‰āļ­āļ‡āļĄāļĩāļāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡āđ„āļĨāļšāļĢāļēāļĢāļĩ Posix threads āļ”āđ‰āļ§āļĒ -lpthread āļ”āļąāļ‡āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡

$ gcc -o main main.c –lpthread

āđ€āļ›āļĢāļĩāļĒāļšāđ€āļ—āļĩāļĒāļšāļ›āļĢāļ°āļŠāļīāļ—āļ˜āļīāļ āļēāļžāļĢāļ°āļŦāļ§āđˆāļēāļ‡ fork() āđāļĨāļ° pthread_create()

āđ€āļĄāļ·āđˆāļ­āđ€āļ›āļĢāļĩāļĒāļšāđ€āļ—āļĩāļĒāļšāļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āđāļĨāļ°āļˆāļąāļ”āļāļēāļĢāđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĨāđ‰āļ§ āļāļēāļĢāđƒāļŠāđ‰ thread āļˆāļ°āļĄāļĩāļāļēāļĢāđƒāļŠāđ‰āļ—āļĢāļąāļžāļĒāļēāļāļĢāļ‚āļ­āļ‡āļĢāļ°āļšāļšāļ™āđ‰āļ­āļĒāļāļ§āđˆāļēāļĄāļēāļ āđāļĨāļ°āđ€āļāļīāļ” overhead āļāļąāļšāļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢāļ—āļĩāđˆāļ™āđ‰āļ­āļĒāļāļ§āđˆāļēāļ­āļĒāđˆāļēāļ‡āđ€āļŦāđ‡āļ™āđ„āļ”āđ‰āļŠāļąāļ” āļˆāļēāļāļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āđ‚āļ›āļĢāđāļāļĢāļĄāļ‚āđ‰āļēāļ‡āļĨāđˆāļēāļ‡āļˆāļ°āđāļŠāļ”āļ‡āļĢāļ°āļĒāļ°āđ€āļ§āļĨāļēāļ‚āļ­āļ‡āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĨāļ° thread āļ”āđ‰āļ§āļĒāļˆāļģāļ™āļ§āļ™ 50,000 āļ•āļąāļ§ āļ āļēāļĒāđƒāļ•āđ‰āļŠāļ āļēāļžāđāļ§āļ”āļĨāđ‰āļ­āļĄāđ€āļ”āļĩāļĒāļ§āļāļąāļ™ āđ‚āļ”āļĒāļˆāļ°āļĄāļĩāļāļēāļĢāļ™āļąāļšāļŦāļ™āđˆāļ§āļĒāđ€āļ§āļĨāļēāļ—āļąāđ‰āļ‡āļŠāļēāļĄāđāļšāļšāļ„āļ·āļ­ real time, user time āđāļĨāļ° system time

/*
 * =========================================================================
 * C Code for fork() creation test
 * =========================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#define NFORKS 50000

void do_nothing() {
	int i;
	i = 0;
}

int main(int argc, char *argv[]) {
	int pid, j, status;

	for (j = 0; j < NFORKS; j++) {

		/*** error handling ***/
		if ((pid = fork()) < 0) {
			printf("fork failed with error code= %d\n", pid);
			exit(0);
		}

		/*** this is the child of the fork ***/
		else if (pid == 0) {
			do_nothing();
			exit(0);
		}

		/*** this is the parent of the fork ***/
		else {
			waitpid(pid, status, 0);
		}
	}
}

/*
 * =========================================================================
 * C Code for pthread_create() test
 * =========================================================================
 */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NTHREADS 50000

void* do_nothing(void *null) {
	int i;
	i = 0;
	pthread_exit(NULL);
}

int main(int argc, char *argv[]) {
	int rc, i, j, detachstate;
	pthread_t tid;
	pthread_attr_t attr;

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	for (j = 0; j < NTHREADS; j++) {
		rc = pthread_create(&tid, &attr, do_nothing, NULL);
		if (rc) {
			printf("ERROR; return code from pthread_create() is %d\n", rc);
			exit(-1);
		}

		/* Wait for the thread */
		rc = pthread_join(tid, NULL);
		if (rc) {
			printf("ERROR; return code from pthread_join() is %d\n", rc);
			exit(-1);
		}
	}

	pthread_attr_destroy(&attr);
	pthread_exit(NULL);

}

āļ•āļēāļĢāļēāļ‡ 5-3 āđāļŠāļ”āļ‡āļœāļĨāļāļēāļĢāļ—āļ”āļŠāļ­āļšāļšāļ™āļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāļĢāļļāđˆāļ™āļ•āđˆāļēāļ‡āđ†

Ref: https://computing.llnl.gov/tutorials/pthreads/

ðŸ–Ĩïļ
āļĢāļđāļ›āđ€āļ›āļĢāļĩāļĒāļšāđ€āļ—āļĩāļĒāļšāļāļēāļĢāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāđ‚āļ›āļĢāđāļāļĢāļĄāđāļšāļš Single-Thread āđāļĨāļ° Multi-Thread
āļĢāļđāļ›āđāļŠāļ”āļ‡āļāļēāļĢāđƒāļŠāđ‰āļžāļ·āđ‰āļ™āļ—āļĩāđˆāđƒāļ™āļŠāđˆāļ§āļ™āļ‚āļ­āļ‡ stack āļĢāļ°āļŦāļ§āđˆāļēāļ‡āđ‚āļ›āļĢāđ€āļ‹āļŠāđāļĨāļ°āđ€āļ—āļĢāļ”
āļĢāļđāļ›āđāļŠāļ”āļ‡āļŸāļąāļ‡āļāđŒāļŠāļąāļ™āļ—āļĩāđˆāđ€āļāļĩāđˆāļĒāļ§āļ‚āđ‰āļ­āļ‡āļ‚āļ­āļ‡āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ‚āļ­āļ‡āđ€āļ—āļĢāļ”āļĨāļđāļāļ•āļąāđ‰āļ‡āđāļ•āđˆāđ€āļĢāļīāđˆāļĄāļˆāļ™āļŠāļīāđ‰āļ™āļŠāļļāļ”
āļĢāļđāļ›āđāļŠāļ”āļ‡āļĢāļēāļĒāļĨāļ°āđ€āļ­āļĩāļĒāļ”āļ‚āļ­āļ‡āđ‚āļ„āļĢāļ‡āļŠāļĢāđ‰āļēāļ‡āļ āļēāļĒāđƒāļ™āđ‚āļ›āļĢāđ€āļ‹āļŠāļ—āļĩāđˆāļĄāļĩāđ€āļ—āļĢāļ”āļĒāđˆāļ­āļĒ
āđāļŠāļ”āļ‡āļœāļĨāļāļēāļĢāļ—āļ”āļŠāļ­āļšāļšāļ™āļŦāļ™āđˆāļ§āļĒāļ›āļĢāļ°āļĄāļ§āļĨāļœāļĨāļĢāļļāđˆāļ™āļ•āđˆāļēāļ‡āđ† (Ref:https://computing.llnl.gov/tutorials/pthreads/)