Trying to pass variables to child process

Dylan

I'm trying to pass variables to a function that forks, most seem to work, but I need to give each an id which I'm trying to pass in an argv[5] which isn't declared before. My code keeps seg faulting and I can't figure out why. Any ideas?

main

char* arg_list_consumer[] = {
        "./consume",
        argv[1], 
        argv[2],
        NULL
    };
    char* arg_list_producer[] = {
        "./produce",
        argv[1], 
        argv[2],
        argv[3],
        argv[4],
        argv[5],
        NULL
    };
   if (getppid()==1){
        producerID=0;
   }
   if(producerID==0){
        for(i=1; i<P; i++){
            sprintf(arg_list_producer[5], "%d", i);
            spawn ("./produce", arg_list_producer);
        }
        for(i=0; i<C; i++){
            sprintf(arg_list_consumer[5], "%d", i);
            spawn ("./consume", arg_list_consumer);
        }
    }

spawn

int spawn (char* program, char** arg_list)
{
  pid_t child_pid;
  /* Duplicate this process.  */
  child_pid = fork();

  if (child_pid != 0)
    /* This is the parent process.  */
    return child_pid;
  else {
    /* Now execute PROGRAM, searching for it in the path.  */
    execvp (program, arg_list);
    /* The execvp function returns only if an error occurs.  */
    fprintf (stderr, "an error occurred in execvp\n");
    abort ();
  }
}
master_latch

arg_list_consumer is declared to only have 4 entries. Thus, it is an invalid memory operation to index into it with [5].

If you want to assign a value to arg_list_consumer[5], you must declare the array to be at least of size 6 pointers (or you can throw in a few more NULLs to make it have a size of at least 6 pointers).

Then, if you want to use sprintf to store something, you must already have space allocated in the destination string. Since you just have an array of pointers, this means you must use malloc to allocate some space:

arg_list_consumer[5] = malloc(sizeof(char)*10);
sprintf(arg_list_consume[5], "%d", i);

(I used 10 but you should obviously use whatever length you think necessary).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pass socket to child process

From Dev

Pass mach port to child process

From Dev

How to pass a password to a child process?

From Dev

PageViewController - Pass variables to child views

From Dev

Trying to pass parameters from Master to child template

From Dev

Pass a Buffer to a Node.js Child Process

From Dev

Pass run time arguments to Nodejs child process

From Dev

Get Environment Variables of Child Process in VC++

From Dev

Get Environment Variables of Child Process in VC++

From Dev

Trying to understand how to pass variables to a CustomValidation?

From Dev

Trying to pass variables outside of input functions Python

From Dev

Trying to pass two variables in a bash script

From Dev

Pass data from parent process to its child process — IPC, UNIX

From Dev

Trying to write to an int in shared memory (using mmap) with a child process

From Dev

How to pass function/callback to child process in Node.js?

From Dev

How to pass an input and retrieve an output to a child process in C language

From Dev

NodeJS - How to pass a mysql connection from main to child process?

From Dev

Qt C++ - How to successfully pass data to child process?

From Dev

Access environment variables passed in docker run from supervisor child process

From Dev

Access environment variables passed in docker run from supervisor child process

From Dev

How do you hide and pass variables in knitr child documents?

From Dev

Pass variables from child template to parent in Jinja2

From Dev

Is it possible to pass environment variables from child to parent in user space?

From Dev

Trying to pass variables to Send-MailMessage arguments and failing miserably

From Dev

Error trying to pass in variables when calling MSI file

From Dev

Trying to Pass Multiple Variables from Multiple Windows through AJAX

From Dev

How to pass messages as well as stdout from child to parent in node.js child process module?

From Dev

Linux pass value between parent process and child process using pipe in c?

From Dev

Linux pass value between parent process and child process using pipe in c?

Related Related

  1. 1

    pass socket to child process

  2. 2

    Pass mach port to child process

  3. 3

    How to pass a password to a child process?

  4. 4

    PageViewController - Pass variables to child views

  5. 5

    Trying to pass parameters from Master to child template

  6. 6

    Pass a Buffer to a Node.js Child Process

  7. 7

    Pass run time arguments to Nodejs child process

  8. 8

    Get Environment Variables of Child Process in VC++

  9. 9

    Get Environment Variables of Child Process in VC++

  10. 10

    Trying to understand how to pass variables to a CustomValidation?

  11. 11

    Trying to pass variables outside of input functions Python

  12. 12

    Trying to pass two variables in a bash script

  13. 13

    Pass data from parent process to its child process — IPC, UNIX

  14. 14

    Trying to write to an int in shared memory (using mmap) with a child process

  15. 15

    How to pass function/callback to child process in Node.js?

  16. 16

    How to pass an input and retrieve an output to a child process in C language

  17. 17

    NodeJS - How to pass a mysql connection from main to child process?

  18. 18

    Qt C++ - How to successfully pass data to child process?

  19. 19

    Access environment variables passed in docker run from supervisor child process

  20. 20

    Access environment variables passed in docker run from supervisor child process

  21. 21

    How do you hide and pass variables in knitr child documents?

  22. 22

    Pass variables from child template to parent in Jinja2

  23. 23

    Is it possible to pass environment variables from child to parent in user space?

  24. 24

    Trying to pass variables to Send-MailMessage arguments and failing miserably

  25. 25

    Error trying to pass in variables when calling MSI file

  26. 26

    Trying to Pass Multiple Variables from Multiple Windows through AJAX

  27. 27

    How to pass messages as well as stdout from child to parent in node.js child process module?

  28. 28

    Linux pass value between parent process and child process using pipe in c?

  29. 29

    Linux pass value between parent process and child process using pipe in c?

HotTag

Archive