két processz kommunikációja HELP

Fórumok

két processz kommunikációja HELP

Hozzászólások

[quote:137ad3ff0b="gsimon"]A hibák:
- az execlp, (mint a többi exec fgv.) nem tér vissza, hanem az aktuális processz _helyett_ indítja a megadottat
- a wait nem a várandó pid-et veszi át, hanem egy int státuszra mutató pointert. ami adott pid-re is tud várni, az a waitpid
- az fdwrite nem int, hanem "FILE*" típusú kell, hogy legyen
- mivel alapból nem szinkronra lett nyitva, fflush kell, hogy tényleg ki is íródjon rá az adat, (így csak 4kbyte után kezdene hozzá)
- a main visszatérési típusa definíció szerint int
- két szükséges header nem volt behúzva

Amúgy valami szőr van a karburátorban, mert ha a fenti betűzött sort csak úgy kiírom, akkor kapok egy olyan üzenetet, hogy tiltott html tag-eket használok. Hmm, már Shakespeare is megmondta, hogy több dolgok vannak földön s égen, mintsem bölcselmem álmodni képes...

Köszi a segítséget!

A gyerek folyamat nem kell hogy visszatérjen, ezért használtam az execlp-t.
Ha setstringbuf-ot használom, akkor viszont nem kell a fflush, mert újsor karakter után, azonnal üríti a buffert.

tudtommal ha a main-nek nem adunk meg visszatérési értéket, akkor az alapon int marad. Az csak akkor változik, ha valaki mást ír helyébe.

[quote:9c917c21dd="Jedite"]A gyerek folyamat nem kell hogy visszatérjen, ezért használtam az execlp-t. Ha setstringbuf-ot használom, akkor viszont nem kell a fflush, mert újsor karakter után, azonnal üríti a buffert.

Mindkettőben igazad van, ott a pont.
[quote:9c917c21dd="Jedite"]tudtommal ha a main-nek nem adunk meg visszatérési értéket, akkor az alapon int marad. Az csak akkor változik, ha valaki mást ír helyébe.

Úgy van, csak így meg sírt, hogy function should return a value. Egyébként mivel a progit indító script/progi ezt kapja meg visszatérési érték gyanánt, célszerű int-en tartani :).

Volna most megint egy kis problémám a kommunikációval a programok közt.
Most a visszafelé beszédről volna szó.

A C program 3 sztringet kap a skripttől., Amelyeket ki kell hogy írja!
A C kód:
[code:1:6a0f23ae95]#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>

main() {
int Pipe[2];
int status;
FILE* fdread;
pid_t pid;
char buffer[256];

printf("making the pipes\n");
if(pipe(Pipe)<0) {
printf("Error by creating the pipe!\n");
exit(0);
}

fdread=fdopen(Pipe[0], "r");
setlinebuf(fdread);

printf("forking the proccess\n");
pid=fork();

if(pid==0) {
printf("running the child proccess\n");

printf("dupping the pipe end\n");
if(dup2(Pipe[1],1)<0) {
printf("Error, by dupping the pipe!\n");
exit(0);
}

printf("executing the outer program\n");
if(execlp("./givename", NULL)<0) {
printf("Error executing program!\n");
exit(0);
}

} else {
printf("running the mother proccess\n");

printf("Getting the data: \n");
fgets(buffer, sizeof(buffer), fdread);
printf("the name is: %s \n", buffer);
sleep(1);
fgets(buffer, sizeof(buffer), fdread);
printf("the secret is: %s \n", buffer);
sleep(1);
fgets(buffer, sizeof(buffer), fdread);
printf("the ip is: %s \n", buffer);

printf("End\n");

wait(&status);
exit(0);
}

} /*main end*/[/code:1:6a0f23ae95]

A script meg 3 printf paranccsal nyomja ki a sztringeket.

És az alábbi kimenetet kapom, ha az stdout-ra nyomom:

making the pipes
forking the proccess
running the child proccess
dupping the pipe end
running the mother proccess
Getting the data:
the name is: executing the outer program

the secret is: pista

the ip is: kispista

End

Míg ha egy file-ba irányítom a kimenetet, akkor ez lesz:

making the pipes
forking the proccess
running the mother proccess
Getting the data:
the name is: pista

the secret is: kispista

the ip is: 192.168.0.14

End

Szemmel láthatóan egyik kimenetem sem jó.
És nem tudom miért csúszik így el a dolog. Próbáltam sleep() beszúrásával a szülő folyamatba, de a gyerek mintha szintén megvárná a sleep()-et és csak utánna futna tovább és indítaná el a külső programot.

