Sponsored Content
Top Forums Programming getting the return code of forked child process (ftp) Post 302131340 by KittyJ on Friday 10th of August 2007 06:53:00 AM
Old 08-10-2007
Hello,

Thank you for the answers.
So, now I'm sure it's not something "impossible" that I'm trying to do.

But I'm not there yet. I've copied what I've got so far below.
With this piece of code, I can send ftp commands and get the response on standard output. But how do I catch it now ?

I thought read(fd[0],receive,strlen(receive)); would do this.
Maybe something is wrong with the close and dup'ing in the child process ?

I haven't tried the popen yet, because I have the idea that I'm very close with what I have now.

Does anyone see what's still wrong ?


int fd[2], nStatus;
pid_t pid;

pipe(&fd[0]); //create pipe
if ((pid=fork()) != 0) {
// parent
close(fd[0]);
//close(STD_OUTPUT);
//dup(fd[1]); // sets stdout to this pipe end
//close(fd[1]); // don't need fd anymore

std::cout << "I'm the parent. I'm a child process of : " << getppid() << ", my pid is " << getpid() << std::endl;

} else {
// child
close(fd[1]);
close(STD_INPUT);
dup(fd[0]); // replace stdin
close(fd[0]);

std::cout << "I'm the child. I'm a child process of : " << getppid() << ", my pid is " << getpid() << std::endl;

std::string szStartCmd = "ftp -i -n -v 192.168.149.31";

int nExecRes = execl("/bin/sh", "sh", "-c", szStartCmd.c_str(), (char *)0);

}

sleep(1);


std::cout << "I'm a child process of : " << getppid() << ", my pid is " << getpid() << std::endl;


// Wait for the child process to end, but without blocking !
// If the child process exits immediately, we assume ftp couldn't be started -> return false
// else the child process is waited for later (it becomes a "zombie" process when ftp is closed,
// by calling waitpid, the zombie processes are cleared.
int nChildDied = waitpid(pid, &nStatus, WNOHANG);

if (nChildDied == 0)
{
// Child is not dead -> protocol analyser was started OK
std::cout << "ftp started, child not dead" << std::endl;

}
else if (nChildDied > 0)
{
// A child process died -> error starting protocol analyser
if (WIFEXITED(nStatus))
{
std::cout << "child process with pid = " << nChildDied << " died. Child WIFEXITED status = "
<< nChildDied << WEXITSTATUS(nStatus);
}
if (WIFSIGNALED(nStatus))
{
std::cout << "child process with pid = " << nChildDied << "died. Child WIFSIGNALED status = "
<< nChildDied << WTERMSIG(nStatus);
}

std::cout << "error starting ftp";

}
else if (nChildDied < 0)
{
// An error occured
if (errno == ECHILD)
{
std::cout << "no child process exists";
}
else if (errno == EINTR)
{
std::cout << "calling process interrupted by a signal";
}
else if (errno == EINVAL)
{
std::cout << "bad argument passed to waitpid";
}
}

char send[80], receive[80] = "initial value";

fprintf(stdout, "Ftp Command ? > "); fgets(send, 80, stdin);

write(fd[1],send,strlen(send));

read(fd[0],receive,strlen(receive));

std::cout << "response from ftp : " << receive << std::endl;

fprintf(stdout, "Ftp Command ? > "); fgets(send, 80, stdin);

write(fd[1],send,strlen(send));

...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

background process return code

Hi I have the following piece of code that is calling another child process archive.ksh in the background while read file; do file_name=`ls $file`; ksh archive.ksh $file_name &; done < $indirect_file The problem is, indirect_file may contain anwhere from 2 to 20 different... (5 Replies)
Discussion started by: Vikas Sood
5 Replies

2. UNIX for Advanced & Expert Users

return code of a process

two programs A and B writting in c++ I am using A to B and I want to know the return code of B. in B ------------------------ int main() { return 11; } ------------------------ in A ------------------------ int main() { system(A); } ------------------------ Is it the right way... (1 Reply)
Discussion started by: filedeliver
1 Replies

3. Programming

return code of a process

two programs A and B writting in c++ I am using A to B and I want to know the return code of B. in B ------------------------ int main() { return 11; } ------------------------ in A ------------------------ int main() { system(A); } ------------------------ Is it the right way... (1 Reply)
Discussion started by: filedeliver
1 Replies

4. Shell Programming and Scripting

Return code of background process

Hi, I have a process that I run in the background that looks like this ${BASEDIR}/ksh/sqler.ksh ${compnames003} & and I would like to get the return code of the sqler.ksh script. so my code is like this ${BASEDIR}/ksh/sqler.ksh ${compnames003} & retcode=$? (3 Replies)
Discussion started by: c19h28O2
3 Replies

5. UNIX for Dummies Questions & Answers

FTP Return Code

Hi All, I have a problem to identify the error code thrown by FTP Server while uploading files. The message is : ftp return 32. I couldn't find out what is the meaning of that. :confused: OS is Sun Solaris 2.10. Anyone can help? Thanks a lot (1 Reply)
Discussion started by: wilsonSurya
1 Replies

6. UNIX for Advanced & Expert Users

commands not working if the executed from forked process

Hi, I have an application where if it runs indivisually could able to execute commands (like system("ls")) and could able to execute tcl script. Same application if started from health monitor process (From health monitor process my application will be forked), it could not execute the... (1 Reply)
Discussion started by: chandrutiptur
1 Replies

7. UNIX Desktop Questions & Answers

ftp return code not working

below is my code , but for some reason the return part is not working, only file transfer is happening and no exit status is checked .please me help me to fix this code #!/bin/sh #set -vx ftp -nv sitelocation << ! user username password lcd localdir cd /remote dir mget *.* ... (4 Replies)
Discussion started by: gwrm
4 Replies

8. Programming

getting the return from forked child process to parent in C++

This needs to work on HPUX and Linux. I do a fork and create a child process. During execution of the child process, it is possible child become lost or get killed. That is the reason why I create the child process. However if the child process doesnt get killed, I do want to know the return... (2 Replies)
Discussion started by: usustarr
2 Replies

9. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

10. UNIX for Dummies Questions & Answers

Why is the return code of child required by parent ?

Hello everyone, I am a complete newbie to UNIX. I am using Debian LXDE 64-bit. I have a question regarding the child and parent process communication. According to wikipedia.org and various other sources, when a child process exits it sends the SIGCHLD signal to its parent... (1 Reply)
Discussion started by: sreyan32
1 Replies
PIPE(2) 						     Linux Programmer's Manual							   PIPE(2)

NAME
pipe, pipe2 - create pipe SYNOPSIS
#include <unistd.h> int pipe(int pipefd[2]); #define _GNU_SOURCE #include <unistd.h> int pipe2(int pipefd[2], int flags); DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For fur- ther details, see pipe(7). If flags is 0, then pipe2() is the same as pipe(). The following values can be bitwise ORed in flags to obtain different behavior: O_NONBLOCK Set the O_NONBLOCK file status flag on the two new open file descriptions. Using this flag saves extra calls to fcntl(2) to achieve the same result. O_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors. See the description of the same flag in open(2) for reasons why this may be useful. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS
EFAULT pipefd is not valid. EINVAL (pipe2()) Invalid value in flags. EMFILE Too many file descriptors are in use by the process. ENFILE The system limit on the total number of open files has been reached. VERSIONS
pipe2() was added to Linux in version 2.6.27; glibc support is available starting with version 2.9. CONFORMING TO
pipe(): POSIX.1-2001. pipe2() is Linux-specific. EXAMPLE
The following program creates a pipe, and then fork(2)s to create a child process; the child inherits a duplicate set of file descriptors that refer to the same pipe. After the fork(2), each process closes the descriptors that it doesn't need for the pipe (see pipe(7)). The parent then writes the string contained in the program's command-line argument to the pipe, and the child reads this string a byte at a time from the pipe and echoes it on standard output. #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, " ", 1); close(pipefd[0]); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unused read end */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); } } SEE ALSO
fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2009-09-15 PIPE(2)
All times are GMT -4. The time now is 07:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy