How to recover all current processes on linux [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to recover all running processes using c ++ and under linux. But I don't find lib or corresponding functions.
I can't run a linux command such as "ps".

(I'm guessing that you need to inspect, scan, list, or discover processes; recovering processes is a difficult thing, read about application checkpointing)
You should consider using proc(5), that is the /proc/ filesystem (Linux specific).
The process of pid 1234 is described by the /proc/1234/ directory. Try ls /proc/$$/ in a terminal.
So you can explore processes by using appropriate calls (e.g. opendir(3), readdir(3), closedir(3), stat(2) etc....) on the /proc/ file tree (and this is what ps, top etc.. are using; check with strace(1) ...).
Beware that many files under /proc/ have a 0 size as given by stat(2) but are sequentially readable (a bit like a pipe). Try for example stat /proc/$$/status then cat /proc/$$/status. See also this.

Related

How to implement a file system for operating system development [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am using Xfce Ubuntu and building on GCC 10.2. I want to use FAT filesystem or EXT2 filesystem for my hobbyist operating system. I want a specific instruction and specific code for this question, and I am using C to develop my OS. I I want to list directories, make files, read files, and more in the OS. In addition, one is making an 32 bit Protected OS and I use my own kernel, which is not based on Linux or such. Means that I don't have any additional drivers or such. Please help me: how to can I use a filesystem in C (or C++, but I like C more than it) for my own OS?
How to implement a file system for operating system development
You first need to read a text book about operating systems.
You don't use a file system, you implement yours. For inspiration, look into Ext2, VFAT, XFS. Study their source code inside the Linux kernel source.
Of course, you'll need some compiler, so port GCC (as a cross-compiler) to your OS.
See also OSDEV and Linux From Scratch
Budget several years of full time development efforts.

Nemiver doesn't find file /build/glibc-LK5gWL/glibc-2.23/stdlib/random.c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
When debugging code, when it comes to the rand () function, it asks where the file is. This file is not in libraries at all. What to do?
Your debugger is looking for source code to make itself more useful. In the case of compiled files that are part of glibc, that's not on your system. The path shown is just the path in the original build environment; that's irrelevant. It can use random.c if found elsewhere, but you need to tell it where to look.
The good news is that you can probably install a package to make this work. Which one depends on your operating system; you did not tell us what that is.
However I'd just hit "cancel". You don't need to debug the internals of glibc, unless you're actually trying to find bugs in glibc.

Pros and cons of using a system call vs library based approach to compress a directory [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Scenario:
I am running on an embedded linux distro with a C++ application on it. I need to simply compress a directory and place the zipped output at a certain location. Thats it.
I know that we can tar compress a directory using the following command on a command line interface.
tar cvzf directory.tar.gz /path/to/directory
I see that I have an option to run system commands in C++. Following is a example ot it.
void CompressDirectory() {
std::system("tar cvzf directory.tar.gz /path/to/directory");
}
Environment:
Embedded Linux
Question:
I wish to know the pros and cons of making a system call to tar a directory vs using a library like zlib, zipios etc.
system() is generally frowned upon because of the potential security risks associated with it. In your specific circumstances where everything is explicitly stated, you'd be OK.
You CAN code it in the program to be handled and processed programmatically and not shelling it out and that's the proper way to do it.
So long as your tarball name and directory are both hard coded, I can't think of any security issues, but I'd be careful that you're sure you're not overwriting anything or something similar.
Do not use system() if anybody other than yourself is even able to touch your server.
At least you don't check the result in the current code. You don't know if compressing fails.
You have to check exit code at least.
In general case tar can be not available in command line (on windows, for example).
It's not your case, so you can ignore this point.
Custom library should give more flexible compressing. Like pause, select files, unlimited number of files and so on.

Force program to use executable's directory for I/O instead of active directory non-programmatically [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
If I have an executable: project/bin/exec, which caches data into a file.txt in its active directory, the location where file.txt will be saved depends on how I run the program:
project/file.txt if I run it from project with bin/exec
project/bin/file.txt if I run it from project/bin with ./exec
I'd like the program to always use the directory of the executable and I'd like to find out if there's a non-programatic way of forcing it to do it.
I'm interested in UNIX/Windows, does this even make sense?
Programatically, I know I could get the executable's directory by using boost or std::experimental filesystem, perhaps args[0] argument and use that path for I/O.
Is that the way to do it?
One way of doing this on Unix (part of the question) would be to use a script which would chdir before running executable.
Something like this:
#!/bin/ksh
loc=`dirname $0`
cd $loc
echo $PWD

Including linux command in C++ program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I intend to include a linux command in C++ program. I have been using the system() function to achieve the effect, like to print the current directory I've been using system("pwd"), for system calls and signals realted to 'ls' I used system("strace ls"), but now I want to use the strace command such that the user should type in the command(like ls,mv,who etc.) or process for which he wants to strace without hard-coding.
For shell builtins like cd or ulimit (etc...) it is useless to use them in the argument of system(3) since it will only affect the /bin/sh process started by system.
You need to use the relevant syscall in your C program, e.g. call chdir(2) or setrlimit(2)
It may have some sense to do system("cd /tmp; gzip a*"); for example.
The list of system calls is in syscalls(2). Read also Advanced Linux Programming
BTW, most commands are giving some output (at least if asked by a suitable program argument). You may want to use popen(3) with pclose ...
You can use system() system call for for executing the linux commands. You can even use execl variants for executing the linux commands by replacing the a process address space.