How to wait? (in C)

Programming, for all ages and all languages.
NunoLava1998
Member
Member
Posts: 273
Joined: Sun Oct 09, 2016 4:38 am
Libera.chat IRC: NunoLava1998

How to wait? (in C)

Post 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.
Last edited by NunoLava1998 on Mon Oct 10, 2016 2:17 am, edited 2 times in total.
Developing TRIODIUM OS. Or call it Dixium if you want. It doesn't matter.

https://github.com/NunoLava1998/DixiumOS
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: How to wait?

Post 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.
NunoLava1998
Member
Member
Posts: 273
Joined: Sun Oct 09, 2016 4:38 am
Libera.chat IRC: NunoLava1998

Re: How to wait?

Post 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.
Developing TRIODIUM OS. Or call it Dixium if you want. It doesn't matter.

https://github.com/NunoLava1998/DixiumOS
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: How to wait? (in C)

Post by Roman »

And..?
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
NunoLava1998
Member
Member
Posts: 273
Joined: Sun Oct 09, 2016 4:38 am
Libera.chat IRC: NunoLava1998

Re: How to wait? (in C)

Post 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.
Developing TRIODIUM OS. Or call it Dixium if you want. It doesn't matter.

https://github.com/NunoLava1998/DixiumOS
User avatar
MichaelFarthing
Member
Member
Posts: 167
Joined: Thu Mar 10, 2016 7:35 am
Location: Lancaster, England, Disunited Kingdom

Re: How to wait? (in C)

Post by MichaelFarthing »

I suppose that is an example of a wait function :)
Peterbjornx
Member
Member
Posts: 116
Joined: Thu May 06, 2010 4:34 am
Libera.chat IRC: peterbjornx
Location: Leiden, The Netherlands
Contact:

Re: How to wait? (in C)

Post 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.
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: How to wait? (in C)

Post 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 ..
NunoLava1998
Member
Member
Posts: 273
Joined: Sun Oct 09, 2016 4:38 am
Libera.chat IRC: NunoLava1998

Re: How to wait? (in C)

Post 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?
Developing TRIODIUM OS. Or call it Dixium if you want. It doesn't matter.

https://github.com/NunoLava1998/DixiumOS
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to wait? (in C)

Post by iansjack »

NunoLava1998
Member
Member
Posts: 273
Joined: Sun Oct 09, 2016 4:38 am
Libera.chat IRC: NunoLava1998

Re: How to wait? (in C)

Post 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.
Developing TRIODIUM OS. Or call it Dixium if you want. It doesn't matter.

https://github.com/NunoLava1998/DixiumOS
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to wait? (in C)

Post by iansjack »

You have already been told that. I assume you are not asking for complete code. (That's not how this site works.)
User avatar
matt11235
Member
Member
Posts: 286
Joined: Tue Aug 02, 2016 1:52 pm
Location: East Riding of Yorkshire, UK

Re: How to wait? (in C)

Post 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.
com.sun.java.swing.plaf.nimbus.InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState
Compiler Development Forum
NunoLava1998
Member
Member
Posts: 273
Joined: Sun Oct 09, 2016 4:38 am
Libera.chat IRC: NunoLava1998

Re: How to wait? (in C)

Post 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.
Developing TRIODIUM OS. Or call it Dixium if you want. It doesn't matter.

https://github.com/NunoLava1998/DixiumOS
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: How to wait? (in C)

Post 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.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Post Reply