Discussion:
Start Acrobat Reader from my application on unix systems, fork/exec/pthread???
(too old to reply)
Andrea
2003-12-02 16:08:52 UTC
Permalink
Hi,

I am working on a C application that has to run on Windows, Linux, Sun
and HP. This application has to start the Acrobat Reader to show some
pdf help files. On windows, it is running know, but I am not so good
on the unix process model.

The following code starts the Acrobat Reader:

void OpenHelpfile()
{
int pid, ret;

if (pid=fork())
{
return;
}
else
{
ret = execl("/home/as12242/Acrobat5/bin/acroread","acroread",NULL);
}
}

But then my problems begin:

1. the user can open a helpfile very often, and as I understand fork,
there is always created a copy of my current process - that might be
very memory intensive with the 10th help file!

2. my idea on problem 1 is to find out if acrobat is alreay running,
and only if not, I call fork. Is there something like:
if (findProcess(childPID) == false)
{
fork
exec(acroread)
}

3. and what is about pthread - might this be a alternative to fork? Is
there any useful documentation on the internet or in a book? I need
some background and concept know-how about these functions and some
good examples on how to use them. getting fork and exec running only
with man-pages is very hard!

Thanks for any help!

Andrea
Giancarlo Niccolai
2003-12-04 12:57:06 UTC
Permalink
You must use fork()
Post by Andrea
1. the user can open a helpfile very often, and as I understand fork,
there is always created a copy of my current process - that might be
very memory intensive with the 10th help file!
do a "ps -A" command from prompt. You are probably running about 100
processes on a pretty empty machine. 10 or 100 more won't cause too much
trouble. In fact, processes under certain unix flavor (as linux) are so
light that they are seamlessy used for threads and for full-weight
processes.

I.e. all /cgi-bin/ based WWW scripts you find are launched as separate
processes. This is a reason why MS internet server is not very popular...
windows processes are pretty heavy.
Post by Andrea
2. my idea on problem 1 is to find out if acrobat is alreay running,
if (findProcess(childPID) == false)
{
fork
exec(acroread)
}
Yes. Install manuals and do man proc (or scan the internet for man page
proc). The /proc directory has the full list of processes as numeric entry:

[***@head gian]$ ls /proc/2*

...

/proc/2924:
binfmt cmdline cwd@ environ exe@ fd/ maps mem mounts root@ stat
statm status

...
[***@head gian]$ cat /proc/3761/cmdline
/bin/bash

[***@head gian]$ cat /proc/3761/stat
3761 (bash) S 2959 3761 3761 34819 3965 0 1177 2042 354 4857 5 2 11 6 15 0 0
0 585083 2777088 413 4294967295 134512640 135113640 3221223168 3221222492
1074648647 0 65536 3686404 1266761467 3222404850 0 0 17 0



All you have to do is use opendir()/readdir() to peek through all the
processes, and find one with the same cmdline entry OR just with the some
process name (second fscanf entry in stat file).
Post by Andrea
3. and what is about pthread - might this be a alternative to fork?
No.
Post by Andrea
there any useful documentation on the internet or in a book? I need
some background and concept know-how about these functions and some
good examples on how to use them. getting fork and exec running only
with man-pages is very hard!
For threads, man pthread_create is a very good starting point.

if you use KDE and konqueror, type "man:pthread" in the address field; a
dropdown list will show you all the arguments you can read. This also can
be achieved with apropos pthread from command line.

Bests,
Giancarlo.
Patrick TJ McPhee
2003-12-04 22:51:40 UTC
Permalink
In article <***@posting.google.com>,
Andrea <***@schmutt.de> wrote:

% 1. the user can open a helpfile very often, and as I understand fork,
% there is always created a copy of my current process - that might be
% very memory intensive with the 10th help file!

It replaces it, but


You might consider using xpdf. I'm not sure what the provisions are
for bundling it, but it has a remote mode that can be accessed
via a command-line switch. It doesn't cut out the cost of fork(),
but it will keep one help window all the time.

% 2. my idea on problem 1 is to find out if acrobat is alreay running,
% and only if not, I call fork. Is there something like:
% if (findProcess(childPID) == false)

Sure, call waitpid with the WNOHANG option, then clear out childPID
if it returns successfully.

% 3. and what is about pthread - might this be a alternative to fork?

No. fork() is the only (until posix_spawn() comes into wide use)
way to create a new process in Unix, and you need a new process.

Of course, depending on the licence conditions, you might be able
to embed a viewer based on xpdf, in which case you could do it in
a thread.
--
Patrick TJ McPhee
East York Canada
***@interlog.com
Patrick TJ McPhee
2003-12-05 20:30:32 UTC
Permalink
In article <bqodps$ria$***@news.eusc.inter.net>,
Patrick TJ McPhee <***@interlog.com> wrote:
% In article <***@posting.google.com>,
% Andrea <***@schmutt.de> wrote:
%
% % 1. the user can open a helpfile very often, and as I understand fork,
% % there is always created a copy of my current process - that might be
% % very memory intensive with the 10th help file!
%
% It replaces it, but

Sorry, I started to say that fork() makes a copy of the current process,
which it then replaces with the image of your new program when you
exec(), but this doesn't usually mean making a full copy of the process
on most current systems. In particular, the text portion (the program
itself) is usually shared between both copies, and the data portion
may be shared until it's modified. This can result in just the stack being
copied before you've had a chance to exec(), which is roughly the same
as what happens when you start a thread.
--
Patrick TJ McPhee
East York Canada
***@interlog.com
Loading...