Specifically, I have two tasks fn and fn1 that are being run by a scheduler. The code for each is below:
int fn(void *arg)
{
for(;;) {
printf("a");
}
return 6;
}
int fn2(void *arg)
{
for(;;) {
printf("b");
}
return 6;
}
Each of these functions are scheduled by a schedule function, which is called on each PIT interrupt:
// See https://wiki.osdev.org/Programmable_Interval_Timer
// The oscillator used by the PIT chip runs at (roughly) 1.193182 MHz.
#define TIMER_INPUT_CLOCK_FREQUENCY 1193180
#define TIMER_CHANNEL_0_DATA_PORT 0x40
#define TIMER_CHANNEL_1_DATA_PORT 0x41
#define TIMER_CHANNEL_2_DATA_PORT 0x42
#define TIMER_COMMAND_PORT 0x43
// number of ticks since system booted
uint32 g_ticks = 0;
// frequency in hertz
uint16 g_freq_hz = 0;
int task_switch_needed = 0;
// See https://wiki.osdev.org/Programmable_Interval_Timer
void timer_set_frequency(uint16 f) {
g_freq_hz = f;
uint16 divisor = TIMER_INPUT_CLOCK_FREQUENCY / f;
// set Mode 3 - Square Wave Mode
outportb(TIMER_COMMAND_PORT, 0b00110110);
// set low byte
outportb(TIMER_CHANNEL_0_DATA_PORT, divisor & 0xFF);
// set high byte
outportb(TIMER_CHANNEL_0_DATA_PORT, (divisor >> 8) & 0xFF);
}
void timer_handler(REGISTERS* r) {
g_ticks++;
printf("timer triggered at frequency %d\n", g_ticks);
schedule();
}
void timer_init() {
timer_set_frequency(20);
isr_register_interrupt_handler(IRQ_BASE, timer_handler);
}
void sleep(int sec) {
uint32 end = g_ticks + sec * g_freq_hz;
while (g_ticks < end);
}
The functions are made tasks here:
uint32 *stack = kmalloc (0x400) + 0x3F0;
task_t *t = create_task(&fn, (void*)0x567, stack);
uint32 *stack2 = kmalloc (0x400) + 0x3F0;
task_t *t2 = create_task(&fn2, (void*)0x567, stack2);
However, the timer is only invoked once, and the first function, namely fn just keeps executing.
OS Source: https://filebin.net/by6ix0jhs1ibcg0i