fork()

This is a discussion on fork() within the C Programming forums, part of the General Programming Boards category; hi all is fork can be used as thread? if yes how can it be? thank you in advace...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    fork()

    hi all
    is fork can be used as thread?
    if yes how can it be?

    thank you in advace

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    fork() creates processes, not threads.
    Use a thread function if you want to create a thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by Salem View Post
    fork() creates processes, not threads.
    Use a thread function if you want to create a thread.
    i treid this
    Code:
        pthread_t thread2;
        int  iret2;
         iret2 = pthread_create( &thread2, NULL, write_data, NULL);
        pthread_join( thread2, NULL);
    i used this thread in seperate function where i need it.(not used in main())
    i am using pthread_join, so that main() application gets hanging.
    sometimes it causes segmentationfualt

    van you please help me

    thank you in advance

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by munna_dude View Post
    sometimes it causes segmentationfualt
    my magic 8-Ball says "Without a doubt."

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Post a small complete example which shows the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by Salem View Post
    Post a small complete example which shows the problem.
    please forgive me,
    it has big code...(more than 1000lines)
    i got problem with
    Code:
    pthread_join( thread2, NULL);
    it is stoping the code untill completion of thread2.
    means my gui based application getting hanged.

    and also onething am not using threads in main() function, using it in some other function.
    with out these some functions are not working in my code.

    do you have any idea about this

    thank you in advance

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Not a bloody clue without some code.

    Segfault simply means that you tried to access memory you don't own.

    There are many many ways which this can happen, and I'm not going to list them all just for you to pick them off one at a time saying "not it"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    136

    thank you for quick replay

    Quote Originally Posted by Salem View Post
    Not a bloody clue without some code.

    Segfault simply means that you tried to access memory you don't own.

    There are many many ways which this can happen, and I'm not going to list them all just for you to pick them off one at a time saying "not it"
    hello
    here is some code
    Code:
    #include <gtk/gtk.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <linux/sockios.h>
    #include <string.h>
    #include <math.h>
    #include <dirent.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <unistd.h>
    #include <syslog.h>
    
    GtkWidget *dialog;
    GtkWidget *parent_window;
    gint result;
    gchar *file;
    gchar *ind;
    gchar *post=NULL;
    void server1();
    void server2();
    int n,i=2010,j=2030;
    gchar *item=NULL,*item2=NULL,*item1=NULL, *text=NULL;
    gchar *text1=NULL,*post1=NULL;
    
    
    
    
    /* Get the selected filename and print it to the console */
    static void file_ok_sel()
    {
            g_print ("%s\n",file );
    
    	item1= g_strconcat  (file,NULL);
    	post=g_strconcat  (file,NULL);
    
        pthread_t thread1;
        int  iret1;
        iret1=pthread_create( &thread1, NULL, server2, NULL);
        pthread_join( thread1, 0);
    
    
    }
    
    int main( int   argc,
              char *argv[] )
    {
        
        gtk_init (&argc, &argv);
        
    dialog = gtk_file_chooser_dialog_new ("File Selection", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
    
    result = gtk_dialog_run (GTK_DIALOG (dialog));
    
    switch (result)
    {
    case (GTK_RESPONSE_OK):
    
     file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
     g_print("file=%s\n",file);
    file_ok_sel();
      /* Use the filename here. */
    default:
      gtk_widget_destroy (dialog);
    }
    
        gtk_main ();
        return 0;
    }
    
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    void server2()
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
      // int i=2030;
        char buffer[256];
      portno = i;
    printf("i=%d\n",i);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname("XXX.XXX.XXX.XXX");
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
    
    
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr, 
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
    
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) 
    {
    
    }
    
    else
    {
      post=g_strconcat(post,NULL);  
         g_print ("post=%s\n",post );  
       n = write(sockfd,post,strlen(post));
        if (n < 0) 
             error("ERROR writing to socket");
       
    g_print("post======%s \n",post);
    
    }
    i++;
     server1();
       
    }
    void server1()
    {
    int sockfd, newsockfd, portno, clilen;
         char buffer[256],*item;
         struct sockaddr_in serv_addr, cli_addr;
             sockfd = socket(AF_INET, SOCK_STREAM, 0);
         if (sockfd < 0) 
            error("ERROR opening socket");
         bzero((char *) &serv_addr, sizeof(serv_addr));
         portno =j;
    printf("j=%d\n",j);
    bzero(item1,strlen(item1));
         serv_addr.sin_family = AF_INET;
         serv_addr.sin_addr.s_addr = INADDR_ANY;
         serv_addr.sin_port = htons(portno);
         if (bind(sockfd, (struct sockaddr *) &serv_addr,
                  sizeof(serv_addr)) < 0) 
                  error("ERROR on binding");
         listen(sockfd,5);
         clilen = sizeof(cli_addr);
         newsockfd = accept(sockfd, 
                     (struct sockaddr *) &cli_addr, 
                     &clilen);
    printf("j=%d\n",j);
    j++;
         if (newsockfd < 0) 
              error("ERROR on accept");
         
         n = read(newsockfd,item1,1024);
         if (n < 0) error("ERROR reading from socket");
         printf("Here is the message: %s\n",item1);
       
         if (n < 0) error("ERROR writing to socket");
    item1[strlen(item1)]='\0';
    
    int m;
    
    g_print("%s \n",item1);
    
    }
    Not a bloody clue without some code.
    common give me the solution of thread here "Mr.god of c"
    i know "as a god of c" you can solve this.......

    thank you in advance

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    First, lets have some code which doesn't look like it was indented by using your feet.
    If it's as messy in your IDE as it is posted here, you're just making it much harder for yourself than it needs to be.

    Code:
    #include <gtk/gtk.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <linux/sockios.h>
    #include <string.h>
    #include <math.h>
    #include <dirent.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <unistd.h>
    #include <syslog.h>
    
    GtkWidget *dialog;
    GtkWidget *parent_window;
    gint result;
    gchar *file;
    gchar *ind;
    gchar *post = NULL;
    void server1();
    void server2();
    int n, i = 2010, j = 2030;
    gchar *item = NULL, *item2 = NULL, *item1 = NULL, *text = NULL;
    gchar *text1 = NULL, *post1 = NULL;
    
    /* Get the selected filename and print it to the console */
    static void file_ok_sel()
    {
        g_print("%s\n", file);
    
        item1 = g_strconcat(file, NULL);
        post = g_strconcat(file, NULL);
    
        pthread_t thread1;
        int iret1;
        iret1 = pthread_create(&thread1, NULL, server2, NULL);
        pthread_join(thread1, 0);
    
    
    }
    
    int main(int argc, char *argv[])
    {
        gtk_init(&argc, &argv);
    
        dialog = gtk_file_chooser_dialog_new("File Selection", NULL,
                                        GTK_FILE_CHOOSER_ACTION_OPEN,
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                        GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
        result = gtk_dialog_run(GTK_DIALOG(dialog));
    
        switch (result) {
        case (GTK_RESPONSE_OK):
            file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
            g_print("file=%s\n", file);
            file_ok_sel();
            /* Use the filename here. */
        default:
            gtk_widget_destroy(dialog);
        }
    
        gtk_main();
        return 0;
    }
    
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    void server2()
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
        // int i=2030;
        char buffer[256];
    
        portno = i;
        printf("i=%d\n", i);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket");
    
        server = gethostbyname("XXX.XXX.XXX.XXX");
        if (server == NULL) {
            fprintf(stderr, "ERROR, no such host\n");
            exit(0);
        }
    
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *) server->h_addr,
              (char *) &serv_addr.sin_addr.s_addr, server->h_length);
    
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0) {
    
        }
        else {
            post = g_strconcat(post, NULL);
            g_print("post=%s\n", post);
            n = write(sockfd, post, strlen(post));
            if (n < 0)
                error("ERROR writing to socket");
    
            g_print("post======%s \n", post);
        }
        i++;
        server1();
    }
    
    void server1()
    {
        int sockfd, newsockfd, portno, clilen;
        char buffer[256], *item;
        struct sockaddr_in serv_addr, cli_addr;
    
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket");
        bzero((char *) &serv_addr, sizeof(serv_addr));
        portno = j;
        printf("j=%d\n", j);
    
        bzero(item1, strlen(item1));
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = INADDR_ANY;
        serv_addr.sin_port = htons(portno);
    
        if (bind(sockfd, (struct sockaddr *) &serv_addr,
                 sizeof(serv_addr)) < 0)
            error("ERROR on binding");
    
        listen(sockfd, 5);
        clilen = sizeof(cli_addr);
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        printf("j=%d\n", j);
        j++;
        if (newsockfd < 0)
            error("ERROR on accept");
    
        n = read(newsockfd, item1, 1024);
        if (n < 0)
            error("ERROR reading from socket");
        printf("Here is the message: %s\n", item1);
    
        if (n < 0)
            error("ERROR writing to socket");
        item1[strlen(item1)] = '\0';
    
        int m;
    
        g_print("%s \n", item1);
    }
    First issue, who allocates this memory, and how much do you actually get?
    $ grep item1 foo.c
    gchar *item = NULL, *item2 = NULL, *item1 = NULL, *text = NULL;
    item1 = g_strconcat(file, NULL);
    bzero(item1, strlen(item1));
    n = read(newsockfd, item1, 1024);
    printf("Here is the message: %s\n", item1);
    item1[strlen(item1)] = '\0';
    g_print("%s \n", item1);


    > bzero(item1, strlen(item1));
    If the rest of your code is correct, then this is just a waste of time. If on the other hand the memory is uninitialised at this point, then you're just going to trash an unknown amount of memory.

    > n = read(newsockfd, item1, 1024);
    Where did 1024 come from, and wasn't this being used for storing a filename?

    > printf("Here is the message: %s\n", item1);
    read() doesn't append a \0 (which is why you fill it with zeros I guess).
    It's still wrong, because if read() fills the entire buffer, there's no room for a \0 anyway.

    Something like this is what you should have...
    char buff[1024];
    n = read( newsockfd, buff, sizeof(buff) - 1 );
    if ( n > 0 ) buff[n] = '\0';

    Without an obvious \0, trying to print that as a string will result in garbage

    > item1[strlen(item1)] = '\0';
    strlen gives you the index to the \0 character, so this line just overwrites the \0 which is already there with another \0 (nothing happens).

    I'm guessing you've chopped out a whole bunch of code showing how some of those variables are in fact initialised.

    Maybe try this
    gcc prog.c -lefence && gdb a.out
    From inside the debugger, just run the program and it will trap at the first point you do something dodgy with allocated memory.

    Or maybe
    gcc prog.c && valgrind a.out
    Then look at all the references to uninitialised and deallocated memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  2. Fork() not working.
    By BENCHMARKMAN in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2007, 12:28 AM
  3. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 01:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 05:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21