How to call a proc file from *.c program?


 
Thread Tools Search this Thread
Operating Systems Linux How to call a proc file from *.c program?
# 1  
Old 02-13-2006
How to call a proc file from *.c program?

Hi,

I am new to Linux programming. As part of learning, I need to create a *.c program where we call certain /proc files (i.e. such as meminfo, version, uptime, etc...) from our program. Can anyone point me to a simple program on how one would do this (i.e. can you directly call uptime() or ... straight from my program - I would be very surprised if one could)?


Thanks,

Pat//
# 2  
Old 02-13-2006
Since you are on a learning curve, read through this - The /proc File System

It has almost everything that you have asked for.
# 3  
Old 02-13-2006
I'm pretty sure you just read() it.

One way to see how other programs does it.

Run strace on a program which is known to access /proc. The system calls involved will be logged. Inside you will find calls to read from the proc filesystem.
# 4  
Old 02-15-2006
Hi cbkihong,

You mention that I can run a program 'known' to access a file in the '/proc' subsystem. Is there a good example program you can recommend I run (and how do you run with strace?).


Thanks,

Patrick//
# 5  
Old 02-15-2006
On Linux, a program which reads /proc I can currently think of is "lsmod".

If I run

Code:
strace lsmod 2>&1 >/dev/null | vim -

In the captured output, you will find lines like
Quote:
open("/proc/modules", O_RDONLY) = 3
fstat64(1, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbff5e5c4) = -1 ENOTTY (Inappropriate ioctl for device)
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fff000
fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7ffe000
read(3, "radeon 125637 2 - Live 0xe0f2400"..., 1024) = 1024
read(3, "e 0xe0caf000\nsnd_pcm_oss 49017 0"..., 1024) = 1024
read(3, "ata_piix, Live 0xe0b81000\nsd_mod"..., 1024) = 108
read(3, "", 1024) = 0
close(3) = 0
So open() /proc/modules gives a file descriptor 3, so you just look for lines whose file descriptors are 3 and you can then trace the system calls involved. Here you see that apparently the lsmod program tries to read() from the open()ed file descriptor in 1024-byte chunks until EOF is reached, and finally close() it.

If you know of a program which reads /proc in a similar fashion you can try to strace it to find out what is involved.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sybase Stored Proc call from UNIX script.

Hi, I am new to shell scripting and Sybase database i need a help that i try to execute a SYBASE stored procedure from a Unix shell script and wanna write the output of the SP into a Text File.somehow i try to find a solution but whwn i try to run the script i am not getting the output file with... (1 Reply)
Discussion started by: Arun619
1 Replies

2. Programming

How to compile and run the ProC (*.pc) program?

Hi Team, I am very new to this forum and hope someone will help me in resolving the issue. I am new to Pro C also. I made some changes to the existing Pro C program and want to run the program with the changes. But I am unable to neither compile nor run the program. Please do the... (2 Replies)
Discussion started by: prakashs1218
2 Replies

3. UNIX for Dummies Questions & Answers

Getting a process/program version from /proc folder

Hello I am writing a script that will first execute ps to get the list of processes running, and the go into the /proc folder for each PID listed and gather relevant information. I looked through the contents of a particular process in the /proc folder and I can't find where I can locate... (2 Replies)
Discussion started by: flagman5
2 Replies

4. Programming

call program

I would need to call the program 'ethtool' in my C++ program, does anyone know how to do that (if its even possible)? (1 Reply)
Discussion started by: Freaky123
1 Replies

5. UNIX for Dummies Questions & Answers

proc program compilation in unix

hi, i need to compile a proc program, say prog.pc can we compile this program in the unix environment? does this need a make file? can anyone help me on this since i am new to this area. Thanks in advance. (1 Reply)
Discussion started by: csprog
1 Replies

6. Shell Programming and Scripting

Call a mainframe program

Is it possible to call a mainframe program in UNIX script. I am using HP-UNIX. If so can any let me know the way to do it. (1 Reply)
Discussion started by: atlantis
1 Replies

7. AIX

AIX equivalent to /proc/self/cmdline to get process name from C++ program

Hi, I'm porting some old C++ code (that I didn't write) from Linux to AIX and have run into a problem in getting the process name from within the code when it is run on AIX. Basically the code is getting the process name so it can then return it to the rest of the code as argv. This code is trying... (1 Reply)
Discussion started by: tbk
1 Replies

8. Shell Programming and Scripting

how to call another program

Hi, I would like to know how to call a program "cmp_size" ... where to put in progam to run it ex: program checkdisk is below, and it will call a nother problem "cmp_size" Do I just put the cmp_size program at the end of this program. Thank you very much, # check all directory for size... (3 Replies)
Discussion started by: xitrum
3 Replies

9. Shell Programming and Scripting

Unix call to Oracle PL/SQL pkg/store.proc

HI, I'm trying to get this right, please can you help. In my unix korn shell script, I call an oracle stored proc within a package and I specify 3 parameters, 2 of which are IN OUT parameters (i.e. I expect the stored proc to change them and return them back to me). Does the unix code... (7 Replies)
Discussion started by: csong2
7 Replies
Login or Register to Ask a Question