socket-programming:fork
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
| socket-programming:fork [2024/06/18 00:32] – ایجاد شد pejman | socket-programming:fork [2024/06/18 01:04] (فعلی) – حذف شد pejman | ||
|---|---|---|---|
| خط 1: | خط 1: | ||
| - | ====== تابع ()fork ====== | ||
| - | از این تابع به وسیله | ||
| - | |||
| - | <code C> | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | pid_t fork(void); | ||
| - | </ | ||
| - | |||
| - | اسکلت کلی استفاده از [[fork]] در سرور | ||
| - | |||
| - | |||
| - | <code C> | ||
| - | pid_t pid; | ||
| - | int listen_sock, | ||
| - | listen_sock = socket(...); | ||
| - | bind(listen_sock, | ||
| - | listen(listen_sock, | ||
| - | |||
| - | // ignoring the SIGCHLD signal | ||
| - | // prevent zombie/ | ||
| - | signal(SIGCHLD, | ||
| - | |||
| - | while(1) { | ||
| - | |||
| - | client_sock = accept(listen_sock, | ||
| - | |||
| - | pid = fork(); | ||
| - | if (pid == 0 ) { // this is the child process | ||
| - | |||
| - | close(listen_sock); | ||
| - | |||
| - | // process the request, doing something using client_sock | ||
| - | |||
| - | close(client_sock); | ||
| - | exit(0); | ||
| - | } | ||
| - | close(client_sock); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | مثال یک سرور multi-process | ||
| - | |||
| - | |||
| - | <code C> | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #define PORT " | ||
| - | #define BACKLOG 120 | ||
| - | |||
| - | int main() | ||
| - | { | ||
| - | int status, listen_sock, | ||
| - | struct addrinfo hints, *res, *rp; | ||
| - | struct sockaddr_storage client_addr; | ||
| - | socklen_t addrlen; | ||
| - | char ipstr[INET_ADDRSTRLEN]; | ||
| - | pid_t pid; | ||
| - | char *msg = " | ||
| - | size_t msglen; | ||
| - | |||
| - | memset(& | ||
| - | hints.ai_flags = AI_PASSIVE; | ||
| - | hints.ai_family = AF_INET; | ||
| - | hints.ai_socktype = SOCK_STREAM; | ||
| - | |||
| - | status = getaddrinfo(NULL, | ||
| - | if (status != 0) { | ||
| - | fprintf(stderr, | ||
| - | gai_strerror(status)); | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | for (rp = res; rp != NULL; rp = rp-> | ||
| - | listen_sock = socket(rp-> | ||
| - | rp-> | ||
| - | if (listen_sock == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | sockopt = 1; | ||
| - | status = setsockopt(listen_sock, | ||
| - | (void*)& | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | status = bind(listen_sock, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | close(listen_sock); | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | break; | ||
| - | } | ||
| - | freeaddrinfo(res); | ||
| - | |||
| - | if (rp == NULL) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | status = listen(listen_sock, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | printf(" | ||
| - | | ||
| - | signal(SIGCHLD, | ||
| - | |||
| - | addrlen = sizeof(client_addr); | ||
| - | while(1) { | ||
| - | client_sock = accept(listen_sock, | ||
| - | & | ||
| - | if (client_sock == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | inet_ntop(client_addr.ss_family, | ||
| - | & | ||
| - | ipstr, INET_ADDRSTRLEN); | ||
| - | printf(" | ||
| - | |||
| - | pid = fork(); | ||
| - | if (pid == 0) { | ||
| - | close(listen_sock); | ||
| - | msglen = strlen(msg); | ||
| - | status = send(client_sock, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | close(client_sock); | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | close(client_sock); | ||
| - | exit(EXIT_SUCCESS); | ||
| - | } else if (pid == -1) { | ||
| - | perror(" | ||
| - | close(client_sock); | ||
| - | continue; | ||
| - | } | ||
| - | printf(" | ||
| - | close(client_sock); | ||
| - | } | ||
| - | |||
| - | close(listen_sock); | ||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | </ | ||
socket-programming/fork.1718658129.txt.gz · آخرین ویرایش: 2024/06/18 00:32 توسط pejman
