Is Viking inside C an infinite loop?

An attempt to learn C-branching. It correctly prints the number of starts of the main loop and the correct number of threads, but the runtime is turned off and the program never ends. Am I doing an infinite number of processes?

After some suggestions, there is a cleaner version of the code here. The old version is below. The updated part still creates many child processes and never exits. I just don’t see what is going wrong.

Update: John Haskall's proposal fixed formatting and streams that did not work. An infinite number of threads are still generated, but now in the correct order. Ie prints the execution time of thread 1, 2, 3, 4 ... etc. Do not think that the problem is syscall expectations, but he will study it and see if I can find anything.

Update **: I found a solution. The first problem, in my opinion, was that I did not have the wait command, and the second - when transferring the wait, I accidentally deleted the check for count <ARGV [1]. I returned it and it seems to be working correctly! Thanks for the help and style pointers! The working version is below.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "./processes.h"

int main(int argc, char** argv) {
  if (argc != 4) {
    printf("Wrong number of arguments entered. Usage: #processes sleepTime inputFile.\n");
    return 1;
  }

  if(atoi(argv[1]) <= 0){
    printf("Incorrect number of children, must be greater than 0");
    return -1;
  }

  int count = 0;
  int index;
  Child *child = malloc(sizeof(Child) * atoi(argv[1]));
  int childIndex;
  int pid;

  do{
    switch (pid = fork()){
      case -1:
        printf("Fork failed\n");
        exit(1);

      case 0:
        sleep(atoi(argv[2]) * childIndex);
        gettimeofday(&child[childIndex].endTime, NULL);
        double elapsed = child[childIndex].endTime.tv_usec - child[childIndex].startTime.tv_usec;
        printf("Time for process %d = %f microseconds\n", childIndex, elapsed);
        break;

     default: 
        childIndex = count + 1;
        gettimeofday(&child[count].startTime, NULL);
        child[count].index = count + 1;
        child[count].pid = pid;
        count++;
    }
  } while((wait(NULL) != -1) && (count < atoi(argv[1])));

  return 1;
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "./processes.h"

int main(int argc, char** argv) {
  if (argc != 4) {
    printf("Wrong number of arguments entered. Try again.");
    return 1;
  }

  if(atoi(argv[1]) <= 0){
    printf("Incorrect number of children, must be greater than 0");
    return -1;
  }

  int count;
  int index;
  Child *child = malloc(sizeof(Child) * atoi(argv[1]));
  int pid = 1;
  int childIndex;

  for (count = 0; count < atoi(argv[1]); count++) {
    if (pid != 0) {
      childIndex = count + 1;
      gettimeofday(&child[count].startTime, NULL);
      child[count].index = count + 1;
      pid = fork();

      if (pid != 0){
        child[count].pid = pid;
        printf("Main thread loop: %d\n", count);
        printf("Child process: %d\n", getpid());
      } 
    }
  }

  if (pid == 0) {
    //this is the child process
    sleep(atoi(argv[2]) * childIndex);
    gettimeofday(&child[childIndex].endTime, NULL);
    double elapsed = child[childIndex].endTime.tv_usec - child[childIndex].startTime.tv_usec;
    printf("Time for process %d = %f microseconds\n", childIndex, elapsed);

    //printf("This is thread %d reporting in.\n", childIndex);
  }

//  printf("Testing\n");
return 1;
}
+4
source share
1 answer

The biggest problem is your child code:

if (pid == 0) {
    ....
}

( ) :

if (pid != 0) {
    ....
}

, pid == -1 ( fork()).

- :

switch (pid = fork()) {
    case -1:
         /* handle fork error */
         exit(1);
    case 0:
         /* child code goes here */
         _exit(0);
    default:
         /* parent code goes here */
}
/* Also you probably want to look into the `wait()` syscall. */
do {} while (wait(NULL) != -1);       /* <--- the very minimum */
+5

Source: https://habr.com/ru/post/1622846/


All Articles