Obsolete
/
libipc-old
Archived
3
0
Fork 0

used thread, and can accept more clients at the same time

more_to_read
lapupe 2016-09-22 18:37:24 +02:00
parent cc87ad527d
commit 3615c3512d
1 changed files with 52 additions and 20 deletions

View File

@ -1,7 +1,39 @@
#include "../lib/communication.h" #include "../lib/communication.h"
#include <pthread.h>
#define PONGD_SERVICE_NAME "pongd" #define PONGD_SERVICE_NAME "pongd"
/* control the file descriptor*/
void * pongd_thread(void * pdata) {
struct process *proc = (struct process*) pdata;
// about the message
size_t msize = BUFSIZ;
char *buf = NULL;
int ret;
while (1) {
// printf ("before read\n");
if ((ret = srv_read (proc, &buf, &msize)) == -1) {
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", ret);
}
// printf ("after read\n");
printf ("read, size %ld : %s\n", msize, buf);
if(msize > 0) {
printf ("before proc write\n");
if ((ret = srv_write (proc, buf, msize)) == -1) {
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", ret);
}
}else {
break;
}
}
return NULL;
}
/* /*
* main loop * main loop
* *
@ -13,14 +45,17 @@
void main_loop (const struct service *srv) void main_loop (const struct service *srv)
{ {
int ret; int ret;
struct process proc; struct process tab_proc[10];
//thread
pthread_t tab_thread[10];
int i;
int cnt = 10; int cnt = 0;
while (cnt--) { while (cnt < 10) {
// -1 : error, 0 = no new process, 1 = new process // -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (srv, &proc); ret = srv_get_new_process (srv, &tab_proc[cnt]);
if (ret == -1) { if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n"); fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
@ -29,28 +64,25 @@ void main_loop (const struct service *srv)
continue; continue;
} }
srv_process_print (&proc); srv_process_print (&tab_proc[cnt]);
//printf ("after print\n"); //printf ("after print\n");
// about the message int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &tab_proc[cnt]);
size_t msize = BUFSIZ; if (ret) {
char *buf = NULL; perror("pthread_create()");
exit(errno);
// printf ("before read\n"); } else {
if ((ret = srv_read (&proc, &buf, &msize)) == -1) { printf("Creation of listen thread \n");
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", ret);
continue;
} }
// printf ("after read\n");
printf ("read, size %ld : %s\n", msize, buf);
// printf ("before proc write\n");
if ((ret = srv_write (&proc, buf, msize)) == -1) {
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", ret);
continue;
}
// printf ("after proc write\n"); // printf ("after proc write\n");
printf ("%d applications to serve\n",cnt); printf ("%d applications to serve\n",cnt);
cnt++;
}
for (i = 0; i < cnt; i++) {
pthread_join(tab_thread[cnt], NULL);
} }
} }