issuing a command using c code

This is a discussion on issuing a command using c code within the Linux Programming forums, part of the Platform Specific Boards category; I am working in Linux. I have written a kernel module that creates a file called /proc/my_clock. I need to ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    35

    issuing a command using c code

    I am working in Linux. I have written a kernel module that creates a file called /proc/my_clock.


    I need to write an integer returned from function, gettimeofday(), to /proc/my_clock using c code. I have a couple of ideas but I am not sure which would be better.

    idea1 - use a file pointer to write a buffer containing the integer(s)

    idea2 - use sprintf of fprintf to write a buffer containing the integer(s)

    No other ideas at this point. Any info would help...

    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I need to write an integer returned from function, gettimeofday()
    The number returned by the function represents success or failure only, not the time. Are you sure its that you want to write out?

    If you've already managed to "written a kernel module", then why don't you try out your file IO ideas? That way you'll see the different types of output your two ideas will give.

    Write some code, and post it here when you have troubles, someone will help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    35

    ideas before writing the code

    gettimeofday returns two integers. the first integer represents seconds and the seconds integer represents microseconds...

    Anyway, its irrelevant.

    The linux environment is tricky. You can do things that will destroy networks when changing kernel variables. I don't want to do that -definitely.

    I will post my code later tonite.

    Peace

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    35

    module code

    this code creates the module named dc_clock_mod in /proc folder.

    I will place my additional code in the init_module function.


    Code:
    #include<linux/kernel.h>
    #include<linux/module.h>
    #include<linux/proc_fs.h>
    #define BUF_LEN 50
    
    int read_my_clock( char *a, char **b, off_t c, int d, int e)
    {
       printk(KERN_ALERT, "\nread_my_clock\n") ;
       /* this is the function that I write to read
          the clock file info that I created in proc
       */
    }
    
    struct proc_dir_entry dc_clock_mod_file = {
      0,
      12,
      "dc_clock_mod",
      S_IFREG | S_IRUGO,
      1,
      0,
      0,
      BUF_LEN,
      NULL,
      read_my_clock,
      NULL
    };
    
    int init_module()
    {
       return proc_register(&proc_root, &dc_clock_mod_file);
    }
    
    void cleanup_module()
    {
       proc_unregister(&proc_root, dc_clock_mod_file.low_ino) ;
    }
    Last edited by DMaxJ; 06-27-2003 at 10:34 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: ideas before writing the code

    Originally posted by DMaxJ
    gettimeofday returns two integers. the first integer represents seconds and the seconds integer represents microseconds...

    Anyway, its irrelevant.
    No it doesn't. No function in C can return more than one value. Not unless you're wrapping them in a returned structure. And if that's the case, then you're still only actually returning one object. In C, any given function can only ever return one object.

    If you don't believe me, simply read the man page.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    35
    Okay, again, I stress that I am working in linux. If you must know how gettimeofday() works then I will explain...

    gettimeofday is a function defined in <sys/time.h>.

    struct timeval contains two variables, .time_sec and time_usec.

    The integers are in those variables

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by DMaxJ
    Okay, again, I stress that I am working in linux. If you must know how gettimeofday() works then I will explain...

    gettimeofday is a function defined in <sys/time.h>.

    struct timeval contains two variables, .time_sec and time_usec.

    The integers are in those variables
    What quzah is saying (obsessing about? ) is the fact that gettimeofday() returns one structure, that structure being the "value" returned.

    Your claim that it is returning two integers is somewhat true -- they are contained in the struct -- but it's still one value that is technically returned. If you had said "returns two integers in a struct" I'll bet q's knickers would not have gotten bunched.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21