8 thoughts on “C Programming in Linux Tutorial #046 – Semaphore pthread

  1. give like this error
    ornek.c:(.text+0x2b): undefined reference to `myfunc1'
    ornek.c:(.text+0x48): undefined reference to `myfunc2'
    collect2: error: ld returned 1 exit status

  2. #include<stdio.h>
    #include<unistd.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include<unistd.h>
    void myfunc1(void *ptr);
    void myfunc2(void *ptr);
    int main(int argc, char *argv [] )
    {
    pthread_t thread1;
    pthread_t thread2;

    char *msg1="Thread1";
    char *msg2="Thread2";

    pthread_create(&thread1,NULL,(void*) &myfunc1,(void *) msg1);
    pthread_create(&thread2,NULL,(void*) &myfunc2,(void *) msg2);

    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);
    return 0;
    }
    void muyfunc1(void *ptr)
    {
    char *msg=(char *)ptr;
    printf("%sn",msg);
    pthread_exit(0);
    }
    void muyfunc2(void *ptr)
    {
    char *msg=(char *)ptr;
    printf("%sn",msg);
    pthread_exit(0);
    }

  3. This code doesn't always ensure that the coping into the buf will occur first and then only the printing of buf. Am I right? Because it can happen that thread 1 can wait on semaphore while its been held by thread 2

Leave a Reply

Your email address will not be published. Required fields are marked *

Udemy