The UNIX Forums
"Join the Network of UNIX System Users"


 
Subject: question on order of headers and wexitstatus
alarik
Newbie
Rank: 1



UID 46
Digest Posts 0
Credits 0
Posts 48
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:26 AM  Profile | P.M. 
question on order of headers and wexitstatus



question on order of headers and wexitstatus



in one of the unix programming faq's they have the following headers in the program to catch sigchld


code:

#include <sys/types.h>  /* include this before any other sys headers */
#include <sys/wait.h>   /* header for waitpid() and various macros */
#include <signal.h>     /* header for signal functions */
#include <stdio.h>      /* header for fprintf() */
#include <unistd.h>     /* header for fork() */

why does <sys/types.h> have to be placed before any other sys headers in this case?

and the second question. in the following code snippet, when i have status>>8 on a sep. line, i get an exit status of 256 when no one is logged on.



code:

if (status != 0) {
    fprintf(stderr, "no one logged on.\n");
    wexitstatus(status);
    status>>8;
    printf("%d\n", status);
    exit(exit_failure);
    }



however, when i put status>>8 in printf() like in the following

code:

if (status != 0) {
    fprintf(stderr, "no one logged on.\n");
    wexitstatus(status);
    printf("%d\n", status>>8);
    exit(exit_failure);
    }


i get an exit status of 1 when no one is logged on. i don't understand how the placement of status>>8 has an impact on the exit code.


Top
clvezlys
Newbie
Rank: 1



UID 92
Digest Posts 0
Credits 0
Posts 44
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:27 AM  Profile | P.M. 
q2 - posix says:

quote:
wexitstatus(stat_val)
if the value of wifexited(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().




the lower eight bits of something is never 256 -- 8 bits range from 0 - 255. first. did you check wifexited?

the >> question: i do not know, but i do know function arguments are sequence points. that means they have to be fully evaluated before they are passed as argument to a function.

are you using an old compiler, like the one that comes as part of hpux?

q1- sys/types.h is required to be included first by wait(), followed by sys/wait.h - if you read the man page for wait.

the real reason is so that test macros in the other include files do not "accidentally" turn off a declaration of some datatype, struct or typedef needed by somebody in another include file later on.
Top
kapengbarako
Newbie
Rank: 1



UID 212
Digest Posts 0
Credits 0
Posts 44
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:27 AM  Profile | P.M. 
the simple answer is "sys/types.h is a fundamental header and contains many types required by other headers".
Top
aju_kup
Newbie
Rank: 1



UID 18
Digest Posts 0
Credits 0
Posts 35
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:27 AM  Profile | P.M. 
quote:
originally posted by jim mcnamara
q2 - posix says:


the lower eight bits of something is never 256 -- 8 bits range from 0 - 255. first. did you check wifexited?

the >> question: i do not know, but i do know function arguments are sequence points. that means they have to be fully evaluated before they are passed as argument to a function.

are you using an old compiler, like the one that comes as part of hpux?

q1- sys/types.h is required to be included first by wait(), followed by sys/wait.h - if you read the man page for wait.

the real reason is so that test macros in the other include files do not "accidentally" turn off a declaration of some datatype, struct or typedef needed by somebody in another include file later on.




i'm using the gnu c compiler. i'm suspecting it might have to do with that fact that the '>> isn't a sequence point.
Top
jmweb
Newbie
Rank: 1



UID 206
Digest Posts 0
Credits 0
Posts 31
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 20, 2007 10:27 AM  Profile | P.M. 
1. you are not using the macros correctly...

it should be
code:
x=wexitstatus(status);

saying
code:
wexitstatus(status);by itself evaluates the exit status and discards the result.

2. similarly
code:
status>>8;effectively does nothing, it shifts a copy of the number right by 8 places and throws the result away.
Top
 

 

All times are GMT, the time now is Mar 22, 2010 01:56 PM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 3.523495 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP