View Full Version : Question About C and System Stuff
noob_Lance
January 1st, 2006, 08:45 PM
Hey... i was wondering if there was a way i could use C to read all the processes using the terminal command ps into a file? there is a purpose to this... so please dont tell me to jsut open up the terinal and type ps..
Thanks
~Lance
toojays
January 1st, 2006, 08:58 PM
It is surely possible, but you'll have to see how ps works, and then take the code you need from there.
noob_Lance
January 1st, 2006, 09:00 PM
hm... lol i forgot for a second.. that linux is open source lol.. :| dumb me lol thx
majikstreet
January 1st, 2006, 10:28 PM
you could use bash and do ps > whatever
#!/bin/sh
ps > whatever
i know thats not what you wanted... but..
noob_Lance
January 1st, 2006, 10:50 PM
yea thats not what i wanted lol... i want to make a quick app to kill any process i want. start it up... reads the file and gives me a selection, hit in a number and its killed.. kinda pointless i kno... but hell it gives me something to do to brush up on my programming for school lol
*edit
actually.. that would have work for me lol... if i knew how to exec a terminal and a command using C then it would be complete... anyone got any ideas?
briancurtin
January 2nd, 2006, 01:25 AM
are you set on doing this in C? if not, look into python, as it is pretty simple to execute system commands like this, and wouldnt take all that much code to do what you are trying to do.
i might look into doing this same thing myself actually. im getting ready for the 2nd semester to start up and im brushing up on stuff just like you are
noob_Lance
January 2nd, 2006, 01:31 AM
i have used python before.... any clue at how to go about this in python??
ekarni
January 2nd, 2006, 01:53 AM
IIRC the C function is just
system("ps > foo.txt");
Then you could parse foo.txt and generate your menu.
briancurtin
January 2nd, 2006, 03:21 AM
i have used python before.... any clue at how to go about this in python??
for starters: you will have to "import os" and then "os.system('ps -ef > /home/brian/Desktop/ps.out')" will write the output of ps (i gave it the ef flag for the hell of it) to my desktop
then read the file in and give each line an id/key/etc and ask the user which one they want to kill, like what ekarni said
coredump
January 2nd, 2006, 10:12 AM
There's another way to do it, apart from redirecting 'ps' output into a file, or using 'popen' to read the output, or reading the (probably obscure) source code to 'ps'. In modern Linux systems, you can read a directory called /proc to find out all sorts of useful info about the machine. Try:
cat /proc/cpuinfo
to see what the kernel knows about your CPU. /proc contains both files and directories and each one corresponds to a Linux kernel object. Some of the directories are numbered and correspond to processes. Some files are named to correspond to kernel data structures (like the cpuinfo example). So, you can build a ps-like program by reading the contents of /proc as required.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.