Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » GTK+
  • » Pointers and spawning processes with glib [RSS Feed]

#1 Dec. 10, 2010 00:40:20

Rupert S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Pointers and spawning processes with glib


Hi,

I'm writing something using glib's process spawning features. A silly
example of what I'm doing might look something like this:static void
foo (const gchar* path)
{
gboolean ret;
gint stdout;
gchar* argv = { "/usr/bin/man", "-R", "utf-8", NULL, NULL };

argv = path;

ret = g_spawn_async_with_pipes (NULL, argv, NULL, 0, NULL, NULL,
NULL, NULL, &stdout, NULL, &err);
if (!ret) {
// Do error handling here
}

// Use the file descriptor from stdout.
}I've just been browsing through the code in glib's gspawn.c and I
*think* that, since it works by calling fork() then exec(), the strings
pointed to by argv are copied on the line with the
g_spawn_async_with_pipes() call.

My first question is, is this right? That is, am I safe to write

g_free (path)

just before the error-checking code, for example? (Yes, I know that
would be silly, but I want to understand what's going on :-) )

If so, I have a second question/suggestion. Should the documentation for
the async spawning functions have an explicit comment saying that the
memory is copied? Maybe developers with more experience with fork() and
exec() see that this is obvious but I, for one, didn't!

Thanks,

Rupertpgp4hYnoV2wH4.pgpDescription:PGP signature_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Attachments:
attachment pgp4hYnoV2wH4.pgp (315 bytes)

Offline

#2 Dec. 10, 2010 02:02:08

Lex T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Pointers and spawning processes with glib


On 10 December 2010 11:36, Rupert Swarbrick <rswarbr***@*mail.com> wrote:
> Hi,
>
> I'm writing something using glib's process spawning features. A silly
> example of what I'm doing might look something like this:
>
>
>
> I've just been browsing through the code in glib's gspawn.c and I
> *think* that, since it works by calling fork() then exec(), the strings
> pointed to by argv are copied on the line with the
> g_spawn_async_with_pipes() call.

Since spawn creates a new process, the argv and envp arrays must be
copied because the new process cannot access the address space of this
process.

>
> My first question is, is this right? That is, am I safe to write
>
>  g_free (path)

yes

>
> just before the error-checking code, for example? (Yes, I know that
> would be silly, but I want to understand what's going on :-) )
>
> If so, I have a second question/suggestion. Should the documentation for
> the async spawning functions have an explicit comment saying that the
> memory is copied? Maybe developers with more experience with fork() and
> exec() see that this is obvious but I, for one, didn't!
>

It is pretty obvious it must be copied, even on windows where
fork/exec doesn't exist.

Cheers
Lex


> Thanks,
>
> Rupert
>
> _______________________________________________
> gtk-list mailing list
> gtk-l***@*nome.org
>http://mail.gnome.org/mailman/listinfo/gtk-list>
>
_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#3 Dec. 10, 2010 08:02:30

Robert P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Pointers and spawning processes with glib


On Fri, 10 Dec 2010 13:01:50 +1100 Lex wrote:
>
> It is pretty obvious it must be copied, even on windows where
> fork/exec doesn't exist.

It's pretty obvious if:
1) you understand how processes work and differ from threads and...
2) you like to think about how things work under the hood

For a lot of users, like Rupert, it's not at all obvious. I don't think
adding a note in the documentation is too much effort, and would be of
some benefit.


Cheers,
Rob
_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#4 Dec. 10, 2010 08:25:42

Lex T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Pointers and spawning processes with glib


On 10 December 2010 19:02, Robert Pearce <r...@bdt-home.demon.co.uk> wrote:
>
> On Fri, 10 Dec 2010 13:01:50 +1100 Lex wrote:
>>
>> It is pretty obvious it must be copied, even on windows where
>> fork/exec doesn't exist.
>
> It's pretty obvious if:
>  1) you understand how processes work and differ from threads and...
>  2) you like to think about how things work under the hood
>
> For a lot of users, like Rupert, it's not at all obvious. I don't think
> adding a note in the documentation is too much effort, and would be of
> some benefit.
>

I am not criticising Rupert, he's been reading the code, good, but he
didn't go far enough, fork/exec is well explained in many books and
Google-able documents, starting with Wikipedia.

The Glib reference documentation is not intended to be an introduction
to any topic. Programmers should not expect to use just a reference
manual if they do not understand the concepts behind it.

So if a programmer doesn't know anything about processes, hashes, URIs
or any of the other concepts handled in Glib( or windowed graphics
topics in GTK+) they should look at the introductory material
available elsewhere. The reference manual would become too big and
unwieldy if it tried to be a conceptual introduction to all its topics
as well.

