I have to read some file (logs) from a server using thread program in c. The program should remain simultaneous means on one hand this child thread should be executing and on same time main thread should be informing time of its child tread.
So in sample program I am trying to run a logging function in child thread and on same time parent process/parent thread/main thread will be printing time in seconds have been passed running the logging.
PS: my problem is if I will use pthread_join() then first logreading function will execute which is actually blocking thread due to infinite loop. And I am not able to get take flow of control in main function. Anyhow I want to execute both together without interrupting each other . I have done similar thing in Java using thread and synchronized methods. But in C I have not that much knowledge of multithreading programming
Currently I am not using locks because I don't want stop/resume condition, my only requirement is that both functions keep started , running at the same time without interacting each other.
Thanks for the help.
void process(FILE *FP, int sockfd);
int create_ma_header(char*, int, int, int, int);
void* t_main(void*);
int i;
int main(int argc, char** argv)
{
void* arg;
void* t_result;
if (argc != 2)
{
printf("usage: tcpcli <IPaddress>");
exit(0);
}
arg = (void*) argv[1];
pthread_t t;
pthread_attr_t t_attr;
pthread_attr_init(&t_attr);
printf("before calling pthread_create getpid: %d getpthread_self %u tid:%u\n",
getpid(), pthread_self(), syscall(SYS_gettid));
pthread_create(&t, &t_attr, t_main, (void*) argv[1]); //loging tread
/* if(pthread_join(t,&t_result) != 0)
{
printf("Thread not joined \n");
exit(1);
}
*/
printf("Thread joined and status is Logs are getting printed what else you want to do .... \n");
while (1)
{
printf(
"Running time of log reading displayed from main thread: %d seconds",
i++);
slep(1);
}
pthread_exit(0);
}
void* loging_thread(void* arg)
{
char* server_ip;
server_ip = (char*) arg;
int sockfd;
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET, server_ip, &servaddr.sin_addr);
printf(
"in loging_thread log has been redirected to log_read.dat getpid: %d getpthread_self %u tid:%u\n",
getpid(), pthread_self(), syscall(SYS_gettid));
connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
process(stdin, sockfd); /* do it all */
pthread_exit(0);
}
void process(FILE *fp, int sockfd)
{
FILE* log_f;
int fd;
fd = open("log_read.dat", O_CREAT | O_WRONLY, 0644);
char sendline[MAXLINE];
char reply[MAXLINE], string2[MAXLINE], *body;
char *p1, *p2, *p3;
create_ma_header(reply, 999, 999, VERSION, 999);
write(sockfd, reply, sizeof(reply));
while (1)
{
char header[SDNS_MA_HEADERSZ];
read(sockfd, body, size);
write(fd, body, size);
}
}
int create_ma_header(char * reply, int type, int size, int version, int lic)
{
sprintf(reply, "%4d%c%10d%c%4d%c%4d", type, SD_PIPE, size, SD_PIPE, version,
SD_PIPE, lic);
if (strlen(reply) != 26 - 1)
{
return 0;
}
reply[strlen(reply)] = SD_PIPE;
return 1;
}
char * get_ma_header(char *curr_p, int *type_p, int *size_p, int *version_p,
int *lic_p)
{
*type_p = (int) strtol(curr_p, NULL, 10);
curr_p += 5;
*size_p = (int) strtol(curr_p, NULL, 10);
curr_p += 11;
*version_p = (int) strtol(curr_p, NULL, 10);
curr_p += 5;
*lic_p = (int) strtol(curr_p, NULL, 10);
curr_p += 5;
return curr_p;
}