The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 08-13-2009
lipun4u lipun4u is offline
Registered User
  
 

Join Date: Sep 2008
Location: bhubaneswar, india
Posts: 19
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
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0