3
#include <stdio.h>
#include "mythreads.h"
#include <stdlib.h>
#include <pthread.h>

void *
mythread(void *arg) {
    printf("%s\n", (char *) arg);
    return NULL;
}

int
main(int argc, char *argv[])
{
    if (argc != 1) {
        fprintf(stderr, "usage: main\n");
        exit(1);
    }

    pthread_t p1, p2;
    printf("main: begin\n");
    Pthread_create(&p1, NULL, mythread, "A");
    Pthread_create(&p2, NULL, mythread, "B");
    // join waits for the threads to finish
    //Pthread_join(p1, NULL); 
    //Pthread_join(p2, NULL); 
    printf("main: end\n");
    return 0;
}

This is a code from Remzi Chapter 27. Playing around, I'm curious to know why sometimes on run, I get A printed twice. I know why this is happening, because I haven't included the join statement. Why should skipping join cause this?

My output:

shubham@HP:~/OS/Code-Threads-Intro$ ./a.out 
main: begin
A
main: end
B
shubham@HP:~/OS/Code-Threads-Intro$ ./a.out 
main: begin
A
main: end
B
shubham@HP:~/OS/Code-Threads-Intro$ ./a.out 
main: begin
main: end
A
shubham@HP:~/OS/Code-Threads-Intro$ ./a.out 
main: begin
main: end
B
A
A
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
FlyingAura
  • 1,541
  • 5
  • 26
  • 41
  • 1
    Please fix the compiler errors in your code and remove the dependency on a non-provided `"mythreads.h"` header. – o11c Oct 16 '15 at 17:23
  • 1
    Are the capitalized function names cover functions for the all lower-case versions of the function name except they do some sort of error handling and perhaps reporting? As it stands, only someone intimately familiar with the `"mythreads.h"` header and the book/course materials can reliably help. We can make (more or less well) educated guesses, but we don't like having to guess. – Jonathan Leffler Oct 16 '15 at 17:30
  • Possible duplicate of [pthread: one printf statement get printed twice in child thread](http://stackoverflow.com/questions/13550662/pthread-one-printf-statement-get-printed-twice-in-child-thread) – Zan Lynx Oct 16 '15 at 17:32

1 Answers1

0

Removing the call the pthread_join() should not cause "A" to be printed twice. (Barring a bug in the implementation.)

But since your fprintf() calls share the same FILE * structure, they may not be not multithread-safe. How and why they could print "A" twice is dependent upon the implementation details of your system's fprintf() function.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • Thank you for adding insight. But I dont think I clearly follow your answer. You say that its being printed twice due to implementation flaw of printf()? – FlyingAura Oct 19 '15 at 19:26
  • Yes. It's a bug: https://sourceware.org/bugzilla/show_bug.cgi?id=14697 *This bug is due to intentional hackery in glibc to avoid hanging on exit() due to locks being held by other threads, under the wrong assumption that exit() "should" immediately exit in this case. There is no language in the standards to support what glibc is doing.* Also: *It seems that this bug can also result in more serious corruption such as duplicate output, even without any explicit file locking.* – Andrew Henle Oct 19 '15 at 20:06