Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

2 children , 2 jobs with execv

Posted on 2011-03-25
3
Medium Priority
?
493 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 85

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 85

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

824 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