Thread: child printing sequences - how do i make it efficient

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    1

    child printing sequences - how do i make it efficient

    Code:
     #include<stdio.h>#include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<string.h>
    
    
    void forking(int n);
    
    
    int main() {
    
    
    int i=0;
    
    
    int n=3;
    
    
    
    
    char child0[12]={'A','b','c','d'};
    char child1[4]={'E','f','g','h'};
    char child2[4]={'I','j','k','l'};
    
    
    
    
    forking(n);
    
    
    
    
    return 0;
    
    
    
    
    }
    
    
    void forking(int n) {
    
    
    
    
    
    
    pid_t pid;
    
    
    char child[4]={'A','b','c','d'};
    char child1[4]={'E','f','g','h'};
    char child2[4]={'I','j','k','l'};
    
    
    
    
    int i=0;
    
    
    int status;
    
    
    
    
    while(i<n) {
    
    
    pid=fork();
    i++;
    
    
    
    
    if(pid<0) { perror("fork");}
    
    
    else if (pid==0) {
    
    
    char c;
    
    
    
    
    printf("Hi I Am Process PID %d \n\n",getpid());
    
    
    
    
    
    
    if(n==1){
       for(c = 'A'; c <= 'D'; ++c){
       printf("%c ", c);}
    printf("\n " );
    }
    
    
    if(n==2){
       for(c = 'E'; c <= 'H'; ++c){
       printf("%c ", c);}
    printf("\n " );
    }
    if(n==3){
       for(c = 'I'; c <= 'L'; ++c){
       printf("%c ", c);}printf("\n " );
    }
    
    
    
    
    exit(1);
    
    
    }
    
    
    else {
    
    
    wait(&status);
    
    
    forking(n-1);
    
    
    exit(0);
    
    
     }}
    
    
    printf("Good Bye From Parent %d \n",getpid());
    
    
    
    
    
    
    
    
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can make it efficient to read by applying a consistent indentation style.
    Indent style - Wikipedia

    Like so.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<string.h>
    
    void forking(int n);
    
    int main()
    {
      int i = 0;
      int n = 3;
    
      //!! these have no use in this scope
      char child0[12] = { 'A', 'b', 'c', 'd' };
      char child1[4] = { 'E', 'f', 'g', 'h' };
      char child2[4] = { 'I', 'j', 'k', 'l' };
    
      forking(n);
    
      return 0;
    }
    
    
    void forking(int n)
    {
      pid_t pid;
      char child[4] = { 'A', 'b', 'c', 'd' };
      char child1[4] = { 'E', 'f', 'g', 'h' };
      char child2[4] = { 'I', 'j', 'k', 'l' };
      int i = 0;
      int status;
    
      while (i < n) {
        pid = fork();
        i++;
        if (pid < 0) {
          perror("fork");
        }
        else if (pid == 0) {
          char c;
          printf("Hi I Am Process PID %d \n\n", getpid());
          if (n == 1) {
            for (c = 'A'; c <= 'D'; ++c) {
              printf("%c ", c);
            }
            printf("\n ");
          }
          if (n == 2) {
            for (c = 'E'; c <= 'H'; ++c) {
              printf("%c ", c);
            }
            printf("\n ");
          }
          if (n == 3) {
            for (c = 'I'; c <= 'L'; ++c) {
              printf("%c ", c);
            }
            printf("\n ");
          }
          exit(1);
        }
        else {
          wait(&status);
          forking(n - 1);
          exit(0);
        }
      }
    
      printf("Good Bye From Parent %d \n", getpid());
    }
    You can make it further efficient by not using fork(), and not using recursion.

    I mean, recursion inside a while loop - seriously.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Make This code more efficient
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 04-12-2010, 01:29 AM
  2. Please help me make this more efficient
    By griffind in forum C Programming
    Replies: 5
    Last Post: 01-23-2009, 01:26 AM
  3. How can I make a hash table for my sequences?
    By advancedk in forum C Programming
    Replies: 6
    Last Post: 08-01-2008, 12:25 PM
  4. make these functions more efficient
    By waxydock in forum C Programming
    Replies: 9
    Last Post: 04-20-2007, 12:46 AM
  5. Printing out a data table in different sequences
    By TheDudeAbides in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 12:06 AM

Tags for this Thread