Good day,
I was wondering if there is a way to do direct system call capture with C or C++?
I know that currently on unix systems you can use SystemTap to do system capture. The problem I'm having is that in order to feed them into another program for analysis I have to pipe them to the other program.
I'd like to pass things along programatically as this is easier than "printing" out into the pipe and then reading in with the other program.
Is there a way of doing this? How difficult would it be?
strace does exactly that.
You can take a look at its source code to see how its done.
Related
I want to make a system were I can run a make file and several other gcc related things within a program, basically to use gcc and compile stuff within the program. If I wrote up all the stuff I want to do to a batch file then I'd need to run that batch file from within the program.
Now everyone says System() calls are extremely bad because of security and various other things. So considering I am using c++ what would be a good alternative to System() to run batch files. If preferable I would like the alternative to cross platform.
Thanks
You could look to use the fork and execl family of calls although these are tied down to Unix / Linux and, depending on how you use them, are arguably no safer than system.
I doubt very much that you'll find a common, cross platform way of doing this if only because all platforms will have different and unique ways of doing this. Also, the scripts you're trying to run will no doubt have to be different on different platforms and there may be different ways of specifying things such as directory paths etc.
My suggestion would be to first ask yourself how you'll take the following questions - which would be my main concerns:
How am I going to prevent accidental / intentional misuse?
How am I going to detect errors or success status within the scripts I'm running?
How am I going to provide for dependencies? E.g. script A must run completely and correctly before script B runs.
How am I going to report the success and failure state.
My final question would be why do you want to do this in C++? Is there a specific reason? Naturally I'm a C++ evangelist although I would have thought this would be better tackled by a scripting language such as Perl, Python or possibly Bash unless you're embarking on something far more radical.
Is there a way to use the unix 'time' command in C++ and store each of its outputs in a variable?
EDIT: If there isn't a way, then what about calling time in a bash script and storing the returned values some way?
Thank you!
Supposing you are on UNIX, then the C++ standard std::system function will behave as defined in POSIX, that is, execute a command as with sh. Before doing this, you can connect your own stdin and stdout to a local pipe by first using dup to create aliases of STDIN_FILENO and STDOUT_FILENO, then close the aforementioned file descriptors, then pipe to open a pipe on the newly freed descriptors.
Then you can interact with std::cin and std::cout. Well, it would be a good idea to flush the C++ interface before beginning.
This isn't all really a good idea, though. It should be simpler to use the POSIX C interface to get the relevant data directly.
If you are ok with using Boost libraries, then this should do it http://www.boost.org/doc/libs/1_49_0/doc/html/date_time/examples.html#date_time.examples.seconds_since_epoch
the good part is it also handles time zones conversions :)
You can use system(3) or popen(3), but there's almost always a better, more portable, and native solution, depending on what exactly you're looking for. In your specific case it seems like what you're really looking for a combination of getrusage(2) and ftime(3).
I know many have asked this question before, but as far as I can see, there's no clear answer that helps C++ beginners. So, here's my question (or request if you like),
Say I'm writing a C++ code using Xcode or any text editor, and I want to use some of the tools provided in another C++ program. For instance, an executable. So, how can I call that executable file in my code?
Also, can I exploit other functions/objects/classes provided in a C++ program and use them in my C++ code via this calling technique? Or is it just executables that I can call?
I hope someone could provide a clear answer that beginners can absorb.. :p
So, how can I call that executable file in my code?
The easiest way is to use system(). For example, if the executable is called tool, then:
system( "tool" );
However, there are a lot of caveats with this technique. This call just asks the operating system to do something, but each operating system can understand or answer the same command differently.
For example:
system( "pause" );
...will work in Windows, stopping the exectuion, but not in other operating systems. Also, the rules regarding spaces inside the path to the file are different. Finally, even the separator bar can be different ('\' for windows only).
And can I also exploit other functions/objects/classes... from a c++
and use them in my c++ code via this calling technique?
Not really. If you want to use clases or functions created by others, you will have to get the source code for them and compile them with your program. This is probably one of the easiest ways to do it, provided that source code is small enough.
Many times, people creates libraries, which are collections of useful classes and/or functions. If the library is distributed in binary form, then you'll need the dll file (or equivalent for other OS's), and a header file describing the classes and functions provided y the library. This is a rich source of frustration for C++ programmers, since even libraries created with different compilers in the same operating system are potentially incompatible. That's why many times libraries are distributed in source code form, with a list of instructions (a makefile or even worse) to obtain a binary version in a single file, and a header file, as described before.
This is because the C++ standard does not the low level stuff that happens inside a compiler. There are lots of implementation details that were freely left for compiler vendors to do as they wanted, possibly trying to achieve better performance. This unfortunately means that it is difficult to distribute a simple library.
You can call another program easily - this will start an entirely separate copy of the program. See the system() or exec() family of calls.
This is common in unix where there are lots of small programs which take an input stream of text, do something and write the output to the next program. Using these you could sort or search a set of data without having to write any more code.
On windows it's easy to start the default application for a file automatically, so you could write a pdf file and start the default app for viewing a PDF. What is harder on Windows is to control a separate giu program - unless the program has deliberately written to allow remote control (eg with com/ole on windows) then you can't control anything the user does in that program.
I have to write a C/C++ program to process a bunch of text files (around 100) and find a pattern (commonly a string). Since the platform I am going to run this will be unix, I thought why wouldn't I make use of the grep system command within my program as it is very fast and effective. But, my friend says using system("grep...") within a program is not advisable. He suggests me to use string pattern matching algorithm which I feel will slow down the program.
So, I want some advice over this. Help me out.
Without knowing what your program is going to do, it's hard to say. But running commands via system() will slow your program, down considerably, though this may not be important. Whatever you do, don't write your own string-matching code if regular expressions can solve the problem - use one of the many existing regex libraries. And if most of your problem could be solved using grep, consider writing a shell script, or using a scripting language like Python instead of a C++ program.
Your two major alternatives are (a) to use grep, or (b) to use a library, linked to your C or C++ program, which provides regular expressions.
Using grep means you get your program running very soon, because you don't have much to learn. Using a regular expression library means your program runs faster.
How much faster? The major speed increase is because you're not setting up a new process and running a new program for each of those 100 files. How significant is this speed saving?
The answer depends on how large each of those files is. If they're very large, it won't make much speed difference which method you use. If small, it will.
If you decide to go with a regular expression library, my guess is that they're all about the same speed. I chose something I was familiar with, since I know Perl: the Perl compatible regular expression library.
make forking and using exec family of command use grep and save its result in a file.
in main wait for process to end.
then in main open the file and use the result.
I want to create a file that only resides in memory... In looking through some documentation I saw a recommendation to use a shell extension as a virtual file. Im not sure that is a workable solution but I would like to know
Is it a good approach (or should I be using a ramdisk instead)
Where is a good place to start to read up on it
Note: This is on the Windows platform
As I understand, you want your program to create a "file", which resides only in memory and that you can pass on to another external program (say, Microsoft Word).
AFAIK this is not possible, short of a ramdrive. I'd suggest using a temporary folder. You will however have to come up with a strategy for deleting the file when it's not needed anymore.
Added: On second though, you might want to check out Reparse points. I'm not familiar with them myself, and they will only work for NTFS formatted disks, but perhaps they can provide you with what you want. It will be a lot of coding though.
You don't say on which plateform you are but I'm guessing Windows. Is mmap() available? If not, I think BerkeleyDB has been ported to Windows so you should be able to use that. Win32 API may have something akin to mmap() but I don't know it.
If you want a file that resides only in memory, use a named pipe or something, though I question your scenario - can you go up a level and describe what you want to do?