LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2007, 12:15 AM   #1
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Rep: Reputation: 34
Question C program will not compile - wait and exit scope


Hello, if you have a C compiler in your grasp, could you see if this compiles on your system without errors or warnings?

Code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int value = 5;

int main()
{
  pid_t pid;
  pid = fork();
  
  if (pid == 0) {
    value += 15;
  } else if (pid > 0) {
    wait(NULL);
    printf("PARENT: value = %d", value);
    exit(0);
  }
}
This is what I get:
Code:
[amm1747@enterprise homework]$ gcc-4.1 -Wall ch3ex4.c -o ch3ex4
ch3ex4.c: In function ‘main’:
ch3ex4.c:15: warning: implicit declaration of function ‘wait’
ch3ex4.c:17: warning: implicit declaration of function ‘exit’
ch3ex4.c:17: warning: incompatible implicit declaration of built-in function ‘exit’
ch3ex4.c:19: warning: control reaches end of non-void function
[amm1747@enterprise homework]$ ll
total 392K
-rwxr-xr-x 1 amm1747 amm1747 6.9K 2007-03-26 01:12 ch3ex4
-rw-r--r-- 1 amm1747 amm1747  259 2007-03-26 00:49 ch3ex4.c
-rw------- 1 amm1747 amm1747  17K 2007-03-21 02:25 chapter2.odt
-rw------- 1 amm1747 amm1747 342K 2007-03-21 02:25 chapter2.pdf
-rw-r--r-- 1 amm1747 amm1747 9.0K 2007-03-26 00:06 chapter3.odt
[amm1747@enterprise homework]$ ./ch3ex4 
[amm1747@enterprise homework]$ prise homework]$
It doesn't print anything.. Instead it messes up the prompt at the end.

I'd appreciate any help! By the way, this is code copied directly out of my OS Concepts book, so it's not something I wrote.

Thank you,
AM
 
Old 03-26-2007, 12:34 AM   #2
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
Well, so I add a \n to the end of the string in the printf statement, and it prints something out. But that still doesn't explain why I am getting those errors/warnings. Any ideas?

Thanks.
 
Old 03-26-2007, 12:42 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You may want to try and #include <sys/wait.h>
Chect out man 2 wait
 
Old 03-26-2007, 12:48 AM   #4
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
Hi, that did get ride of my wait error messages (but leaving exit still). I do not have a man page entry for wait, for some reason. I appreciate the feedback

-AM
 
Old 03-26-2007, 12:57 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
either use the keyword return or _exit(int)
 
Old 03-26-2007, 01:01 AM   #6
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
_exit(int) works! Thanks a lot, I'll keep this in mind.

-AM
 
Old 03-26-2007, 08:59 AM   #7
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
You probably still get this warning,
Code:
ch3ex4.c:19: warning: control reaches end of non-void function
Because you define main as returning an int, but then there is no return statement. You should add return 0; just before the final closing brace. Alternatively, make sure both branches of the if statement return a value. BTW, in this situation I'd prefer return over exit_(...).

Last edited by Quigi; 03-26-2007 at 09:00 AM.
 
Old 03-26-2007, 10:02 AM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by student04
_exit(int) works! Thanks a lot, I'll keep this in mind.

-AM
_exit() is declared in unistd.h

but

exit() is declared in stdlib.h

What’s going on (or what was going on in the original question) is very simple: you were trying to use functions that you hadn’t declared (in this case, wait() and exit()). Normally, I believe that such a function is assumed to return an int and take as it’s parameters whatever types you passed. Some functions, however, are so commonly used that gcc has them built-in, and this is where the problem with exit() was — the implicit declaration (which returns an int) did not match the built-in declaration (of void).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
program exit return 0 or 1? blackzone Programming 4 07-20-2006 05:00 AM
scope of an object and global scope lucky6969b Programming 7 12-09-2005 09:09 PM
get exit status from a C-program in Bash Yoko Programming 1 07-12-2005 04:33 PM
[SOLVED] Run program on Linux Startuo (Save on exit won't work!) ziphem Linux - Newbie 1 01-13-2005 07:01 AM
http config program doesnt save and exit!!! benrose111488 Linux - Software 0 06-09-2004 08:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration