Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-02-2009, 12:23 AM   #1
LQ Newbie
Registered: Oct 2009
Posts: 4

Rep: Reputation: 0
How do you write to a proc file from a user program?

I know how to write to a proc file from the command line (e.g. echo hello > myProcFile) but I need to do that in a user program (written in C). I tried using fopen but it returned an error. So is there anything else I can do besides using something like execl echo?
Old 11-02-2009, 12:31 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
/proc is a virtual file system---physically you are just accessing data structures in memory. I would have assumed you would access it as you would any ohter file. The big difference is that writing to /proc is not something that you typically should be doing.

for something like fopen to open a file, I assume the user running the program must have the appropriate privileges. What happens if you run you program as root?
Old 11-02-2009, 12:50 AM   #3
LQ Newbie
Registered: Oct 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you for replying!

I get the same result when I run my program as root.

And to clarify, I am doing this for a school assignment. I had to make a module that creates a /proc file entry when it is initialized. The next step, which I am stuck on, is to have a user program write and read to/from the /proc file.

I thought that perhaps fopen didn't work because /proc is virtual. Or maybe I need to set permissions on the /proc file when I create it?
Old 11-02-2009, 01:13 AM   #4
LQ Newbie
Registered: Oct 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Actually, scratch that. I just realized I had coded the path of the file using '~' which obviously means different things when I'm me vs when I'm root. So that means you solved my problem. Thank you very much!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Write into File from Kernel and read the same file from User space saurabhchokshi Programming 0 05-01-2009 02:26 PM
I (sometimes) cant write my program file when a copy of it is running foof Linux - Newbie 6 11-13-2008 04:17 PM
A normal user now has write permissions for the whole file system 16777216 Ubuntu 2 10-23-2006 09:32 AM
how to write a batch file to make a program run during boot up in windows??? b0nd Programming 7 09-04-2006 06:16 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration