Solved

2 children , 2 jobs with execv

Posted on 2011-03-25
3
490 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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