Thread: nanosleep messing things up.

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    nanosleep messing things up.

    As the title says, it messes things up. My program is supposed to read in char by char, which works fine without nanosleep, when I add it in, messes things up. Yes I MUST use nanosleep which is why Im using it. Why is it not working in its spot? And where does it belong

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <signal.h>
    #define TEN_MILLIS_IN_NANOS 10000000
    
    typedef  struct {
         char  data;
         off_t offset;
         char state;
    } BufferItem ;
    //nanosleep(&t, NULL);
    
    
    BufferItem buffer[10000];
    
    pthread_mutex_t mutex;
    
    
    void *read_file(void *arg)
    {
    
            pthread_mutex_init(&mutex, NULL);
    
            struct timespec t;
            t.tv_sec = 0;
            t.tv_nsec = rand()%(TEN_MILLIS_IN_NANOS+1);
            while(1){
    
    //              pthread_mutex_lock(&mutex);
                    buffer->data = fgetc(arg);
                    buffer->offset = ftell(arg);
                    buffer->state = 1;
    //              pthread_mutex_unlock(&mutex);
    
                    if (buffer->data == EOF)
                    {
                            break;
                    }
                    printf("%c", buffer->data);
                    nanosleep(&t, NULL);
            }
    
            pthread_exit(NULL);
    }
    int main(int argc, char *argv[])
    {
            int KEY = *argv[1];
            int nIN = *argv[2];
            int nWORK = *argv[3];
            int nOUT = *argv[4];
    
    
            pthread_t IN[nIN];
            pthread_t OUT[nOUT];
            pthread_t WORK[nWORK];
    
    
            struct timespec t;
            t.tv_sec = 0;
            t.tv_nsec = rand()%(TEN_MILLIS_IN_NANOS+1);
    
            char c;
            FILE *file_in;
            file_in = fopen(argv[5], "r");
    
    
    
            int i;
            for(i=0;i<nIN;i++)
            {
                    pthread_create(&IN[i], NULL, read_file, file_in);
                    nanosleep(&t, NULL);
            }
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What do you expect it to do?
    What is it doing?

    Do you really want to set KEY, nIN, nWORK and nOUT to the value of the first char in the first 4 command line arguments? Or should you be using atoi() there?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    30
    Quote Originally Posted by oogabooga View Post
    What do you expect it to do?
    What is it doing?

    Do you really want to set KEY, nIN, nWORK and nOUT to the value of the first char in the first 4 command line arguments? Or should you be using atoi() there?
    I want it to just print out a file, the other things is excess I guess I should have taken out. But the key thing is that it works without nanosleep. I want it to sleep for a random time and pick up a char and store it in the buffer. and keep sleeping randomly after. It was able to print the file before, but with nanosleep only prints a little over half or so and just cuts off.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's because you're not waiting for your threads in main. With nanosleep, your threads sleep long enough for main to end before the file is fully printed out. One way to wait is to simply add a pthread_exit(NULL); as the last statement in main (besides a possible return 0; that's never actually reached).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    I also found a nifty way for a program to wait that is "transportable" (which I think means it works on multi-OSs).

    Code:
    for (x = 0; x < 1000; x++) {
        for (y=0;y < 100000; y++) {}
    }
    I've been doing this in my program . It works great! It pauses the program for at least 1 real-time second. It doesn't require a user input, which is a plus .

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by vinniev99 View Post
    I also found a nifty way for a program to wait that is "transportable" (which I think means it works on multi-OSs).
    Are you serious? A lot of compiler will just optimise that out, some machines will take fractions of a second to do it (especially if they just put those values in registers!) and others will take much, much longer. Hell, different optimisation levels in the same compiler on the same computer will have vastly different lengths of pause. On my copy of gcc, it takes 280ms to run that loop with -O0, and LITERALLY 0ms at -O3. You can even whack up the numbers even further - it makes no difference because the optimisation of the compiler wipes it out.

    Now think what that does on different machines, different compilers, different operating systems, etc.

    Why not just loop in a while loop until the number of seconds passed is one (or the number of milliseconds passed is one thousand or whatever)? All you need is a SINGLE function call that can give you time accurate to say, half-a-second or better, which is pretty much part of the C standard ("clock"). That works, works the same on ALL machines, works consistently in terms of timing, and can't be optimised away by the compiler.

    Code:
    #include <time.h>
    clock_t start_time = clock();
        while(clock() < (start_time + 1 * CLOCKS_PER_SEC));
    ;

    Which is probably why everyone who wants to pause for a second does just that.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. messing around with bitmaps
    By seanJ in forum C Programming
    Replies: 18
    Last Post: 01-04-2012, 04:52 PM
  2. CWinThread messing itself up
    By LowlyIntern in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2009, 08:08 AM
  3. nanosleep() -system call does some confusing things
    By jtk in forum Linux Programming
    Replies: 5
    Last Post: 08-30-2007, 04:15 AM
  4. Messing with folders
    By Probose in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2006, 03:16 PM
  5. Is someone messing with m$ again?
    By exluddite in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-16-2004, 01:56 PM