PDA

View Full Version : [C][C++] quale errore?


_fast_
21-03-2009, 04:39
è tutta la notte che cerco di fare questo semplice albero:

ho scritto questo codice:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
pid_t pid;
int level;
int fila;
fila=0;


for(level = 0; level <= 5; level++ ){


if (fork()==0){ // FIGLIO
fila++;
exit(0);
}
else //PADRE
{
if (fork()){
if (fork()){
fila++;
}
}
}
if (fila!=level)
{
if (fila%2==0) //FILE PARI
{
wait();
}
else
{
pid_t wait();
}
}
else //ULTIMA FILA
{
sleep(30);
}

}


cosa sbaglio?????????

_fast_
21-03-2009, 04:46
ora ho messo anche l'allegato!
scusa!


mentre gli errori che ricevo sono:
arbol.c:in function 'main':
arbol.c:34: error too few arguments to function wait;
arbol.c:46: expected decleration or statement at the end of input.

sottovento
21-03-2009, 06:30
- Alla wait() dovrai passare qualche parametro, altrimenti non sa per quale motivo aspettare;

- pid_t wait(); e' solo una dichiarazione di prototipo. E' proprio quello che volevi fare? Dubito!

- Il numero di parentesi aperte/chiuse e' errato. Ne devi chiudere una piu', quantomeno.

Cmq il programma che hai scritto non implementa l'albero di processi che avevi in mente. Suggerimento: devi per forza farlo con un solo ciclo?

_fast_
21-03-2009, 17:33
è tutto il giorno che ci lavoro ma non ne vengo fuori..
ho sistemato il codice ma non mi funziona..

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[]){
..pid_t pid;
..int uscita,pidf1,pidf2,status;
..int fila, nivel;
..fila=0;
..printf("escribes el nivel: ");
..scanf(" %d", &nivel);


..while(nivel==fila){ //SERVE UN WHILE? UN FOR? SOLO IF/THIS? NON CI CAPISCO PIU'..
//FIGLIO
....if(fork()==0) {
......fila++;
......exit(0);
....}

....else {
//PADRE
......if(fork()) {
........if(fork()) {
......}
....}

....wait(&status);
....fila++;
..}

//SE LA FILA E' PARI:
..if (fila!=nivel) {
...if ((fila%2)==0) {
......wait(&status);
......}else{
........pid_t wait();
......}
....}
//ULTIMA FILA
....else{
......sleep(30);
....}
..}
}

//senza puntini: (per fare copia/incolla per il buon uomo che vuole provarlo)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[]){
pid_t pid;
int uscita,pidf1,pidf2,status;
int fila, nivel;
fila=0;
printf("escribes el nivel: ");
scanf(" %d", &nivel);

while(nivel==fila){
//FIGLIO
if(fork()==0) {
fila++;
exit(0);
}

else {
//PADRE
if(fork()) {
if(fork()) {
}
}
wait(&status);
fila++;
}

//SE LA FILA E' PARI:
if (fila!=nivel) {
if ((fila%2)==0) {
wait(&status);
}else{
pid_t wait();
}
}
//ULTIMA FILA
else{
sleep(30);
}
}
}