Find the answer to your Linux question:
Results 1 to 6 of 6
i'm using RHEL.i'm new to linux and shell scripting. i got this command that shows cpu usage : "ps ux | awk '/process_name/ && !awk/ { print $2}" 1)can someone ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    12

    how to include command line in c program?

    i'm using RHEL.i'm new to linux and shell scripting.
    i got this command that shows cpu usage :
    "ps ux | awk '/process_name/ && !awk/ { print $2}"

    1)can someone explain me what each thing signifies?

    i know ps-process stat
    awk-Find and Replace text


    2)also..i;ve to write a c code.
    how do i include this line in c so that it is executable in linux?

    3)for example: i have to read a file from /proc/stat
    how can i do that in c program?
    in shell it is 'cat'...
    like to print the file stat from /proc
    whats the code for that?



    thanks in advance

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Post

    Quote Originally Posted by kits View Post
    i
    2)also..i;ve to write a c code.
    how do i include this line in c so that it is executable in linux?
    i'm not sure about 1 and 3,
    to write a command line program..it's very easy ...

    hi.c
    -----
    main(int argc,char [] argv){
    //some initial variables
    if(argv[1] == 'a'){
    //call some function
    }
    else if(argv[2] == 'b'){
    //call some function-2
    }
    }

    ---------
    gcc hi.c
    ./a.out a b
    that' s it








    }
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    85
    for 3.
    may be you can use like this

    system("cat </proc/stat");



    Quote Originally Posted by kits View Post
    i'm using RHEL.i'm new to linux and shell scripting.
    i got this command that shows cpu usage :
    "ps ux | awk '/process_name/ && !awk/ { print $2}"

    1)can someone explain me what each thing signifies?

    i know ps-process stat
    awk-Find and Replace text


    2)also..i;ve to write a c code.
    how do i include this line in c so that it is executable in linux?

    3)for example: i have to read a file from /proc/stat
    how can i do that in c program?
    in shell it is 'cat'...
    like to print the file stat from /proc
    whats the code for that?



    thanks in advance

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Reading a file in C is a very simple task: if you don't know how, I'd recommend studying up some more on C before trying to write anything.

    If you want to just print a file to stdout:
    Code:
    int c;
    FILE *file;
    
    file = fopen("file", "r");
    while((c = getc(file)) != EOF)
        putchar(c);
    fclose(file);
    getc(FILE *) returns the next character (or the special symbol EOF if at the end of file). putchar(char) prints a character to stdout.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Nov 2006
    Location
    Hyderabad
    Posts
    85
    iam sorry cabhan,
    reading from a file easy thing as you said.
    But i thought mr kits asking the same(shell) in c.




    Quote Originally Posted by Cabhan View Post
    Reading a file in C is a very simple task: if you don't know how, I'd recommend studying up some more on C before trying to write anything.

    If you want to just print a file to stdout:
    Code:
    int c;
    FILE *file;
    
    file = fopen("file", "r");
    while((c = getc(file)) != EOF)
        putchar(c);
    fclose(file);
    getc(FILE *) returns the next character (or the special symbol EOF if at the end of file). putchar(char) prints a character to stdout.

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I actually was referring to kits, not you. Though with cat, since it takes in a filename, you don't need to do "cat < /proc/stat". You can just say "cat /proc/stat".

    My message to kits is just that if you're going to try writing a full program in C, it would likely be useful to know how to do I/O first...
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...