/* Process Manager Server
 * Handles process creation, termination, and management
 * Based on MINIX3 PM server
 */

#include "../kernel/kernel.h"

extern int printf(const char* format, ...);
extern int receive(int source, message_t* msg);

#define PM_NAME "Process Manager"

typedef struct {
    int pid;
    int parent;
    int uid;
    int gid;
    char name[32];
} pm_proc_t;

static pm_proc_t proc_table[MAX_PROCESSES];

void pm_init() {
    printf("[%s] Initializing...\n", PM_NAME);
    
    /* Initialize process table */
    for (int i = 0; i < MAX_PROCESSES; i++) {
        proc_table[i].pid = -1;
    }
    
    printf("[%s] Ready\n", PM_NAME);
}

int pm_fork(int parent_pid) {
    /* Find free slot */
    for (int i = 0; i < MAX_PROCESSES; i++) {
        if (proc_table[i].pid == -1) {
            proc_table[i].pid = i;
            proc_table[i].parent = parent_pid;
            return i;
        }
    }
    return -1; /* No free slots */
}

void pm_exit(int pid, int status) {
    printf("[%s] Process %d exited with status %d\n", PM_NAME, pid, status);
    proc_table[pid].pid = -1;
}

void pm_main() {
    message_t msg;
    
    pm_init();
    
    /* Main server loop */
    for(;;) {
        /* Receive message from any process */
        receive(-1, &msg);
        
        /* Handle different message types */
        switch(msg.type) {
            case SYS_FORK:
                /* Handle fork request */
                break;
            case SYS_EXIT:
                /* Handle exit request */
                break;
            default:
                printf("[%s] Unknown message type: %d\n", PM_NAME, msg.type);
        }
    }
}
