473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Replicate in C++, in Unix, this command: ps -A | grep argv[1] | wc -l

Hi,

I'm in unix trying to code a c++ program to replicate this command:

ps -A | grep argv[1] | wc -l.

Here is what I have, which doesn't work, and I'm totally lost. Can anyone please help me ? !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>


using namespace std;

int main(int argc, char *argv[])
{
int pid,pid1,pid2;
int a[2];
int rv;
pipe(a);


if ((pid = fork() == 0))
{

if ((pid1 = fork() == 0))
{

dup2(a[1],1);
close(a[1]);
close(a[0]);

if ((pid2 = fork()) == 0)
{

dup2(a[1],1);
close(a[1]);
close(a[0]);
execlp("/bin/ps","ps","-A",NULL);
}
else
{
dup2(a[0],0);

close(a[1]);
close(a[0]);
execlp("/bin/grep" , "grep" ,argv[1], NULL );
}

}
cout << "back to child" << endl;
execlp("/usr/bin/wc" , "wc" , "-l", NULL );
}
else
{
wait(&rv);
}


return 0;
}
Jan 17 '10 #1
1 5029
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. if ((pid = fork() == 0))              <<?
  2. {
  3.  
  4. if ((pid1 = fork() == 0))             <<?
  5. {
  6.  
  7. etc...
  8.  
  9. if ((pid2 = fork()) == 0)
  10. {
I didn't look at the logic butin the fist two cases, the == operator will execute first due to its higher precedance than the = operator.

The last case looks right.
Jan 17 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hue | last post by:
I am trying to parse command line options using getopt module. I have written the following Code import string import getopt def usage(): print '''haarp_make.py -- uses getopt to...
4
by: rkoida | last post by:
Hello evryone I am a newbie to python. I have a makefile which i can compile in UNIX/LINUX, But i I am planning to write a python script which actually does what my MAKEFILE does. The make file...
22
by: Ryan M | last post by:
I've been programming for a while, but most of my experience is on unix. How do C compilers work on operating systems that weren't written in C? And that have no libc? Compiling C on unix seems...
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
0
by: niket patel | last post by:
Hi There is a unix server and and we access unix and perform its commond using exceed or ftp through dos. Now i want to create an application in Visual Studio C#.Net 2003 such that i can create...
4
by: Ignoramus6539 | last post by:
There were some strange requests to my server asking for config.php file (which I do not have in the requested location). I did some investigation. Seems to be a virus written in perl,...
9
by: niteck07 | last post by:
I am using following shell script to ftp files to another server but this is failing as the shell script changes the user name for the ftp login the correct user name is 'ag\invprint' which the...
1
by: jazon | last post by:
Let me start by saying this for an Operating Systems class. No, I don't expect the work to be done for me. The assignment is as follows: To be honest, I feel like a fish out of water, like...
1
by: andrewkl | last post by:
hi, I wrote a C program (runas.c) that runs a command as a different user on Solaris 8. The problem I'm having is that the new user's group memberships aren't going into effect. Take the...
13
by: Anton Slesarev | last post by:
I've read great paper about generators: http://www.dabeaz.com/generators/index.html Author say that it's easy to write analog of common linux tools such as awk,grep etc. He say that performance...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.