The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > Linux
Google UNIX.COM


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Call C Program From Shell Script Chanakya.m Shell Programming and Scripting 1 05-22-2008 10:40 PM
Unix call to Oracle PL/SQL pkg/store.proc csong2 Shell Programming and Scripting 7 01-07-2008 08:49 AM
how to call awk in a csh Program bikas_jena Shell Programming and Scripting 5 11-25-2007 02:24 AM
how to call c executable inside c program?? zedex High Level Programming 3 02-28-2007 10:34 AM
tcsh user failed to call library in ksh program nir_s Shell Programming and Scripting 1 04-04-2006 05:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-12-2006
Registered User
 

Join Date: Feb 2006
Posts: 3
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//
Reply With Quote
Forum Sponsor
  #2  
Old 02-12-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
Since you are on a learning curve, read through this - The /proc File System

It has almost everything that you have asked for.
Reply With Quote
  #3  
Old 02-13-2006
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,469
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.
Reply With Quote
  #4  
Old 02-14-2006
Registered User
 

Join Date: Feb 2006
Posts: 3
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//
Reply With Quote
  #5  
Old 02-14-2006
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,469
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
inappropriate ioctl for device

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0