Sobes.tech
Junior — Middle

Kuidas on rakendatud ülesannete järjekorrad protsesside planeerimise süsteemis?

sobes.tech AI

Vastus AI-lt

In process scheduling systems (scheduler) in Go, task queues are typically 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 on slices or lists: Sometimes, for more complex logic, custom implementations of queues based on slices or lists with mutexes 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)
}

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