- 論壇徽章:
- 0
|
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.c aemon_foreground():fork()\033[0m\n" ;
exit(1);
} else if (pid > 0) {
exit(0);
}
//業(yè)務處理部分
。。。。。。。。
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è)務處理程序的部分代碼
程序在接受請求時創(chuàng)建的子進程PID為8896,父進程PID為8895,在業(yè)務處理完畢后,發(fā)送相應的時候,子進程PID為8896,父進程PID為1,為什么父進程的PID會改變,是我程序
的問題么,還有我的這個程序在進行業(yè)務處理的時候會不會產(chǎn)生線程沖突,因為這個程序要同時大量的操作一個數(shù)據(jù)表。下面是程序的輸出部分:
SOCK 5 PID_8896 PPID_8895 recv packet:12000009053047939166
SOCK 5 PID_8896 PPID_1 send packet: 1200|00|0009053047939166 |
|