2024.2.22

1.将互斥机制的代码实现重新敲一遍

#include <myhead.h>
int num = 520;
pthread_mutex_t mutex;
 
 
void *task1(void *arg)
{
	printf("11111111\n");
	pthread_mutex_lock(&mutex);
	num = 1314;
	sleep(3);
 
	printf("task1:num=%d\n",num);
	pthread_mutex_unlock(&mutex);
 
}
 
void *task2(void *arg)
{
	printf("22222222\n");
	pthread_mutex_lock(&mutex);
	num++;
	sleep(1);
	printf("task2:num=%d\n",num);
	pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&mutex,NULL);
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task1,NULL)!=0)
	{
		printf("tid1 create error\n");
		return 0;
	}
	if(pthread_create(&tid2,NULL,task2,NULL)!=0)
	{
		printf("tid2 create error\n");
		return 0;
	}
	printf("tid1:%#lx,tid2:%#lx\n",tid1,tid2);
 
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
 
	pthread_mutex_destroy(&mutex);
 
	return 0;
}

效果显示:

2.将无名信号量的代码实现重新敲一遍

#include <myhead.h>
//定义无名信号量
sem_t sem;
//生产者线程
void *task1(void *arg)
{
	int num=5;
	while(num--)
	{
		//申请资源
		//sem_wait(&sem);
		sleep(1);
		printf("生产成功\n");
		//释放资源
		sem_post(&sem);
	}
	pthread_exit(NULL);
}
//消费者线程
void *task2(void *arg)
{
	int num=5;
	while(num--)
	{
		//申请资源
		sem_wait(&sem);
		printf("消费成功\n");
		//释放资源
		//sem_post(&sem);
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	//初始化无名信号量
	sem_init(&sem,0,0);
	//创建线程(生产者+消费者)
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,task1,NULL)!=0)
	{
		printf("tid1 create error");
		return 0;
	}
	if(pthread_create(&tid2,NULL,task2,NULL)!=0)
	{
		printf("tid2 create error");
		return 0;
	}
	//回收资源
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	//释放无名信号量
	sem_destroy(&sem);
	return 0;
}

效果显示:

3.将条件变量的代码实现重新敲一遍

#include<myhead.h>
#define MAXSIZE 128
 
pthread_cond_t cond;
pthread_mutex_t mutex;
 
void* task1(void* arg)
{
    int num = 5;
 
    while (num--)
    {
        sleep(1);
        printf("%#lx:生产\n", pthread_self());
    }
 
    pthread_cond_broadcast(&cond);
    pthread_exit(NULL);
}
 
void* task2(void* arg)
{
    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond, &mutex);
 
    printf("%#lx:获得\n", pthread_self());
 
    pthread_mutex_unlock(&mutex);
}
 
int main(int argc, char const *argv[])
{
    pthread_mutex_init(&mutex, NULL);
 
    pthread_t thread1, thread2, thread3, thread4, thread5, thread6;
    if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \
        pthread_create(&thread2, NULL, task2, NULL) != 0 || \
        pthread_create(&thread3, NULL, task2, NULL) != 0 || \
        pthread_create(&thread4, NULL, task2, NULL) != 0 || \
        pthread_create(&thread5, NULL, task2, NULL) != 0 || \
        pthread_create(&thread6, NULL, task2, NULL) != 0)
    {
        perror("thread create error");
        return -1;
    }
 
    printf("thread1:%#lx, thread2:%#lx, thread3:%#lx, thread4:%#lx, thread5:%#lx, thread6:%#lx\n", 
            thread1, thread2, thread3, thread4, thread5, thread6);
 
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    pthread_join(thread4, NULL);
    pthread_join(thread5, NULL);
    pthread_join(thread6, NULL);
 
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
 
    return 0;
}

效果显示:

4.将无名管道的代码实现重新敲一遍

#include <myhead.h>
 
#define MAXSIZE 1024
 
int main(int argc, char const *argv[])
{
    int pipefd[2] = { 0 };
    if (pipe(pipefd) == -1)
    {
        perror("pipe error");
        return -1;
    } 
    printf("pipefd[0]=%d, pipefd[1]=%d\n", pipefd[0], pipefd[1]);
 
    pid_t pid = fork();
    if (pid > 0)
    {
        close(pipefd[0]);
 
        char wbuf[128] = "";
        while (1)
        {
            bzero(wbuf, sizeof(wbuf));
 
            fgets(wbuf, sizeof(wbuf), stdin);
            wbuf[strlen(wbuf) - 1] = 0;
 
            write(pipefd[1], wbuf, strlen(wbuf));
            
            if (strcmp(wbuf, "quit") == 0)
            {
                break;
            }
        }
 
        close(pipefd[1]);
 
        wait(NULL);
    }
    else if (pid == 0)
    {
        close(pipefd[1]);
 
        char rbuf[128] = "";
        while (1)
        {
            bzero(rbuf, sizeof(rbuf));
 
            read(pipefd[0], rbuf, sizeof(rbuf));
            printf("父进程传入的字符串是:%s\n", rbuf);
            
            if (strcmp(rbuf, "quit") == 0)
            {
                break;
            }
        }
 
        close(pipefd[0]);
 
        exit(EXIT_SUCCESS);
    }
 
    return 0;
}

效果显示:

5.将有名管道的代码实现重新敲一遍

#include <myhead.h>
 
#define MAXSIZE 128
 
void menu()
{
    printf("请选择:\n");
    printf("1.创建管道\n");
    printf("2.输入\n");
    printf("3.输出\n");
    printf("4.删除管道\n");
    printf("选择:");
}
 
int init_fifo(char* filename)
{
    if (mkfifo(filename, 0664) == -1)
    {
        return -1;
    }
    return 0;
}
 
void* send(void* arg)
{
    char* filename = (char*)arg;
    
    int wfd = -1;
    if ((wfd = open(filename, O_WRONLY)) == -1)
    {
        perror("open error");
        exit(EXIT_FAILURE);
    }
 
    char wbuf[MAXSIZE] = "";
    while (1)
    {
        printf("输入>>>");
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf) - 1] = 0;
 
        write(wfd, wbuf, strlen(wbuf));
 
        if (strcmp(wbuf, "quit") == 0)
        {
            break;
        }
    }
 
    close(wfd);
    exit(EXIT_SUCCESS);
}
 
void* receive(void* arg)
{
    int rfd = -1;
    if ((rfd = open("./myfifo", O_RDONLY)) == -1)
    {
        perror("open error");
        exit(EXIT_FAILURE);
    }
 
    char rbuf[MAXSIZE] = "";
    while (1)
    {
        bzero(rbuf, sizeof(rbuf));
        read(rfd, rbuf, sizeof(rbuf));
 
        printf("收到的数据为:%s\n", rbuf);
 
        if (strcmp(rbuf, "quit") == 0)
        {
            break;
        }
    }
 
    close(rfd);
    exit(EXIT_SUCCESS);
}
 
int main(int argc, char const *argv[])
{
    int n;
    menu();
    scanf("%d", &n);
    getchar();
 
    char filename[] = "./myfifo";
    if (n == 1)
    {
        if (init_fifo(filename) == 0)
        {
            puts("创建成功!");
        }
        else
        {
            puts("创建失败!");
        }
        return 0;
    }
 
    if (n == 4)
    {
        char cmd[MAXSIZE];
        snprintf(cmd, sizeof(cmd), "rm %s", filename);
        system(cmd);
        return 0;
    }
 
    pthread_t thread = -1;
    if (n == 2)
    {
        if (pthread_create(&thread, NULL, send, filename) != 0)
        {
            perror("create thread error");
            return -1;
        }
    }
 
    if (n == 3)
    {
        if (pthread_create(&thread, NULL, receive, filename) != 0)
        {
            perror("create thread error");
            return -1;
        }
    }
 
    pthread_join(thread, NULL);
 
    return 0;
}

 效果显示:

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值