Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a problem in using system()

Status
Not open for further replies.

13sio

Technical User
May 21, 2003
141
MO
hello,

i'm writing a interface by gtk+2.0 (a c based language) in linux. it's a window and have some buttons inside. when click a button, the program will call another software by system() command. if i do so, the interface window will halt. i wanna call another software and the window works normally.

thank you for your helping.
 
The behaviour you observe is valid, the system() call doesn't return until the called application terminates.

You need to look at forking off a separate thread to do the work and examine it's return code when it finally completes. This allows your main GUI thread to continue to run.

You could fork off a new "worker" thread every time you need to run a system() command and have a reaping process to monitor the exit codes from these threads.
 
hi,

thx newmangj.

running the following code under RedHat 8.0,

----------------
#include <pthread.h>
#include <stdio.h>

void cb(void)
{
printf(&quot;%s&quot;, &quot;hi&quot;);
}

int main (int argc, char *argv[])
{
pthread_t threads;
int rc;
rc = pthread_create(&threads, NULL, (void *)cb, NULL);
if (rc) {
printf(&quot;ERROR; return code from pthread_create() is %d\n&quot;, rc);
}
return 0;
}
----------------

there are statements as follows

/thread.o In function `main':
/thread.o(.text+0x3c): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
 
Are you linking the pthreads library? - try adding
-lpthread to you gcc command line

Cheers - Gavin
 
thx newmangj again. but i don't know how to add -lpthread in my makefile. the manual of gcc is hard to understand for me. here is my makefile.

------------
CC = gcc

app: window.o dialog.o callback.o simset.o filesel.o
$(CC) window.o dialog.o callback.o simset.o filesel.o -o app -lpthread `pkg-config --libs gtk+-2.0`

window.o: window.c dialog.h callback.h filesel.h
$(CC) -c window.c -o window.o `pkg-config gtk+-2.0 --cflags`

dialog.o: dialog.c dialog.h
$(CC) -c dialog.c -o dialog.o `pkg-config gtk+-2.0 --cflags`

callback.o: callback.c callback.h simset.h filesel.h
$(CC) -c callback.c -o callback.o -lpthread `pkg-config gtk+-2.0 --cflags`

simset.o: simset.c simset.h callback.h filesel.h
$(CC) -c simset.c -o simset.o `pkg-config gtk+-2.0 --cflags`

filesel.o: filesel.c filesel.h
$(CC) -c filesel.c -o filesel.o `pkg-config gtk+-2.0 --cflags`

clean:
rm -f *.o
------------

and the results are shown below.

gcc -c callback.c -o callback.o -lpthread `pkg-config gtk+-2.0 --cflags`
gcc: -lpthread: linker input file unused because linking not done
gcc -c simset.c -o simset.o `pkg-config gtk+-2.0 --cflags`
gcc -c filesel.c -o filesel.o `pkg-config gtk+-2.0 --cflags`
gcc window.o dialog.o callback.o simset.o filesel.o -o app -lpthread `pkg-config --libs gtk+-2.0`

another, sorry for the last question...it was raised and solved several days ago.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top