C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 14th, 2003, 07:03 PM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Child process return results using exit()

Hello! Given this example code:
/*
* The child executes the code inside the if.
*/
if (pid == 0) {
/*do something here*/
exit(numbersRead);
/*
* The parent executes the wait.
*/
while (wait(&status) != pid)
/* empty */ ;

What I'd like to do is, spawn a child process, make it work on something while the parent waits for it to finish (let's says, for example, read numbers from stdin) and then return it's results (in this case, return the amount of numbers read) to the parent process, using the exit() call. Also, I can't understand the line
"while (wait(&status) != pid)", because it seems "status" isn't even used on the whole example program!

Reply With Quote
  #2  
Old May 14th, 2003, 07:43 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 26 m 52 sec
Reputation Power: 1001
You know what they say... when the going gets tough, the tough get man. In your case, the man page you're looking for is
http://www.openbsd.org/cgi-bin/man....386&format=html

Happy reading

Reply With Quote
  #3  
Old May 14th, 2003, 11:43 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 38 sec
Reputation Power: 480
That's an entire subject area in UNIX/Linux programming that you're asking about there. As Scorpions points out, the man pages are your best friend in this new territory. Consult them often.

If you read the man page for wait, you will see that if a valid status is returned, then it contains the process' exit code. It does not return data read by the child process.

There are various ways to pass data between processes. The easiest for you to start off with, especially since you're dealing with a parent and child process, should be a pipe. Before you fork, call pipe to create a pair of file descriptors (eg, int fds[2]; ). Then for the child to pass data back to the parent, have the child write to the write end of the pipe (fds[1]) while the parent reads from the read end (fds[0]). When you're done, close the fds with the close() function.

The source of your example code should have examples of this.

Reply With Quote
  #4  
Old May 15th, 2003, 12:44 PM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
If you read the man page for wait, you will see that if a valid status is returned, then it contains the process' exit code. It does not return data read by the child process.

This was what I was asking; If the child, for example, reads 5 lines from stdin, then I can inform the parent of it by using exit(5), correct?

As to the man pages, I have a ton of print outs, though they're quite not begginer-friendly...

Reply With Quote
  #5  
Old May 15th, 2003, 03:02 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 38 sec
Reputation Power: 480
Quote:
Originally posted by bass20
As to the man pages, I have a ton of print outs, though they're quite not begginer-friendly...

I don't know if they all have this, but I once ate at the Hard Rock Cafe in Newport Beach. On the wall in the dining area was a large LED display constantly updating how much rain forest had been destroyed so far. I could just picture in my mind the sudden jump in that count when you printed out those man pages.

They may not be beginner-friendly, but have you ever seen a Bible that is? They form the basis for command and function documentation, so you do need to learn to use them. Otherwise, you run the risk of getting mostly "RTFM" responses ("read the manual") to your questions.

Since you're using fork, I know that you're doing this under some kind of UNIX, most likely Linux. The man pages are accessible from the command line and there are man pages for the C library functions; eg:
man pipe

If there's also a shell command by the same name as the C function, you'll have to specify which man page group it's in; eg:
man 2 wait

I haven't learned yet how that grouping is organized. If you want to search for a particular term in the man pages, use the -k option to list the man pages it occurs on; eg:
man -k wait | more

Under Linux, I normally open multiple consoles so that I can work in one and do something else in another, like open a man page. Or, if you're working online, you could open a browser and Google for a particular man page.

Quote:
This was what I was asking; If the child, for example, reads 5 lines from stdin, then I can inform the parent of it by using exit(5), correct?


Yes, you could do that way, but should you? The purpose of the exit code is to inform the parent process (eg, the shell) of the reason why that process had terminated: zero means it ran successfully and non-zero that there if failed and the specific value should say why it failed (you, the programmer, assigns these specific values). Using the exit code for an entirely different purpose is a questionable practice.

Also, what if you want to send the parent more than just an integer value? This mechanism will not allow you to do that. But something like a pipe would.

To give you an example of using a pipe, I've attached the lab assignment I'd just written for class. It's tar'd so that the forum will accept it; just use the command "tar xvf pfiles.tar" to extract the single .cpp file.

The program forks a child process which reads in a file and pipes the file contents back to the parent, who writes it out to another file. Both input and output file names are entered at the command line. That part of the program follows:
Code:
        if (pid == 0)
        {       // child
                close(pipefd[0]);
                if ((fd=open(infilename,O_RDONLY)) == -1)
                {
                        close(pipefd[1]);
                        perror("open input file error");
                        exit(2);
                }
                while (bytesread = read(fd,&buf,MAX_BUFSIZE))
                {
                        write(pipefd[1],&buf,bytesread);
                }
                close(pipefd[1]);
                close(fd);
        }
        else
        {       // parent
                close(pipefd[1]);
                if ((fd=open(outfilename,O_WRONLY | O_CREAT, 0600)) == -1)
                {
                        close(pipefd[0]);
                        perror("open output file error");
                        exit(2);
                }
                while (bytesread = read(pipefd[0],&buf,MAX_BUFSIZE))
                {
                        write(fd,&buf,bytesread);
                }
                close(pipefd[0]);
                close(fd);
        }
 

Remember that pipefd[0] is the read end of the pipe and that pipefd[1] is the write end.

Hope that helps.
Attached Files
File Type: tar pfiles.tar (10.0 KB, 259 views)

Last edited by dwise1_aol : May 15th, 2003 at 03:07 PM.

Reply With Quote
  #6  
Old May 15th, 2003, 04:27 PM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Child process return results using exit()


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT