I tried looking for a thread on this subject, however couldn't find one. So posting this question.
Assume, I have created couple of threads in C++ in Linux and the code is running.
Now I would like to monitor the process and the threads of the process using a shell script and do some additional processing.
Also I would need the amount of CPU and Memory being used by each thread. I know that a thread is associated to a process, however my requirement is to identify the resources utilized by this thread.
I couldn't find the exact way to identify the threads associated to a process. I tried using PS however I couldn't find any clues. Running RHEL.
From a man page of ps:
To get info about threads:
ps -eLf
ps axms
The manpage for ps might give you more clues.
In particular, it should tell you that -L shows threads, and -o %cpu,%mem will display the amount of CPU and memory being used.
Note that memory is associated with the process, not with any thread, so there is no concept of "memory used by each thread".
Apart from using Linux commands you can use Generic Memory Manager library. Here it defined ThreadingModel class.
Related
I have a source code that is using some threads.
I want to see how many percent cpu and percent memory are used each threads.
So I used "htop" command. (I am using ubuntu.)
There are "PID" columns which is Process/thread ID.
I googled how to get thread id.
https://en.cppreference.com/w/cpp/thread/thread/id/hash
How to convert std::thread::id to string in c++?
But, somehow the thread ids I got from source code are not matched with PID values, one of htop command's output.
Any better idea or help would be great, thank you.
PID is Process ID. Run top -H
man 1 top
-H :Threads-mode operation
Instructs top to display individual threads. Without this
command-line option a summation of all threads in each
process is shown. Later this can be changed with the `H'
interactive command
Upon Running strace on a Java Application I notice some long time the syscall(mostly futex).
futex(0x7f8578001fd4, FUTEX_WAIT_PRIVATE, 1311, NULL) = 0 <15.082094>
I really want to understand the wait on futex is for which shared resources over here.
But, I'm not sure how?
I did some googling and found GDB can be helpful for finding the above cause. But unfortunately, I'm not much aware of GDB as I had barely used it before.
Can some help me understand how to find the answer that I'm looking at.
The futex operation is waiting for another thread to release a lock. You should first look at Java-aware tools to see if this is a high-level Java lock. Perhaps even sending SIGQUIT (by pressing Ctrl+\ is sufficient) for that.
I am trying to run multiple command in ubuntu using c++ code at the same time.
I used system() call to run multiple command but the problem with system() call is it invoke only one command at a time and rest commands are in waiting.
below I wrote my sample code, may this help you to get what I am trying to do.
major thing is I want to run all these command at a time not one by one. Please help me.
Thanks in advance.
main()
{
string command[3];
command[0]= "ls -l";
command[1]="ls";
command[2]="cat main.cpp";
for(int i=0;i<3;i++){
system(command[i].c_str());
}
}
You should read Advanced Linux Programming (a bit old, but freely available). You probably want (in the traditional way, like most shells do):
perhaps catch SIGCHLD (set the signal handler before fork, see signal(7) & signal-safety(7)...)
call fork(2) to create a new process. Be sure to check all three cases (failure with a negative returned pid_t, child with a 0 pid_t, parent with a positive pid_t). If you want to communicate with that process, use pipe(2) (read about pipe(7)...) before the fork.
in the child process, close some useless file descriptors, then run some exec function (or the underlying execve(2)) to run the needed program (e.g. /bin/ls)
call (in the parent, perhaps after having got a SIGCHLD) wait(2) or waitpid(2) or related functions.
This is very usual. Several chapters of Advanced Linux Programming are explaining it better.
There is no need to use threads in your case.
However, notice that the role of ls and cat could be accomplished with various system calls (listed in syscalls(2)...), notably read(2) & stat(2). You might not even need to run other processes. See also opendir(3) & readdir(3)
Perhaps (notably if you communicate with several processes thru several pipe(7)-s) you might want to have some event loop using poll(2) (or the older select(2)). Some libraries provide an event loop (notably all GUI widget libraries).
You have a few options (as always):
Use threads (C++ standard library implementation is good) to spawn multiple threads which each perform a system call then terminate. join on the thread list to wait for them all to terminate.
Use the *NIX fork command to spawn a new process, then within each child process use exec to execute the desired command (see here for an example of "getting the right string to the right child"). Parent process can use waitpid to determine when all children have finished running, in order to move on with the program.
Append "&" to each of your commands, which'll tell the shell to run each one in the background (specifically, system will start the process in the background then return, without waiting for the result). Not tried this, don't know if it'll work. You can't then wait for the call to terminate though (thanks PSkocik).
Just pointing out - if you run those 3 specific commands at the same time, you're unlikely to be able to read the output as they'll all print text to the terminal at the same time.
If you do require reading the output from within the program (though not mentioned in your question), this is relevant (although it doesn't use system).
There must be a function that gets the current status of a thread in the system because
there is this application: http://www.softwareverify.com/cpp/thread-status-monitor/index.html
It must be using some sort of API function or something...
How can I get a thread state myself in C++/Windows?
thanks :)
(this is my last question for today. I promise :))
That's done via Toolhelp library, check information at MSDN : http://msdn.microsoft.com/en-us/library/ms686780%28v=VS.85%29.aspx
You can use the following examples to get the running processes and, when you have a process ID, the threads.
Taking a Snapshot and Viewing Processes
Traversing the Thread List
EDIT: After getting the handle to the thread(s) you are interested in you can call GetExitCodeThread but that will only tell you if the thread is STILL_ACTIVE until it ends, when you can find the exit code.
While searching for the additional information for your comment I also came across this thread on SO which might be of interest to you.
You get the most bang out of WMI, Win32_Thread class. The linked article has a link to the C++ code you need. Experiment with the WMI Code Creator tool.
In C++, I have a resource that is tied to a pid. Sometimes the process associated with that pid exits abnormally and leaks the resource.
Therefore, I'm thinking of putting the pid in the file that records the resource as being in use. Then when I go to get a resource, if I see an item as registered as being in use, I would search to see whether a process matching the pid is currently running, and if not, clean up the leaked resource.
I realize there is a very small probability that a new unrealated pid is now sharing the same number, but this is better than leaking with no clean up I have now.
Alternatively, perhaps there is a better solution for this, if so, please suggest, otherwise, I'll pursue the pid recording.
Further details: The resource is a port number for communication between a client and a server over tcp. Only one instance of the client may use a given port number on a machine. The port numbers are taken from a range of available port numbers to use. While the client is running, it notes the port number it is using in a special file on disk and then cleans this entry up on exit. For abnormal exit, this does not always get cleaned up and the port number is left annotated as being in use, when it is no longer being used.
To check for existence of process with a given id, use kill(pid,0) (I assume you are on POSIX system). See man 2 kill for details.
Also, you can use waitpid call to be notified when the process finishes.
I would recommend you use some kind of OS resource, not a PID. Mutexes, semaphores, delete-on-close files. All of these are cleaned up by the OS when a process exits.
On Windows, I would recommend a named mutex.
On Linux, I would recommend using flock on a file.
How about a master process that starts your process (the one which terminates abnormally) waits for your process to crash (waitpid) and spawns it again when waitpid returns.
while(1) {
fork exec
waitpid
}
The problem domain isn't clear, unfortunately, you could try re-explaining it in some other way.
But if I understand you correctly, you could create a map like
std::map< ProcessId, boost::shared_ptr<Resource> > map;
// `Resource` here references to some abstract resource type
// and `ProcessId` on Windows system would be basically a DWORD
and in this case you simply have to list every running process (this can be done via EnumProcesses call on Windows) and remove every entry with inappropriate id from your map. After doing this you would have only valid process-resource pairs left. This action can be repeated every YY seconds depending on your needs.
Note that in this case removing an item from your map would basically call the corresponding destructor (because, if your resource is not being used in your code somewhere else, it's reference count would drop to zero).
The API that achieves that on windows are OpenProcess which takes process ID as input, and GetExitCodeProcess which returns STILL_ACTIVE when the process is, well, still active. You could also use any Wait function with zero timeout, but this API seems somewhat cleaner.
As other answers note, however, this doesn't seem a promising road to take. We might be able to give more focused advice if you provide more scenario details. What is your platform? What is the leaked resource exactly? Do you have access to the leaking app code? Can you wrap it in a high-level try-catch with some cleanup? If not, maybe wait on the leaker to finish with a dedicated thread (or dedicated process altogether)? Any detail you provide might help.