C program not sleeping in main() [duplicate]












0
















This question already has an answer here:




  • Why does printf not flush after the call unless a newline is in the format string?

    9 answers




I'm trying to force my program to sleep for a second after it creates a thread. I can make the thread sleep within its process but not in the main. Everything I read says it is just a system process that should work wherever it's called. I want it to sleep after printing that the thread has been created (before the join). There is no delay between the "create" and "joined and exited" print statements. Can you tell why it isn't working?



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define SECONDS 1 //seconds to sleep
#define SIZE 25 //number of fibonaccis to be computed

int *fibResults; //array to store fibonacci results
int idx; //index for the threads

//executes and exits each thread
void *run(void *param) {
if (idx == 0) {

sleep(SECONDS);
fibResults[idx] = 0;
pthread_exit(0);

} else if (idx == 1) {

sleep(SECONDS);
fibResults[idx] = 1;
pthread_exit(0);

} else {

sleep(SECONDS);
fibResults[idx] = fibResults[idx -1] + fibResults[idx -2];
pthread_exit(0);

}
}


int main(void) {
fibResults = malloc(SIZE * sizeof(*fibResults));
pthread_attr_t attrs;
pthread_attr_init(&attrs);

for (idx = 0; idx < SIZE; idx++) {
pthread_t thread;
pthread_create(&thread, &attrs, run, NULL);

printf("Thread[%d] createdt", idx);

sleep(SECONDS); //THIS IS WHERE SLEEP ISN'T WORKING!!!!!!!!!!!!!

pthread_join(thread, NULL);

printf("Thread[%d] joined & exitedt", idx);
printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
}
return 0;
}









share|improve this question















marked as duplicate by larsks, John Kugelman c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • what makes you think it isn't working?

    – mangusta
    Nov 22 '18 at 3:08











  • Works on my machine. What error are you getting/what is the behavior you're getting that makes you think it isn't working?

    – torstenvl
    Nov 22 '18 at 3:10











  • There is no delay between the "create" and "joined and executed" print statements.

    – efuddy
    Nov 22 '18 at 3:19






  • 1





    Or use fflush(stdout);

    – torstenvl
    Nov 22 '18 at 3:22











  • Did you consider trying a larger value for SECONDS? That would have been the very first thing I tried in order to see if the call was working or not.

    – Ken White
    Nov 22 '18 at 3:22


















0
















This question already has an answer here:




  • Why does printf not flush after the call unless a newline is in the format string?

    9 answers




I'm trying to force my program to sleep for a second after it creates a thread. I can make the thread sleep within its process but not in the main. Everything I read says it is just a system process that should work wherever it's called. I want it to sleep after printing that the thread has been created (before the join). There is no delay between the "create" and "joined and exited" print statements. Can you tell why it isn't working?



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define SECONDS 1 //seconds to sleep
#define SIZE 25 //number of fibonaccis to be computed

int *fibResults; //array to store fibonacci results
int idx; //index for the threads

//executes and exits each thread
void *run(void *param) {
if (idx == 0) {

sleep(SECONDS);
fibResults[idx] = 0;
pthread_exit(0);

} else if (idx == 1) {

sleep(SECONDS);
fibResults[idx] = 1;
pthread_exit(0);

} else {

sleep(SECONDS);
fibResults[idx] = fibResults[idx -1] + fibResults[idx -2];
pthread_exit(0);

}
}


int main(void) {
fibResults = malloc(SIZE * sizeof(*fibResults));
pthread_attr_t attrs;
pthread_attr_init(&attrs);

for (idx = 0; idx < SIZE; idx++) {
pthread_t thread;
pthread_create(&thread, &attrs, run, NULL);

printf("Thread[%d] createdt", idx);

sleep(SECONDS); //THIS IS WHERE SLEEP ISN'T WORKING!!!!!!!!!!!!!

pthread_join(thread, NULL);

printf("Thread[%d] joined & exitedt", idx);
printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
}
return 0;
}









share|improve this question















