Pleasant Goat Series "thread" Mutex + semaphore

Source: Internet
Author: User
Tags semaphore

1. Write a basic multithreaded program (the main thread creates a sub-thread inside)

/*************************************************************************> File name:1_homework.c> Author: Liang > Addr: > Created time:2015 April 22 Wednesday 15:22 34 sec. *********************************************************** /#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h># include<pthread.h> #include <semaphore.h>//sem_t declaration void *fun1 (void *arg) {Pthread_detach (pthread_self           () );        Detach from the main thread while (1) {* (int *) arg) + = 2;        printf ("Fun1 The num is:%d \ n", * ((int*) arg));    Sleep (1);      } pthread_exit (0);    Main thread End}int main () {int num=3, ret1;    pthread_t Thread1;    Ret1 = Pthread_create (&thread1, NULL, FUN1, (void *) &num);        if (Ret1! = 0) {perror ("pthread_create1");    Exit (0);    } sleep (1);    printf ("=================== main End ================\n");      Pthread_exit (0); Main thread End}

2. Change the above program to a child thread to pass the return value to the parent thread

/*************************************************************************> File name:2_homework.c> Author: Liang > Addr:> Created time:2015 April 22 Wednesday 15:39 49 seconds *********************************************************** /#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>#    include<pthread.h> void *fun1 (void *arg) {int num=100;    printf ("---------------pthread--------------\ n");    printf ("The num is:%d \ n", num);    printf ("---------------end--------------\ n");      Pthread_exit (void *) num);    Main thread End}int main () {int ret1, num;    pthread_t Thread1;    Ret1 = pthread_create (&thread1, NULL, FUN1, NULL);        if (Ret1! = 0) {perror ("pthread_create1");    Exit (0);    } pthread_join (Thread1, (void *) &num);    printf ("---------------main--------------\ n");    printf ("Return the num is:%d \ n", (int) num);        printf ("---------------end--------------\ n"); Pthread_exit (0); Main thread End}


3. Write a program with multithreading to read and write data (can be a global variable or other data)

/*************************************************************************> File name:3_homework.c> Author: Liang > Addr: > Created time:2015 April 22 Wednesday 15:53 00 sec. *********************************************************** /#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h># include<pthread.h> #include <semaphore.h>//sem_t declaration void *fun1 (void *arg) {Pthread_detach (pthread_self           () );        Detach from the main thread while (1) {* (int *) arg) + = 2;        printf ("The num write:%d \ n", * ((int*) arg));    Sleep (1);      } pthread_exit (0);           Main thread end}void *fun2 (void *arg) {Pthread_detach (pthread_self ());        Detach from the main thread while (1) {printf ("The NUM read is:%d \ n", * ((int*) arg));    Sleep (1);      } pthread_exit (0);    Main thread End}int main () {int num=0, ret1;    pthread_t Thread1, Thread2;    Ret1 = Pthread_create (&thread1, NULL, FUN1, (void *) &num);    if (Ret1! = 0) {    Perror ("pthread_create1");    Exit (0);    } Ret1 = Pthread_create (&thread2, NULL, fun2, (void *) &num);        if (Ret1! = 0) {perror ("Pthread_create2");    Exit (0);    } sleep (1);    printf ("=================== main End ================\n");      Pthread_exit (0); Main thread End}


4. Using semaphores and mutexes respectively to realize the read-write mutex of the 3rd job

/*************************************************************************> File name:4_homework.c> Author: Liang > Addr:> Created time:2015 April 22 Wednesday 15:57 54 seconds *********************************************************** /#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>#     Include<pthread.h> #include <semaphore.h>//sem_t's statement sem_t SEM;  Signal volume declaration pthread_mutex_t mutex;           Mutual exclusion Lock declaration void *fun1 (void *arg) {Pthread_detach (pthread_self ());        Separate while (1) {sem_wait (&AMP;SEM) from the main thread;        * ((int *) arg) + = 2;        printf ("= = semaphore = = num write:%d \ n", * ((int*) arg));        Sleep (1);    Sem_post (&sem);      } pthread_exit (0);           Main thread end}void *fun2 (void *arg) {Pthread_detach (pthread_self ());        Separate while (1) {sem_wait (&AMP;SEM) from the main thread;        printf ("= = semaphore = = num Read is:%d \ n", * ((int*) arg));        Sleep (1);    Sem_post (&sem); } pthread_exit (0);      Main thread end}void *fun3 (void *arg) {Pthread_detach (pthread_self ());        Separate while (1) {Pthread_mutex_lock (&mutex) from the main thread;        * ((int *) arg) + = 2;        printf (">> Mutex >> num write:%d \ n", * ((int*) arg));        Sleep (1);    Pthread_mutex_unlock (&mutex);      } pthread_exit (0);           Main thread end}void *fun4 (void *arg) {Pthread_detach (pthread_self ());        Separate while (1) {Pthread_mutex_lock (&mutex) from the main thread;        printf (">> mutex >> num Read is:%d \ n", * ((int*) arg));        Sleep (1);    Pthread_mutex_unlock (&mutex);      } pthread_exit (0);    Main thread End}int main () {//------------------------semaphore Mutex--------------------int num1=0, RET1;    pthread_t Thread1, Thread2;    Sem_init (&sem, 0, 1);    Ret1 = Pthread_create (&thread1, NULL, FUN1, (void *) &AMP;NUM1);        if (Ret1! = 0) {perror ("pthread_create1");    Exit (0); } Ret1 = Pthread_create (&thread2, NULL, fun2, (void *) &num1);        if (Ret1! = 0) {perror ("Pthread_create2");    Exit (0);    }//------------------------mutex mutex--------------------int num2=1000, Ret2;    pthread_t Thread_1, Thread_2;    Pthread_mutex_init (&mutex, NULL);    Ret2 = Pthread_create (&thread_1, NULL, FUN3, (void *) &num2);        if (ret2!= 0) {perror ("pthread_create1");    Exit (0);    } Ret1 = Pthread_create (&thread_2, NULL, Fun4, (void *) &num2);        if (Ret2! = 0) {perror ("Pthread_create2");    Exit (0);    } printf ("=================== main End ================\n");      Pthread_exit (0); Main thread End}


5. Use the semaphore to realize the reading and writing synchronization of the 3rd job

/*************************************************************************> File name:5_homework.c> Author: Liang > Addr: > Created time:2015 April 22 Wednesday 16:36 01 sec. *********************************************************** /#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>#     Include<pthread.h> #include <semaphore.h>//sem_t's statement sem_t W_sem, R_sem;           Semaphore declaration void *fun1 (void *arg) {Pthread_detach (pthread_self ());        Separate while (1) {sem_wait (&AMP;W_SEM) from the main thread;        * ((int *) arg) + = 2;        printf (">>>> num write:%d \ n", * ((int*) arg));        Sleep (1);    Sem_post (&r_sem);      } pthread_exit (0);           Main thread end}void *fun2 (void *arg) {Pthread_detach (pthread_self ());        Separate while (1) {sem_wait (&AMP;R_SEM) from the main thread;        printf (">>>> num Read is:%d \ n", * ((int*) arg));        Sleep (1);    Sem_post (&w_sem); } Pthread_exiT (0);    Main thread End}int main () {//------------------------semaphore Mutex--------------------int num1=0, RET1;    pthread_t Thread1, Thread2;    Sem_init (&w_sem, 0, 1);    Sem_init (&r_sem, 0, 0);    Ret1 = Pthread_create (&thread1, NULL, FUN1, (void *) &AMP;NUM1);        if (Ret1! = 0) {perror ("pthread_create1");    Exit (0);    } Ret1 = Pthread_create (&thread2, NULL, fun2, (void *) &AMP;NUM1);        if (Ret1! = 0) {perror ("Pthread_create2");    Exit (0);    } printf ("=================== main End ================\n");      Pthread_exit (0); Main thread End}


Pleasant Goat Series "thread" Mutex + semaphore

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.