process sequence

This is a discussion on process sequence within the Linux Programming forums, part of the Platform Specific Boards category; hello ...I dont know if here is the right place for my question : Write a C program linear.c that ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    15

    process sequence

    hello ...I dont know if here is the right place for my question :
    Write a C program linear.c that creates a sequence of processes with a given length. By
    sequence it is meant that each created process has exactly one child.
    Let's look at some example outputs for the program.
    Here the entire process sequence consists of process 18181:
    Sara@dell:~/OSSS$ ./linear 1
    Creating process sequence of length 1.
    18181 begins the sequence.
    An example for a sequence of length three:
    Sara@dell:~/OSSS$ ./linear 3
    Creating process sequence of length 3.
    18233 begins the sequence.
    18234 is child of 18233
    18235 is child of 18234


    ........ this is coad .... BUt i could not compleate it .....http://im.cprogramming.com/images/smilies/frown.png
    Code:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    int main(int argc, char *argv[]) {
    
    int N;
    pid_t pid;
    
    int cont;
    
    if (argc != 2) {
    printf("Wrong number of command-line parameters!\n");
    return 1;
    }
    
    N = atoi(argv[1]);
    
    printf("Creating process sequence of length %d.\n",N);
    
    printf("%d begins the sequence.\n",getpid());
    
    /* What I have to do next ?????? */
    
    }
    Last edited by zooro; 04-17-2012 at 10:18 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    You need to use the fork function with a simple loop. Type "man fork" from your command line for the documentation on the function, and Google for some examples. Here's one site to get you started: Introduction to C/Unix Multiprocess Programming - Fork and Exec - We'll just go through some very simple example code, to see how to use fork() and execl()..

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    what is wrong ....?!!!
    i dont get right ....any hints??


    Code:
     #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    int main(int argc, char *argv[]) {
    
    int N;
    pid_t pid;
    
    int cont;
    
    if (argc != 2) {
    printf("Wrong number of command-line parameters!\n");
    return 1;
    }
    
    N = atoi(argv[1]);
    
    printf("Creating process sequence of length %d.\n",N);
    
    printf("%d begins the sequence.\n",getpid());
    
    /* What I have to do next ?????? */
     for(cont=0;cont<N;cont++)
    
     {
     if(fork() == 0){
     
    printf("%d is child of %d \n",getpid(),getppid());
     }
     }
     
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Fix your indentation, it's horrible. Clear, readable code is an imperative, especially when working with others, like you are doing on this forum. If you don't have a style you like yet, I recommend one of the first 3 styles mentioned here: Indent style - Wikipedia, the free encyclopedia. They're widely used in the C world.

    Then, you need to work on providing a clear description of what is wrong. Does this code compile? If not, post the exact error messages with line numbers (copy-paste the compiler output). If it does compile, do you get no output or the wrong output? Provide the incorrect output. Does it crash?

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    One problem is that both the child AND the parent are going to keep looping, but you don't want the parent to continue looping. You want the parent to break out of the loop and then wait for its child.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    Here what I should get if I enter the following

    (An example for a sequence of length three):

    Sara@dell:~/OSSS$ gcc –o file file.c
    Sara@dell:~/OSSS$ ./file 3
    Creating process sequence of length 3.
    18233 begins the sequence.
    18234 is child of 18233
    18235 is child of 18234


    Now I am getting the following :
    Sara@dell:~/OSSS$ gcc –o file file.c
    Sara@dell:~/OSSS$ ./file3
    Creating process sequence of length 3.
    13324 begins the sequence.
    Sara@dell:~/OSSS$ 13327is child of 1.
    13326is child of 1.
    13325is child of 1.
    13330is child of 1.
    13328is child of 1.
    13329is child of 1.
    13331is child of 1.
    Sara@dell:~/OSSS$

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Well, post your code ... and remember to break out of the loop if it's the parent (if the return value of fork() is not 0).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    Thank u oogabooga http://im.cprogramming.com/images/st...ser-online.png
    i did not get your point ...why I breack out the loop??!!

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    int main(int argc, char *argv[]) {
     
    int N;
    pid_t pid;
     
    int cont;
     
    if (argc != 2) {
    printf("Wrong number of command-line parameters!\n");
    return 1;
    }
     
    N = atoi(argv[1]);
     
    
    printf("Creating process sequence of length %d.\n",N);
     
    printf("%d begins the sequence.\n",getpid());
     
    /* What I have to do next ?????? */
     for(cont=0;cont<N;cont++)
     
     {
     if(fork() == 0){
      
    printf("%d is child of %d \n",getpid(),getppid());
     }
     }
      
    }

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Also, remember your other thread (here fork ()), where I told you to read up on the wait() funciton? Well, do that, and pay particular attention to the "NOTES" section, it will explain why it says "child process of 1".

    As for why you break out of the loop, you must remember, after a call to fork, there is both a parent and a child process that are at the exact same spot in execution.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    i did not get your point ...why I breack out the loop??!!
    I didn't say to "breack" out of anything. I don't even know what that means. If you're not even going to try, then forget it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    I Got it ..... it were fork the parent every time where it should fork the child.......Here the right coad ......
    Code:
    #include <stdio.h>
     #include <sys/types.h>
     #include <unistd.h>
     #include <stdlib.h>
     
     int main (int argc, char *argv[])
     {
     int N;
     pid_t pid,child_pid;
     int cont;
     
     if (argc !=2)
     {
     printf("Wrong number of command-line parameters!\n");
     return 1;
     }
     
     N = atoi (argv[1]);
     printf("Creating process sequence of length %d.\n", N);
     printf("%d begins the sequence.\n",getpid());
     
     
     
     
     child_pid= fork();
     for (cont =0 ; cont<N-1; cont++)
     {
     
     pid=getpid();
     
     if ( child_pid==0)
     {
     
     printf("%dis child of %d.\n", getpid(),getppid());
     child_pid=fork();
     }
     wait();
     
     }
     }
    

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creat sequence process c programe unix
    By zooro in forum C Programming
    Replies: 6
    Last Post: 04-17-2012, 11:12 AM
  2. how to get process info ( to extract process thread id )
    By umen242 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2009, 01:08 PM
  3. % sequence
    By goran00 in forum C Programming
    Replies: 9
    Last Post: 01-28-2008, 04:48 PM
  4. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 08:36 PM
  5. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 01:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21