PDA

View Full Version : Problem using java programs within C code



lemania
05-07-2005, 09:33 PM
Hi guys, I am new to systems programming using C and on the Linux platform.i got a rather demoralizing problem, after developing my program over the weekend.

I was tasked to develop an application which instructed me to use small java programs within my C code to call the small GUI programs using exepl. I have managed to code most of the code execpt after clicking on the OptionDialog( also a java based GUI program) i could not return to my main Listbox(java based Listbox)and select other files. Below is a general algorithm of how i used the pipe,fork and exepl commands.

Aplogies if the code is too long or i hope some C systems programming expert could help me out. My guess is with the inner forks at processFileDesc_3 and processFileDesc_4.I have the full code and relevant java programs on hand.If anyone is interested in helping me out, drop me and email and i will send out the tar file.


main(int argc,char * argv[])

{
int processFileDesc_1[2];

int processFileDesc_3[2];
int processFileDesc_4[2];


pipe(processFileDesc_1);

if(fork () != 0 )
{
//************************************************** ************************
// Start of parent process processFileDesc_1
//************************************************** ************************

int processFileDesc_2[2];
pipe(processFileDesc_2);

if(fork()== 0)
{
//************************************************** ********************
// Start of child process processFileDesc_2
//************************************************** ********************
close(processFileDesc_2[1]);
dup2(processFileDesc_2[0],0);
close(processFileDesc_2[0]);

char inputFile[MAXPATHLEN];

while(scanf("%s",inputFile) > 0)
{ if(strstr(inputFile,".java")) // checks if the file is a java file
{

int processFileDesc_3[2];

int processFileDesc_4[2];

pipe(processFileDesc_3);



if (fork() == 0)

{
//************************************************** **************
// start of child process processFileDesc_3
//************************************************** **************

close(processFileDesc_3[1]);

dup2(processFileDesc_3[0],0);

close (processFileDesc_3[0]);

pipe(processFileDesc_4);


if(fork() == 0)

{
//************************************************** ************
// start of child process processFileDesc_4
//************************************************** ************


char userInput[20];

close(processFileDesc_4[1]);

dup2(processFileDesc_4[0],0);

close(processFileDesc_4[0]);


while(scanf("%s",userInput) > 0)

{


if(strstr(userInput,"Edit"))
{
if(fork() == 0)
{
printf("File to be opened is %s\n",inputFile);


execlp("gedit","gedit",inputFile, NULL);
}

}

else if(strstr(userInput,"Compile"))
{

printf("File to be compiled is %s\n",inputFile);




}

}//end if while
//************************************************** ************
// End of child process processFileDesc_4
//************************************************** ************

}//end if fork == 0 for processFileDesc_4

else

{
//************************************************** ************
// Start of parent process processFileDesc_4
//************************************************** ************


close(processFileDesc_4[0]);

dup2(processFileDesc_4[1],1);

close(processFileDesc_4[1]);

execlp("java","java","ButtonSelect","Please make a choice", NULL);

//************************************************** ************
// End of parent process processFileDesc_4
//************************************************** ************



}//end else fork ==0 for processFileDesc_4

//************************************************** ****************
// End of child process processFileDesc_3
//************************************************** ****************

}//end else fork ==0 for processFileDesc_3

else

{
//************************************************** **************
// Start of parent process processFileDesc_3
//************************************************** **************


close(processFileDesc_3[0]);

dup2(processFileDesc_3[1],1);

close(processFileDesc_3[1]);

execlp("echo","echo","-e","Edit\nCompile",NULL);

//************************************************** **************
// End of parnent process processFileDesc_3
//************************************************** **************

}//end else fork ==0 for processFileDesc_3


}//end else (strstr == java)

}//end while
}//end if

//************************************************** **********************
// End of child process processFileDesc_2
//************************************************** **********************
else
{
//************************************************** ********************
// Start of parent process processFileDesc_2
//************************************************** ********************
close(processFileDesc_2[0]);
dup2(processFileDesc_2[1],1);

close(processFileDesc_2[1]);

close (processFileDesc_1[1]);

dup2(processFileDesc_1[0],0);

close(processFileDesc_1[0]);

execlp("java","java","ListSelect","-persistent",NULL);

//************************************************** ********************
// End of parent process processFileDesc_2
//************************************************** ********************
}//end else
//************************************************** ************************
// End of parent process processFileDesc_1
//************************************************** ************************
}//end if
else
{
//************************************************** ************************
// Start of child process processFileDesc_1
//************************************************** ************************

printdir(toBeListed,processFileDesc_1[1]);
//************************************************** ************************
// End of child process processFileDesc_1
//************************************************** ************************

}//end else


}//end main

Salem
05-08-2005, 02:02 AM
Some general comments
1. When posting code on a message board, set your text editor to use spaces for indentation (not tabs). Tabs get interpreted differently by every message board, and by every browser looking at that board. So for me, your code is a mess before anything else happens.

2. All your comments are useless - remove them or add something better. At the moment, they're just bloating the code for no good reason.

3. Don't use unreadable constants


#include <unistd.h>
#define PIPE_RD 0
#define PIPE_WR 1

Then code like this
dup2(processFileDesc_2[0],0);
would be


dup2(processFileDesc_2[PIPE_RD],STDIN_FILENO);

Which on the whole is more readable, and it enables you to make sense of which direction a particular pipe is meant to be going in.

4. I'd suggest you use some functions to break up the code into more manageable blocks - it's over 200 lines long as it is, with 5 nested levels of fork()
Say for example a function which just looks after edit/compile. With any luck, you should then be able to just test that function in isolation from all the other stuff you have going on.

5.
> int processFileDesc_3[2];
> int processFileDesc_4[2];
You have two lots of these, declared at different scopes.
The outer ones are looking redundant.