- 论坛徽章:
- 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.caemon_foreground():fork()\033[0m\n"
exit(1);
} else if (pid > 0) {
exit(0);
}
//业务处理部分
。。。。。。。。
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;
}
这是我业务处理程序的部分代码
程序在接受请求时创建的子进程PID为8896,父进程PID为8895,在业务处理完毕后,发送相应的时候,子进程PID为8896,父进程PID为1,为什么父进程的PID会改变,是我程序
的问题么,还有我的这个程序在进行业务处理的时候会不会产生线程冲突,因为这个程序要同时大量的操作一个数据表。下面是程序的输出部分:
SOCK 5 PID_8896 PPID_8895 recv packet:12000009053047939166
SOCK 5 PID_8896 PPID_1 send packet: 1200|00|0009053047939166 |
|