Posts

Showing posts from August, 2020

Thread vs Process

  https://www.guru99.com/difference-between-process-and-thread.html Properties of Process Here are the important properties of the process: Creation of each process requires separate system calls for each process. It is an isolated execution entity and does not share data and information. Processes use the IPC(Inter-Process Communication) mechanism for communication that significantly increases the number of system calls. Process management takes more system calls. A process has its stack, heap memory with memory, and data map. Properties of Thread Here are important properties of Thread: Single system call can create more than one thread Threads share data and information. Threads shares instruction, global, and heap regions. However, it has its register and stack. Thread management consumes very few, or no system calls because of communication between threads that can be achieved using shared memory.

Why is TCP more reliable than UDP

https://www.ques10.com/p/11133/how-is-tcp-better-than-udp-explain-services-offe-1/  TCP is better than UDP because of following reasons: TCP is Reliable as it provides reliability of delivery of packets to the receiver while UDP is Non-reliable and does not give information about the packets. TCP is Connection oriented it means connection is to be setup before data is sent that is done in form of 3-way handshake while UDP is Connectionless. TCP provides flow control and error control characteristics while UDP doesn't provide it. TCP gives guarantee that a packet will reach on the destination without any duplication and the order of data will also be same.

What is the difference between 2 code blocks

package main import ( "fmt" "github.com/sirupsen/logrus" "runtime/debug" "time" ) func main () { defer func () { if err := recover (); err != nil { logrus . Errorln ( "main" , err ) debug . PrintStack () } }() GoInside ( nil ) time . Sleep ( 2 * time . Second ) fmt . Println ( "After panic" ) } type Test struct { X int } func GoInside ( v * Test ) { go NullPointer ( v . X ) } func NullPointer ( X int ) { fmt . Println ( X ) } This above code can handle a panic error , but the code below can not handle a panic ==> why  package main import ( "fmt" "github.com/sirupsen/logrus" "runtime/debug" "time" ) func main () { defer func () { if err := recover (); err != nil { logrus . Errorln ( "main" , err ) debug . PrintStack () } }() GoInside () time . Sleep ( 2 * ti...

How to handle panic error in Golang

ref:  https://ednsquare.com/story/learn-go-panic-and-recover-in-golang------1HSVNg func main () { NullPointer ( nil ) time . Sleep ( 2 * time . Second ) fmt . Println ( "After panic" ) } type Test struct { X int } func NullPointer ( v * Test ) { fmt . Println ( v . X ) } Đoạn code trên dễ dàng gây ra lỗi panic và stop hàm main panic: runtime error: invalid memory address or nil pointer dereference Vậy chúng ta cần làm gì để tránh gây chết hàm main đây Nếu là java hoặc ngôn ngữ khác thì chúng ta đã có try catch finally  Còn trong golang bạn sẽ có một cách trông đẹp mắt hơn func main () { defer func () { if err := recover (); err != nil { logrus . Errorln ( err ) debug . PrintStack () } }() NullPointer ( nil ) time . Sleep ( 2 * time . Second ) fmt . Println ( "After panic" ) } type Test struct { X int } func NullPointer ( v * Test ) { fmt . Println ( v . X ) } Theo cách này thì trong hàm main chúng ta ...

How to use library for stack and queue in golang

package main import ( "container/list" "fmt" ) func main () { stack := list . New () //[] stack . PushBack ( 2 ) //[2] stack . PushBack ( 3 ) //[3,2] stack . PushBack ( 4 ) //[4,3,2] stack . PushBack ( 5 ) //[5,4,3,2] stack . PushFront ( 6 ) //[5,4,3,2,6] e := stack . Front () //-> 6 fmt . Printf ( " %v " , e . Value ) e = stack . Front () fmt . Printf ( " %v " , e . Value ) v := stack . Remove ( stack . Front ()) //remove 6 fmt . Printf ( " %v " , v ) v = stack . Remove ( stack . Front ()) //remove 2 fmt . Printf ( " %v " , v ) v = stack . Remove ( stack . Back ()) //remove 5 fmt . Printf ( " %v " , v ) stack . Len () }