A small example of process synchronization problem in Linux __linux

Source: Internet
Author: User

The following code allocates space to the memory file using Mmap, and uses fork to generate a parent-child process that synchronizes and deadlocks through the semaphore in Semaphore. Completes the synchronization of the parent-child process.

#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <semaphore.h> #include <
sys/mman.h> int* count = 0;
sem_t *mutex1 = NULL;
sem_t *mutex2 = NULL;
int finish = 0; int main () {mutex1 = (sem_t*) mmap (NULL, sizeof (sem_t), prot_read| Prot_write, map_anon|
    Map_shared,-1, 0); Mutex2 = (sem_t*) mmap (NULL, sizeof (sem_t), prot_read| Prot_write, map_anon|
    Map_shared,-1, 0); Count = (int*) mmap (null,sizeof (int), prot_read| Prot_write, map_anon|
    Map_shared, -1,0);
    Sem_init (MUTEX1, 1, 1);
    Sem_init (mutex2, 1, 0);
    *count = 0;
            if (fork () ==0) {while (1) {printf ("Child continue!\n");
            Sem_wait (MUTEX1);
            *count + 2;
            printf ("child:%d\n", *count);
            finish++;
            Sem_post (MUTEX2);
        if (finish = = ten) break;
        printf ("Child finish!\n");
    Exit (0);
while (1) {printf ("Father continue!\n");        Sem_wait (MUTEX2);
        *count-= 1;
        printf ("father:%d\n", *count);
        finish++;
        Sem_post (MUTEX1);
    if (finish = = ten) break;
    } Sem_destroy (MUTEX1);
    Sem_destroy (MUTEX2);
    Munmap (mutex1, sizeof (sem_t));
    Munmap (mutex2, sizeof (sem_t));
    Munmap (count, sizeof (int));
    printf ("Finished all!\n");
return 0;
 }

Compiling under Linux

[Root@localhost myfile]# gcc testsem.c-  o testsem-lpthread

Run Result:

[Root@localhost myfile]#./testsem 
Father continue!
Child continue!
Child:2 Child
continue!
Father:1
Father continue!
Child:3 Child
continue!
Father:2
Father continue!
Child:4 Child
continue!
Father:3
Father continue!
Child:5 Child
continue!
Father:4
Father continue!
Child:6 Child
continue!
Father:5
Father continue!
Child:7 Child
continue!
Father:6
Father continue!
Child:8 Child
continue!
Father:7
Father continue!
Child:9 Child
continue!
Father:8
Father continue!
Child:10 Child
continue!
Father:9
Father continue!
Child:11 Child
finish!
Father:10
finished all!

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.