view kernel/src/process.c3 @ 393:6ae782a085e0

Added init program
author Windel Bouwman
date Sat, 17 May 2014 21:17:40 +0200
parents 2ec730e45ea1
children
line wrap: on
line source

module process;

import memory;
import kernel;

// process type definition:
type struct {
    int id;
    int status;
    process_t* next; // For linked list..
} process_t;

// Or, use this list structure:
// List<process_t> procs;

// init is the root of all processes:
var process_t* root_process;

var int next_pid;

function void init()
{
    next_pid = 0;
    root_process = cast<process_t*>(0);
    // init_pid = Create();
}

/*
    Create a new process.
*/
function process_t* Create()
{
    var process_t* p;

    p = cast<process_t*>(memory.alloc(sizeof(process_t)));
    p->id = next_pid;
    p->status = 0; // Ready!
    p->next = cast<process_t*>(0);

    // Increment PID:
    next_pid = next_pid + 1;

    // Store it in the list:
    if (root_process == cast<process_t*>(0))
    {
        root_process = p;
    }
    else
    {
        var process_t* parent;
        parent = root_process;
        while (parent->next != cast<process_t*>(0))
        {
            parent = parent->next;
        }

        parent->next = p;
    }

    return p;
}


function void Kill(process_t* p)
{
    // clean memory
}

function process_t* byId(int id)
{
    // Perform lookup
    return 0;
}