Üdvözletem!

Alábbi gonddal szebvedek:

Van egy progi, ami futás közben létrehoz egy pipe-ot, majd forkolja magát. A gyerek folyamat dup2-ővel átállítja a szabványos bemenetét a cső kimenetére, majd futtat egy shell scriptet, ami a szabványos bemenetre vár egy sztringet.

Eközben a szülőfolyamat vár egy kicsit, majd fdopen-nel filedscriptor-ként megnyitja a pipe bementi végét és belerak fputs-sel, egy sztringet. És végül megvárja, hogy leálljon a gyerekfolyamat, majd ő is leáll.

A problémám az volna, hogy a szülő által elküldött sztringet a gyerekfolyamat láthatóan nem kapja meg.

Ez a C kód:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

main() {
int Pipe[2];
int fdwrite;
pid_t pid;

printf("making the pipe\n");
if(pipe(Pipe)<0) {
printf("Error by creating the pipe!\n");
exit(0);
}

printf("making the file descriptors\n");

printf("forking the proccess\n");
pid=fork();

if(pid==0) {
printf("running the child proccess\n");

printf("dupping the pipe end\n");
if(dup2(Pipe[0],0)<0) {
printf("Error, by dupping the pipe!\n");
exit(0);
}

printf("executing the outer program\n");
if(execlp("./getname", NULL)<0) {
printf("Error executing program!\n");
exit(0);
}

} else {
sleep(1);
printf("running the mother proccess\n");

fdwrite=fdopen(Pipe[1], "w");

printf("sending name in pipe\n");
if(fputs("ez a string\n", fdwrite)<0) {
printf("Error while writing in pipe\n");
exit(0);
}

printf("wait child to terminate\n");
wait(pid);

}

} /*main end*/

Előre is köszönöm!

Amilyen gyorsan kiabáltam a segítségért, olyan gyorsan meg is oldódott a dolog!

A hiba az volt, hogy a bufferkezelést nem állítottam be, hogy stringenként kezelje.

setlinebuf(fdwrite);

Így már minden működik!
:o

A hibák:
- az execlp, (mint a többi exec fgv.) nem tér vissza, hanem az aktuális processz _helyett_ indítja a megadottat
- a wait nem a várandó pid-et veszi át, hanem egy int státuszra mutató pointert. ami adott pid-re is tud várni, az a waitpid
- az fdwrite nem int, hanem "FILE*" típusú kell, hogy legyen
- mivel alapból nem szinkronra lett nyitva, fflush kell, hogy tényleg ki is íródjon rá az adat, (így csak 4kbyte után kezdene hozzá)
- a main visszatérési típusa definíció szerint int
- két szükséges header nem volt behúzva

Ezek alapján javítva:[code:1:f59a006633]
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

int main(void) {
int Pipe[2], status;
FILE *fdwrite;
pid_t pid;

printf("making the pipe\n");
if(pipe(Pipe)<0) {
printf("Error by creating the pipe!\n");
exit(0);
}

printf("making the file descriptors\n");

printf("forking the proccess\n");
pid=fork();

if(pid==0) {
printf("running the child proccess\n");

printf("dupping the pipe end\n");
if(dup2(Pipe[0],0)<0) {
printf("Error, by dupping the pipe!\n");
exit(0);
}

printf("executing the outer program\n");
if(system("/bin/bash -c /tmp/test.sh")<0) {
printf("Error executing program!\n");
exit(0);
}

} else {
sleep(1);
printf("running the mother proccess\n");

fdwrite=fdopen(Pipe[1], "w");

printf("sending name in pipe\n");
if(fputs("fflush\n", fdwrite)<0) {
printf("Error while writing in pipe\n");
exit(0);
}
fflush(fdwrite);

printf("wait child to terminate\n");
wait(&status);

}
return 0;
} /*main end*/
[/code:1:f59a006633]
Az a /tmp/test.sh, amit próbaképp meghív:
[code:1:f59a006633]
#!/bin/bash
read
#### Erre a sorra anyázik a fórummotor, úgyhogy betűzöm:
echo macskaköröm Ezt kaptam kettőspont szóköz dollár REPLY macskaköröm nagyobbjel /tmp/test.log
[/code:1:f59a006633]
Amúgy valami szőr van a karburátorban, mert ha a fenti betűzött sort csak úgy kiírom, akkor kapok egy olyan üzenetet, hogy tiltott html tag-eket használok. Hmm, már Shakespeare is megmondta, hogy több dolgok vannak földön s égen, mintsem bölcselmem álmodni képes...