LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2012, 09:11 AM   #1
mrneo
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
Question execlp() doesnt wait for the input??


i am just starting with programming in linux.i am trying to fork a child process and replace it with the "sort" program. below is the code iam using for this.

i expeceted that when i invoked sort programm it would wait for me to give it input but instead the program just terminates.

how can i make the child process take input interactively?

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main ()
{
pid_t pid;
pid = fork ();
if (pid == (pid_t) 0) {
execlp ("sort","sort",0);
}
else {
waitpid (pid, NULL, 0);
}
return 0;
}
 
Old 11-02-2012, 09:15 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
I don't see any code in your post. Why isn't this in the programming forum?

Code:
execl("/bin/sort", "sort" , "input_filename",  (char *)0 );
That won't wait because the input file should be there already.
Code:
execl("/bin/sort", "sort" , "-",  (char *)0 );
That should take standard input.
 
Old 11-02-2012, 10:22 AM   #3
mrneo
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
@linosaurusroot
i missed to paste the code first time i submitted the code but then immediately i did edit and pasted it. however below is the code. i tried execl("/bin/sort", "sort" , "-", (char *)0 ) but it didnt work, the program just terminates.

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main ()
{
pid_t pid;
pid = fork ();
if (pid == (pid_t) 0) {
execlp ("sort","sort",0);
}
else {
waitpid (pid, NULL, 0);
}
return 0;
}
 
Old 11-03-2012, 05:18 AM   #4
mrneo
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
sorry about the confusion, my code has a line which was duplicating the standard input of child process to a stream created in the parent process.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
execlp() doesnt wait for the input?? mrneo Linux - Newbie 1 11-02-2012 09:16 AM
C++. cin does not wait for an input by the operator iqbala Programming 7 01-11-2010 12:32 PM
PHP (CGI) Wait For User Input? br00tal Programming 2 05-21-2008 11:03 AM
How to make a script wait for input farmerjoe Linux - General 2 12-28-2004 05:18 AM
Make script wait for input farmerjoe Linux - General 4 12-28-2004 01:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration