Sponsored Content
Full Discussion: [C] Multithread Server
Top Forums Programming [C] Multithread Server Post 302219644 by ramen_noodle on Tuesday 29th of July 2008 06:26:38 PM
Old 07-29-2008
Non-trivial with a process oriented paradigm. Making the list global won't help except
to save memory in the child.
There is a little pipe synchronization thing I wrote as an exercise as a while ago.
You may like it. Smilie It's chuckle worthy.

Code:
#include "dlist.h"
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>
#include <time.h>


#define MAXTIMES 300

long dlist_count;

struct _encap_ {
int timer;
};

void *interrupt_hup(void *val) {
struct _encap_ p;
time_t now, then;                 
                                                               
                               pthread_detach(pthread_self());
                               memcpy(&p,val,sizeof(p));
                               printf("Interval set at %d\nThread id %d waiting till %d -- now = %d\n",p.timer,pthread_self(),time(NULL) + p.timer,time(NULL));
                               then = now = time(NULL);
                               while ((then = time(NULL)) < (now + p.timer)) {sleep(1);}
                               kill(getpid(),SIGHUP);
                               pthread_exit(NULL);
}
                                       
void walk_dlist(DLIST * h)
{
    int x = 0;
    while (h != NULL) {
	printf("Node %d at %p with prv = %p, nxt = %p and data = %p\n", x,
	       h, h->prv, h->nxt, h->data);
	x++;
	h = h->nxt;
    }
}

void bwalk_list(DLIST * h, int place)
{

    while (h != NULL) {
	printf("Node %d at %p with prv = %p, nxt = %p and data = %p\n",
	       place, h, h->prv, h->nxt, h->data);
	place--;
	h = h->prv;
    }
}

#define BSZ 256

/*idea create two separate dlists and keep them synched with pipes from parent to child
1) Allocate and populate pipe from stdin/file in  parent. Create pipe. Close read end.
2) Create signal handler in parent for sighup and on receipt update the child list.
3) sigsuspend event loop
***child***
1) fork() and  destroy the original dlist copy via dlist_destroy. Close write end of pipe.
2) Create list head and then event loop -- block on read from pipe and add to list with inbound data.
*/

DLIST *global;
int pip[2];

void handlehup(int sig) {
int v = 0;
char *dta;
              while (v < dlist_count) {
                     dta = (dlist_element(global,v))->data;
                     if (dta != NULL) {write(pip[1],dta,strlen(dta));}
                     v++;
              }
              printf("Finished update...\n");
}
                   
int do_child(void) {
int ret;
pid_t cn;
char buf[BSZ];
char *rec;
DLIST *synchrony;
sigset_t mask;
                    
                    if ( (cn = fork()) == 0) {
                         sigemptyset(&mask);
                         sigprocmask(SIG_SETMASK,&mask,NULL);
                         
                         close(pip[1]);
                         dlist_destroy(global,free);
                         synchrony =  dlist_element_init(NULL);

                         bzero(buf,BSZ);
                         while ((ret = read(pip[0],buf,BSZ)) > 0) {
                                  rec = malloc(BSZ);
                                  strcpy(rec,buf);
                                  dlist_append(synchrony,dlist_element_init(&rec));
                                  printf("Received new record in list at %p with %d elements = \n%s\n at pid %d\n",synchrony,dlist_count,rec,getpid());
                        }
                         printf("Exiting due to pipe read error in %d with return = %d\n",getpid(),ret);
                         perror("read()");
                         walk_dlist(synchrony);
                         if (dlist_count > 0) {dlist_destroy(synchrony,free);}
                         exit(1);
                     } else {
                         return cn;
                     }
}
                                 
