Article View: be.comp.programming
Article #16692Re: multithreading onder C++
From: Philip
Date: Thu, 13 Apr 2000 00:00
Date: Thu, 13 Apr 2000 00:00
95 lines
3308 bytes
3308 bytes
gusto wrote:
>
> Als ik bij het maken van linux-KDE/QT programma's in C++ met Kdevelop,
> processen op de achtergrond wil laten uitvoeren.
> Hoe gaat dit het eenvoudigste ?
> "Fork()" werkt wel, maar vind ik niet echt ideaal, is er geen ander
> commando ? (eventueel een KDE of QT commando)
fork() kopieerd het lopende programma naar het geheugen. En vanuit dit
nieuw programma kan je dan je process opstarten zodat dit process in de
plaats van het nieuwe komt.
Dit is een voorbeeldje dat ik ooit eens heb gemaakt :
(het start een programma of script ./start met 4 argumenten ... en wacht
tot dit programma beindigt is). Het is wel c , maar in c++ zal dit niet
veel verschillen.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
//#include <stdio.h>
int unixspawn(char *in1, char arg[][]) {
pid_t pid;
int n,g;
int exit_code;
pid = fork();
switch(pid) {
case -1:
exit(1);
case 0:
execv(in1,arg);
n = 5;
exit_code = 37;
break;
default:
n = 3;
exit_code = 0;
break;
}
for(; n > 0; n--);
if(pid) {
int stat_val;
pid_t child_pid;
child_pid = wait(&stat_val);
if(WIFEXITED(stat_val))
return WEXITSTATUS(stat_val);
else
return -1;
}
}
int main()
{
int x;
char *arg[5];
for (x=0;x<5;x++) {
arg[x] = (char *) malloc(sizeof(char)*10);
/* if you want args bigger than 9 increase here
*/
if (arg[x] == NULL)
return (1);
}
strcpy(arg[0],"argument 1");
strcpy(arg[1],"argument 2");
strcpy(arg[2],"argument 3);
strcpy(arg[3],"argument 4");
arg[4]=0;
printf("%s\n","Parent");
printf("%d\n",unixspawn("./start",arg));
printf("%s\n","Parent");
for (x=0;x<5;x++) /* it is important free resources */
free(arg[x]);
return (0);
}
> --
> Your mouse has moved. Windows must be restarted for the change
> to take effect. Reboot now?
--
Philip van Hoof aka freax _/_/_/
http://users.pandora.be/skatan _/_/ _/ _/_/ _/_/_/ _/_/ _/ _/
mailto:freax@pandora.be _/_/ _/ _/ _/ _/ _/ _/ _/
Running Win.on a Pentium is _/_/_/_/ _/ _/ _/_/ _/_/_/_/ _/_/
like a Porsche, driving back _/_/ _/ _/ _/ _/ _/ _/
wards with the handbrake on _/_/ _/ _/_/_/ _/ _/ _/ _/
Technical Details
Message-ID:
<38F5E137.8BA89239@pandora.be>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<38F57442.F20A0F45@student.kuleuven.ac.be>Show Full Headers
Complete Article Headers
X-Google-Language: DUTCH,ASCII-7-bit X-Google-Thread: fcea4,6b125efd35a6e602 X-Google-Attributes: gidfcea4,public From: Philip <freax@pandora.be> Subject: Re: multithreading onder C++ Date: 2000/04/13 Message-ID: <38F5E137.8BA89239@pandora.be> X-Deja-AN: 610693558 Content-Transfer-Encoding: 7bit References: <38F57442.F20A0F45@student.kuleuven.ac.be> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@pandora.be X-Trace: afrodite.telenet-ops.be 955638071 195.130.128.118 (Thu, 13 Apr 2000 17:01:11 MET DST) Organization: http://users.pandora.be/skatan MIME-Version: 1.0 Reply-To: freax@pandora.be NNTP-Posting-Date: Thu, 13 Apr 2000 17:01:11 MET DST Newsgroups: be.comp.programming