Home  C-language   Why we have ...

why we have different C code for POSIX and windows system

The OS-level calls mentioned are primarily part of the POSIX (Portable Operating System Interface) standard, which is a family of standards specified by the IEEE for maintaining compatibility between operating systems. Unix-like operating systems (such as Linux and macOS) are POSIX-compliant and thus provide these system calls. Windows, on the other hand, does not natively adhere to the POSIX standard, hence it uses different system calls and APIs.

Differences between Windows and Unix-like Systems

  1. System Architecture:

    • Unix-like systems (POSIX): Use POSIX-compliant APIs for system calls, which provide a standardized interface for operations such as file manipulation, process control, and inter-process communication.
    • Windows: Uses its own set of APIs, known as the Windows API (or WinAPI), which is designed specifically for the Windows operating system.
  2. File and Directory Operations:

    • Unix-like systems: Use open(), close(), read(), write(), etc.
    • Windows: Use CreateFile(), CloseHandle(), ReadFile(), WriteFile(), etc.
  3. Process Management:

    • Unix-like systems: Use fork(), execve(), wait(), etc.
    • Windows: Use CreateProcess(), ExitProcess(), WaitForSingleObject(), etc.
  4. Memory Management:

    • Unix-like systems: Use malloc(), free(), mmap(), munmap(), etc.
    • Windows: Use HeapAlloc(), HeapFree(), VirtualAlloc(), VirtualFree(), etc.
  5. Inter-process Communication (IPC):

    • Unix-like systems: Use pipes, message queues, shared memory, semaphores, and sockets.
    • Windows: Use named pipes, mailslots, shared memory, semaphores, and Winsock for socket programming.
  6. Thread Management:

    • Unix-like systems: Use POSIX threads (pthreads).
    • Windows: Use Windows threads with functions such as CreateThread(), TerminateThread(), etc.
  7. Signals:

    • Unix-like systems: Use signal(), sigaction(), raise(), etc.
    • Windows: Uses a different mechanism for handling similar functionality, often using events and other synchronization objects.
  8. Time and Date:

    • Unix-like systems: Use time(), gettimeofday(), alarm(), sleep(), etc.
    • Windows: Use GetSystemTime(), SetSystemTime(), Sleep(), etc.

Example of Equivalent System Calls

Here is a comparison of how to open and read a file in both Unix-like systems and Windows:

Unix-like System (POSIX)

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int fd;
    ssize_t bytesRead;
    char buffer[100];

    // Open the file
    fd = open("example.txt", O_RDONLY);
    if (fd == -1) {
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }

    // Read from the file
    bytesRead = read(fd, buffer, sizeof(buffer) - 1);
    if (bytesRead == -1) {
        perror("Error reading file");
        close(fd);
        exit(EXIT_FAILURE);
    }

    // Null-terminate the buffer and print it
    buffer[bytesRead] = '\0';
    printf("Read from file: %s\n", buffer);

    // Close the file
    if (close(fd) == -1) {
        perror("Error closing file");
        exit(EXIT_FAILURE);
    }

    return 0;
}

Windows

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main() {
    HANDLE hFile;
    DWORD bytesRead;
    char buffer[100];

    // Open the file
    hFile = CreateFile("example.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        fprintf(stderr, "Error opening file: %ld\n", GetLastError());
        exit(EXIT_FAILURE);
    }

    // Read from the file
    if (!ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, NULL)) {
        fprintf(stderr, "Error reading file: %ld\n", GetLastError());
        CloseHandle(hFile);
        exit(EXIT_FAILURE);
    }

    // Null-terminate the buffer and print it
    buffer[bytesRead] = '\0';
    printf("Read from file: %s\n", buffer);

    // Close the file
    if (!CloseHandle(hFile)) {
        fprintf(stderr, "Error closing file: %ld\n", GetLastError());
        exit(EXIT_FAILURE);
    }

    return 0;
}
Published on: Jun 25, 2024, 08:05 AM  
 

Comments

Add your comment