0

I have a big C code I am testing (here is the is the full code if anyone is interested but I will just put the relevant parts: https://github.com/brentmitchell25/EECS-678/blob/master/Lab6/pthreads_pc/producer_consumer.c

It is the producer-consumer problem, now at the start of the main() function I have the following:

int main (int argc, char *argv[])
{
  pthread_t *con;
  int        cons;
  int       *concount;

  queue     *fifo;
  int        i;

  pthread_t *pro;
  int       *procount;
  int        pros;

  pcdata    *thread_args;

  /*
   * Check the number of arguments and determine the numebr of
   * producers and consumers
   */
  if (argc != 3) {
    printf("Usage: producer_consumer number_of_producers number_of_consumers\n");
    exit(0);
  }

  pros = atoi(argv[1]);
  cons = atoi(argv[2]);

//that is not the entire `main()` function I just pasted that part

From my understanding, I should run this and input three numbers: first being the number of arguments argc, second being the number of producers and third being the number of consumers. However, when I compile then put anything in the command line like ./producer_consumer 3 1 1 (or any other numbers other than the 1s) I get the following:

Usage: producer_consumer number_of_producers number_of_consumers

I think this implies that I should follow this format? but I am not sure if producer_consumer is the program's name or simply another argument I should write.

so my question is: does the code detect argc itself or do I have to mention it?

note: in my examples I put argc as 3 as the first entry argv[0] is reserved for the program's name from my understanding

Sergio
  • 275
  • 1
  • 15
  • *"does the code detect argc itself or do I have to mention it"* It's set automatically. If you know that `argv[0]` is reserved for the program name, why do you input 3 rather than 2 arguments? – HolyBlackCat Feb 27 '21 at 08:04
  • *"does the code detect argc itself or do I have to mention it?"* - Why don't you just try leaving it out? Especially since the usage guide is pretty specific about how the program should be invoked. – klutt Feb 27 '21 at 08:07
  • I input 3 1 1 for example to indicate that the number of arguments is 3 (my 2 arguments and the program name) then 1 for the number of producers and the other one for the number of consumers, but since you say that it is set automatically then I don't have to do this @HolyBlackCat thank you – Sergio Feb 27 '21 at 08:09
  • Oh! I didn't realize that `3` was an attempt at setting `argc`. – HolyBlackCat Feb 27 '21 at 08:12
  • @Sergio I marked this question as a duplicate of another question, so it's not possible to answer this. – klutt Feb 27 '21 at 08:38

0 Answers0