marked as duplicate by larsks, John Kugelman c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • what makes you think it isn't working?

    – mangusta
    Nov 22 '18 at 3:08











  • Works on my machine. What error are you getting/what is the behavior you're getting that makes you think it isn't working?

    – torstenvl
    Nov 22 '18 at 3:10











  • There is no delay between the "create" and "joined and executed" print statements.

    – efuddy
    Nov 22 '18 at 3:19






  • 1





    Or use fflush(stdout);

    – torstenvl
    Nov 22 '18 at 3:22











  • Did you consider trying a larger value for SECONDS? That would have been the very first thing I tried in order to see if the call was working or not.

    – Ken White
    Nov 22 '18 at 3:22
















0












0








0









This question already has an answer here:




  • Why does printf not flush after the call unless a newline is in the format string?

    9 answers




I'm trying to force my program to sleep for a second after it creates a thread. I can make the thread sleep within its process but not in the main. Everything I read says it is just a system process that should work wherever it's called. I want it to sleep after printing that the thread has been created (before the join). There is no delay between the "create" and "joined and exited" print statements. Can you tell why it isn't working?



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define SECONDS 1 //seconds to sleep
#define SIZE 25 //number of fibonaccis to be computed

int *fibResults; //array to store fibonacci results
int idx; //index for the threads

//executes and exits each thread
void *run(void *param) {
if (idx == 0) {

sleep(SECONDS);
fibResults[idx] = 0;
pthread_exit(0);

} else if (idx == 1) {

sleep(SECONDS);
fibResults[idx] = 1;
pthread_exit(0);

} else {

sleep(SECONDS);
fibResults[idx] = fibResults[idx -1] + fibResults[idx -2];
pthread_exit(0);

}
}


int main(void) {
fibResults = malloc(SIZE * sizeof(*fibResults));
pthread_attr_t attrs;
pthread_attr_init(&attrs);

for (idx = 0; idx < SIZE; idx++) {
pthread_t thread;
pthread_create(&thread, &attrs, run, NULL);

printf("Thread[%d] createdt", idx);

sleep(SECONDS); //THIS IS WHERE SLEEP ISN'T WORKING!!!!!!!!!!!!!

pthread_join(thread, NULL);

printf("Thread[%d] joined & exitedt", idx);
printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
}
return 0;
}









share|improve this question

















This question already has an answer here:




  • Why does printf not flush after the call unless a newline is in the format string?

    9 answers




I'm trying to force my program to sleep for a second after it creates a thread. I can make the thread sleep within its process but not in the main. Everything I read says it is just a system process that should work wherever it's called. I want it to sleep after printing that the thread has been created (before the join). There is no delay between the "create" and "joined and exited" print statements. Can you tell why it isn't working?



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define SECONDS 1 //seconds to sleep
#define SIZE 25 //number of fibonaccis to be computed

int *fibResults; //array to store fibonacci results
int idx; //index for the threads

//executes and exits each thread
void *run(void *param) {
if (idx == 0) {

sleep(SECONDS);
fibResults[idx] = 0;
pthread_exit(0);

} else if (idx == 1) {

sleep(SECONDS);
fibResults[idx] = 1;
pthread_exit(0);

} else {

sleep(SECONDS);
fibResults[idx] = fibResults[idx -1] + fibResults[idx -2];
pthread_exit(0);

}
}


int main(void) {
fibResults = malloc(SIZE * sizeof(*fibResults));
pthread_attr_t attrs;
pthread_attr_init(&attrs);

for (idx = 0; idx < SIZE; idx++) {
pthread_t thread;
pthread_create(&thread, &attrs, run, NULL);

printf("Thread[%d] createdt", idx);

sleep(SECONDS); //THIS IS WHERE SLEEP ISN'T WORKING!!!!!!!!!!!!!

pthread_join(thread, NULL);

printf("Thread[%d] joined & exitedt", idx);
printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
}
return 0;
}




This question already has an answer here:




  • Why does printf not flush after the call unless a newline is in the format string?

    9 answers








c multithreading sleep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 3:16







efuddy

















asked Nov 22 '18 at 2:49









efuddyefuddy

495




495




