简体   繁体   中英

adding a 0 to an array of char* based on the argc being even or odd

so I'm working on an assignment where the user simply calls the parent.c class like "./parent 1 2 3 4 5 6 7 8" and the child.c class will compute the sum of the result.

It works by reading 2 numbers each on the line like this... "1 + 2" "3 + 4" "5 + 6" "7 + 8"

I've gotten it to do this successfully, but I hit a complete brick wall when the input ends up being odd. As I keep looping the child processes, each 2 numbers will keep adding up, but it becomes a problem when the loop comes to the end of the input and there are no longer 2 numbers to add, but simply one (odd number scenario).

So if the input became something such as... "./parent 1 2 3 4 5 6 7" or "./parent 1 2 3"

it will just flat out return as 0.

My file will compute the even numbers, but it will not add up an odd amount of numbers. The overall goal I would like to accomplish is to just be able to add a zero if the input ever hits an odd number. One attempted solution I had was to just check at the beginning of the while loop if the amount of values in data array was odd, and then increment index by one and then add a 0 to that very index. But it still didn't provide the correct solution.

parent.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>


int main(int argc, char *argv[])
{
int status, workerid, id;
int i = 0;
int loopcount = 0;


char* data[argc];
for(i = 1; i <= argc; i++)
{
  data[i] = argv[i];
}

int numberloops = argc / 2; 
int index = 0;

while(numberloops > index)
{

loopcount = 0;

for(i = 1; i <= argc; i += 2)
{
  loopcount++;

  id = fork();

  if(id < 0)
  {
    perror("fork() ERROR.\n");

  }
  else if(id > 0)
  {        
    workerid = waitpid(id, &status, 0); 

    status = WEXITSTATUS(status);

    //update data array
    char* statStr;
    statStr = (char*) malloc(16);
    snprintf(statStr, sizeof(statStr), "%d", status);
    data[loopcount] = statStr;
  }
  else
  { 
     execlp("./child", "child", data[i], data[i+1], NULL); 
  }
}

int arrayNum = atoi(data[loopcount]);

// Adds a 0 to the array.

while(loopcount < argc)
{
  loopcount++;
  data[loopcount] = 0;
} 

//change argc (number of values in data array)
if(argc % 2 == 1)
{
  argc = (argc + 1) / 2;   
}
else
{
  argc /= 2;
}

index++;
}

printf("Final Sum: %s.\n\n", data[1]);
}

child.c

 #include<stdio.h>
 #include<stdlib.h>

 int main(int argc, char *argv[])
 {
 int x = atoi(argv[1]);
 int y = atoi(argv[2]);

 int sum = x + y;

 printf("%d + %d = %d Worker PID: %d\n", x, y, sum, getpid());

 exit(sum);
 }

To get around the " odd -issue" one simple approach would be to define char * data[argc + 1]; and initialise all it's elements to point to a literal "0" before setting the relevant elements to point to argv 's elements.

It seems to me you are making the code much more complicated than need. Some specific items:

1) Why do you have two nested loops (ie a while with a for inside)? As far as I can see that will just lead to too many calls of child

2) workerid seems unused!?

3) arrayNum seems unused!?

4) Why convert the return values into strings? You want to calculate a sum so just keep it as integer

Simplifying your code could give something like:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
  int status, id;
  int i = 1;
  int sum = 0;
  int numberloops = argc / 2;
  int index = 0;

  while(numberloops > index)
  {
    id = fork();
    if(id < 0)
    {
      perror("fork() ERROR.\n");
    }
    else if(id > 0)
    {
      waitpid(id, &status, 0);
      status = WEXITSTATUS(status);
      sum += status;
    }
    else
    {
      if (i == argc-1)
      {
        execlp("./child", "child", argv[i], "", NULL);
      }
      else
      {
        execlp("./child", "child", argv[i], argv[i+1], NULL);
      }
    }

    i = i + 2;
    index++;
  }

  printf("Final Sum: %d.\n\n", sum);
}

Example:

./parent 1 2 3 4 5
1 + 2 = 3 Worker PID: 12961
3 + 4 = 7 Worker PID: 12962
5 + 0 = 5 Worker PID: 12963
Final Sum: 15.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM