Solved

2 children , 2 jobs with execv

Posted on 2011-03-25
3
476 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
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
zeroMAx challenge 20 87
Message not shown 5 33
Way to decrease size of apk file 9 43
WiFi Router device supports GPON! 3 30
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now