C program not sleeping in main() [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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;
}
c multithreading sleep
marked as duplicate by larsks, John Kugelman
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.
|
show 3 more comments
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;
}
c multithreading sleep
marked as duplicate by larsks, John Kugelman
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 usefflush(stdout);
– torstenvl
Nov 22 '18 at 3:22
Did you consider trying a larger value forSECONDS
? 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
|
show 3 more comments
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;
}
c multithreading sleep
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
c multithreading sleep
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
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
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 usefflush(stdout);
– torstenvl
Nov 22 '18 at 3:22
Did you consider trying a larger value forSECONDS
? 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
|
show 3 more comments
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 usefflush(stdout);
– torstenvl
Nov 22 '18 at 3:22
Did you consider trying a larger value forSECONDS
? 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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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]);
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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]);
}
add a comment |
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]);
}
add a comment |
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]);
}
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]);
}
edited Nov 23 '18 at 15:17
John Kugelman
248k54406460
248k54406460
answered Nov 22 '18 at 3:25
efuddyefuddy
495
495
add a comment |
add a comment |
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