Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7773581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:09:53+00:00 2026-06-01T17:09:53+00:00

My friend has an below assignment , can anyone can guide how to do

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:09:55+00:00Added an answer on June 1, 2026 at 5:09 pm

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Afternoon all, A very quick question... a friend has set up a form for
My document has the format as show below: { _id:ObjectId(4fabcd13), users:[ {id:001, tags:[friend, buiz]},
A friend has asked me to look into developing a custom system for his
A friend has created a few tables for me and used nvarchar(max) on around
One of my friend has been asked with a question Retrieving the max top
background: My friend has a minecraft server with a white list. The community for
A friend of mine has inherited an old Joomla 1.0.x site, that amongst other
A friend of mine has to send a normal email using ONLY HTML/HTML5 (or
My friend says that Django only has 1 thread or something? And I have
today i have talk with other friend ,he said he has logic programming skill

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.