![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ How to use pipe() & fork() with stdin and stdout to another program | vvaidyan | High Level Programming | 2 | 05-16-2008 08:30 PM |
| Hanging port? | NycUnxer | UNIX for Dummies Questions & Answers | 1 | 01-09-2008 02:21 PM |
| multi threaded program is hanging | hikrishn | AIX | 0 | 12-15-2006 08:39 AM |
| how to tell if process is hanging | dangral | UNIX for Dummies Questions & Answers | 2 | 01-07-2004 08:43 AM |
| mq_open Hanging | Deepa | High Level Programming | 0 | 06-24-2003 07:06 AM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
fork() and the program is hanging...
Consider the following code.. Code:
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t childpid;
int retval;
int status;
childpid = fork();
if(childpid >= 0)
{
if(childpid == 0)
{
printf("CHILD : I am the child process\n");
printf("CHILD : Here's my PID : %d\n", getpid());
printf("CHILD : My parent's PID is : %d\n", getppid());
printf("CHILD : The value of my copy of childpid is : %d\n", childpid);
printf("CHILD : Sleeping for 1 second...\n");
sleep(1);
printf("CHILD : Enter an exit value (0 to 255) : ");
scanf("%d\n", &retval);
printf("CHILD : Goodbye\n");
exit(retval);
}
else
{
printf("PARENT : I am the parent process\n");
printf("PARENT : Here's my PID : %d\n", getpid());
printf("PARENT : The value of my copy of childpid is : %d\n", childpid);
printf("PARENT : I will now wait for my child to exit\n");
wait(&status);
printf("PARENT : Child's exit code is : %d", WEXITSTATUS(status));
printf("PARENT : Goodbye\n");
exit(0);
}
}
else
{
perror("fork error");
exit(0);
}
}
Why the program hangs after the scanf statement ??? |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|