Solved

2 children , 2 jobs with execv

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 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

12 Experts available now in Live!

Get 1:1 Help Now