View Single Post
Old 12-06-2007, 14:53   #1
magix2003
Senior Member
 
L'Avatar di magix2003
 
Iscritto dal: Aug 2005
Città: Wien
Messaggi: 435
[C] Problema urgente con fork

Ciao a tutti,
ritorno con un altro cocente problema. Io devo chiamare una funzione n volte, e questa funzione deve eseguire alcuni controlli sui processi...

Questo è il corpo della funzione:

Codice:
void scanAll(int pidNr[], int n) {
    int i;
    int pids[n];
    for (i = 0; i < n; i++) {
        pids[i] = -1;
    }
    int ret = 0;
    for (i = 0; i < n; i++) {
        pids[i] = fork();
        if (pids[i] == 0) {
            scanPid(pidNr[i]);
            exit(0);
        } else {
            wait(NULL);
        }
    }
}
Questa invece è la funzione che viene chiamata:
Codice:
int scanPid(int pid) {
    pid_t traceme_pid;
    long  retval;
    int status;
    int syscall_id;
    int inside_syscall = 0;
    traceme_pid = pid;
    retval = ptrace(PTRACE_ATTACH, traceme_pid, NULL, NULL);
    if (retval == -1) {
        char str[89];
        sprintf(str, "ERROR: In function scanPid. Attaching failed to PID %d", pid);
        closeError(str);
        exit(1);
    }
    printw("sucessfully attached to pid = %d\n", traceme_pid);
    char c = 0;
    char ch;
    int row,col;
    getmaxyx(stdscr,col,row);
    halfdelay(1);
    while(getch() != 'q') {
        refresh();
        // wait for traced program to stop
        // (the program will stop at each entry and at each exit from
        // a system call)
        wait(&status);
        if (WIFEXITED(status)) {
            //exit(0);
            printw("the traced programm has exited\n");
            printw("Press any key to continue...");
            getch();
            return 0;
        }


        // ok, the traced program is stopped and we can inspect it

        // ...

        // get content of EAX register (contains the system call number)
        syscall_id = ptrace(PTRACE_PEEKUSER, traceme_pid, 4 * ORIG_EAX, NULL);
        if (retval == -1) {
            char str[200];
            sprintf(str, "ERROR: In function scanPid. Cannot get the content of the EAX register");
            closeError(str);
            exit(1);
        }
        if (!inside_syscall) {
            Lista * p = get_syscall_name(syscall_id);
            if (p != NULL) {
                move(1,1);
                p->number = p->number+1;
                printall(root);
                int x,y;
                getyx(stdscr,y,x);
                if ((p->bl == 1)){
                      blroot = ListInsertBlack(blroot, p);
                      printBList(blroot, traceme_pid);
                }
                inside_syscall = 1;
            } else {
                printw("enter to system call number %d (unknown name)\n");
                inside_syscall = 1;
            }
        } else {
            inside_syscall = 0;
        }

        // ...

        // continue the traced program, telling the kernel to stop it again
        // at the next system call entry or exit

        ptrace(PTRACE_SYSCALL, traceme_pid, NULL, NULL);
        if (retval == -1) {
            char str[89];
            sprintf(str, "ERROR: In function scanPid. Attaching failed to PID %d", pid);
            closeError(str);
            exit(1);
        }

    }
    retval = ptrace(PTRACE_DETACH, traceme_pid, NULL, NULL);
    if (retval == -1) {
        perror("detaching failed");
        return 1;
    }
    printw("\n succesfully detach\n");
    getch();
    return 0;
}
Bene ogni volta che provo ad utilizzare questa funzione il pc si impalla, perché vengono creati n processi e la mia bella Ubuntu non sopporta il carico.

Qualcuno mi può dire dove sbaglio?

Grazie, è importante...
__________________
"Sono 126 miglia per Chicago. Abbiamo il serbatoio pieno, mezzo pacchetto di sigarette, è buio, e portiamo tutt'e due gli occhiali da sole"

magix2003 è offline   Rispondi citando il messaggio o parte di esso