mihalis's picture

Concurrency and Parallelism

It is a very common misconception that Concurrency and Parallelism is the same thing, which is far from true! Parallelism is the simultaneous execution of multiple things whereas Concurrency is a way of structuring your components so that they can be independently executed when possible.
It is only when you build things concurrently that you can safely execute them in parallel, when and if your operating system and your hardware permits it. The Erlang programming language did this a long time ago, long before CPUs had multiple cores and computers had lots of RAM.
In a valid concurrent design, adding concurrent entities makes the whole system run faster because more things can run in parallel. So, the desired parallelism comes from a better concurrent expression and implementation of the problem. The developer is responsible for taking concurrency into account during the design phase of a system and benefit from a potential parallel execution of the components of the system. So, the developer should not think about parallelism but about breaking things into independent components that solve the initial problem when combined.
Even if you cannot run your functions in parallel on a UNIX machine, a valid concurrent design will still improve the design and the maintainability of your programs. In other words, Concurrency is better than Parallelism!

Want to learn more about the Go Concurrency model?
Get my book Go Systems Programming from Packt or from
Or get my other book Mastering Go from Packt or from

mihalis's picture

Copying a file in Go

This blog post will show some of the ways that you can copy a file in Go.

Using io.Copy()
The simplest way to copy a file is by using the io.Copy() function. You can find the entire Go code at
The most important part of the utility is the next Go code:

nBytes, err := io.Copy(destination, source)

So, with just a single call, you can copy a file. Although this is fast, it does not give you any flexibility or any control over the whole process.

Using ioutil.WriteFile() and ioutil.ReadFile()
You can copy a file in Go by using ioutil.WriteFile() and ioutil.ReadFile(). You can find the entire source file at
The most important part of readWriteAll.go is the next two Go statements:

input, err := ioutil.ReadFile(sourceFile)
err = ioutil.WriteFile(destinationFile, input, 0644)

The first statement reads the entire source file whereas the second statement writes the contents of the input variable to a new file.
Notice that reading the entire file and storing its contents to a single variable might not be very efficient when you want to copy huge files. Nevertheless, it works!

Using os.Read() and os.Write()
The last technique uses os.Read() for reading small portions of the input file into a buffer and os.Write() for writing the contents of that buffer to the new file. Notice that the size of the buffer is given as a command line argument, which makes the process very flexible.
You can find the entire code at
The most important statements of the implementation of the Copy() function are the next:

buf := make([]byte, BUFFERSIZE)
n, err := source.Read(buf)
_, err := destination.Write(buf[:n])

The first statement creates a byte slice with the desired size. The second statement reads from the input file whereas the third statement writes the contents of the buf buffer to the destination file.

Want to learn more about File I/O in Go?
Get my book Go Systems Programming from Packt or from

Want to be able to benchmark File I/O operations?
Get my book Mastering Go from Packt or from

mihalis's picture

The Go Garbage Collector (GC)

Garbage Collection is the process of freeing memory space that is not being used. In other words, the garbage collector sees which objects are out of scope and cannot be referenced any more and frees the memory space they consume. This process happens in a concurrent way while a Go program is running, not before or after the execution of a Go program. The operation of the Go GC is based on the tricolor algorithm.

Strictly speaking the official name for the algorithm used in Go is tricolor mark-and-sweep algorithm, can work concurrently with the program and uses a write barrier. This means that when a Go program runs, the Go scheduler is responsible for the scheduling of the application and the garbage collector as if the Go scheduler had to deal with a regular application with multiple goroutines!

The core idea behind this algorithm belongs to Edsger W. Dijkstra, Leslie Lamport, A. J. Martin, C. S. Scholten and E. F. M. Steffens and was first illustrated on a paper named On-the-fly garbage collection: an exercise in cooperation.

The primary principle behind the tricolor mark-and-sweep algorithm is that it divides the objects of the heap into three different sets according to their color, which is assigned by the algorithm. The objects of the black set are guaranteed to have no pointers to any object of the while set. However, an object of the white set can have a pointer to an object of the black set because this has no effect on the operation of the GC! The objects of the grey set might have pointers to some objects of the while set. Last, the objects of the white set are the candidates for garbage collection.

So, when the garbage collection begins, all objects are white and the garbage collector visits all the root objects and colors them grey – the roots are the objects that can be directly accessed by the application, which includes global variables and other things on the stack – these objects mostly depend on the Go code of a particular program. After that, the garbage collector picks a grey object, makes it black and starts searching if that object has pointers to other objects of the white set. This means that when a grey object is being scanned for pointers to other objects, it is colored black. If that scan discovers that this particular object has one or more pointers to a white object, it puts that white object to the grey set. This process keeps going for as long as there exist objects in the grey set. After that, the objects in the white set are unreachable and their memory space can be reused. Therefore, at this point the elements of the white set is said to be garbage collected.

Go allows you to manually initiate a garbage collection by putting a runtime.GC() statement in your Go code. However, have in mind that runtime.GC() will block the caller and it might block the entire program, especially if you are running a very busy Go program with many objects. This mainly happens because you cannot perform garbage collections while everything else is rapidly changing as this will not give the garbage collector the opportunity to clearly identify the members of the while, black and grey sets! This garbage collection status is also called garbage collection safe-point.

Want to learn more about the Go Garbage Collector? Get my book Mastering Go at
Want to start writing UNIX system tools? Get my book Go Systems Programming at or from (

mihalis's picture

Why I write

As you might have noticed, I write a lot and there are many reasons that I do so:

  • I like the writing process!
  • I enjoy writing, especially when I finish writing about a topic.
  • When I write about something, I learn more things about it.
  • I like helping other people learn new things.
  • Although writing is not an easy task, It feels natural to me after so many years. However, it was not that easy in the beginning.

One more thing that I forgot to mention: from time to time I read my writings!

mihalis's picture

2017 Resolutions


  • Read more books.
  • Take more risks.
  • Study CS related research subjects.
  • Write a book!
  • Spend less time on the Internet.
  • Take more photographs.
  • Print more photographs.
  • Run faster!


Subscribe to RSS - blog