Don't get me wrong, I think that the GLib/GTK+ documentation has lots
of problems, but this isn't one of them.

Cheers
Lex

>
> Cheers,
> Rob
> _______________________________________________
> gtk-list mailing list
> gtk-l***@*nome.org
>http://mail.gnome.org/mailman/listinfo/gtk-list>
_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

#5 Dec. 10, 2010 16:11:57

Rupert S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Pointers and spawning processes with glib


Lex Trotman <ele***@*mail.com> writes:

> On 10 December 2010 19:02, Robert Pearce <r...@bdt-home.demon.co.uk> wrote:
>>
>> On Fri, 10 Dec 2010 13:01:50 +1100 Lex wrote:
>>>
>>> It is pretty obvious it must be copied, even on windows where
>>> fork/exec doesn't exist.
>>
>> It's pretty obvious if:
>>  1) you understand how processes work and differ from threads and...
>>  2) you like to think about how things work under the hood
>>
>> For a lot of users, like Rupert, it's not at all obvious. I don't think
>> adding a note in the documentation is too much effort, and would be of
>> some benefit.
>>
>
> I am not criticising Rupert, he's been reading the code, good, but he
> didn't go far enough, fork/exec is well explained in many books and
> Google-able documents, starting with Wikipedia.
>
> The Glib reference documentation is not intended to be an introduction
> to any topic. Programmers should not expect to use just a reference
> manual if they do not understand the concepts behind it.
>
> So if a programmer doesn't know anything about processes, hashes, URIs
> or any of the other concepts handled in Glib( or windowed graphics
> topics in GTK+) they should look at the introductory material
> available elsewhere. The reference manual would become too big and
> unwieldy if it tried to be a conceptual introduction to all its topics
> as well.
>
> Don't get me wrong, I think that the GLib/GTK+ documentation has lots
> of problems, but this isn't one of them.

Ok, so naturally I don't think I'm stupid :-)

More to the point, I don't think that I was being lazy. By the time I
sent that email, I was absolutely certain that the memory was copied
(I'd even read about how copy-on-write semantics makes "copying
everything" not utterly crazy). But, since this was the first post I've
made to this list, I wanted to make sure that I didn't start with "The
following change should be made to the documentation" when there was the
slightest glimmer of a chance I might have been wrong about what was
going on.

Right. Now about "knowing about processes". In hindsight, yes, once the
other process is running, it can't possibly talk about the same memory
(without some sort of clever shared memory thing going, which isn't the
point of this discussion). However, I was worried that maybe there was
some handler, maybe called on exit of the child process or something,
which would read the memory at the address I'd passed to the function
earlier. I don't *think* this is a completely crazy thing to wonder
about. Looking at the code for the implementation, I see that there
isn't. But surely one should be able to use a library without reading
the implementation to work out how?

Indeed, most APIs (intend to) document what they do for pointer
arguments which look like there's some chance they might be
retained. "We'll take control of this memory. Don't free it" for
example.

Anyway, this was meant to be a quick question, not the start of a flame
war, so I'll shut up now. Thank you both very much for the responses.

RupertpgpV6aNeuCkIO.pgpDescription:PGP signature_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Attachments:
attachment pgpV6aNeuCkIO.pgp (315 bytes)

Offline

#6 Dec. 10, 2010 16:47:49

Petr T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Pointers and spawning processes with glib


>
> The Glib reference documentation is not intended to be an introduction
> to any topic. Programmers should not expect to use just a reference
> manual if they do not understand the concepts behind it.
>
> So if a programmer doesn't know anything about processes, hashes, URIs
> or any of the other concepts handled in Glib( or windowed graphics
> topics in GTK+) they should look at the introductory material
> available elsewhere. The reference manual would become too big and
> unwieldy if it tried to be a conceptual introduction to all its topics
> as well.

But perhaps a short note with a link wouldn't make it too big?

> Don't get me wrong, I think that the GLib/GTK+ documentation has lots
> of problems, but this isn't one of them.
>
> Cheers
> Lex

--
Petr Tomasek <http://www.etf.cuni.cz/~tomasek>
Jabber: but***@*abbim.cz

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EA 355:001 DU DU DU DU
EA 355:002 TU TU TU TU
EA 355:003 NU NU NU NU NU NU NU
EA 355:004 NA NA NA NA NA
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

  • Root
  • » GTK+
  • » Pointers and spawning processes with glib [RSS Feed]

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support