Multi-process Control and sample programs for Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

When we write C, if we use multi-process, we usually use the signal, management and so on to the process of communication between processes, then golang how to achieve this?? Just look at the code and talk.

Package Mainimport (  "FMT"   "Time") Func main () {  timeout: = Make (chan bool, 1)   go func () {  &N Bsp Fmt. Println ("------------Sub-process 1--------------")     T1: = time. Now (). Unixnano ()     FMT. PRINTLN (t1)     FMT. Println ("This will certainly be done")         time. Sleep (3 * time. Second)    //timeout <-true    timeout <-true } ()     FMT. PRINTLN ("First logical OR response main function")     go func () {    FMT. Println ("------------Sub-process 2--------------")     t2: = time. Now (). Unixnano ()     FMT. Println (T2)     FMT. Println ("Equivalent to fork a subprocess in progress")  } ()     ch: = make (chan int)   Select {    Case <-ch:& nbsp   Case <-timeout:      FMT. Println ("------------back to main function--------------")       FMT. PRINTLN ("task is timeout!")  }    FMT. PRINTLN ("Output of the main function itself")}

We execute the code and take a look at the results

首先逻辑还是响应 main 函数------------ 子进程2--------------1471577916141068800相当于fork一个子进程在进行------------ 子进程1--------------1471577916142068800这个一定会执行------------ 回到main函数 --------------task is timeout!main 函数本身的输出

From the time stamp of nanosecond, it can be seen that two go func () is executed almost simultaneously, this is the Golang parallel processing, is the direct call system Epoll processing flow, Golang also provides a monitoring method to monitor the return of each parallel processing logic, Select is the way

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.