#include #include #include #include int main() { pid_t pid; printf("--- Server Booted ---\n"); // Fork the process to handle a new 'client connection' pid = fork(); if (pid < 0) { // Error handling perror("Fork failed"); exit(1); } else if (pid == 0) { // --- CHILD PROCESS --- // This is the worker handling the specific task printf("Child [PID: %d]: Processing client request...\n", getpid()); sleep(2); // Simulate processing time FILE *f = fopen("fork.txt","w"); char *str = "Your requested processing is done.\n"; fputs(str, f); fclose(f); printf("Child [PID: %d]: Request processed. Closing connection.\n", getpid()); exit(0); // Child terminates after completing work } else { // --- PARENT PROCESS --- // The parent continues to listen for new connections printf("Parent [PID: %d]: Dispatched child %d. Waiting for next request...\n", getpid(), pid); // Parent waits for the child to finish to prevent 'zombie' processes wait(NULL); printf("Parent [PID: %d]: Child process cleaned up.\n", getpid()); } return 0; }