Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "os"
- "os/signal"
- "strconv"
- "syscall"
- "time"
- )
- func main() {
- sigs := make(chan os.Signal)
- done := make(chan bool, 1)
- prios := []int{}
- signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTSTP)
- go handle(sigs, &prios)
- go exec(&prios)
- brojac := 0
- go func() {
- for true {
- if len(prios) == 0 {
- fmt.Println("Glavni program: " + strconv.Itoa(brojac) + "/30")
- brojac += 1
- if brojac == 30 {
- done <- true
- }
- time.Sleep(time.Second)
- }
- }
- }()
- <-done
- }
- func handle(sigs chan os.Signal, prios *[]int) {
- sig := <-sigs
- if sig == syscall.SIGINT {
- fmt.Println("signal SIGINT")
- if arrFind(prios, 1) == -1 {
- *prios = append(*prios, 1)
- }
- } else if sig == syscall.SIGQUIT {
- fmt.Println("signal SIGQUIT")
- if arrFind(prios, 2) == -1 {
- *prios = append(*prios, 2)
- }
- } else if sig == syscall.SIGTSTP {
- fmt.Println("signal SIGTSTP")
- if arrFind(prios, 3) == -1 {
- *prios = append(*prios, 3)
- }
- }
- handle(sigs, prios)
- }
- func exec(prios *[]int) {
- brojac := [4]int{0, 0, 0, 0}
- for true {
- for brojac[3] != 30 && arrHighest(prios, 3) {
- fmt.Println("izvrsavam prioritet 3 " + strconv.Itoa(brojac[3]) + "/30")
- brojac[3] += 1
- time.Sleep(time.Second)
- }
- if brojac[3] == 30 {
- arrDeleteVal(prios, 3)
- brojac[3] = 0
- }
- for brojac[2] != 30 && arrHighest(prios, 2) {
- fmt.Println("izvrsavam prioritet 2 " + strconv.Itoa(brojac[2]) + "/30")
- brojac[2] += 1
- time.Sleep(time.Second)
- }
- if brojac[2] == 30 {
- arrDeleteVal(prios, 2)
- brojac[2] = 0
- }
- for brojac[1] != 30 && arrHighest(prios, 1) {
- fmt.Println("izvrsavam prioritet 1 " + strconv.Itoa(brojac[1]) + "/30")
- brojac[1] += 1
- time.Sleep(time.Second)
- }
- if brojac[1] == 30 {
- arrDeleteVal(prios, 1)
- brojac[1] = 0
- }
- }
- }
- func arrFind(arr *[]int, find int) int {
- ret := -1
- for i, val := range *arr {
- if val == find {
- ret = i
- break
- }
- }
- return ret
- }
- func arrHighest(arr *[]int, find int) bool {
- max := 0
- for _, val := range *arr {
- if val > max {
- max = val
- }
- }
- return find == max
- }
- func arrDeleteVal(arr *[]int, val int) {
- idx := arrFind(arr, val)
- (*arr)[idx] = (*arr)[len(*arr)-1]
- (*arr)[len(*arr)-1] = 0
- (*arr) = (*arr)[:len(*arr)-1]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement