?
Solved

2 children , 2 jobs with execv

Posted on 2011-03-25
3
Medium Priority
?
491 Views
Last Modified: 2013-12-26
a parent process creates 2 children.
child1: ls
child2: ask for 2 numbers, and add them
but output is not what I really expect...
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
int pid1, pid2;
char* args[] = {"/bin/ls",(char*)0};
      p1=fork();
      if(p1!=0){
                printf("\n parent");
                wait(0);
      }
      else{//1st child
                 p2=fork();
                 if(p2!=0){//1st child
                           printf("\n result of ls is..\n ");
                           execv("/bin/ls",args);
                           wait(0);
                           exit(0);
                           }

                 else{//2nd child
                     printf("\n give 2 ints separ. with space \n");
                     scanf("%d  %d",&a,&b);
                     c=a+b;
                     printf("\n summ of the 2 numbers is: %d\n",c);
                     exit(0);
                 }//end 2nd child
      }//end 1st child
      return 0;
}//end main 

Open in new window

0
Comment
Question by:dimi67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 35213871
p1, p2, a, b, c are undeclared

/bin/ls is closing stdin
either dup it or
               pid2=fork();
                 if(pid2!=0){//1st child                                                    
                   wait(0);
                   printf("\n result of ls is..\n ");
                   execv("/bin/ls",args);
                   exit(0);
                 }
                 else{//2nd child                                                          
                   int a,b,c;                                                            
                   printf("\n give 2 ints separ. with space \n");
                   scanf("%d  %d",&a,&b);
                   c=a+b;
                   printf("\n summ of the 2 numbers is: %d\n",c);
                   exit(0);
0
 

Author Comment

by:dimi67
ID: 35214082
what do you mean "/bin/ls is closing stdin"? It means I cannot read from keyboard?
And why dup it?
0
 
LVL 84

Expert Comment

by:ozo
ID: 35214195
          o   The child process has its own copy of the parent's descriptors.
               These descriptors reference the same underlying objects, so
               that, for instance, file pointers in file objects are shared
               between the child and the parent, so that an lseek(2) on a
               descriptor in the child process can affect a subsequent read or
               write by the parent.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question