Skip to content

Commit

Permalink
Merge pull request #18 from tomasanchez/feature/CFG-28-pcb_handling
Browse files Browse the repository at this point in the history
Feature/cfg 28 pcb handling
  • Loading branch information
tomasanchez authored Apr 24, 2022
2 parents c26980f + 60cf776 commit 36c7032
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 6 deletions.
56 changes: 56 additions & 0 deletions cpu/include/module/cpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @file cpu.h
* @author Tomás Sánchez <tosanchez@frba.utn.edu.ar>
* @brief
* @version 0.1
* @date 04-23-2022
*
* @copyright Copyright (c) 2022
*
*/

#pragma once

#include "pcb.h"
#include "thread_manager.h"

#define MODULE_NAME "cpu"

/**
* @brief CPU Module.
*
*/
typedef struct CPU
{

// TODO: Add server for Kernel - For PCB.
// TODO: Add server for Kernel - For Interruptions
// TODO: Add client connection to Memory.
// CPU's Thread Launcher;
thread_manager_t tm;
// Current PCB in execution.
pcb_t *pcb;
} cpu_t;

/**
* @brief Inits a CPU module.
*
* @param cpu to be initialized
*/
int on_init(cpu_t *cpu);

/**
* @brief Handles the main execution procedure.
*
* @param cpu to run
* @return the exit status code
*/
int on_run(cpu_t *cpu);

/**
* @brief Frees memory usage of a CPU module.
*
* @param cpu the module reference
* @return an exit staus code
*/
int on_before_exit(cpu_t *cpu);
27 changes: 27 additions & 0 deletions cpu/include/server/pcb_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file pcb_controller.h
* @author Tomás Sánchez <tosanchez@frba.utn.edu.ar>
* @brief
* @version 0.1
* @date 04-23-2022
*
* @copyright Copyright (c) 2022
*
*/

#pragma once

#include "pcb.h"
#include "server.h"

// ============================================================================================================
// ***** Public Functions *****
// ============================================================================================================

/**
* @brief Receivesa a PCB stream.
*
* @param fd the file descriptor
* @return a new PCB instance
*/
pcb_t *receive_pcb(int fd);
15 changes: 12 additions & 3 deletions cpu/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
*
*/

#include <stdio.h>
#include <stdlib.h>
#include "cpu.h"

extern cpu_t g_cpu;

int main(void)
{
puts("Hello World!");
int exit_status = EXIT_SUCCESS;

if ((exit_status = on_init(&g_cpu)) == EXIT_SUCCESS)
{
on_run(&g_cpu);

return on_before_exit(&g_cpu);
}

return EXIT_SUCCESS;
return exit_status;
}
135 changes: 135 additions & 0 deletions cpu/src/module/cpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* @file cpu.c
* @author Tomás Sánchez <tosanchez@frba.utn.edu.ar>
* @brief
* @version 0.1
* @date 04-23-2022
*
* @copyright Copyright (c) 2022
*
*/

#include "lib.h"
#include "cpu.h"
#include "log.h"
#include "cfg.h"
#include <signal.h>

// ============================================================================================================
// ***** Private Functions *****
// ============================================================================================================

cpu_t g_cpu;

static void handle_sigint(int signal)
{
if (signal == SIGINT)
{
LOG_WARNING("SIGNINT was received.");
on_before_exit(&g_cpu);
exit(SIGINT);
}
}

/**
* @brief Initializes the CPU reference.
*
* @param cpu the module reference
* @return the return code.
*/
static int
on_cpu_init(cpu_t *cpu);

/**
* @brief Frees the CPU memory usage.
*
* @param cpu to be deleted
* @return the exit status.
*/
static int
on_cpu_destroy(cpu_t *cpu);

static int on_cpu_init(cpu_t *cpu)
{

cpu->pcb = NULL;
cpu->tm = new_thread_manager();

// TODO: Add server for Kernel - For PCB.
// TODO: Add server for Kernel - For Interruptions
// TODO: Add client connection to Memory.
return EXIT_SUCCESS;
}

static int
on_cpu_destroy(cpu_t *cpu)
{
pcb_destroy(cpu->pcb);
thread_manager_destroy(&cpu->tm);
// TODO: Add server for Kernel - For PCB.
// TODO: Add server for Kernel - For Interruptions
// TODO: Add client connection to Memory.
return EXIT_SUCCESS;
}

// ============================================================================================================
// ***** Public Functions *****
// ============================================================================================================

// ------------------------------------------------------------
// Life Cycle
// ------------------------------------------------------------

