c - fork(), pipe() and exec() process creation and communication -


i have write program create process using pipe().

my first task write parent process generates 4 child processes using fork() function.

once fork() successful, replace child process process rover1, rover2, rover3, , rover4, though of them have same code.

the function of processes follows.

  1. each child process given own number. receives new number parent. using following formula creates own new number follows , forwards parent:

    mynumber = (3 * mynumber + 4 * numberreceived)/7

  2. this process continues until parent sends message system stable. parent has initial number. receives numbers of children , computes new number follows:

    mynumber = (3 * mynumber + numbers sent children)/7

  3. the parent send number children. process continue until parent finds number not changing anymore. @ time tell children system has become stable.

this did professor said have use exec() execute child , replace child process child process. not sure how use exec(). please me this.

i attaching first child generation.

// included stdio.h, unistd.h stdlib.h , errno.h  int main(void) {   // values returned 4 fork() calls   pid_t rover1, rover2, rover3, rover4;    int parentnumber, mynumber1, mynumber2, mynumber3, mynumber4;    int childownnumber1 = 0, status = 1, childownnumber2 = 0,       childownnumber3 = 0, childownnumber4 = 0, numberreceived = 0;    printf("enter parent number: ");   printf("%d", parentnumber);   printf("enter each children number");   printf("%d %d %d %d", mynumber1, mynumber2, mynumber3, mynumber4);    // create pipes communication between child , parent   int p1[2], p2[2];   // attempt open pipe   if (pipe(p1) == -1) {     perror("pipe call error");     exit(1);   }   // attempt open pipe   if (pipe(p2) == -1) {     perror("pipe call error");     exit(1);   }    // parent process generates 4 child processes   rover1 = fork();    // if fork() returns 0, we're in child process;   // call exec() each child replace process   if (rover1 == 0) {     for(; numberreceived != 1; ) {         close(p1[1]); // close write end of pipe       close(p2[0]); // close read end of second pipe        // read parent's number pipe       read(p1[0], &numberreceived, sizeof(int));        if (numberreceived == 1) {         // system stable, end child process         close(p1[0]);         close(p2[1]);         _exit(0); // end child process       }        mynumber1 = (int)((3*mynumber1 + 4*numberreceived)/7.0);        printf("\nrover1 number: ");       printf("%i", mynumber1);        // write pipe       write(p2[1], &mynumber1, sizeof(int));         }          }   /* error:    * if fork() returns negative number, error happened;    * output error message    */   if (rover1 < 0) {     fprintf(stderr,             "can't fork, child process 1 not created, error %d\n",             errno);     exit(exit_failure);   } } 

the exec family of functions used replace current process new process. note use of word replace. once exec called, current process gone , new process starts. if want create separate process, must first fork, , exec new binary within child process.

using exec functions similar executing program command line. program execute arguments passed program provided in call exec function.

for example, following execcommand* equivalent subsequent shell command:

execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);

/bin/ls -r -t -l

* note "arg0" command/file name execute


since homework, important have understanding of process. start reading documentation on pipe, fork, , exec combined few tutorials gain better understanding each step.

the following links should started:


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -