开发者

Capturing and displaying return value from a call to C system()

I am on ubuntu linux 10.04 I have the following

#simplec.c  
#include "stdio.h"

int main()
{

    printf("Hello World\n");
    system("ps -H");
    return 开发者_如何学Go12;


}

AND

#callsimplec.c
#include "stdio.h"

int main()
{

    int ret =0;
    ret = system("./simplec");
    printf("In callsimplec ret is %d\n", ret);

}

When I do

gcc callsimplec.c -o callsimplec
gcc simplec.c -o simplec
./callsimplec

I get:

Hello World
  PID TTY          TIME CMD
27238 pts/2    00:00:00 bash
28066 pts/2    00:00:00   callsimplec
28067 pts/2    00:00:00     simplec
28068 pts/2    00:00:00       ps
In callsimplec ret is 3072

So I figured out that 3072 is printed because 256 times 12 is 3072. Whatever return value I use in simplec.c I get that value multiplied by 256 as the output in print. Why is that? I am just trying to make sense of it.


The value returned by system should be used with the macros: WEXITED, WIFEXITSTATUS etc.

The value returned by system (and by the wait family) is, acording to case:

  • Normal termination:

    15 ............. 8 ............. 0
        exit status         0x00
    
  • Killed by signal:

    15 ............. 8 7............ 0
          unused        killer signal
    
  • Stopped by signal

    15 ............. 8 ............. 0
        stop signal         0x7f
    
  • Continued by signal

    15 ............................. 0
                   0xFFF
    

So in your case the process exited normally and system returned 12 shifted 8 times.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