i used to get a list of processs under linux by enumerating the /proc file system, since it had plain-text files that i can read data from (stat, status, exe link....) but thats not the case on solaris, i tried porting my tools to Oracle Solaris 11 (my first solaris) but it wont work, i tried accessing the /proc folder manually, but couldn't find anything readable, but ps -fu user works !
is it possible that someone can point me on how to get a list of processes uneder solaris?
im coding in gcc btw.
thanks.
Unlike Linux, Solaris /proc is providing binary data, not text one.
Solaris has an extensive and detailed manual page proc(4) describing what the different files under a process number hierarchy contain, how to access them and what structures to use in order to get their content.
This manual page is of course also accessible locally with man -s 4 proc
Similar to this: How to get process info programmatically in C/C++ from a Solaris system?
You want the interface described by /usr/include/procfs.h and /usr/include/sys/procfs.h to decode the binary data in /proc
Related
Is there a way to execute a file while i'm in cd in the kernel. I am using Unix based systems (Arch, Manjaro, MacOSX, etc). I am trying to get htop for macOSX Big Sur, and I wrote cd /Users/peter/Downloads/htop-master . What should I do to execute the file (or is there even a way to execute it) now that i am into the file? Also, I am a beginner in the unix and linux space, so I'm not the brightest out there, so any advice helps. Thank you!
First of all, make sure the file is executable, you can run:
$ chmod +x $FILE
To make it executable, where $FILE in your case is /Users/peter/Downloads/htop-master, you might need sudo privileges to run the command. After that, you run htop:
$ /Users/peter/Downloads/htop-master
If your file is executable (ie a script or binary file) it should work. However, in your question you mentioned executing a directory, while all directories are executable, it doesn't mean you can run them as programs, it means you can cd into them.
If you downloaded the source code for htop, you'd first need to build it (compile it). That process varies from program to program, but generally, there is a Makefile that handles that for you. You'd need to check the process and requirements for htop, you should be able to find that from the website where you downloaded the program.
While I don't use MacOS, I believe there is a package manager called brew that can install htop for you. A package manager is a terminal program that installs programs from trusted sources automatically, it also handles dependencies and updates for you. I highly recommend you check it out.
Finally, I'd like to clarify some terms you seem to have confused:
Kernel: The kernel is the core of the operating system, its job is to comunicate software and hardware, you don't normally interact with it, unless you are developing drivers or code that interacts in some lower level way with the hardware.
Terminal: This is what you probably meant by kernel, it is commonly a Terminal Emulator, a program that lets you use a terminal from within a graphic environment. It is usually a black box with white text (or viceversa). The name comes from the days in which computers had to be shared, every user had a terminal, a dumb box that allowed the user to interact with the computer.
Folder: While this term is completely correct, when using terminal programs, folders are more commonly referred to as Directories. Directory was the original term, but once graphical environments took off, the name Folder became more popular.
UNIX-like systems may seem complicated at first, but once you get the hang of it you'll realize they are way easier than Windows systems, welcome aboard!
I am trying to build a bash like script provides some functionalities such as ls,pwd,cat etc. working on NTFS in a linux system. Suppose that I have an NTFS image and I open that as a file with fopen. Then, I read some sectors such as BPB residing at 0x0B and fetched some general info about the NTFS image. I need to reach to the root directory pointer then traverse through the filesystem in order to implement those functions especially for ls and pwd. I google'd a lot about internal details and offsets of NTFS but I could not find out how to achieve the goal. I can not progress further without understandable documentation or samples.
Any help, documentation, hint, offset table etc. would be highly appreciated.
Thank you.
I'm guessing this is a learning exercise. So, first:
Writing a bashlike interpreter for a specific filesystem is the wrong thing to do. You should be concentrating on understanding the details of the NTFS filesystem instead.
Writing ls, cat to be able to work with files in a specific filesystem is the wrong thing to do. You should be concentrating on understanding the details of the NTFS filesystem instead.
If you write a filesystem driver (say using FUSE), then the original bash, ls, cat will automatically work with that filesystem. Because the driver will be able to translate system calls like open and read into the filesystem specific procedure.
Finally:
Learn about FUSE. It is awesome. See this Hello World FUSE module. Run it, play with it.
Download the sources for NTFS-3G, which is the NTFS driver used by most GNU/Linux distros these days. It uses FUSE. Learn how it works.
I want to know, how can we find user's process statistics about resource utilization( like CPU, Memory) using c program and without using any user command tool. Currently I am running ubuntu 10.10.
Thanks
The canonical way these days is to parse the information in the /proc virtual filesystem procfs. It contains textual information on nearly all aspects of the system, including detailed per-process statistics. The information is structured, and is intended for ease of parsing and programmatic access. (This is how tools such as ps work.)
For example, to query the I/O metrics of a given process, you would read the file under /proc/<pid>/io. This contains a series of name: value pairs, like so:
rchar: 14823550
wchar: 138670414
syscr: 11549
syscw: 3013
read_bytes: 483328
write_bytes: 8192
cancelled_write_bytes: 0
For detailed information, see:
"The /proc Filesystem" - kernel reference documentation
Is there a way to write a C code that allow us to determine if a previous instance of an application is already running? I need to check this in a portable way for Linux and Windows, both using the last version of GCC avaiable.
Any examples of portable code would be of enormous help. I see two options now:
Check process list. Here linux has good tools, but I don't think the same functions apply to windows. Maybe some gnu libraries for both SO? What libraries, or functions?
Save and lock a file. Now, how to do that in a way that both systems can understand? One problem is where to save the file? Path trees are different from each systems. Also, if a relative path is chosen, two applications can still run with different locked files in different directories.
Thanks!
Beco.
PS. The SO have different requisites, so if you know one and not another, please answer. After all, if there is no portable "single" way, I still may be able to use #ifdef and the codes proposed as answer.
C language (not c++), console application, gcc, linux and windows
Unfortunately, if you limit yourself to C, you may have difficulty doing this portably. With C++, there's boost interprocess's named_mutex, but on C, you will have to either:
UNIXes (including Mac OS): Open and flock a file somewhere. Traditionally you will also write your current PID into this file. NOTE: This may not be safe on NFS; but your options are extremely limited there anyway. On Linux you can use a /dev/shm path if you want to ensure it's local and safe to lock.
Windows: Open and lock a named mutex
for windows, a mutex works well.
http://msdn.microsoft.com/en-us/library/ms682411(v=vs.85).aspx
the article also mentions an alternative to a mutex....
To limit your application to one instance per user, create a locked file in the user's profile directory.
The sort of canonical method in Unixland is to have the process write its own PID to a file in a known location. If this file exists, then the program can check its own pid (available by system call) with the one in that file, and if it's unfamiliar you know that another process is running.
C does not give in-built facilities to check if an application is already running, so, making it cross platform is difficult/impossible. However, on Linux, one can use IPC. And, on Windows (I'm not very experienced in this category), you may find this helpful.
I did an application that create a partintion and format the disk using system calls...
In the middle of the process there is a query asking to type the size of the disk... What can i do in my application in order to automaticly answer that query??
can you please help me?
This is certainly possible with, for instance libexpect but I never tried it (but Google found what seems to be a good example). On my Debian machine, man libexpect says:
libexpect - programmed dialogue library with interactive programs
This library contains functions that allow Expect to be used as a Tcl
extension or to be used directly from C or C++ (without Tcl).
Depending on your operating system (windows can do it for instance) you can have the stdin for the programmed redirected to come from an output of your program.
Maybe you can use system() do run utilities like expect to control the process
fortunately, There is nothing you can do. :)