Linux programming help

This is a discussion on Linux programming help within the C Programming forums, part of the General Programming Boards category; I'm trying my hand with a little unix programming, but my second execvp in main() below doesn't appear to be ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    10

    Linux programming help

    I'm trying my hand with a little unix programming, but my second execvp in main() below doesn't appear to be working correctly.
    A sample call to this would be ./rungenerator 20 1 and the output is as follows:
    parent: 27461
    child: 27462
    0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
    child: 27463

    the output should be:
    parent: 27461
    child: 27462
    0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
    child: 27463
    1 3 5...etc

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(int argc, char* argv[]){
      int fd[2];
      pid_t child[2];
      char *args[4];
    
      if(argc != 3){
        write(2,"Invalid # of arguments\n",23);
        return 1;
      }
    
      args[0]="./generate1";
      args[2]=argv[1];
      args[3]=argv[2];
      args[4]="0";
    
      printf("parent: %ld\n",(long) getpid());
    
      if(child[0] = fork())
        child[1] = fork();
    
      if(!child[0]){
        printf("child: %ld\n",(long) getpid());
        args[1]="even";
        execvp(args[0],args);
      }
      if(!child[1]){
        printf("child: %ld\n",(long) getpid());
        args[1]="odd";
        execvp(args[0],args);
      }
    
      return 0;
    }
    here's the code to generate1:
    Code:
    int main(int argc, char* argv[]){
      int which, i, num, fd;
      char buf[3];
    
      if(argc != 4){
        write(2,"Invalid # of arguments\n",23);
        return 1;
      }
    
      if(!strcmp(argv[1],"odd"))
        which=1;
      else if(!strcmp(argv[1],"even"))
        which=2;
      else{
        write(1,"Must be odd or even!\n",21);
        return 2;
      }
    
      srand(which);
    
      fd = atoi(argv[3]);
    
      /* if we're counting 0 as even */
      if(which==2)
        write(fd,"0 ",2);
    
      num = atoi(argv[2]) * 2;
      for(i=which;i<num;i=i+2){
        sprintf(buf,"%d ",i);
        write(fd,buf,3);
      }
      write(fd,"\n",1);
    
      return 0;
    }
    Any help would be appreciated. Thanks, PJ.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Code:
    #ifdef SLAVE
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    int main(int argc, char* argv[]){
      int which, i, num, fd;
      char buf[3]; /* !! This is way too small given your following usage */
      /*!! it's only large enough for "N \0" (a single digit followed by a space) */
    
      if(argc != 4){
        write(2,"Invalid # of arguments\n",23);
        return 1;
      }
    
      if(!strcmp(argv[1],"odd"))
        which=1;
      else if(!strcmp(argv[1],"even"))
        which=2;
      else{
        write(1,"Must be odd or even!\n",21);
        return 2;
      }
    
      srand(which); /*!! pointless, you're not calling rand() */
    
      fd = atoi(argv[3]);
    
      /* if we're counting 0 as even */
      /*!! so why not start with which=0 then if you want to count 0 ? */
      if(which==2)
        write(fd,"0 ",2);
    
      num = atoi(argv[2]) * 2;
      for(i=which;i<num;i=i+2){
        sprintf(buf,"%d ",i);
        write(fd,buf,3);
      }
      write(fd,"\n",1);
    
      return 0;
    }
    #else
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(int argc, char* argv[]){
      int fd[2];
      pid_t child[2];
      char *args[5];  /*!! not 4, you have an argv[4] below */
    
      if(argc != 3){
        write(2,"Invalid # of arguments\n",23);
        return 1;
      }
    
      args[0]="./generate1";
      args[2]=argv[1];
      args[3]=argv[2];
      args[4]=NULL; /*!! NOT "0"; */
    
      printf("parent: %ld\n",(long) getpid());
    
      if ( (child[0] = fork()) == 0 ) {
        printf("child: %ld\n",(long) getpid());
        args[1]="even";
        execvp(args[0],args);
      }
      if ( (child[1] = fork()) == 0 ) {
        printf("child: %ld\n",(long) getpid());
        args[1]="odd";
        execvp(args[0],args);
      }
    
      return 0;
    }
    #endif
    
    
    
    $ gcc -DSLAVE -o generate1 hello.c
    $ gcc hello.c
    $ ./a.out 10 1 && sleep 2
    parent: 26283
    child: 26284
    0 2 4 6 8 10 12 14 16 18
    child: 26285
    1 3 5 7 9 11 13 15 17 19
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    10
    thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 11:40 AM
  2. Wireless Network Linux & C Testbed
    By james457 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-11-2009, 11:03 AM
  3. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  4. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 04:15 AM

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