fork() and the program is hanging...


 
Thread Tools Search this Thread
Top Forums Programming fork() and the program is hanging...
# 1  
Old 08-13-2009
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 ???
# 2  
Old 08-13-2009
Try to remove the \n in your scanf statement.

Code:
scanf("%d", &retval);

# 3  
Old 08-14-2009
I entered 100....but it still hangs...
# 4  
Old 08-14-2009
It works for me once the \n is removed. Are you hitting the enter key after typing in 100?
# 5  
Old 08-17-2009
thamx...I got this
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sftp hanging

Hi All, I am transfering a file through sftp. But the script is hanging at exit occasionally. I am suspecting that sftp is hanging due to buffer size issue. If that is the case can any body suggest a solution. Please find the code. echo "cd /${CUST_ID}/inbound/${SAFET_ID}" >... (0 Replies)
Discussion started by: Girish19
0 Replies

2. UNIX for Dummies Questions & Answers

netstat -a is hanging??

Hi, I am on Solaris 10 server. I wanted to see a port is ESTABLISHED or in the LISTEN mode. But when I do netstat -a |grep 22205, it's hanging. I have waited over two minutes. Is it possible, something else is wrong on the server? I don't see anything on /var/adm/messages file. ... (3 Replies)
Discussion started by: samnyc
3 Replies

3. Shell Programming and Scripting

sed hanging

/bin/sed -n '$q;5633653,0 p' lfile lfile is a log file that is being updated several times a second. so, the command above figures out the line number it wants in the lfile and then proceeds to output all lines from that line number to the end of the file. the problem is, the end of the... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Programming

How forbid use fork() in exec() program.

Hello World! I am writing code in C++ which have to launch another application X using exec(). I would like to set some limits on it using setrlimit etc... My problem is that i don't know how to forbid using fork() and strlimit by application X. How can i do it? (3 Replies)
Discussion started by: kzi
3 Replies

5. Shell Programming and Scripting

Run shell script from C program by calling fork and execl

I need to write a c program that uses the fork and excel system calls to run the shell script mode invoked like this: "./mode 644 ls -l" (that is the argumetns will always be 644 ls -l) here's the mode script: #!/bin/sh octal="$1" shift find . -maxdepth 1 -perm $octal -exec $@ {} \; ... (3 Replies)
Discussion started by: computethis
3 Replies

6. Programming

Change Pseudo Code to C Program (print and fork)

I am very new at programming and this is probably an easy question, but I am desperate. I need to change this low level code into a C program that I can run so I can print out every "A" that appears with the fork() command. You help is greatly appreciated. PRINT A p=fork() if( p == 0) {... (0 Replies)
Discussion started by: tpommm
0 Replies

7. Programming

C++ How to use pipe() & fork() with stdin and stdout to another program

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question:... (2 Replies)
Discussion started by: vvaidyan
2 Replies

8. Solaris

hanging sockets!??!

hi all, i had a program which created a socket on port 7113, but for some reason the program was hunbg and I had to Ctrl+c it. I ran the program for a couple of times and now when I do a netstat -a I see that these sockets are lingering and that might be the reason why my program is not... (1 Reply)
Discussion started by: Naanu
1 Replies

9. AIX

multi threaded program is hanging

I have a Multithreaded program which is hanging on AIX. OS Version: AIX 5.2 and thread library version : 5.2.0.75 We Initiate the process with 50 threads..when we are disconnecting from the process it hangs.There is lots of other stuff involved here.I am just sending the piece of the problem with... (0 Replies)
Discussion started by: hikrishn
0 Replies

10. UNIX for Dummies Questions & Answers

how to tell if process is hanging

On Solaris 8.. 28166 user 3693M 2736M sleep 5 0 0:05.38 0.0% PROCESS/4 How can I tell if this process is doing anything or just hanging? (2 Replies)
Discussion started by: dangral
2 Replies
Login or Register to Ask a Question