亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 請教 [打印本頁]

作者: my253629725    時間: 2009-05-30 13:25
標(biāo)題: 請教
int main(int argc, char** argv) {

    /*variables for accountant server*/
    int sock, new_sock;
    struct sockaddr_in local_addr;
    pid_t pid;


    /*initial server for financial server*/
    sock = Init_data_comm_server(26987, &local_addr);

    /*begin accept the query from business server*/
    while (1) {
        if ((new_sock = accept(sock, (struct sockaddr *) & local_addr, &sin_size)) < 0) {
            perror("\033[1m\033[40;31maccepting query from client\033[0m\n";
            continue;
        }

        /*fork a child process*/
        if ((pid = fork()) < 0) {
            perror("\033[1m\033[40;31merror:server.c:main():fork\033[0m\n";
            close(new_sock);
            sleep(1);
            continue;
        } else if (0 == pid) {
            /* In the first child process*/
            /* Close the listening socket description */
            close(sock);
            if ((pid = fork()) < 0) {
                perror("\033[1m\033[40;31mproxy_init.caemon_foreground():fork()\033[0m\n";
                exit(1);
            } else if (pid > 0) {
                exit(0);
            }
                                                //業(yè)務(wù)處理部分
                                                。。。。。。。。
                                               
            printf(DEBUGFMT"\033[1m\033[40;32m SOC %d ,PID_%d PPID_%d send pkt: %s\033[0m\n\n", DEBUGARGS,new_sock ,getpid(),getppid(),buf_send);
            /*send packet to  client*/
            if ((count = send(new_sock, buf_send, strlen(buf_send), 0)) < 0) {
                perror("\033[1m\033[40;31msend data:\033[0m\n";
                exit(1);
            }

            close(new_sock);
            exit(0);
        }

        /* In the parent process */
        close(new_sock);

        if (waitpid(pid, NULL, 0) != pid) {
            perror("\033[1m\033[40;31mserver.c:waitpid\033[0m\n";
        }
    }

    PQfinish(conn_db);
    conn_db = NULL;

    return 0;
}

這是我業(yè)務(wù)處理程序的部分代碼
程序在接受請求時創(chuàng)建的子進(jìn)程PID為8896,父進(jìn)程PID為8895,在業(yè)務(wù)處理完畢后,發(fā)送相應(yīng)的時候,子進(jìn)程PID為8896,父進(jìn)程PID為1,為什么父進(jìn)程的PID會改變,是我程序
的問題么,還有我的這個程序在進(jìn)行業(yè)務(wù)處理的時候會不會產(chǎn)生線程沖突,因為這個程序要同時大量的操作一個數(shù)據(jù)表。下面是程序的輸出部分:
SOCK 5 PID_8896 PPID_8895 recv packet:12000009053047939166
SOCK 5 PID_8896 PPID_1 send packet: 1200|00|0009053047939166
作者: ark211    時間: 2009-05-30 15:12
if ((pid = fork()) < 0) {
            perror("\033[1m\033[40;31merror:server.c:main():fork\033[0m\n";
            close(new_sock);
            sleep(1);
            continue;
        } else if (0 == pid) {
            /* In the first child process*/
            /* Close the listening socket description */
            close(sock);
            if ((pid = fork()) < 0) {
                perror("\033[1m\033[40;31mproxy_init.caemon_foreground():fork()\033[0m\n";
                exit(1);
            } else if (pid > 0) {
                exit(0);
            }
你這里不是FORK了兩次嗎?而且在第二次FORK后你將父進(jìn)程(也就是第一次FORK的子進(jìn)程)退出,那第二次FORK的子進(jìn)程就成孤兒進(jìn)程了,他會被 INIT(1號進(jìn)程)接管。所以前父進(jìn)程為1。

如果你在進(jìn)程中有對資源進(jìn)程的競態(tài)操作,就可能出現(xiàn)沖突,最好加上用于進(jìn)程間通信的互斥鎖,讀寫鎖,條件變量那些。
作者: my253629725    時間: 2009-05-30 16:07
也就是說在線程A操作數(shù)據(jù)表的時候要把數(shù)據(jù)表鎖死,其他的線程只能等待線程A退出后才能操作數(shù)據(jù)表




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2