Sobes.tech
Junior — Middle

How are task queues implemented within the process scheduling system?

sobes.tech AI

Answer from AI

In process scheduling systems (scheduler) in Go, task queues are usually implemented using data structures such as channels and queues.

Main methods:

  • Channels: Go provides built-in channels that allow safe transfer of tasks between goroutines. A channel can be considered as a FIFO queue.

  • Queues based on slices or lists: Sometimes, for more complex logic, custom queue implementations based on slices or lists with locking (mutex) for synchronization are used.

Example of a simple task queue using a channel:

package main

import (
    "fmt"
    "time"
)

func worker(tasks <-chan int) {
    for task := range tasks {
        fmt.Println("Processing task", task)
        time.Sleep(time.Second) // simulate work
    }
}

func main() {
    tasks := make(chan int, 5) // buffered channel

    go worker(tasks)

    for i := 1; i <= 10; i++ {
        tasks <- i
        fmt.Println("Task added", i)
    }
    close(tasks)
    time.Sleep(2 * time.Second)
}

Thus, channels provide a safe and convenient task queue for the scheduler.