Lesson 05intermediateProcesses

Windows Process Spawning

Windows-specific process creation with _spawnl, _spawnlp, and _spawnv. Compares Windows spawn with Linux fork+exec and demonstrates quirky Windows argv parsing. Note: these examples require Windows (process.h) and won't compile on Linux — view the code for reference.

spawnWindows_spawnlcross-platform
1

Calculator for Spawn

A calculator child program adapted for use with the Windows spawn functions.

calculator.c54 lines
/*
 * calculator.c — Calculator for Windows spawn examples
 *
 * Key concepts: Simple calculator, used as child process by main.c
 * Compile: gcc -o calculator calculator.c
 * Run:     ./prog
 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    printf("\n");
    printf("========================================\n");
    printf("  Calculator for Windows spawn example\n");
    printf("========================================\n\n");
    if (argc != 4) {
        //recived the following arguments:
        printf("argc in calculator: %d\n", argc);
        for (int i = 0; i < argc; i++) {
            printf("argv[%d]: %s\n", i, argv[i]);
        }
        fprintf(stderr, "Usage: %s <num1> <operator> <num2>\n", argv[0]);
        return 1;
    }

    int num1 = atoi(argv[1]);
    char operator = argv[2][0];
    int num2 = atoi(argv[3]);
    int result;

    
    switch (operator) {
        case '+': result = num1 + num2; break;
        case '-': result = num1 - num2; break;
       // the * is a "wildcard" in the shell, so we cant use it as an argument
       // case '*': result = num1 * num2; break;
        case 'X': result = num1 * num2; break;
        case '/': 
            if (num2 == 0) {
                fprintf(stderr, "Error: Division by zero\n");
                return 1;
            }
            result = num1 / num2; 
            break;
        default:
            fprintf(stderr, "Error: Unknown operator '%c'\n", operator);
            return 1;
    }

    printf("[Calculator] %d %c %d = %d\n", num1, operator, num2, result);
    printf("\n========================================\n");
    return 0;
}
Expected Outputclick Run to execute live
========================================
  Calculator for Windows spawn example
========================================

argc in calculator: 1
argv[0]: ./calculator
Usage: ./calculator <num1> <operator> <num2>
2

Spawn Variants

Using _spawnl, _spawnlp, and _spawnv to create child processes on Windows — analogous to fork+exec on Linux.

main.c125 lines
3

Windows argv Parsing Quirks

Demonstrating surprising Windows behavior when argv values contain spaces or special characters.

weird_behaviours/shell.c69 lines
/*
 * shell.c — Windows shell argv parsing quirks
 *
 * Key concepts: Windows command line parsing, argv escaping edge cases
 * Compile: cl shell.c (MSVC only)
 * Run:     ./prog
 */

#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    printf("\n");
    printf("========================================\n");
    printf("  Windows shell argv parsing quirks\n");
    printf("========================================\n\n");
    char program_name[100];
    char input_string[100];


    ////// this example is to recreate wierd C windows run time behaviours
    // using spawn
    //// if we pass a string like "aa bb cc" as an argument to the program
    // without the quotes, the spawn function, and the c windows run time, will separate the string
    // into 3 different argv arguments so: aa, bb, cc
    // but we we pass the string with quotes, the spawn function will pass the string as a single argument
    // try for yourself, run the program first and enter the following:
    // program name: ./unique_str
    // input string: aa bb cc      - without qutoes

    // than run it again and enter the following:
    // program name: ./unique_str
    // input string: "aa bb cc"    - with quotes

    // Get the name of the program to run
    printf("Enter the program name to run (e.g., unique_str): ");
    fflush(stdout);
    scanf("%99s", program_name);

    // Get the input string
    printf("Enter the string to pass as a single argument (e.g., \"aa bb cc\"): ");
    fflush(stdout);
    getchar(); // Consume the newline left in the buffer
    fgets(input_string, sizeof(input_string), stdin);

    // Remove newline character if present
    size_t len = strlen(input_string);
    if (len > 0 && input_string[len - 1] == '\n') {
        input_string[len - 1] = '\0';
    }
    printf("[Shell] Received input: \"%s\"\n", input_string);
    printf("[Shell] Spawning %s with this as a single argument...\n\n", program_name);
    // Arguments for spawn
    char *args[] = {program_name, input_string, NULL};

    // Spawn the program with _P_WAIT so the parent waits for the child to complete
    int status = spawnv(_P_WAIT, program_name, args);
    if (status == -1) {
        perror("spawnv failed");
        return 1;
    } else {
        printf("[Shell] Child finished with exit status: %d\n\n", status);
        printf("========================================\n");
    }

    return 0;
}
4

String Uniqueness Check

A small utility to check if all characters in a string are unique.

weird_behaviours/unique_str.c29 lines
/*
 * unique_str.c — String uniqueness check (utility)
 *
 * Key concepts: character counting, duplicate detection
 * Compile: gcc -o unique unique_str.c
 * Run:     ./prog
 */
#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("\n");
    printf("========================================\n");
    printf("  String uniqueness check (utility)\n");
    printf("========================================\n\n");
    printf("[unique_str] Received %d argument(s):\n\n", argc);
    printf("  %-8s | %s\n", "Index", "Value");
    printf("  -------- | -----\n");
    for (int i = 0; i < argc; i++) {
        printf("  argv[%d]  | \"%s\"\n", i, argv[i]);
    }
    printf("\n");
    if (argc == 2) {
        printf("  String was passed as a SINGLE argument (correct)\n");
    } else if (argc > 2) {
        printf("  String was SPLIT into %d args (Windows shell quirk!)\n", argc - 1);
    }
    printf("\n========================================\n");
    return 0;
} 
Expected Outputclick Run to execute live
========================================
  String uniqueness check (utility)
========================================

Number of arguments received: 1
argv[0]: ./unique_str

Open Full Terminal

Get a Linux terminal with all 4 files from this lesson pre-loaded. Compile, run, and experiment freely.