Can someone please tell me interview type questions related to multithreading and GDB.
I already know Deadlock, race condition, synchronization and basics of threads.
Thanks in advance
Some sample questions:
How do you list out all the threads?
How do you set breakpoints in individual threads?
How do you see stacktrace of a particular thread?
Your program is in a deadlock; How do you find the root cause using gdb?
There is no end to questions. I would suggest that the best way to learn is to get knees deep in the dirt and play for yourself:
Make a sample multi-threaded program, debug it and try to find all possible info about all the threads.
Put some deadlock situation, and then debug it.
Related
what is the difference between thread and process, i know you think this is duplicate , but seriously no where and nobody given one exact and to the point answer . I am really tired when somebody says thread are light weight process , even books does not clearly mention it why ?
So I gone through different stackoverflow answer and made a final answer. I feel it is incomplete. Please contribute to this answer ,as it will really help thousand of people who still struggling to understand this puzzle.
My research reached till here so far ....
Linux calls clone API for creation of thread and creation of process , only difference is there are 28 flags which are configured differently for thread and process.
https://stackoverflow.com/a/64942352
I'm not qualified to speak about the implementation of Linux threads. (I've heard that it has been evolving over the past decade or so.) But, the purpose of threads and processes has remained pretty stable:
A thread is an execution path through your program's code. That's pretty much all there is to say about that.
A process is a container for all of the resources needed by an instance of your program; The process has a virtual address space, it has open file handles, it may have sockets, it may have memory mappings, it may own IPC objects, I'm not sure what all else. And, a process has some number of threads. (Always at least one thread, sometimes more.)
The threads in your program do all the work. The process is the place in which they do the work.
Would anyone know a way of explaining or could you direct me to some material regarding single Thread and multiple thread? I don't understand them at all. Every explanation I read is in very complicated English.
I want to understand them completely. Either a really good article / book / website etc where this is explained well would be well appreciated.
Threads are the smallest sequences of programmed instructions that
can be managed independently.
Multithreading uses multiple threads of the same process to reduce algorithm runtime. Threads of the same process run in the same memory space.
How can I get stacktraces across all threads of an already running process, on Linux x64, in a way that is the least invasive and impacting as possible?
Things I've thought of till now:
gdb - I'm afraid it would slow the process too much, and for too long;
strace+ - no idea what performance it has, any experience anybody? still, IIUC, it traces only syscalls, and I can't even expect each thread enters a syscall, specifically some threads may be already hanging;
force crash & get a coredump - yeah... if I could do that easily, I would probably already be busy debugging... please, let's assume there's no elephant in the room, for the purpose of this question, ok?... pretty please...
There's a gcore utility that comes with gdb. You don't need to force a crash to get a core dump.
That's exactly what pstack does. See http://www.linuxcommand.org/man_pages/pstack1.html
I use Qt and C++. I have a list of threads (Qlist<QtThread>).
I try to synchronize them. All threads calculate some values. And I want to take them.
Have you any ideas? thank a lot
You can use the finished() signal of a thread to execute a slot when your thread finishes executing.
Your question is a bit broad, but Qt has a lot of documentation on different thread synchronization methods.
The base of the docs is at Threading Support in Qt. The specific part you should find the info in is the Synchronizing Threads section. It lists the various mutex, locks, semaphores, wait conditions that are available in the Qt framework.
The documentation for all these classes has example usage code. Also have a look at the Threading and Concurrent Programming Examples, you'll probably find what you're after in there.
What are the possible ways to debug deadlocking threads in a MT program, other than gdb?
On some platforms deadlock detection tools may help you find already observed and not yet observed deadlocks, as well as other bugs.
On Solaris, try LockLint.
On Linux, try Helgrind or DRD.
If you're using POSIX, try investigating PTHREAD_MUTEX_ERRORCHECK.
I've always invested some time into writing or grafting on a flexible logging facility into projects I've worked on, and it always paid off handsomely in turning difficult bugs into easy ones. At the very least, wrapping locking primitives in functions or methods that log before and after logging, and display the object being locked and the thread that's doing the locking always helped me to zero in on the offending thread in a matter of minutes - assuming that the problem can be reproduced at all, of course.
Loading the program under a debugger is actually a pretty limited method of determining what happened once a process deadlocks, since all it can give you is a snapshot of how badly you messed up, rather than a step by step explanation of how you screwed up, which I find a lot more helpful.
Or get the Intel Thread Checker. Fine work.