Thread: creat sequence process c programe unix

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

    creat sequence process c programe unix

    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 .....
    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 ?????? */
    
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This is really quite simple. Do you know how to use fork()? Give it a try. Remember that the forked process (the child) will have the same value of N as the parent. Use a loop and count N down to 0 and stop forking at that point.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    oogabooga
    thanku .... do u mean like this ....

    but still therer is error ....


    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());
     }
     }
     
    }
    Last edited by zooro; 04-17-2012 at 09:51 AM.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    when i enter any number like 3

    i only get this line without sequences...

    Creating process sequence of length 3

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    for(cont=0;cont>N;cont++)
    Take a close look at your loop condition.

    EDIT: And since fork() is *nix specific, it should go in the Linux/Unix forum like your other posts.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    Thank you .....anduril462 http://im.cprogramming.com/images/st...ser-online.png

    yah it should be ...

    for(cont=0;cont<N;cont++)

    but still it dosent work .....

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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. process creation in unix
    By taurus in forum C Programming
    Replies: 1
    Last Post: 05-12-2010, 11:04 PM
  2. Replies: 1
    Last Post: 02-04-2009, 06:54 AM
  3. Replies: 16
    Last Post: 08-25-2008, 11:08 PM
  4. could not creat process?!
    By samiei in forum C Programming
    Replies: 11
    Last Post: 02-20-2005, 08:52 AM