marked as duplicate by larsks, John Kugelman c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by larsks, John Kugelman c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • what makes you think it isn't working?

    – mangusta
    Nov 22 '18 at 3:08











  • Works on my machine. What error are you getting/what is the behavior you're getting that makes you think it isn't working?

    – torstenvl
    Nov 22 '18 at 3:10











  • There is no delay between the "create" and "joined and executed" print statements.

    – efuddy
    Nov 22 '18 at 3:19






  • 1





    Or use fflush(stdout);

    – torstenvl
    Nov 22 '18 at 3:22











  • Did you consider trying a larger value for SECONDS? That would have been the very first thing I tried in order to see if the call was working or not.

    – Ken White
    Nov 22 '18 at 3:22





















  • what makes you think it isn't working?

    – mangusta
    Nov 22 '18 at 3:08











  • Works on my machine. What error are you getting/what is the behavior you're getting that makes you think it isn't working?

    – torstenvl
    Nov 22 '18 at 3:10











  • There is no delay between the "create" and "joined and executed" print statements.

    – efuddy
    Nov 22 '18 at 3:19






  • 1





    Or use fflush(stdout);

    – torstenvl
    Nov 22 '18 at 3:22











  • Did you consider trying a larger value for SECONDS? That would have been the very first thing I tried in order to see if the call was working or not.

    – Ken White
    Nov 22 '18 at 3:22



















what makes you think it isn't working?

– mangusta
Nov 22 '18 at 3:08





what makes you think it isn't working?

– mangusta
Nov 22 '18 at 3:08













Works on my machine. What error are you getting/what is the behavior you're getting that makes you think it isn't working?

– torstenvl
Nov 22 '18 at 3:10





Works on my machine. What error are you getting/what is the behavior you're getting that makes you think it isn't working?

– torstenvl
Nov 22 '18 at 3:10













There is no delay between the "create" and "joined and executed" print statements.

– efuddy
Nov 22 '18 at 3:19





There is no delay between the "create" and "joined and executed" print statements.

– efuddy
Nov 22 '18 at 3:19




1




1





Or use fflush(stdout);

– torstenvl
Nov 22 '18 at 3:22





Or use fflush(stdout);

– torstenvl
Nov 22 '18 at 3:22













Did you consider trying a larger value for SECONDS? That would have been the very first thing I tried in order to see if the call was working or not.

– Ken White
Nov 22 '18 at 3:22







Did you consider trying a larger value for SECONDS? That would have been the very first thing I tried in order to see if the call was working or not.

– Ken White
Nov 22 '18 at 3:22














1 Answer
1






active

oldest

votes


















0














for (idx = 0; idx < SIZE; idx++)
{
pthread_t thread;
pthread_create(&thread, &a, run, NULL);
printf("Thread[%d] createdt", idx);
fflush(stdout);
sleep(SECONDS);
pthread_join(thread, NULL);
printf("Thread[%d] joined & exitedt", idx);
printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
}





share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    for (idx = 0; idx < SIZE; idx++)
    {
    pthread_t thread;
    pthread_create(&thread, &a, run, NULL);
    printf("Thread[%d] createdt", idx);
    fflush(stdout);
    sleep(SECONDS);
    pthread_join(thread, NULL);
    printf("Thread[%d] joined & exitedt", idx);
    printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
    }





    share|improve this answer






























      0














      for (idx = 0; idx < SIZE; idx++)
      {
      pthread_t thread;
      pthread_create(&thread, &a, run, NULL);
      printf("Thread[%d] createdt", idx);
      fflush(stdout);
      sleep(SECONDS);
      pthread_join(thread, NULL);
      printf("Thread[%d] joined & exitedt", idx);
      printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
      }





      share|improve this answer




























        0












        0








        0







        for (idx = 0; idx < SIZE; idx++)
        {
        pthread_t thread;
        pthread_create(&thread, &a, run, NULL);
        printf("Thread[%d] createdt", idx);
        fflush(stdout);
        sleep(SECONDS);
        pthread_join(thread, NULL);
        printf("Thread[%d] joined & exitedt", idx);
        printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
        }





        share|improve this answer















        for (idx = 0; idx < SIZE; idx++)
        {
        pthread_t thread;
        pthread_create(&thread, &a, run, NULL);
        printf("Thread[%d] createdt", idx);
        fflush(stdout);
        sleep(SECONDS);
        pthread_join(thread, NULL);
        printf("Thread[%d] joined & exitedt", idx);
        printf("The fibonacci of %d= %dn", idx, fibResults[idx]);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 15:17









        John Kugelman

        246k54406459




        246k54406459










        answered Nov 22 '18 at 3:25









        efuddyefuddy

        495




        495

















            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini