Advertisement
cunha1

Untitled

Mar 5th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "os"
  6.     "os/signal"
  7.     "strconv"
  8.     "syscall"
  9.     "time"
  10. )
  11.  
  12. func main() {
  13.     sigs := make(chan os.Signal)
  14.     done := make(chan bool, 1)
  15.     prios := []int{}
  16.  
  17.     signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTSTP)
  18.  
  19.     go handle(sigs, &prios)
  20.     go exec(&prios)
  21.  
  22.     brojac := 0
  23.     go func() {
  24.         for true {
  25.             if len(prios) == 0 {
  26.                 fmt.Println("Glavni program: " + strconv.Itoa(brojac) + "/30")
  27.                 brojac += 1
  28.                 if brojac == 30 {
  29.                     done <- true
  30.                 }
  31.                 time.Sleep(time.Second)
  32.             }
  33.         }
  34.     }()
  35.  
  36.     <-done
  37. }
  38.  
  39. func handle(sigs chan os.Signal, prios *[]int) {
  40.     sig := <-sigs
  41.     if sig == syscall.SIGINT {
  42.         fmt.Println("signal SIGINT")
  43.         if arrFind(prios, 1) == -1 {
  44.             *prios = append(*prios, 1)
  45.         }
  46.     } else if sig == syscall.SIGQUIT {
  47.         fmt.Println("signal SIGQUIT")
  48.         if arrFind(prios, 2) == -1 {
  49.             *prios = append(*prios, 2)
  50.         }
  51.     } else if sig == syscall.SIGTSTP {
  52.         fmt.Println("signal SIGTSTP")
  53.         if arrFind(prios, 3) == -1 {
  54.             *prios = append(*prios, 3)
  55.         }
  56.     }
  57.  
  58.     handle(sigs, prios)
  59. }
  60.  
  61. func exec(prios *[]int) {
  62.     brojac := [4]int{0, 0, 0, 0}
  63.     for true {
  64.         for brojac[3] != 30 && arrHighest(prios, 3) {
  65.             fmt.Println("izvrsavam prioritet 3 " + strconv.Itoa(brojac[3]) + "/30")
  66.             brojac[3] += 1
  67.             time.Sleep(time.Second)
  68.         }
  69.         if brojac[3] == 30 {
  70.             arrDeleteVal(prios, 3)
  71.             brojac[3] = 0
  72.         }
  73.         for brojac[2] != 30 && arrHighest(prios, 2) {
  74.             fmt.Println("izvrsavam prioritet 2 " + strconv.Itoa(brojac[2]) + "/30")
  75.             brojac[2] += 1
  76.             time.Sleep(time.Second)
  77.         }
  78.         if brojac[2] == 30 {
  79.             arrDeleteVal(prios, 2)
  80.             brojac[2] = 0
  81.         }
  82.         for brojac[1] != 30 && arrHighest(prios, 1) {
  83.             fmt.Println("izvrsavam prioritet 1 " + strconv.Itoa(brojac[1]) + "/30")
  84.             brojac[1] += 1
  85.             time.Sleep(time.Second)
  86.         }
  87.         if brojac[1] == 30 {
  88.             arrDeleteVal(prios, 1)
  89.             brojac[1] = 0
  90.         }
  91.     }
  92. }
  93.  
  94. func arrFind(arr *[]int, find int) int {
  95.     ret := -1
  96.     for i, val := range *arr {
  97.         if val == find {
  98.             ret = i
  99.             break
  100.         }
  101.     }
  102.     return ret
  103. }
  104.  
  105. func arrHighest(arr *[]int, find int) bool {
  106.     max := 0
  107.     for _, val := range *arr {
  108.         if val > max {
  109.             max = val
  110.         }
  111.     }
  112.     return find == max
  113. }
  114.  
  115. func arrDeleteVal(arr *[]int, val int) {
  116.     idx := arrFind(arr, val)
  117.     (*arr)[idx] = (*arr)[len(*arr)-1]
  118.     (*arr)[len(*arr)-1] = 0
  119.     (*arr) = (*arr)[:len(*arr)-1]
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement