Page 1 of 2
How to wait? (in C)
Posted: Sun Oct 09, 2016 12:44 pm
by NunoLava1998
I have managed to link/etc. the assembly/C files, and i'm now editing a functions file called funct.h which can do simple VGA functions (put pixel, start VGA, fill screen), and i want to implement a wait() function and be informed if there is a wait function from the default libs (and if then, what lib and how to use it). How do i do this?
I'm using C and editing a header.
Re: How to wait?
Posted: Mon Oct 10, 2016 12:26 am
by Boris
Hello,
The wait function is based upon :
- A programmable timer that generates interrupts ( PIT, HPET...)
- A task switch. Or , if you don't have any task to switch to ,the hlt instruction. ( Remember to enable interrupts before doing HLT)
The PiT is easy to configure, and there are tutorials about it in the wiki.
Re: How to wait?
Posted: Mon Oct 10, 2016 1:19 am
by NunoLava1998
Boris wrote:Hello,
The wait function is based upon :
- A programmable timer that generates interrupts ( PIT, HPET...)
- A task switch. Or , if you don't have any task to switch to ,the hlt instruction. ( Remember to enable interrupts before doing HLT)
The PiT is easy to configure, and there are tutorials about it in the wiki.
It's in C. I will include it in a header file called "funct.h" which is similar to "tty.h" in Meaty Skeleton.
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 2:20 am
by Roman
And..?
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 2:23 am
by NunoLava1998
Roman wrote:And..?
i like cats
i had nothing to say after what i said and was waiting for an answer so i said this.
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 5:14 am
by MichaelFarthing
I suppose that is an example of a wait function
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 6:05 am
by Peterbjornx
When writing a kernel you have to write all functions you want to use yourself, you do not have any kind of standard library available, please see the wiki.
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 10:47 am
by Boris
Whatever the language is, you will still have to make your CPU execute assembler. It's your job to make the sugar candy around it and call it "void wait(unsigned howmuch)", "(define wait(howmuch))" or whatever ..
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 11:37 am
by NunoLava1998
Boris wrote:Whatever the language is, you will still have to make your CPU execute assembler. It's your job to make the sugar candy around it and call it "void wait(unsigned howmuch)", "(define wait(howmuch))" or whatever ..
inline assembly is not a thing by yo.. okay, but how do i do it?
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 12:57 pm
by iansjack
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 1:06 pm
by NunoLava1998
iansjack wrote:http://wiki.osdev.org/Inline_Assembly
i know how to do inline assembly (literally asm();, so easy lol), i ment the assembly code to wait.
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 1:34 pm
by iansjack
You have already been told that. I assume you are not asking for complete code. (That's not how this site works.)
Re: How to wait? (in C)
Posted: Mon Oct 10, 2016 3:08 pm
by matt11235
If you wanted to be incredibly lazy you could just have a loop with hundreds of thousands of iterations. Just make sure the compiler doesn't optimize it out.
Re: How to wait? (in C)
Posted: Tue Oct 11, 2016 12:24 am
by NunoLava1998
zenzizenzicube wrote:If you wanted to be incredibly lazy you could just have a loop with hundreds of thousands of iterations. Just make sure the compiler doesn't optimize it out.
that uses clock cycles, so it really depends on your CPU. Not going for that one.
Re: How to wait? (in C)
Posted: Tue Oct 11, 2016 11:29 am
by Schol-R-LEA
NunoLava1998 wrote:Roman wrote:And..?
i like cats i had nothing to say after what i said [...]
Unless this is a direct translation of an idiom from some other language, then I assume you are referring to the English language pun, "like cats, it goes without saying" (based on the general disdain cats seem to show to human endeavors, leaving in the middle of being petted and so forth). That usually is meant to imply that the implications of something were obvious, which, well... the fact that Roman asked that indicates it might not have been so obvious after all.
As for the solution, that is going to depend on the OS itself - it isn't something any canned response will give.
Usually, the solution in a multitasking system is to provide a system call that requests that the scheduler halt the running process and place it in the wait queue until after a given period of time. How you would implement that is entirely dependent on your scheduler design, and even if we had access to it (you
do have it in a publicly accessible offsite version control repo,
right? If not, then shame on you, drop everything and do that now), we could not and would not give you the solution because at that point it would become
our OS project, not yours.
For the case where all processes are waiting, you would normally have a dummy no-op process, called the null process, which on an x86 CPU can often be just:
Code: Select all
pause_loop:
sti
hlt
jmp short pause_loop
Assuming that the interrupts are set (hence the first instruction), then any new interrupt will trap to the scheduler, and if some other process is now schedulable, it will go to that, otherwise, it returns to the loop at the JMP instruction.
Note that this depends on the interrupt handlers and the scheduler being in place
first. This approach will
not work inline to a process, as it will hang the process - the whole point is for the null process to hang itself, you see, and if you insert this loop into some other code you will never break out of it. It also is not sufficient for a multi-core system, as you would need a null process for each core.