Initial commit
This commit is contained in:
parent
89fdf35b3f
commit
985714a816
|
|
@ -0,0 +1,42 @@
|
||||||
|
cmake_minimum_required(VERSION 2.8)
|
||||||
|
project(ipc)
|
||||||
|
|
||||||
|
set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
|
||||||
|
set(SRC_DIR ${CMAKE_SOURCE_DIR}/src)
|
||||||
|
|
||||||
|
execute_process(
|
||||||
|
COMMAND mkdir -p bin
|
||||||
|
COMMAND mkdir -p /usr/include/ipc/
|
||||||
|
)
|
||||||
|
|
||||||
|
include_directories(${INCLUDE_DIR})
|
||||||
|
|
||||||
|
set(INCLUDE_FILES
|
||||||
|
${INCLUDE_DIR}/utils/kassert
|
||||||
|
${INCLUDE_DIR}/utils/kpanic
|
||||||
|
${INCLUDE_DIR}/utils/kprint
|
||||||
|
${INCLUDE_DIR}/utils/null
|
||||||
|
${INCLUDE_DIR}/ipc
|
||||||
|
${INCLUDE_DIR}/pm
|
||||||
|
${INCLUDE_DIR}/process
|
||||||
|
${INCLUDE_DIR}/semaphore
|
||||||
|
${INCLUDE_DIR}/shm
|
||||||
|
)
|
||||||
|
|
||||||
|
set(SOURCE_FILES
|
||||||
|
${SRC_DIR}/ipc.c
|
||||||
|
${SRC_DIR}/pm.c
|
||||||
|
${SRC_DIR}/process.c
|
||||||
|
${SRC_DIR}/semaphore.c
|
||||||
|
${SRC_DIR}/shm.c
|
||||||
|
)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
|
||||||
|
|
||||||
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -march=native")
|
||||||
|
|
||||||
|
add_executable(./bin/kmain ${SOURCE_FILES} ./src/kmain.c)
|
||||||
|
|
||||||
|
target_include_directories(./bin/kmain PUBLIC ${INCLUDE_DIR})
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
#ifndef __IPC_H__
|
||||||
|
|
||||||
|
#include "process.h"
|
||||||
|
#include "shm.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#define QEQUE_SIZE 16
|
||||||
|
|
||||||
|
typedef struct ipc_message {
|
||||||
|
__pid_t pid;
|
||||||
|
size_t size;
|
||||||
|
char* message;
|
||||||
|
} ipc_message_t;
|
||||||
|
|
||||||
|
typedef struct ipc {
|
||||||
|
__pid_t first_pid;
|
||||||
|
__pid_t second_pid;
|
||||||
|
shmseg *shm;
|
||||||
|
ipc_message_t qeque[QEQUE_SIZE];
|
||||||
|
} ipc_t;
|
||||||
|
|
||||||
|
extern void ipc_init();
|
||||||
|
extern void ipc_send(ipc_t ipc, struct process process);
|
||||||
|
extern void ipc_receive(ipc_t ipc, struct process process);
|
||||||
|
extern void ipc_destroy(ipc_t ipc);
|
||||||
|
|
||||||
|
#endif // __IPC_H__
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef __PM_H__
|
||||||
|
|
||||||
|
|
||||||
|
extern void pm_init();
|
||||||
|
extern void pm_destroy();
|
||||||
|
extern void pm_create_process();
|
||||||
|
#endif // __PM_H__
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef __PROCESS_H__
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct process {
|
||||||
|
uint32_t pid;
|
||||||
|
char* name;
|
||||||
|
uint32_t priority;
|
||||||
|
bool alive;
|
||||||
|
bool state;
|
||||||
|
uint32_t next;
|
||||||
|
}process_t;
|
||||||
|
|
||||||
|
extern void process_init();
|
||||||
|
extern void process_destroy(__pid_t pid);
|
||||||
|
extern __pid_t process_create();
|
||||||
|
extern void exit_all_process();
|
||||||
|
|
||||||
|
#endif // __PROCESS_H__
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef __SEMAPHORE_H__
|
||||||
|
|
||||||
|
typedef struct semaphore{
|
||||||
|
int value;
|
||||||
|
int waiting;
|
||||||
|
int* queue;
|
||||||
|
}sem_t;
|
||||||
|
|
||||||
|
extern void semaphore_init(sem_t sem, int value);
|
||||||
|
extern void semaphore_wait(sem_t sem);
|
||||||
|
extern void semaphore_signal(sem_t sem);
|
||||||
|
extern void semaphore_destroy(sem_t sem);
|
||||||
|
|
||||||
|
#endif // __SEMAPHORE_H__
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef __SHM_H__
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
typedef unsigned int shm_id_t;
|
||||||
|
|
||||||
|
#define SHM_SIZE 4096
|
||||||
|
|
||||||
|
typedef struct shm_segment{
|
||||||
|
size_t size;
|
||||||
|
shm_id_t id;
|
||||||
|
uint32_t buff[SHM_SIZE];
|
||||||
|
}shmseg;
|
||||||
|
|
||||||
|
extern shmseg *shm_init();
|
||||||
|
extern void shm_write(shmseg *shm, uint8_t data[]);
|
||||||
|
extern void shm_destroy(shmseg *shm);
|
||||||
|
|
||||||
|
#endif // __SHM_H__
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef __KASSERT_H__
|
||||||
|
|
||||||
|
#include "kpanic.h"
|
||||||
|
|
||||||
|
#define KASSERT(exp, failed_msg) exp ? 0 : kpanic("ASSERTION FAILED! " failed_msg)
|
||||||
|
|
||||||
|
#endif // __KASSERT_H__
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
#ifndef __PANIC_H__
|
||||||
|
|
||||||
|
#define kpanic(x) printf("panic: %s\n", x); while(1);
|
||||||
|
|
||||||
|
#endif // __PANIC_H__
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef __KPRINT_H__
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define kprint(msg) printf("%s", msg);
|
||||||
|
#endif // __KPRINT_H__
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
#ifndef __KNULL_H__
|
||||||
|
|
||||||
|
#define KNULL ((void*)0)
|
||||||
|
#endif // __KNULL_H__
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
|
||||||
|
#include "../include/ipc.h"
|
||||||
|
#include "../include/shm.h"
|
||||||
|
|
||||||
|
static void ipc_init()
|
||||||
|
{
|
||||||
|
ipc_t ipc;
|
||||||
|
|
||||||
|
ipc.first_pid = 0;
|
||||||
|
ipc.second_pid = 0;
|
||||||
|
ipc.shm = shm_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void ipc_send(ipc_t ipc, struct process process)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
static void ipc_receive(ipc_t ipc, struct process process)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ipc_destroy(ipc_t ipc)
|
||||||
|
{
|
||||||
|
|
||||||
|
process_destroy(ipc.first_pid);
|
||||||
|
process_destroy(ipc.second_pid);
|
||||||
|
free(ipc.qeque);
|
||||||
|
shm_destroy(ipc.shm);
|
||||||
|
free(ipc);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
|
||||||
|
int main(int argc, int *argv[])
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
#include "../include/pm.h"
|
||||||
|
#include "../include/process.h"
|
||||||
|
|
||||||
|
static void pm_init()
|
||||||
|
{
|
||||||
|
kprint("Initializing process manager");
|
||||||
|
process_init();
|
||||||
|
}
|
||||||
|
static void pm_destroy()
|
||||||
|
{
|
||||||
|
exit_all_process();
|
||||||
|
}
|
||||||
|
|
||||||
|
__pid_t pm_create_process()
|
||||||
|
{
|
||||||
|
__pid_t pid = process_create();
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
#include "../include/process.h"
|
||||||
|
#include "../include/utils/null.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define MAX_PROCESSES 16
|
||||||
|
#define ROOT_PID 1
|
||||||
|
|
||||||
|
process_t processes[MAX_PROCESSES];
|
||||||
|
|
||||||
|
static __pid_t next_pid = 0;
|
||||||
|
|
||||||
|
void process_init()
|
||||||
|
{
|
||||||
|
process_create();
|
||||||
|
processes[ROOT_PID-1].alive = true;
|
||||||
|
|
||||||
|
for (int i = 1; i < MAX_PROCESSES; i++)
|
||||||
|
{
|
||||||
|
process_create();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void process_destroy(__pid_t pid)
|
||||||
|
{
|
||||||
|
KASSERT(pid >= ROOT_PID && pid <= MAX_PROCESSES, "Invalid pid");
|
||||||
|
KASSERT(pid != ROOT_PID, "Cannot destroy root process");
|
||||||
|
|
||||||
|
processes[pid-1].pid = 0;
|
||||||
|
processes[pid-1].name = NULL;
|
||||||
|
processes[pid-1].priority = 0;
|
||||||
|
processes[pid-1].state = false;
|
||||||
|
processes[pid-1].next = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void exit_all_process()
|
||||||
|
{
|
||||||
|
for(int curr_pid = ROOT_PID; curr_pid < MAX_PROCESSES; curr_pid++)
|
||||||
|
{
|
||||||
|
process_destroy(curr_pid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__pid_t process_create()
|
||||||
|
{
|
||||||
|
KASSERT((++next_pid) < MAX_PROCESSES, "Max processes reached");
|
||||||
|
|
||||||
|
process_t process;
|
||||||
|
process.pid = ++next_pid;
|
||||||
|
process.name = NULL;
|
||||||
|
process.priority = 0;
|
||||||
|
process.state = false;
|
||||||
|
process.next = 0;
|
||||||
|
processes[process.pid-1] = process;
|
||||||
|
return process.pid;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include "../include/semaphore.h"
|
||||||
|
|
||||||
|
static void semaphore_init(sem_t sem, int value)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void semaphore_wait(sem_t sem)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void semaphore_signal(sem_t sem)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void semaphore_destroy(sem_t sem)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
#include "../include/shm.h"
|
||||||
|
#include "../include/utils/kassert.h"
|
||||||
|
#include <sys/shm.h>
|
||||||
|
|
||||||
|
#define BUF_SIZE 1024
|
||||||
|
#define SHM_KEY 0x1234
|
||||||
|
|
||||||
|
static shmseg *shm_init()
|
||||||
|
{
|
||||||
|
shm_id_t shmid;
|
||||||
|
shmseg *shmp;
|
||||||
|
|
||||||
|
shmid = shmget(SHM_KEY, sizeof(shmseg), 0644|IPC_CREAT);
|
||||||
|
|
||||||
|
KASSERT(shmid != -1, "Shared memory creation error!");
|
||||||
|
|
||||||
|
// Attach to the segment to get a pointer to it.
|
||||||
|
shmp = shmat(shmid, NULL, 0);
|
||||||
|
|
||||||
|
KASSERT(shmp != (void*) -1, "Shared memory attach error!");
|
||||||
|
|
||||||
|
shmp->size = 0;
|
||||||
|
shmp->id = shmid;
|
||||||
|
|
||||||
|
return shmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void shm_write(shmseg *shm, uint8_t data[])
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void shm_destroy(shmseg *shm)
|
||||||
|
{
|
||||||
|
shmctl(shm->id, IPC_RMID, NULL);
|
||||||
|
free(shm);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue