Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:12:34+00:00 2026-06-16T01:12:34+00:00

I am begining with the pthread library and I would like to execute a

  • 0

I am begining with the pthread library and I would like to execute a function like this several time in parallel :

while(true) {
    //Part A
    //do several stuff
    //End of Part A

    //Part B
    //do other stuffs
    //End of Part B
}

I can do that easily by lopping on pthread_create(). The problem is that the main program seems to switch thread before the part B is executed. As a result I see part A executed over and over but never part B. Could someone help?

Some actual code =>

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>

#define error(msg) \
        perror(msg); \
        exit(EXIT_FAILURE)


//Declarations
int mastersocket;
struct sockaddr_in addr;
struct hostent *fromhost, *tohost;
int nthread=5;
int  decdomain=13;
long serveurdns=0x0101a8c0;

static void * test(void * p_data) {

    while(1) {
        printf("hello\n");
    }
    return NULL;
}

int main(int argc, char **argv) {

        int k, l;
        pthread_t idt[5];

    //Assignation, mallocs
    //Strings

    //Appel a socket
    if( (mastersocket=socket(PF_INET, SOCK_DGRAM, 0)) < 0 ) {
        error("socket");
    }

    //Configuration de sockaddr pour l'appel a bind
    addr.sin_family=AF_INET;
    addr.sin_addr.s_addr=htonl(INADDR_ANY);
    addr.sin_port=htons(53);

    //Appel a bind
    if( (bind(mastersocket, (struct sockaddr *)&addr, sizeof(addr))) < 0 ) {
        error("bind");
    }

    for(k=0; k<nthread; k++) {

        pthread_create(&idt[k], NULL, mainbis, (void *)mastersocket);

    }
    mainbis((void *)mastersocket);

    return 0;

}

static void * mainbis(void * p_data) {

    int socketh= (int) p_data;
    int found=0, len, cc;
    char *buf, *curfish;
    struct sockaddr_in *from, *to;
    u_long fromaddr, toaddr;
    int lenbuf=5000;

    buf=(char *)malloc(lenbuf);
    curfish=(char *)malloc(100);

    len=sizeof(struct sockaddr_in);
    to=(struct sockaddr_in *)malloc(len);
    from=(struct sockaddr_in *)malloc(len);
    //Structure sockaddr du serveur DNS vers lequel on redirige les requete
    to->sin_family=AF_INET; 
    to->sin_addr.s_addr=serveurdns;
    to->sin_port=htons(53);

    //Let's go
    while(1) {

        found=0;
        curfish=NULL;
        //Reception des requetes
        if((cc=recvfrom(socketh, buf, lenbuf, 0, (struct sockaddr *)from, &len))<0) {
            error("recv");
        }
        buf[cc]='\0';

        //Identification de l'host appelant
        //fromaddr=(*from).sin_addr.s_addr;
        //fromhost=gethostbyaddr((void *)&fromaddr, sizeof(fromaddr), AF_INET);

        //Sortie utilisateur
        printf("\n\n--   New Request From $$ on Port ... Domain = %s ||\n--  ***********************\n", buf+13);

        curfish=somefunction();
        if( curfish!=NULL ) {

            found=1;

        }

        if(sendto(socketh, buf, cc, 0, (struct sockaddr *)to, len)<0) {
            error("send");
        }

        if((cc=recvfrom(socketh, buf, lenbuf, 0, (struct sockaddr *)to, &len))<0) {
            error("recv");
        }
        buf[cc]='\0';

        printf("\n\n--   New Response From $$ on Port ||\n--  ***********************\n");

        if(found) {

            buf[cc-1]=0x81;
            buf[cc-2]=0x2;
            buf[cc-3]=0xa8;
            buf[cc-4]=0xc0;

        }

        if(sendto(socketh, buf, cc, 0, (struct sockaddr *)from, len)<0) {
            error("send");
        }

    }
    return NULL;

}

It’s a UDP redirector for DNS request, when I launch and try to use dig for instance, my program always output the “New Request…” string but never the “New Response…” one and dig never get the response.

Jules

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:12:36+00:00Added an answer on June 16, 2026 at 1:12 am

    Your code can’t work. You bind 1 socket mastersocket , and you create several threads using this socket.

    This is by itself ok, but the logic inside mainbis() assumes that it will read a reply to what it sent.
    That might not happen, the reply could be read by any of the other threads you create that is blocking in recvfrom on that same socket, and the logic falls apart.

    Create a new socket in each thread that it uses to communicate with the real dns server, and only use socketh to communicate with the clients.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside the begining of a function I have this: if(false); { return 'TRUE'; }
I'm trying to create XML Schema using lxml. For the begining something like this:
I would like to add a progress animation to a function so that it
i use selenium web driver with junit in Eclipse. This is begining of my
I am just begining to explore the basic of using generics and would have
I've been watching this tutorial http://windowsclient.net/learn/video.aspx?v=296114 . At the very begining empty Windows forms
I would like to be able to load more data into a listbox when
I am following this tutorial on how to create custom spinners. Near the begining
I'm begining the development of a personal Web Application project. I'd like to have
I've seen this post:  characters appended to the begining of each file .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.