0

My friend has an below assignment , can anyone can guide how to do this in "C", just guidence is enough.

Write a program to store all processes list into a file and sort all the processes with UID.

For example:

./a.out processidlist.txt

it has to save the info to processidlist.txt.

In this processidlist.txt it has to sort the processes with UID.

he tried the below

ps –A –o UID > outputfile

Thanks

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

int main( int argc, char *argv[] )
{
  FILE *fp1, *fp2;
  FILE *fp;
  int status;
  char path[1035];

   fp1 = fopen( argv[1], "w" );
   if ( ! fp1 )
   {
      printf("Error opening file %s\n", argv[1]);
   }

  /* Open the command for reading. */
  fp = popen("ps -Af | sort -k1", "r");
  if (fp == NULL) {
    printf("Failed to run command\n" );
    exit;
  }

  /* Read the output a line at a time - output it. */
  while (fgets(path, sizeof(path)-1, fp) != NULL) {
    printf("%s", path);
    fputs( path, fp1 );
  }

  /* close */
  pclose(fp);
  fclose(fp1);
  return 0;
}
kobe
  • 15,671
  • 15
  • 64
  • 91

2 Answers2

2

Something on these lines should work

system("ps -Af | sort -k1");

A indicates all processes
f generates full listing
k denotes sort by column
1 denotes first column which is UID of the processes

And if you don't want the annoying header

UID        PID  PPID  C STIME TTY          TIME CMD

along with your processes list, then use sed to delete first line of ps output

system("ps -Af | sed "1 d" | sort -k1");
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • pavan , you mean we can use this lin in c program directly – kobe Apr 10 '12 at 06:24
  • If you are running on Linux, then yes! `system` executes a command just as we execute in shell – Pavan Manjunath Apr 10 '12 at 06:27
  • you are saying using the line given by you in c program , can you change your code like a c program like the way i asked in the question – kobe Apr 10 '12 at 06:31
  • See, as usual,write a regular C program and add the `system` line and then compile/link to get your `a.out`. After that as you've already shown, redirect output to `processidlist.txt` – Pavan Manjunath Apr 10 '12 at 06:32
  • pavan , can i do one of the answer mentioned above , it has 24 up points http://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output – kobe Apr 10 '12 at 06:52
  • I did this and its working , but how to redirect the command output to that txt file int main( int argc, char *argv[] ) { FILE *fp; int status; char path[1035]; /* Open the command for reading. */ fp = popen("ps -Af | sort -k1", "r"); if (fp == NULL) { printf("Failed to run command\n" ); exit; } /* Read the output a line at a time - output it. */ while (fgets(path, sizeof(path)-1, fp) != NULL) { printf("%s", path); } /* close */ pclose(fp); return 0; } – kobe Apr 10 '12 at 06:55
  • kobe, whats the difference, instead of system, you are using `popen`. And `popen` is to parse output of a command yourself, here `sed` and `sort` have already done that. You just need to do `./a.out >processedlist.txt` – Pavan Manjunath Apr 10 '12 at 06:58
  • i think i have to pass file name as command line right , that means i have to handle inside the code? do i need to open connection to file and write ? – kobe Apr 10 '12 at 07:02
  • Oh. Ok. Then its easy, just fopen(argv[1],..) and `fwrite`, or `fputs` etc – Pavan Manjunath Apr 10 '12 at 07:04
  • thanks pavan , thats , working , can you review my code, i will post the final answer – kobe Apr 10 '12 at 07:16
  • hi pavan ,appreciate your time on this question , i posted final one which is working , please review the same – kobe Apr 10 '12 at 07:22
  • @kobe seems fine. Why do you need a `printf` there? You want the output into a file right? – Pavan Manjunath Apr 10 '12 at 07:32
  • I did both to see the out put , i will remove printf for sure – kobe Apr 10 '12 at 07:35
1

You need to provide the context of the question. i.e. What is the homework assignment trying to teach you?

Is there a specific API you have been learning about to check all the processes? (and so one could sensibly assume you are expected to use it).

If not, something like Pavan's system() call might work. (But then why were you asked to write a C program if it's solved by a 1 line shell script?)

Also: the "ps" in the comments on the question - it specifically says write a program, so why did "he" think a ps command line was adequate?

John3136
  • 28,809
  • 4
  • 51
  • 69
  • thanks , then i think we are not supposed to use system command , is there any other simple way of doing that – kobe Apr 10 '12 at 06:32
  • Again, what has been suggested by the teacher? /proc on the filesystem directly? libproc? – John3136 Apr 10 '12 at 07:23