Sponsored Content
Full Discussion: Function main returning int?
Top Forums Programming Function main returning int? Post 302727693 by alister on Tuesday 6th of November 2012 04:20:01 PM
Old 11-06-2012
void exit(int status)
pid_t wait(int *stat_loc);

As already mentioned, only the low 8 bits of exit()'s int argument, status, are meaningful. The rest are practically ignored. However, it is an error to assume that the low 8 bits written by wait() to stat_loc are the low 8 of exit()'s status. This is often not the case. Since the layout of wait()'s *stat_loc bitmask is implementation dependent, POSIX specifies macros for examination and retrieval.

The 139 "exit status" in Gaurav's post isn't a true exit status. When a process exits abnormally, the bits which encode exit status have no meaningful value (which is why you are required to consult WIFEXITED() to confirm that the process exited normally before examining the exit status with WEXITSTATUS()).

If it's not an exit status, then what is it? If it wasn't an exit() argument, and if it wasn't main()'s return value, and if it's not provided by the kernel, then where does the 139 come from?

Before checking the exit status of it's child, the shell confirms that it exited normally, with WIFEXITED(). When the confirmation fails, the shell consults WIFSIGNALED(). Determining that the process was signaled and terminated abnormally, the signal number is retrieved with WTERMSIG(). By convention, the shell adds 128 to the signal number and stores that result in its ? parameter.

If your C code wait()ed on a process that was killed by that same signal, in the signal bits examined by WTERMSIG(), it would see 11 and not 139.

To avoid ambiguity, if you plan to invoke your binaries with the shell, it's a good idea to keep to exit values in the range 0 to 125 inclusive. The remaining values are spoken for: 126 (command found but not executable), 127 (command not found), and values larger than 128 (signal number + 128).

(Some of the following may be x86 specific.)

Returning to the original question: Where is the exit status stored? Inside the kernel.

When you call exit(n), the least significant 8 bits of the integer n are written to a cpu register. The kernel system call implementation will then copy it to a process-related data structure.

What if your code doesn't call exit()? The c runtime library responsible for invoking main() will call exit() (or some variant thereof) on your behalf. The return value of main(), which is passed to the c runtime in a register, is used as the argument to the exit() call.

When the parent calls wait(stat_loc), the exit status value (along with other status information) is copied from the kernel process structure to the address pointed to by wait()'s stat_loc.

Once a dead process is wait()'d on and its status information delivered, the kernel can destroy that process' data structure. Until then, the lingering data structure is the hallmark of a zombie.

Regards,
Alister

Last edited by alister; 11-06-2012 at 10:10 PM.. Reason: amend stack statement (register is used)
 

10 More Discussions You Might Find Interesting

1. Programming

c++ calling main() function

i just finished a project for a c++ class that i wrote at home on my computer, compiled with gcc. when i brought the code into school it would not compile, it would complain that cannot call main() function. at school we use ancient borland c++ from 1995. anyway my program has 20 different... (3 Replies)
Discussion started by: norsk hedensk
3 Replies

2. Programming

Return value (int) from main to calling shell

What is the sytax to return an int from C program main back to calling shell? #!/usr/bin/ksh typeset -i NO_RECS $NO_RECS=process_file # Process file is a C program that is set up to return an int from main. The #program complies with no issues, but an error is generated when the... (3 Replies)
Discussion started by: flounder
3 Replies

3. Programming

main function

Is it possible to execute any function before main() function in C or C++. (6 Replies)
Discussion started by: arun.viswanath
6 Replies

4. Programming

signal handling while in a function other than main

Hi, I have a main loop which calls a sub loop, which finally returns to the main loop itself. The main loop runs when a flag is set. Now, I have a signal handler for SIGINT, which resets the flag and thus stops the main loop. Suppose I send SIGINT while the program is in subloop, I get an error... (1 Reply)
Discussion started by: Theju
1 Replies

5. Shell Programming and Scripting

problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor ' BEGIN { printf("wgt:" wgt "\n"); printf("factor:" fac "\n"); total = sprintf("%.0f", wgt * fac); total2 = sprintf("%.0f", int(wgt * fac)); printf("total:" total "\n"); printf("total2:" total2 "\n"); } ' if vWeight=326.4 vFactor=100 the result... (2 Replies)
Discussion started by: qa.bingo
2 Replies

6. Shell Programming and Scripting

Awk issue using int function

Hi, I am having issue with awk command . This command is running in the command prompt but inside a shell script. awk -F'| ' 'int($1)==$1 && int($3) ==$3' int_check.txt $cat int_check.txt 123|abc|123x 234|def|345 When i run it inside a shell script i am getting the error "bailing... (5 Replies)
Discussion started by: ashwin3086
5 Replies

7. Shell Programming and Scripting

Perl int function solved

Hello, I have the below perl function int to return the integer value from the expression but it is not. I am not sure if something misses out here. Any help on this? Thanks in advance. # Code sample Start my $size = int (`1134 sample_text_here`); print "$size \n"; # Code end ----------... (0 Replies)
Discussion started by: nmattam
0 Replies

8. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

9. Programming

How to access argv[x] from another function other than main???

Hi friends, when I am passing arguments to main, I want another function to be able to have access to that function, the problem is that I am creating athread, which has a function like void *xyz(void *), how can pass the refernce of argv to this function, if you see my program, you will better... (2 Replies)
Discussion started by: gabam
2 Replies

10. UNIX for Beginners Questions & Answers

A function that refuses to run anywhere else but main()

Hi. I have some code, that for some reason, I could not post it here in this post. Here's the address for it: #if 0 shc Version 4.0.1, Generic Shell Script Compiler GNU GPL Version 3 Md - Pastebin.com First off, I used "shc" to convert the code from shell script to C. And The... (6 Replies)
Discussion started by: ignatius
6 Replies
PAPS(1) 						      General Commands Manual							   PAPS(1)

NAME
paps - UTF-8 to PostScript converter using Pango SYNOPSIS
paps [options] files... DESCRIPTION
paps reads a UTF-8 encoded file and generates a PostScript language rendering of the file. The rendering is done by creating outline curves through the pango ft2 backend. OPTIONS
These programs follow the usual GNU command line syntax, with long options starting with two dashes (`-'). A summary of options is included below. --landscape Landscape output. Default is portrait. --columns=cl Number of columns output. Default is 1. Please notice this option isn't related to the terminal length as in a "80 culums terminal". --font=desc Set the font description. Default is Monospace 12. --rtl Do right to left (RTL) layout. --paper ps Choose paper size. Known paper sizes are legal, letter and A4. Default is A4. Postscript points Each postscript point equals to 1/72 of an inch. 36 points are 1/2 of an inch. --bottom-margin=bm Set bottom margin. Default is 36 postscript points. --top-margin=tm Set top margin. Default is 36 postscript points. --left-margin=lm Set left margin. Default is 36 postscript points. --right-margin=rm Set right margin. Default is 36 postscript points. --gutter-width=gw Set gutter width. Default is 40 postscript points. --help Show summary of options. --header Draw page header for each page. --markup Interpret the text as pango markup. --lpi Set the lines per inch. This determines the line spacing. --cpi Set the characters per inch. This is an alternative method of specifying the font size. --stretch-chars Indicates that characters should be stretched in the y-direction to fill up their vertical space. This is similar to the texttops behaviour. AUTHOR
paps was written by Dov Grobgeld <dov.grobgeld@gmail.com>. This manual page was written by Lior Kaplan <kaplan@debian.org>, for the Debian project (but may be used by others). April 17, 2006 PAPS(1)
All times are GMT -4. The time now is 05:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy