There are four threads 1,2,3,4. The function of thread 1 is to output 1, the function of thread 2 is to output 2, and so on...... Now there are four files ABCD

/**************************************************** ***************
 There are four threads 1,2,3,4.
 The function of thread 1 is to output 1, and the function of thread 2 is to output 2. By analogy,
 there are now four files ABCD. Initially all are empty. Now let the four files have the following format:
A: 1 2 3 4 1 2....
B: 2 3 4 1 2 3....
C: 3 4 1 2 3 4....
D: 4 1 2 3 4 1....
******************************************* ***************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>

pthread_t ids[4];

int fds[4];

sem_t sems[4];//定义信号量

void my_exit(int sig)
{
    for (int i = 0; i < 4; i++)//关线程
    {
       pthread_cancel(ids[i]);
       sem_destroy(sems+i);
    }
    
}

void * write_A(void *arg)
{
    int flag=0;
    char temp='A';
    while (1)
    {
        sem_wait(&sems[0]);

        for(int i=0;i<4;i++)
        {
            if (flag !=0 || i<=0)
            {
               write(fds[i],&temp,1);
            }         
        }
        flag=1;
        sem_post(&sems[1]);
    }    
}
void * write_B(void *arg)
{
    int flag=0;
    char temp='B';
    while (1)
    {
        sem_wait(&sems[1]);

        for(int i=0;i<4;i++)
        {
            if (flag !=0 || i<=1)
            {
               write(fds[i],&temp,1);
            }
            
        }
        flag=1;
        sem_post(&sems[2]);
    }
}
void * write_C(void *arg)
{
    int flag=0;
    char temp='C';
    while (1)
    {
        sem_wait(&sems[2]);

        for(int i=0;i<4;i++)
        {
            if (flag !=0 || i<=2)
            {
               write(fds[i],&temp,1);
            }
            
        }
        flag=1;
        sem_post(&sems[3]);
    }
}
void * write_D(void *arg)
{
    int flag=0;
    char temp='D';
    while (1)
    {
        sem_wait(&sems[3]);

        for(int i=0;i<4;i++)
        {
            if (flag !=0 || i<=3)
            {
               write(fds[i],&temp,1);
            }
            
        }
        flag=1;
        sem_post(&sems[0]);
    }
}

void open_file()
{
    char temp[1]="A";

    for (int i = 0; i < 4; i++)
    {
        temp[0]='A';
        temp[0]=temp[0]+i;
        if ((fds[i]=open(temp,O_RDWR | O_CREAT | O_TRUNC,0655))<0)
        {
            printf("打开文件错误!");
            exit(-1);
        }
        
    }
    
}

void init_sems()//初始化信号量
{
    for (int i = 0; i < 4; i++)
    {
        if(i==0)
        {
            sem_init(sems+i,0,1);
        }
        else
        {
            sem_init(sems+i,0,0);
        }
    }
    
}

int main(int argc,char **argv)
{
    int ret;
    int i;
    signal(SIGINT,my_exit);
    open_file();
    init_sems();
    void *(*p_func[4])(void *)={write_A,write_B,write_C,write_D};

    for ( i = 0; i < 4; i++)
    {
        ret=pthread_create(ids+i,NULL,p_func[i],NULL);

        if (ret<0)
        {
            perror("pthread create error!\n");
            exit(1);
        }
        pthread_detach(ids[i]);//自由回收
        
        
    }
    pause();//挂起
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_42849105/article/details/123409616