Execute a C program and retrieve information


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute a C program and retrieve information
# 1  
Old 12-23-2010
Execute a C program and retrieve information

Hi
I have the following script:
#!/bin/sh
gcc -o program program.c
./program &
PID=$!

where i execute a C program and i get its pid. I want to retrieve information about this program (e.g memory consumption) using command top.
So far i have: top -d 1.0 -p $PID
But i dont know how to stop top. Commands after 'top' never execute.
# 2  
Old 12-23-2010
You may be better using ps Depending on your flavour & version of operating system, ps can have many variations, for instance some operating systems allow you to:
Code:
ps -lp $pid

The memory usage (size) is about the 9th or 10th field.


If I have missed the point, do write back and we'll see if someone can answer.





Robin
Liverpool/Blackburn
UK
# 3  
Old 12-23-2010
Thanks for your reply,
I want to retrieve information for the process every 1 sec and i am not sure if tha's possible with ps, also the script is for a project where i am asked to use top. So i need to use top Smilie
# 4  
Old 12-30-2010
Well a simple loop should suffice. Following your code:-
Code:
flag=0
while [ $flag -eq 0 ]
do
   ps -lp $PID | grep -v PID
   flag=$?
   sleep 1
done

The grep -v PID removes the headings. You can either count your way along to the correct field, or have a look at man ps and decide which fields you want and just output them with the -o flag, e.g.
Code:
   ps -p $pid -o vsz=             # No grep -v PID bit required

should give you the vitual memory size without headings for the process specified.




I hope that this helps, but do write back if it does not. Smilie





Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 12-30-2010
Hi
That seems to work great and that's what i want to do but as i said in a previous reply i need to use command top.
# 6  
Old 12-30-2010
There is much variation in "top" "ps" etc.. .
Quote:
top -d 1.0 -p $PID
Not clear what Operating System you have or what information you require.
Assuming you are on some sort of Linux I would expect the syntax to be:
Code:
top -d 1 -p $PID

As others suggest, "ps" is probably better.
# 7  
Old 12-30-2010
I am on ubuntu 10.10 and i want to know the memory consumption of my program (program.c). I understand that ps is the best choice and i made it work ( #4 reply) but i wonder if it is possible to use command top to do the same thing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Program Does not execute

Hi there, When I am trying to execute any shell script. The shell script only execute line 1 and I notice that the rest o the program was not executed. Please advise. # bash +x vmscript-4.sh Even when I enter this command there is not output. sudo su cd /tmp cp... (2 Replies)
Discussion started by: alvinoo
2 Replies

2. Solaris

Solaris commands to retrieve chipset information

I need to know what are the commands in Solaris to retrieve the below information about the hardware platform. 1. Chipset information (information about various hardware controller cards on the mother boards, system BIOS versions, PCI firmware version etc..) 2. Serial number of the work... (2 Replies)
Discussion started by: rajujayanthy
2 Replies

3. Ubuntu

I can't execute a C++ program, help!

My professor gave me a code with no errors. When I compile it's fine, it doesn't show any errors, but when I try to execute it shows this: line 3: syntax error near unexpected token '(' line 3: 'int main()' I searched through the Internet but I couldn't find any solution. Please!!!... (1 Reply)
Discussion started by: rosiiieee
1 Replies

4. Shell Programming and Scripting

Match and retrieve information from file

Hello I just want to ask how to get the match of information column 2 file 1 and retrieve information from column 2 file 2. The column exon in file 1 and column color code in file 2. File 1 //NODECOLORCODE "Exon 1" "ENST00000595813" //NODECOLORCODE "Exon 1" ... (4 Replies)
Discussion started by: Wan Fahmi
4 Replies

5. Shell Programming and Scripting

Retrieve information Text/Word from HTML code using awk/sed

awk/sed newbie here. I have a HTML file and from that file and I would like to retrieve a text word. <font face=arial size=-1><li><a href=/value_for_clients/Tokyo/abc_process.txt>abc</a> NDK Version: 4.0 </li> <font face=arial size=-1><li><a... (6 Replies)
Discussion started by: sk2code
6 Replies

6. UNIX for Dummies Questions & Answers

how to execute program without using ./ or sh

Hi, I am a complete newbie for unix. I have just installed mysql on my MAC. I was wondering every time I wanted to use mysql I had to ./mysql or sh mysql everytime on /usr/local/bin/mysql/bin. How can I execute the mysql program without using ./ or sh. I chmod +x already. And what do I have to... (3 Replies)
Discussion started by: noppanit
3 Replies

7. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

8. Solaris

command to retrieve user information

Hi, I want the command to retrieve the existing user information such as * authorization * Profile * role * exipre(expiration date of login) * inactive please tell me how to do that Thank you. (3 Replies)
Discussion started by: S_venkatesh
3 Replies

9. Shell Programming and Scripting

how to execute this program?

Filesystem 1024-blocks Free %Used Iused %Iused Mounted on /dev/hd4 32768 16016 52% 2271 14% / /dev/hd2 4587520 1889420 59% 37791 4% /usr /dev/hd9var 65536 12032 82% 518 4% /var /dev/hd3 819200 637832 ... (1 Reply)
Discussion started by: sathyaac
1 Replies

10. Shell Programming and Scripting

A script pls( To retrieve database information)

KSH - Unix -AIX - db2 ************** Get the input from the user say '123' (It varies) Then i want to connect to a database say "D1" then i want th extract from the database by giving "select * from tablename where input = '123' I also want to connect to another database "D2" then i... (3 Replies)
Discussion started by: rollthecoin
3 Replies
Login or Register to Ask a Question