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
Related
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).
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.
Is there a way to know, at a real time, what threads are opened and what application opened them?
You can look in /proc/<PID>/task/ (where <PID> is a process-ID) which will have a number of subdirectories, each with the name equal to the thread-ID of one of the threads in that task.
Note that this is only sort-of real-time though -- unless you were to "freeze" the entire system for the duration, the information you get can always be stale, because a process may create or destroy threads even as you're looking at the data.
In modern Linuxes, threads are very much like processes. Each thread has an LWP ("light-weight process") identifier, which is internally implemented as PID. However, if such "light-weight process" (i.e. thread) is queried for a PID, the system yields the PID of the process that spawned the thread (instead of LWP). Note also, that if the process has only one thread, it's LWP will be equal to its PID.
ps is capable to processing threads with -L modifier, as ereOn described in his answer. But I should note that ps is not just for manual invocation. It has capabilities to print output in such a way that's easy to parse by another program.
The following command will print LWPs (-o lwp=) and PIDs (-o pid=) of all (-A) threads (-L) in the system. Each string represents one thread, second column being the process that spawned it:
$ ps -A -L -o lwp= -o pid=
...
27747 27747
27749 27749
27750 27750
27751 27750
27752 27750
27755 27750
27756 27750
27772 27772
27858 27858
30457 30457
30886 30886
Quite easy to parse with C or C++, isn't it? To actually read this from your program, you can use popen or one of its C++ equivalents.
Note that using ps is not only easier than reading /proc. It's also much safer than manually parsing /proc filesystem. ps is a POSIX command*, it's guaranteed to work; it does use /proc under Linux, but that's internal details. When underlying infrastructure changes, ps will be rewritten, and will keep working, while your code, if written based on /proc, will break.
*To be honest, POSIX does not specify -L switch. But in any Linux, which has GNU toolchain, it will be available.
I guess ps -L should do the trick.
Here is psdocumentation.
Not sure if you ask how to do that programmaticaly, but in this case, since ps is open-source, so you can probably take a look at the sources.
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.
I am writing a shell where I need to launch several child processes at once and record the system time and user time.
So far I am able to do it. The only problem is that I am using wait4 to grab the system resources used by the child program and put it in my rusage structure called usage.
How can I launch all the processes at the same time and keep track of the user and system times? I can remove the wait4() system call and use it outside to loop so I can make the parent wait, but if I do that then I can only record the times for the last process and not all of them.
Do you have any idea how I can fix this?
execute(commandPipev,"STANDARD",0);
wait4(pid,&status,0,&usage);
printf("Child process: %s\t PID:%d\n", commandPipev[0], pid);
printf("System time: %ld.%06ld sec\n",usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
printf("User time: %ld.%06ld sec\n\n",usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
A convoluted answer.
In a POSIX environment, launch the children, then use waitid() with the WNOWAIT option to tell you that some child has exited. The option leaves the child in a waitable state - that is, you can use another wait-family call to garner the information you need. You can then use the non-POSIX wait4() system call to garner the usage information for the just exited child, and deal with the accounting you need to do. Note that you might find a different process has terminated between the waitid() and wait4() calls; you need to use a loop and appropriate flags and tests to collect all the available corpses (dead child processes) before going back to the waitid() call to find out about the other previously incomplete child processes. You also have to worry about any of the wait-family of functions returning the information for a process that was previously started in the background and has now finished.
The Linux man page for wait4(2) suggests that WNOWAIT might work directly with wait4(2), so you may be able to do it all more cleanly - if, indeed, you need the option at all.
Consider whether you can use process groups to group the child processes together, to make waiting for the members of the process group easier.