C study buddy?

Ioannis Nompelis nompelis at nobelware.com
Sat Nov 17 22:27:35 UTC 2018


I finally got the 10 minutes to read, and actually mostly understand, what was
in Joe's weblog post. It has some of the geekiest things I have ever read,
and is written in an esoteric enough language --assuming a lot of what the
reader should know to read it-- that I find myself privileged to understand it!

Take this in good spirit Joe! This is great!

I will voice this: I never thought, ever in life, of treating pointers without
type-casting, or treating anything in C without type-casting. If you want to
be that lazy, use "unions" maybe, and suffer the consequences, maybe?

It was a good read. When I was a kid, I cared less about what was inside that
8-bit processor, and went straight for "Basic" commands. Those were the days.


One more thing on using libraries, just as a tangent. I am building this...
thing... let's not worry about what it does. It has the header file shown below.
Look at the included header files to get an idea of what libraries it is using.
Maybe I am jumping a little far ahead from Joe's learning target, so feel free
to ignore me.

However...

If you want to learn POSIX threads, unix and INET sockets, the select() magic,
dynamic library loading, multi-threaded inter-process synchronization and
communication, and signal-handling, you may want to look at the single .c file
that goes with this.

Have a good weekend everyone.

#ifndef _INDAEMON_H
#define _INDAEMON_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
//#include <dlfcn.h>     // used in the .c file
//#include <pthread.h>   // used in the .c file


#define NAME_SIZE 256
#define COMM_BUF_SIZE 256

struct indaemon_worker_s {
   int istat;
};

struct indaemon_client_s {
   int istat;
   pthread_t t;
   pthread_attr_t t_attr;
   int isocket;
};

struct indaemon_data_s {
   int istat;
   int irun;
   char name[NAME_SIZE];

   // functions to execute
   void *lib_handle;
   int (*indaemon_User_Init)( void * arg );
   int (*indaemon_User_Term)( void * arg );

   // data for network operations
   int unix_socket;
   struct sockaddr_un unix_socket_name;
   char unix_socket_string[NAME_SIZE];
   int inet_socket, inet_backlog;
   struct sockaddr_in inet_socket_name;
   in_port_t inet_port;
   char inet_socket_string[NAME_SIZE];

   // data to communicate between network thread and main thread
   int imanage_pipefd[2],icontrol_pipefd[2];

   // data for the networker
   pthread_t nett;
   pthread_attr_t nett_attr;
   int inetworker_run;

   // functions to execute in the networker
   int (*indaemon_Networker_Ending)( void * arg );

   // worker threads data
   int num_workers;
   struct indaemon_worker_s *workers;

   // client threads data
   int num_clients;
   struct indaemon_client_s *clients;
};

static struct indaemon_data_s *global_ddata = NULL;

int indaemon_Start( const char name[], const char libname[] );
int indaemon_Init( struct indaemon_data_s **ddata,
                   const char name[], const char libname[] );
int indaemon_Term( struct indaemon_data_s **ddata );
int indaemon_MainLoop( struct indaemon_data_s *ddata );
void indaemon_SignalHandler( int isignal );
int indaemon_LoadLibrary( struct indaemon_data_s *ddata, const char libname[] );

void *indaemon_Thread_Networker( void * );
void *indaemon_Thread_Client( void * );
int indaemon_Thread_Networker_Ending( void *arg );
int indaemon_Thread_HandleClient( struct indaemon_data_s *s,
                                  char buffer[], 
                                  int socket_fd );
int indaemon_Thread_AttachClient( struct indaemon_data_s *s, int socket_fd );
int indaemon_Thread_DetachClient( struct indaemon_data_s *s, int socket_fd );
int indaemon_Thread_SpawnClient( struct indaemon_data_s *s, int new_socket );

int indaemon_Worker_Init( struct indaemon_worker_s *w );
int indaemon_Client_Init( struct indaemon_client_s *c );

#endif



More information about the Friends mailing list