Page 1 of 1

System Calls Implementation

Posted: Mon Jan 29, 2018 1:39 pm
by Chpetrou
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

Re: System Calls Implementation

Posted: Mon Jan 29, 2018 2:27 pm
by Korona
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).

Re: System Calls Implementation

Posted: Mon Jan 29, 2018 2:37 pm
by Chpetrou
Hello

Thanks for the quick answer.
How far did you get? Does your keyboard driver work now?
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.
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.
Also how can i make the keyboard look as a file, and read from that file?

Thanks

Re: System Calls Implementation

Posted: Mon Jan 29, 2018 4:47 pm
by ronsor
Well you need some things first:

1. A VFS (Virtual File System) Implementation
2. I/O syscalls (open, read, write, close)

Re: System Calls Implementation

Posted: Tue Jan 30, 2018 4:41 am
by Chpetrou
1. A VFS (Virtual File System) Implementation
I wrote this.
2. I/O syscalls (open, read, write, close)
If you mean the actual system calls, i have the write one. I don't know how to make the read one.

Re: System Calls Implementation

Posted: Tue Jan 30, 2018 5:25 am
by AJ
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

Re: System Calls Implementation

Posted: Tue Jan 30, 2018 5:35 am
by Chpetrou
Hello
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.
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.

Code: Select all

static int sys_read(int fd, char *buf, size_t len) {
    kscanf("%s", buf);

    return 0;
}
This is what i use inside the kscanf:

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;
}
Thanks

Re: System Calls Implementation

Posted: Mon Feb 05, 2018 7:39 pm
by StudlyCaps
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

Code: Select all

scanning(...) what does clearBuffer(buffer) do? newBuffer is the param but what does buffer contain?

Re: System Calls Implementation

Posted: Wed Feb 14, 2018 11:14 am
by Chpetrou
Hello,

It runs but it doesn't populate buf.

clearBuffer just populates the buffer (which is just an extra temporary buffer) with null - '\0' .