以前操作系统课程设计的时候,我们在Linux操作系统中用C语言和POSIX线程库编写过线程同步与互斥。

/*
 * thread_synchronization.c
 * 
 * Copyright 2012 叶剑飞
 *
 * 编译命令:
 *        gcc thread_synchronization.c -o thread_synchronization \
 *              -std=gnu99 -pedantic-errors -D_REENTRANT -pthread -Wall
 * 
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <time.h>

typedef int COUNT;

sem_t p12, p13, p24, p25, p35, p46, p56;
int maxLoop[7];

void * thread1 ( void * args )
{
    for ( COUNT i = 0 ; i < maxLoop[1] ; i ++ )
    {
        puts( "I am thread one." );
        usleep(500000);
    }
    sem_post( &p12 );
    sem_post( &p13 );
    return NULL;
}

void * thread2 ( void * args )
{
    sem_wait( &p12 );
    for ( COUNT i = 0 ; i < maxLoop[2] ; i ++ )
    {
        puts( "I am thread two." );
        usleep(500000);
    }
    sem_post( &p24 );
    sem_post( &p25 );
    return NULL;
}

void * thread3 ( void * args )
{
    sem_wait( &p13 );
    for ( COUNT i = 0 ; i < maxLoop[3] ; i ++ )
    {
        puts( "I am thread three." );
        usleep(500000);
    }
    sem_post( &p35 );
    return NULL;
}

void * thread4 ( void * args )
{
    sem_wait( &p24 );
    for ( COUNT i = 0 ; i < maxLoop[4] ; i ++ )
    {
        puts( "I am thread four." );
        usleep(500000);
    }
    sem_post( &p46 );
    return NULL;
}

void * thread5 ( void * args )
{
    sem_wait( &p25 );
    sem_wait( &p35 );
    for ( COUNT i = 0 ; i < maxLoop[5] ; i ++ )
    {
        puts( "I am thread five." );
        usleep(500000);
    }
    sem_post( &p56 );
    return NULL;
}

void * thread6 ( void * args )
{
    sem_wait( &p46 );
    sem_wait( &p56 );
    for ( COUNT i = 0 ; i < maxLoop[6] ; i ++ )
    {
        puts( "I am thread six." );
        usleep(500000);
    }
    return NULL;
}

int main (void)
{
    pthread_t threadid[7];
    const int n = 10;

    srand( time(NULL) );

    for ( COUNT i = 1 ; i < 7 ; i ++ )
        maxLoop[i] = (rand() % n) + 1;

    if ( sem_init( &p12, 0, 0 ) == -1 )
    {
        fprintf( stderr, "Error Initializing Semaphore\n\n");
        return EXIT_FAILURE;
    }
    if ( sem_init( &p13, 0, 0 ) == -1 )
    {
        fprintf( stderr, "Error Initializing Semaphore\n\n");
        return EXIT_FAILURE;
    }
    if ( sem_init( &p24, 0, 0 ) == -1 )
    {
        fprintf( stderr, "Error Initializing Semaphore\n\n");
        return EXIT_FAILURE;
    }
    if ( sem_init( &p25, 0, 0 ) == -1 )
    {
        fprintf( stderr, "Error Initializing Semaphore\n\n");
        return EXIT_FAILURE;
    }
    if ( sem_init( &p35, 0, 0 ) == -1 )
    {
        fprintf( stderr, "Error Initializing Semaphore\n\n");
        return EXIT_FAILURE;
    }
    if ( sem_init( &p46, 0, 0 ) == -1 )
    {
        fprintf( stderr, "Error Initializing Semaphore\n\n");
        return EXIT_FAILURE;
    }
    if ( sem_init( &p56, 0, 0 ) == -1 )
    {
        fprintf( stderr, "Error Initializing Semaphore\n\n");
        return EXIT_FAILURE;
    }

    if ( pthread_create( &threadid[1], NULL, thread1, NULL ) )
    {
        fprintf( stderr, "Error Creating Thread.\n\n" );
        return EXIT_FAILURE;
    }
    if ( pthread_create( &threadid[2], NULL, thread2, NULL ) )
    {
        fprintf( stderr, "Error Creating Thread.\n\n" );
        return EXIT_FAILURE;
    }
    if ( pthread_create( &threadid[3], NULL, thread3, NULL ) )
    {
        fprintf( stderr, "Error Creating Thread.\n\n" );
        return EXIT_FAILURE;
    }
    if ( pthread_create( &threadid[4], NULL, thread4, NULL ) )
    {
        fprintf( stderr, "Error Creating Thread.\n\n" );
        return EXIT_FAILURE;
    }
    if ( pthread_create( &threadid[5], NULL, thread5, NULL ) )
    {
        fprintf( stderr, "Error Creating Thread.\n\n" );
        return EXIT_FAILURE;
    }
    if ( pthread_create( &threadid[6], NULL, thread6, NULL ) )
    {
        fprintf( stderr, "Error Creating Thread.\n\n" );
        return EXIT_FAILURE;
    }

    for ( COUNT i = 1 ; i < 7 ; i ++ )
    {
        if ( pthread_join( threadid[i] , NULL ) )
        {
            fprintf( stderr, "Error Joining Thread.\n\n" );
            return EXIT_FAILURE;
        }
    }

    return EXIT_SUCCESS;
}
C语言+POSIX线程库 实现的线程同步与互斥 代码

相关文章:

  • 2021-07-08
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
  • 2021-11-18
  • 2021-08-09
  • 2021-07-15
猜你喜欢
  • 2021-12-04
  • 2022-01-22
  • 2021-07-15
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
  • 2021-05-04
相关资源
相似解决方案