Re: multithread--how can I update two text widgets in a window concurrently?



On Tue, 27 Feb 2001, godsatan wrote:

I am designing a pop3 client which can get mails of multiserver concurrently.
I try to call fork() , but I failed.
Can I use this system call "fork()"?
Hereafter is my code.
Please help me to fix it.
<...>

The problem is that you don't seem to quite undersand what fork() does.
It's purpose is to create a new process, which has *copies* of the
original's variables, stack, etc. (file descriptors in particular get
shared).  So, updating the text widget from the child process will not do
what is expected, because the child process is updating a copy, not the
actual one from the parent (which GTK deals with).  A consequence of the
file descriptor sharing is that you must call _exit() rather than exit()
in the child process, so it doesn't confuse GTK and X (GTK adds a function
to clean up using atexit() and this must not get called in the child
process).

There are several ways around this.  The easiest is to use threads
instead, if your OS supports that.  On Linux, you need LinuxThreads, which
should be a standard part of the C library if you use glibc2.x.  Make sure
you read the GTK+ FAQ questions about threads carefully, the locking stuff
is very important (all sorts of nasty stuff can happen otherwise).  For
information about the POSIX threads API, start with the pthread_create(3)
man page, or there are several tutorials around.

The other way is to continue to use fork().  You'll need some form of IPC
(Interprocess Communication) between the parent and child processes fork()
creates.  In this case, the best thing to use is probably a pipe or
socketpair (see pipe(2) and socketpair(2)).  A good place to start for
information about fork() is the FAQ from comp.unix.programmer, which can
be found at

http://www.erlenstar.demon.co.uk/unix/faq_toc.html

It has a good description of fork() and some examples.

Finally, a third (and probably the best) alternative is to do things
asynchronously, using GTK (or Glib)'s I/O monitoring facilities.  This is
probably more efficient than forking new processes for each connection.
The FAQ above has some information about select() and poll() some of which
is relevant here.  Extra information, related to sockets in particular,
can be found in the Unix Socket FAQ, at

http://www.lcg.org/sock-faq/

Jonathan








[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]