LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2018, 02:17 PM   #1
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Rep: Reputation: Disabled
subprogram called at run time


I have an embedded program (runs in continuous loop) that needs to call one of several subprograms, depending on circumstances. I don't need to have the subprograms loaded in memory permanently.
So, the following program represents the embedded program:
Code:
// test.c
#include<stdio.h>

int main(){
  struct {int i, j; float x, y; char *string;}subinput;
  int i;
  subinput.i = 7;
  subinput.j = 11;
  subinput.x = 3.14159;
  subinput.y = 13.4567;
  subinput.string = "Hello world";
  
  i = execve("subprog", &subinput, "./");
  printf("i = %d\n", i);
}
and here is its subprogram:
Code:
 //subprog.c
#include<stdio.h>
#include<stdlib.h>

typedef struct common common;

int main(struct common {int i, j; float x, y; char *string;} *input){
  printf("You are here\n");
  printf("i = %d, j = %d, x = %f, y = %f, string = %s!\n", input->i, input->j, input->x, input->y, input->string);
  return(3);
}
test prints "i = -1".
Suggestions?
 
Old 07-30-2018, 02:38 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,151

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
See this in the man pages
Code:
...
execve() does not return on success, and the text, initialized data, uninitialized data (bss), and stack of the calling process are overwritten according to the contents of the newly loaded program.
...
When I need to run and get data from an external program I use the 'popen' call.
 
1 members found this post helpful.
Old 07-30-2018, 02:52 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Also main's parameters ('int argc' and 'char **argv) are predefined, you cannot re-define them.
 
1 members found this post helpful.
Old 07-30-2018, 04:53 PM   #4
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Original Poster
Rep: Reputation: Disabled
Well, phooey.
Thank you.

Quote:
Originally Posted by NevemTeve View Post
Also main's parameters ('int argc' and 'char **argv) are predefined, you cannot re-define them.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Each time a script is called. A log file is created with time and date + Bash Script. y0_gesh Programming 6 08-17-2012 03:16 AM
What's it called where Wallpapers change colors over time of day? barnac1e Linux - Software 3 07-12-2012 12:39 PM
script works when run but not when called by cron grob115 Linux - Server 7 08-28-2010 11:24 AM
First Time - Trying to Use GPIO Expander Driver called pca953x.c Peatmoss Linux - Embedded & Single-board computer 1 08-23-2010 05:44 PM
getenv Returns different pointers every time it is called? worldgnat Programming 3 08-15-2008 09:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration