Jonathan Baxevanidis
Jonathan Baxevanidis

Reputation: 119

Multithreading printf() with semaphores in c

What I want to achieve is for the three separate prints to be executed one after the other, printing the message “What a wonderful world!”. What I get instead is “what a what a what a wonderful wonderful wonderful world world world”. I think there is something wrong with the pthread_join(). The program will end when the user inputs ctrl+c.

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

sem_t Asem;
sem_t Bsem;
sem_t Csem;

void *print_WhatA(){
    while(1){
        sem_wait(&Asem);
        printf("What A ");
        sem_post(&Bsem);
    }
} 

void *print_Wonderful(){
    while(1){
        sem_wait(&Bsem);
        printf("Wonderful ");
        sem_post(&Csem);
    }
}

void *print_World(){
    while(1){
        sem_wait(&Csem);
        printf("World!\n");
        sem_post(&Asem);
    }
}

void main(){
    sem_init(&Asem,0,0);    //initialization of the first semaphore
    sem_init(&Bsem,0,1);    //initialization of the second semaphore
    sem_init(&Csem,0,2);    //initialization of the third semaphore
    pthread_t A_thread;    
    pthread_t B_thread;
    pthread_t C_thread;
    pthread_create(&A_thread,NULL, print_WhatA,NULL);       //thread creation for print_WhatA function 
    pthread_create(&B_thread,NULL, print_Wonderfull,NULL);  //thread creation for print_Wonderful function
    pthread_create(&C_thread,NULL, print_World,NULL);       //thread creation for print_World function
    pthread_join(A_thread,NULL);
    pthread_join(B_thread,NULL);
    pthread_join(C_thread,NULL);
}

Upvotes: 1

Views: 1592

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60068

You should initialize semaphores B and C to 0 and semaphore A to 1. This works:

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

sem_t Asem;
sem_t Bsem;
sem_t Csem;

void *print_WhatA(){
    while(1){
        sem_wait(&Asem);
        printf("What A ");
        sem_post(&Bsem);
    }
} 

void *print_Wonderful(){
    while(1){
        sem_wait(&Bsem);
        printf("Wonderful ");
        sem_post(&Csem);
    }
}

void *print_World(){
    while(1){
        sem_wait(&Csem);
        printf("World!\n");
        sem_post(&Asem);
    }
}

void main(){
    sem_init(&Asem,0,1);    //initialization of the first semaphore
    sem_init(&Bsem,0,0);    //initialization of the second semaphore
    sem_init(&Csem,0,0);    //initialization of the third semaphore
    pthread_t A_thread;    
    pthread_t B_thread;
    pthread_t C_thread;
    pthread_create(&A_thread,NULL, print_WhatA,NULL);       //thread creation for print_WhatA function 
    pthread_create(&B_thread,NULL, print_Wonderful,NULL);  //thread creation for print_Wonderful function
    pthread_create(&C_thread,NULL, print_World,NULL);       //thread creation for print_World function
    pthread_join(A_thread,NULL);
    pthread_join(B_thread,NULL);
    pthread_join(C_thread,NULL);
}

Upvotes: 2

Related Questions