Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

'ps' and 'df' functionality in a C program

andym297andym297 Member Posts: 3
Just wondering if anyone knows how to check if a process is running on Unix directly from a C program, and similarly, if its possible to establish the available and used space on a logical volume. I'm currently using an approach along these lines...

sprintf( SysCom, "ps -e|grep %s > tmpfile", ProcName );
system( SysCom );

stat( "tmpfile", &TmpFile );

if( TmpFile.st_size > 0 ).....

For the disk capacity, I pass a df -k command, redirected to a temp file, then read the token of the output which relates to the disk capacity back into a variable.

Making the program dependent on the OS like this is rather weak though, and I'm keen to find a more direct method if anyone knows of one.

Thanks in advance.

Andy

Comments

  • vasialekvasialek Member Posts: 56
    : Just wondering if anyone knows how to check if a process is running on Unix directly from a C program, and similarly, if its possible to establish the available and used space on a logical volume. I'm currently using an approach along these lines...

    Indeed, interesting theme... AFAIK, every process, that is created is listed in /proc (Linux). So, logically thinking, u could searc ALL info from /proc/PROGRAM_PID, until u find name of your program :)

    Better way to go www.gnu.org & try to study code of ps & df!

    Good luck!

    Lekha

  • andym297andym297 Member Posts: 3
    : : Just wondering if anyone knows how to check if a process is running on Unix directly from a C program, and similarly, if its possible to establish the available and used space on a logical volume. I'm currently using an approach along these lines...
    :
    : Indeed, interesting theme... AFAIK, every process, that is created is listed in /proc (Linux). So, logically thinking, u could searc ALL info from /proc/PROGRAM_PID, until u find name of your program :)
    :
    : Better way to go www.gnu.org & try to study code of ps & df!
    :
    : Good luck!
    :
    : Lekha
    :
    :

    Thanks for the advice Lekha.

    I had a look, and basically you can use the following...

    FILE *psoutput;

    psoutput = popen( "ps -e", "r" );...

    and then access the results of ps directly.

    This is recommended because otherwise you'd have to setuid to root to read the kmem area, which is risky. Thanks for the pointer...

  • Shawn CarterShawn Carter Member Posts: 0

    ___ / http://forcoder.org / free ebooks and video tutorials about // Assembly, C, Objective-C, Delphi, C++, Python, PL/SQL, R, JavaScript, Swift, Ruby, Perl, Scratch, Visual Basic, PHP, C#, Java, Visual Basic .NET, Go, MATLAB Ada, Fortran, Scheme, Dart, F#, Lisp, Awk, FoxPro, Logo, Scala, Julia, Alice, COBOL, Apex, Prolog, VBScript, LabVIEW, Bash, Erlang, Clojure, Rust, ABAP, SAS, Lua, D, Crystal, Hack, Kotlin, Transact-SQL, ML // ___

Sign In or Register to comment.