-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
io: allocate I/O descriptors for process
- Loading branch information
1 parent
156cedf
commit 7de83bb
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |