Question

Starting New Processes We can create a new process from within another program using the system...

Starting New Processes

We can create a new process from within another program using the system library function:

#include <stdlib.h>

int system ( const char *str );

The system function runs the command passed to it as str and waits for it to complete. The command is executed as if the command,

$ sh -c str

Use the "man" command to study both the "sh" and "system" command. i.e. Execute

$ man sh

$ man system

For example,

$ sh -c "echo 'Hello, CSE 460'"

prints out the message "Hello, CSE 460".

Try the following program ( test_system.cpp ) that uses system to run ps.

//test_system.cpp
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
  cout << "Running ps with system\n";

  system ( "ps -ax" );
  
  cout << "Done \n";

  return 0;
}

Compile and run the program by:

$ g++ -o test_system test_system.cpp

$ ./test_system

What do you see? Change the system statement in test_system.cpp to system ( "ps -ax &" );. Compile and run it again. What happens? What does '&' here do?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
this line execute first in both 
cout << "Running ps with system\n";

then

system ( "ps -ax" );

will execute and then

cout << "Done \n";

will execute

now after modification to system ( "ps -ax &" );.

cout << "Running ps with system\n";
system ( "ps -ax &" );. // this command will execute in background whatever time it may take but next instruction will execute to avoid waiting
cout << "Done \n";

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)

Add a comment
Know the answer?
Add Answer to:
Starting New Processes We can create a new process from within another program using the system...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help me . This program needs to run in visual studio and written in...

    Can someone help me . This program needs to run in visual studio and written in c++. I want to call to the function string eraseChar in the main function to use the variables str and ch. I cannot use global variables. #include<iostream> #include <string> using namespace std; string eraseChar(char ch = 'e', string str = "the bike fell in the water");    int main() {    int pos;    pos = str.find(ch);    while (pos != string::npos)    {...

  • Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std;...

    Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std; void *PrintHello(void *arg) { int actual_arg = *((int*) arg); cout << "Hello World from thread with arg: " << actual_arg << "!\n"; return 0; } int main() { pthread_t id; int rc; cout << "In main: creating thread \n"; int t = 23; rc = pthread_create(&id, NULL, PrintHello, (void*) &t); if (rc){ cout << "ERROR; return code from pthread_create() is " << rc <<...

  • Update the program in the bottom using C++ to fit the requirements specified in the assignment....

    Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...

  • create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

    create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • Assume that we are writing a game like Minecraft in C++. We will have a World...

    Assume that we are writing a game like Minecraft in C++. We will have a World class that stores a 3-dimensional array of "Voxels". A voxel is a simple element of volume (think Pixel, but 3D). Your task for this assignment is to create a world, set some Voxels and display them. You are provided with a simple class for a Voxel that stores a RGB color value, as well as a character code for the type of element. You...

  • Instructions: Consider the following C++ program. At the top you can see the interface for the...

    Instructions: Consider the following C++ program. At the top you can see the interface for the Student class. Below this is the implementation of the Student class methods. Finally, we have a very small main program. #include <string> #include <fstream> #include <iostream> using namespace std; class Student { public: Student(); Student(const Student & student); ~Student(); void Set(const int uaid, const string name, const float gpa); void Get(int & uaid, string & name, float & gpa) const; void Print() const; void...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • Can I get a C++ code and output for this program using classes instead of using...

    Can I get a C++ code and output for this program using classes instead of using struct. The following program implements a Last In First Out (LIFO) stack. ( I want to use class for function definitions too and if main need.) #include <iostream> using namespace std; const int MAX = 100; struct stack {    int s[MAX]; // an array of integers    int top; // the index of the last number }; void push(stack &, int); void pop(stack&,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT