3.2线程作业

本文展示了三个C语言程序示例,分别使用信号量、条件变量以及信号量实现特定的并发控制。第一个程序使用线程和信号量来读取文件内容并打印;第二个程序利用条件变量保证线程按A、B、C的顺序打印;第三个程序则通过两个信号量实现打印和倒置字符串的交替执行。这些示例展示了并发编程中同步和互斥的概念。
摘要由CSDN通过智能技术生成

1.将一个文件中的数据打印到终端上,类似cat一个文件。要求如下

a.线程读取文件中的数据

b.线程将A线程读取到的数据打印到终端上

c.文件打印完毕后,结束进程。

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdlib.h>

char buf[128] = "";
sem_t sem;

void* callback1(void* arg)
{
    FILE *p = fopen("./07.c","r");
    if(NULL == p)
    {
        perror("fopen");
        pthread_exit(NULL);
    }
    while(1)
    {
        //P操作
        if(sem_wait(&sem) < 0)
        {
            perror("sem_wait");
            break;
        }
        //读文件内容
        if(fgets(buf,sizeof(buf),p) == NULL)
        {
            sem_post(&sem);
            break;
        }
        //V操作
        if(sem_post(&sem) < 0)
        {
             perror("sem_post");
             break;
        }
        fclose(p);
        pthread_exit(NULL);
    }
}

void* callback2(void* arg)
{
    while(1)
    {
        //P操作
        if(sem_wait(&sem) < 0)
        {
            perror("sem_wait");
            break;
        }
        //打印到终端
        if(strlen(buf) == 0)
            break;
        printf("%s",buf);
        fflush(stdout);
        bzero(buf,sizeof(buf));
        //V操作
        if(sem_post(&sem) < 0)
        {
             perror("sem_post");
             break;
        }
        pthread_exit(NULL);
    }
}
int main(int argc, const char *argv[])
{
    //创建信号量
    if(sem_init(&sem,0,1) < 0)
    {
        perror("sem_init");
        return -1;
    }
    pthread_t tid1,tid2;
    //创建线程,读文件内容
    if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }
    pthread_detach(tid1);
    //创建线程,打印到终端
    if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }
    pthread_join(tid2,NULL);
    sem_destroy(&sem);
    return 0;
}

2.用条件变量实现,有编号为ABC的三个线程,线程内分别打印自己的线程编号,要求打印的顺序为ABC。

提示:多个条件变量

#include <stdio.h>
#include <string.h>
#include <pthread.h>

pthread_mutex_t mutex;
pthread_cond_t cond;

int flag = 0;//0为A,1为B,2为C
int i=0;
void* fun1(void* arg)
{
    while(i<1)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        if(0!=flag)
        {
             pthread_cond_wait(&cond,&mutex);
        }
        else
        {
             printf("A\n");
             fflush(stdout);
             flag=1;
        }
        //唤醒cond上的线程
        pthread_cond_signal(&cond);
        //解锁
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void* fun2(void* arg)
{
    while(i<1)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        if(1!=flag)
        {
             pthread_cond_wait(&cond,&mutex);
        }
        else
        {
             printf("B\n");
             fflush(stdout);
             flag=2;
        }
        //唤醒cond上的线程
        pthread_cond_signal(&cond);
        //解锁
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void* fun3(void* arg)
{
    while(i<1)
    {
        //上锁
        pthread_mutex_lock(&mutex);
        if(2!=flag)
        {
             pthread_cond_wait(&cond,&mutex);
        }
        else
        {
             printf("C\n");
             fflush(stdout);
             flag=0;
             i++;
        }
        //唤醒cond上的线程
        pthread_cond_signal(&cond);
        //解锁
        pthread_mutex_unlock(&mutex);
    }

    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
    //创建互斥锁
    if(pthread_mutex_init(&mutex,NULL)!=0)
    {
         fprintf(stderr,"pthread_mutex_init failed __%d__\n",__LINE__);
         return -1;
    }
    //创建条件变量
    if(pthread_cond_init(&cond,NULL)!=0)
    {
         fprintf(stderr,"pthread_cond_init failed __%d__\n",__LINE__);
         return -1;
    }
    pthread_t A,B,C;
    //创建线程A
    if(pthread_create(&A, NULL, fun1, NULL) != 0)
    {
        fprintf(stderr, "pthread_create failed\n");
        return -1;
    }
    //创建线程B
    if(pthread_create(&B, NULL, fun2, NULL) != 0)
    {
        fprintf(stderr, "pthread_create failed\n");
        return -1;
    }
    //创建线程C
    if(pthread_create(&C, NULL, fun3, NULL) != 0)
    {
        fprintf(stderr, "pthread_create failed\n");
        return -1;
    }
    pthread_join(A,NULL);
    pthread_join(B,NULL);
    pthread_join(C,NULL);
    //销毁互斥锁
    pthread_mutex_destroy(&mutex);
    //销毁条件变量
    pthread_cond_destroy(&cond);
    return 0;
}

3.要求用信号量的方式实现,打印一次倒置一次

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>

char arr[]="1234567";
sem_t sem1, sem2;

void* callback1(void* arg)
{
    while(1)
    {
         //sem1 P操作
         if(sem_wait(&sem1) < 0)
         {
             perror("sem_wait");
             break;
         }
         printf("%s\n", arr);
         //sem2 V操作
         if(sem_post(&sem2) < 0)
         {
             perror("sem_post");
             break;
         }
    }
    pthread_exit(NULL);
}

void* callback2(void* arg)
{
    char temp = 0;
    //sem2 P操作
    while(1)
    {    
        if(sem_wait(&sem2) < 0)
        {
            perror("sem_wait");
            break;
        }
        //倒置
        for(int i=0;i<strlen(arr)/2;i++)
        {
            temp=arr[i];
             arr[i]=arr[strlen(arr)-i-1];
            arr[strlen(arr)-i-1]=temp;
        }
        //sem1 V操作
        if(sem_post(&sem1) < 0)
        {
            perror("sem_post");
            break;
        }
    }
    pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
    //创建信号量
    if(sem_init(&sem1,0,1) < 0)
    {
        perror("sem_init");
        return -1;
    }
    if(sem_init(&sem2,0,1) < 0)
    {
        perror("sem_init");
        return -1;
    }
    pthread_t tid1,tid2;
    //创建线程,打印
    if(pthread_create(&tid1,NULL,callback1,NULL)!=0)
    {
        fprintf(stderr, "pthread_create failed\n");
        return -1;
    }
    pthread_detach(tid1);
    //创建线程,倒置
    if(pthread_create(&tid2,NULL,callback2,NULL)!=0)
    {
        fprintf(stderr, "pthread_create failed\n");
        return -1;
    }
    pthread_join(tid2,NULL);
    sem_destroy(&sem1);
    sem_destroy(&sem2);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值