int on_init(cpu_t *cpu)
{

if (log_init(MODULE_NAME, true) EQ ERROR)
return LOG_INITIALIZATION_ERROR;

LOG_DEBUG("Logger started.");

if (config_init(MODULE_NAME) EQ ERROR)
{
LOG_ERROR("Could not open Configuration file.");
log_close();
return CONFIGURATION_INITIALIZATION_ERROR;
}

LOG_DEBUG("Configurations loaded.");

// Attach del evento de interrupcion forzada.
signal(SIGINT, handle_sigint);

on_cpu_init(cpu);
LOG_DEBUG("Module started SUCCESSFULLY");
return EXIT_SUCCESS;
}

int on_run(cpu_t *cpu)
{
LOG_TRACE("Hello World!");

return EXIT_SUCCESS;
}

int on_before_exit(cpu_t *cpu)
{
int exit_code = EXIT_SUCCESS;

LOG_WARNING("Closing Module...");

config_close();
LOG_WARNING("Configurations unloaded.");

on_cpu_destroy(cpu);
LOG_WARNING("CPU has been shut down.");

LOG_TRACE("Program ended.");
log_close();

return exit_code;
}

// ------------------------------------------------------------
// Event Handlers
// ------------------------------------------------------------
36 changes: 36 additions & 0 deletions cpu/src/server/pcb_controller.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file pcb_controller.c
* @author Tomás Sánchez <tosanchez@frba.utn.edu.ar>
* @brief
* @version 0.1
* @date 04-23-2022
*
* @copyright Copyright (c) 2022
*
*/
#include <stdlib.h>
#include "pcb_controller.h"
#include "log.h"

// ============================================================================================================
// ***** Definitions *****
// ============================================================================================================

#define LOG_PCB(pcb) \
{ \
LOG_INFO("PCB= [id: %d, size: %lu, estimation: %d, program_counter: %d]", pcb->id, pcb->size, pcb->estimation, pcb->pc); \
}

// ============================================================================================================
// ***** Public Functions *****
// ============================================================================================================

pcb_t *receive_pcb(int fd)
{
ssize_t recv_bytes = -1;

pcb_t *pcb = NULL;
pcb = pcb_from_stream(servidor_recibir_stream(fd, &recv_bytes));
LOG_PCB(pcb);
return pcb;
}
2 changes: 2 additions & 0 deletions lib/inc/modules/pcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ typedef struct PCB
void *page_table;
// CPU Burst estimation.
uint32_t estimation;
// Program Counter.
uint32_t pc;
} pcb_t;

/**
Expand Down
11 changes: 8 additions & 3 deletions lib/src/modules/pcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pcb_t *new_pcb(uint32_t id, size_t size, uint32_t estimation)
pcb->instructions = list_create();
pcb->page_table = NULL;
pcb->estimation = estimation;
pcb->pc = 0;

return pcb;
}
Expand Down Expand Up @@ -54,7 +55,7 @@ void *pcb_to_stream(pcb_t *pcb)
* @brief The serialized stream will be:
*
* --------------------------------------------------------------
* ID | SIZE | ESTIMATION | <List_Count> | Instructions...
* ID | SIZE | ESTIMATION | PC | <List_Count> | Instructions...
* --------------------------------------------------------------
*
*/
Expand All @@ -64,6 +65,8 @@ void *pcb_to_stream(pcb_t *pcb)
offset += sizeof(uint32_t);
memcpy(stream + offset, &pcb->estimation, sizeof(uint32_t));
offset += sizeof(uint32_t);
memcpy(stream + offset, &pcb->pc, sizeof(uint32_t));
offset += sizeof(uint32_t);
int list_count = list_size(pcb->instructions);
memcpy(stream + offset, &list_count, sizeof(uint32_t));
offset += sizeof(uint32_t);
Expand Down Expand Up @@ -103,6 +106,8 @@ pcb_t *pcb_from_stream(void *stream)
offset += sizeof(uint32_t);
memcpy(&pcb->estimation, stream + offset, sizeof(uint32_t));
offset += sizeof(uint32_t);
memcpy(&pcb->pc, stream + offset, sizeof(uint32_t));
offset += sizeof(uint32_t);

pcb->instructions = instruction_list_from(stream + offset);
pcb->page_table = NULL;
Expand All @@ -114,8 +119,8 @@ size_t pcb_bytes_size(pcb_t *pcb)
{
// The PCB final size.
size_t size = 0;
// The size of [ID, SIZE, ESTIMATION]
size += sizeof(uint32_t) * 3;
// The size of [ID, SIZE, ESTIMATION, PC]
size += sizeof(uint32_t) * 4;
// Size of the List_SIZE value.
size += sizeof(uint32_t);
// The size of the instruction list.
Expand Down

0 comments on commit 36c7032

Please sign in to comment.