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
-
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.
-
File and Directory Operations:
- Unix-like systems: Use
open()
,close()
,read()
,write()
, etc. - Windows: Use
CreateFile()
,CloseHandle()
,ReadFile()
,WriteFile()
, etc.
- Unix-like systems: Use
-
Process Management:
- Unix-like systems: Use
fork()
,execve()
,wait()
, etc. - Windows: Use
CreateProcess()
,ExitProcess()
,WaitForSingleObject()
, etc.
- Unix-like systems: Use
-
Memory Management:
- Unix-like systems: Use
malloc()
,free()
,mmap()
,munmap()
, etc. - Windows: Use
HeapAlloc()
,HeapFree()
,VirtualAlloc()
,VirtualFree()
, etc.
- Unix-like systems: Use
-
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.
-
Thread Management:
- Unix-like systems: Use POSIX threads (pthreads).
- Windows: Use Windows threads with functions such as
CreateThread()
,TerminateThread()
, etc.
-
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.
- Unix-like systems: Use
-
Time and Date:
- Unix-like systems: Use
time()
,gettimeofday()
,alarm()
,sleep()
, etc. - Windows: Use
GetSystemTime()
,SetSystemTime()
,Sleep()
, etc.
- Unix-like systems: Use
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