Skip to content

Commit

Permalink
io: allocate I/O descriptors for process
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 6, 2024
1 parent 156cedf commit 7de83bb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* lux - a lightweight unix-like operating system
* Omar Elghoul, 2024
*
* Core Microkernel
*/

/* Abstractions for file systems and sockets */

#include <errno.h>
#include <stdlib.h>
#include <kernel/sched.h>
#include <kernel/io.h>

/* openIO(): opens an I/O descriptor in the current process
* params: p - process to open descriptor in
* params: iod - destination to store pointer to I/O descriptor structure
* returns: I/O descriptor, negative error code on fail
*/

int openIO(void *pv, void **iodv) {
Process *p = (Process *)pv;
IODescriptor *iod = (IODescriptor *)iodv;

if(p->iodCount >= MAX_IO_DESCRIPTORS) return -ESRCH;

/* randomly allocate descriptors instead of sequential numbering */
int desc;
do {
desc = rand() % MAX_IO_DESCRIPTORS;
} while(p->io[desc].valid);

p->io[desc].valid = true;
p->io[desc].type = IO_WAITING;
return desc;
}

0 comments on commit 7de83bb

Please sign in to comment.