Solved

Return value of program invoked with system()

Posted on 2003-11-04
2
230 Views
Last Modified: 2010-04-22
Hi,
I'm writing a program that listens for network connections on a certain port.
Every X minutes it has to run a certain program (a program that I have written myself).
I do not wish to use cron or anything like it.
I invoke the program with the "system()" function and it works.
The only thing that annoys me is that I can't get hold of the return value of my program.

If I write this in my second program:

int main(...)
{
     ...
     if(...)
         return 999;
     ...
     return 666;
}

I want to use the return value for status purposes, how do I do this?
I want to be able to write something like this in my server program:

int runProgram()
{
    ...
    //INVOKE PROGRAM HERE
    switch (returnvalue)
    {
        case 666:
            ...
    }
    ...
}

Is this possible with the system() command?
If not, what should I use?

Thanks in advance,

Nicolas
0
Comment
Question by:The_LowRider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
mtmike earned 250 total points
ID: 9680232
The system() command returns the exit code like wait() does:

status = system("...");
if (status >= 0) {
  exitcode = WEXITSTATUS(status);
}

The macro WEXITSTATUS is defined in <sys/wait.h>.

Note that exit codes must be small integers in the range 0-127.
0
 
LVL 1

Author Comment

by:The_LowRider
ID: 9680282
Exactly what I needed, thanks!
Works great!

Nicolas.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question