int main(int argc, char **argv) {
int x;
FILE *pt;
char buf[BSZ];
char *rec;
pid_t chld;
struct _encap_ f;
sigset_t mask;
pthread_t interrupto;

                 if (argc != 2) {printf("Error: Please specify file for information in list.\n"); return -1;}
                 if ( (pt = fopen(argv[1],"r")) == NULL) {perror("fopen()"); return -1;}

                 signal(SIGHUP,handlehup);
                 global = dlist_element_init(NULL);
                 
                 srand(time(NULL));
                 bzero(buf,BSZ);
                 while (fgets(buf,BSZ,pt) != NULL) {
                       rec = malloc(BSZ);
                       strcpy(rec,buf);
                       dlist_append(global,dlist_element_init(&rec));
                 }
                 walk_dlist(global);
                 
                 
                 pipe(pip);

                 sigfillset(&mask);
                 sigdelset(&mask,SIGHUP);
                 sigdelset(&mask,SIGINT);
                 sigdelset(&mask,SIGCHLD);
 
                 printf("Starting child process at %d\n",chld = do_child());
                 close(pip[0]);

                 while (1) {f.timer = (1 + rand() % MAXTIMES); pthread_create(&interrupto,NULL,interrupt_hup,&f); sigsuspend(&mask); waitpid(0,NULL,WNOHANG);}
                 return 1;
}

The list adt and functionality is a coarse copy of that described in
'MasterIng Algorithms in C'. The pipe could be exchanged for socket,
named pipe, etc..
 

9 More Discussions You Might Find Interesting

1. Programming

Timeout with multithread server

I wrote a server which creates a thread for every client connection. I have to include timeout function that will kill the server thread if the client doesn't respond for specific time. That too using signal(SIGALRM). For this i am using alarm() function. When the server thread detects signal it... (1 Reply)
Discussion started by: Nads
1 Replies

2. Programming

Multithread,libcurl

Hi i m codding a programm,it can download any packet from ftp,I use libcurl library. But i want to use threads for downloading.(Multithreading).i cant get ftp file size from ftp and divide packet small pieces,like threads use. Please share your experince with me ,thanks. (0 Replies)
Discussion started by: canerbulut
0 Replies

3. Programming

Multithread app - Read-Only Data

Hello, I'm coding an application using pthreads.At some point the threads will read some read-only variables.Is it safe NOT to use mutexes, in order to make the program lighter since mutex operations are resource-demanding... Thanks (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

4. Solaris

How to find an application running on multithread?

Dear Friends, We have one T5240 server with 128vcpus in our lab.Performance of the server is very poor. Application uses only 2% of processor..I heard that single thread application performs slowly in coolthread.How can we find whether the application running on multithread or single thread? If... (7 Replies)
Discussion started by: nicktrix
7 Replies

5. Shell Programming and Scripting

Bash script multithread in group of 3

I Have an script like ./bang 1 ./bang 2 ./bang 3 ./bang 4 ./bang 5 ./bang 6 ./bang 7 ./bang 8 ./bang 9 ./bang 10 ./bang 11 ./bang 12 and i wanna execute him in groups of 3 , i mean he execute bang 1 , bang 2 and bang 3 after it finish the next 3 commands it will be executed and... (5 Replies)
Discussion started by: Gyeah11
5 Replies

6. Shell Programming and Scripting

KSH fetching files from server A onto server B and putting on server C

Dear Friends, Sorry for this basic request. But I just started learning Ksh recently and still I am a newbie in this field. Q: I have files on one server and the date format is 20121001000009_224625.in which has year (yyyy) month (mm) and date (dd). I have these files on server A. The task... (8 Replies)
Discussion started by: BrownBob
8 Replies

7. Shell Programming and Scripting

Connect to server-1 from server-2 and get a file from server-1

I need to connect to a ftp server-1 from linux server-2 and copy/get a file from server-1 which follows a name pattern of FILENAME* (located on the root directory) and copy on a directory on server-2. Later, I have to use this file for ETL loading... For this I tried using as below /usr/bin/ftp... (8 Replies)
Discussion started by: dhruuv369
8 Replies

8. UNIX for Dummies Questions & Answers

Transfer file from server B to server C and running the script on server A

I have 3 servers A, B, C and server B is having some files in /u01/soa/ directory, these files i want to copy to server C, and i want to run the script from server A. Script(Server A) --> Files at Server B (Source server) --> Copy the files to Server C(Target Server). We dont have RSA key... (4 Replies)
Discussion started by: kiran_j
4 Replies

9. Red Hat

Kcryptd - Which kernel supports MultiThread

I am currently have Centos 5.5 which consumes more CPU waiting for Kcrypt process. Later came to know that kcrypt is single threaded and hence consumes one CPU, results in performance degradation. Does any one really knows/practically experimented multithread of Kcryptd process with any of... (1 Reply)
Discussion started by: ragavendraganes
1 Replies
All times are GMT -4. The time now is 08:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy