2

I have executed a command "watch grep \"cpu MHz \" /proc/cpuinfo".After executing this command i got following result. Result of The Command

But when I am trying this command using c code.

#include<stdio.h>
#include<stdlib.h>

int main(){
    FILE *fp;
    char path[1035];
    char command[]="watch grep \"cpu MHz \" /proc/cpuinfo";
    fp = popen(command, "r");
    if (fp == NULL) {
            printf("Failed to run command\n" );
        exit(1);
    }

    /* Read the output a line at a time - output it. */
    while (fgets(path, sizeof(path)-1, fp) != NULL) {
        printf("%s",path);
    }
    pclose(fp);
    return 0;
}
    I am getting following result.

Result of The Code tell me where am I going wrong?

3
  • Try enclosing the whole command you pass to watch in single quotes. E.g. "watch 'grep \"cpu MHz\" /proc/cpuinfo'"; Commented Dec 8, 2015 at 9:39
  • Yes it is showing correct result but it is taking almost 1 min for showing the result. Commented Dec 8, 2015 at 9:52
  • That's because the output of the watch command contains no newline characters, so fgets is not returning until the path buffer is full. By the way, you don't need to use sizeof(path)-1 in your call to fgets. You can just use sizeof(path). Commented Dec 8, 2015 at 10:25

2 Answers 2

1

Try something like this:

#include <stdio.h>
#include <stdlib.h>

int main(void){
    FILE *fp;
    char path[1035];
    char command[]="while grep \"cpu MHz\" /proc/cpuinfo; do sleep 2; done";
    fp = popen(command, "r");
    if (fp == NULL) {
            printf("Failed to run command\n" );
        exit(1);
    }

    /* Read the output a line at a time - output it. */
    while (fgets(path, sizeof(path), fp) != NULL) {
        printf("%s",path);
    }
    pclose(fp);
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes It is working.Can you please explain it a little?How is it giving correct result now without any delay?
@Yunis Khan - This is showing each line immediately because not the single command watch … is called which buffers all its output to the pipe until the buffer is full, but rather the command grep … is called repeatedly.
0

I think this is what you want. Don't forget to use memset.

#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *fp;
    char path[1035];
    char command[]="watch grep 'cpu MHz'  /proc/cpuinfo";

    fp = popen(command, "r");
    if (fp == NULL) {
        printf("Failed to run command\n" );
        exit(1);
    }

    memset(path,'\0',sizeof(path));
    /* Read the output a line at a time - output it. */
    while (fgets(path, sizeof(path)-1, fp) != NULL) {
        printf("%s",path);
    }
    pclose(fp);
    return 0;
}

1 Comment

It is showing the same result which I have included at Result of The Code link but I need the result which I have included at Result of the command link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.