LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2006, 03:33 AM   #1
wasp
LQ Newbie
 
Registered: Mar 2005
Posts: 9

Rep: Reputation: 0
write() not reaching child process


Code:
void die()
{
  perror("fatal");
  _exit(112);
}

int main()
{
  int pfds[2];
  int pid;
  int stat;
  char *args[5];

  if (pipe(pfds) == -1) die();

  switch (pid = fork()) {
    case -1:
      die();
      break;
    case 0:
      if (close(0) == -1) _exit(112);
      if (dup2(pfds[0], 0) == -1) _exit(112);
      args[0] = "/usr/bin/sort";
      args[1] = 0;
      if (execve(args[0], args, 0) == -1) _exit(112);
      break;
   default:
      if (write(pfds[1], "a\nb\nc\n", 6) == -1) die();
      if (waitpid(pid, &stat, 0) == -1) die();
      break;
  }

  return 0;
}
The write() call is sucessful, but no output is seen from
/usr/bin/sort and the program hangs. I'm guessing that
sort waits for input on stdin but for some reason never
receives it. The parent process sits at the waitpid()
call forever.

What did I do wrong?
 
Old 07-07-2006, 06:16 AM   #2
debulu
Member
 
Registered: May 2006
Location: India
Distribution: Redhat
Posts: 49

Rep: Reputation: 15
Code:
  Try this modified code:

void die()
{
  perror("fatal");
  exit(112);
}

int main()
{
  int pfds[2];
  int pid;
  int stat;
  char *args[5];

  if (pipe(pfds) == -1) die();

  switch (pid = fork()) {
    case -1:
      die();
      break;
    case 0:
      if (close(0) == -1) die();
      if (dup2(pfds[0], 0) == -1) die();
      close(pfds[0]);
      close(pfds[1]);
      args[0] = "/bin/sort";
      args[1] = 0;
      if (execve(args[0], args, 0) == -1) die();
   default:
      close(pfds[0]);
      if (write(pfds[1], "a\nb\nc\n", 6) == -1) die();
      close(pfds[1]);
      if (waitpid(pid, &stat, 0) == -1) die();
      break;
  }

  return 0;
}
From the code you must be understanding that You have to close the unused Fds. In child You have to close both read and write as you are duplicating the read as stdin.In parent you have to close read fd first and then close the write fd.
NB: My machine sort is in /bin
enjoy

Last edited by debulu; 07-07-2006 at 06:24 AM.
 
Old 07-07-2006, 04:01 PM   #3
wasp
LQ Newbie
 
Registered: Mar 2005
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks, it works.
 
  


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
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
process and child process in java xhi Programming 4 03-28-2006 10:36 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 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