System Calls Implementation
-
- Posts: 17
- Joined: Sun Aug 21, 2016 10:18 am
- Libera.chat IRC: Chpetrou
- Location: Cyprus, Greece
- Contact:
System Calls Implementation
Hello,
I am trying to implement system calls in my OS. How can i implement the read system call?
I want to be able to use scanf / get user input from keyboard.
Thanks in Advance
I am trying to implement system calls in my OS. How can i implement the read system call?
I want to be able to use scanf / get user input from keyboard.
Thanks in Advance
Re: System Calls Implementation
That is a broad question. You posted a topic about reading from the keyboard before. How far did you get? Does your keyboard driver work now?
In general, read() will never read directly from the keyboard. In UNIX-like systems, read() gets its data from an abstract "file", which can be a terminal, a regular file, a socket or something entirely different. Thus, you probably want a file abstraction in your kernel (assuming its a monolithic kernel) that provides a read() call that is overriden for each individual file implementation. Then you want a certain file that represents the keyboard. In the simplest version, the keyboard driver fetches scan codes from the device (i.e. the keyboard), translates the scan codes to characters and puts them into a buffer and notifies processes that are blocked in a read() on the keyboard file. The read() on this file then dequeues data from the buffer. Note that this would really only be a rudimentary design though, as scan code to character translation should probably not happen in the kernel (as it has no change of knowing the correct input method that a user wants to use).
In general, read() will never read directly from the keyboard. In UNIX-like systems, read() gets its data from an abstract "file", which can be a terminal, a regular file, a socket or something entirely different. Thus, you probably want a file abstraction in your kernel (assuming its a monolithic kernel) that provides a read() call that is overriden for each individual file implementation. Then you want a certain file that represents the keyboard. In the simplest version, the keyboard driver fetches scan codes from the device (i.e. the keyboard), translates the scan codes to characters and puts them into a buffer and notifies processes that are blocked in a read() on the keyboard file. The read() on this file then dequeues data from the buffer. Note that this would really only be a rudimentary design though, as scan code to character translation should probably not happen in the kernel (as it has no change of knowing the correct input method that a user wants to use).
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
-
- Posts: 17
- Joined: Sun Aug 21, 2016 10:18 am
- Libera.chat IRC: Chpetrou
- Location: Cyprus, Greece
- Contact:
Re: System Calls Implementation
Hello
Thanks for the quick answer.
Thanks
Thanks for the quick answer.
I have the keyboard driver working. I just don't know how to write the read system call so when scanf calls it, to make it read from the keyboard.How far did you get? Does your keyboard driver work now?
Also how can i make the keyboard look as a file, and read from that file?Then you want a certain file that represents the keyboard. In the simplest version, the keyboard driver fetches scan codes from the device (i.e. the keyboard), translates the scan codes to characters and puts them into a buffer and notifies processes that are blocked in a read() on the keyboard file.
Thanks
Re: System Calls Implementation
Well you need some things first:
1. A VFS (Virtual File System) Implementation
2. I/O syscalls (open, read, write, close)
1. A VFS (Virtual File System) Implementation
2. I/O syscalls (open, read, write, close)
-
- Posts: 17
- Joined: Sun Aug 21, 2016 10:18 am
- Libera.chat IRC: Chpetrou
- Location: Cyprus, Greece
- Contact:
Re: System Calls Implementation
I wrote this.1. A VFS (Virtual File System) Implementation
If you mean the actual system calls, i have the write one. I don't know how to make the read one.2. I/O syscalls (open, read, write, close)
Re: System Calls Implementation
Hi,
Generally you will store keyboard events in some kind of (ring?) buffer in the driver. The call to scanf may, for example, tell the keyboard driver that a particular process is expecting keyboard input and the system call may release the buffer once it hits a LF/CR/CRLF.
If you are multitasking, you will of course need to track the process that has focus and will also need to decide how to handle throwing away unwanted keystrokes from the buffer if nothing has focus at the moment.
Cheers,
Adam
Generally you will store keyboard events in some kind of (ring?) buffer in the driver. The call to scanf may, for example, tell the keyboard driver that a particular process is expecting keyboard input and the system call may release the buffer once it hits a LF/CR/CRLF.
If you are multitasking, you will of course need to track the process that has focus and will also need to decide how to handle throwing away unwanted keystrokes from the buffer if nothing has focus at the moment.
Cheers,
Adam
-
- Posts: 17
- Joined: Sun Aug 21, 2016 10:18 am
- Libera.chat IRC: Chpetrou
- Location: Cyprus, Greece
- Contact:
Re: System Calls Implementation
Hello
This is what i use inside the kscanf:
Thanks
I already have something like this. It is a kscanf function, which puts the characters in a buffer and then copies the buffer to the target buffer. It works just fine in kernel space. But when i use it in the read system call, it doesn't work.Generally you will store keyboard events in some kind of (ring?) buffer in the driver. The call to scanf may, for example, tell the keyboard driver that a particular process is expecting keyboard input and the system call may release the buffer once it hits a LF/CR/CRLF.
Code: Select all
static int sys_read(int fd, char *buf, size_t len) {
kscanf("%s", buf);
return 0;
}
Code: Select all
int scanning(char *newBuffer, int size) {
clearBuffer(newBuffer);
clearBuffer(buffer);
bufferNum = 0;
isScanEnabled = 1;
char tempChar;
while (isScanEnabled == 1) {
tempChar = getChar();
if (tempChar == '\n') {
isScanEnabled = 0;
return 1;
}
else {
if (addToBuffer(newBuffer, size, tempChar) == 0) return 0;
monPut(tempChar);
}
}
return 0;
}
-
- Member
- Posts: 232
- Joined: Mon Jul 25, 2016 6:54 pm
- Location: Adelaide, Australia
Re: System Calls Implementation
Hi.
Could you describe what happens when you call sys_read(...)? Does it run but not populate buf or is there a fault of some type generated? Also in
Could you describe what happens when you call sys_read(...)? Does it run but not populate buf or is there a fault of some type generated? Also in
Code: Select all
scanning(...) what does clearBuffer(buffer) do? newBuffer is the param but what does buffer contain?
-
- Posts: 17
- Joined: Sun Aug 21, 2016 10:18 am
- Libera.chat IRC: Chpetrou
- Location: Cyprus, Greece
- Contact:
Re: System Calls Implementation
Hello,
It runs but it doesn't populate buf.
clearBuffer just populates the buffer (which is just an extra temporary buffer) with null - '\0' .
It runs but it doesn't populate buf.
clearBuffer just populates the buffer (which is just an extra temporary buffer) with null - '\0' .