1.13 多线程编程

1.思维导图

2.创建两个子进程,父进程负责:向文件中写入数据;两个子进程负责:从文件中读取数据。

要求:一定保证1号子进程先读取,2号子进程后读取,使用文件IO去实现。

1>程序代码
cs 复制代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

// 打开文件,写入数据
void task1()
{
    int fd = open("t1.txt", O_CREAT | O_RDWR | O_TRUNC, 0666);
    if (fd == -1)
    {
        perror("open");
        return;
    }
    char buf[60] = "Hello world!";
    write(fd, buf, sizeof(buf));
    close(fd);
}

// 读取数据
void task2()
{
    int fd = open("t1.txt", O_RDONLY);
    lseek(fd, 0, SEEK_SET);
    char buf[60] = {0};
    int res;
    while ((res = read(fd, buf, 1)) > 0)
    {
        // 打印读取的字节
        putchar(buf[0]);
    }
    close(fd);
}


int main(int argc, const char *argv[])
{
    // 创建第一个子进程
    pid_t pid1 = fork();
    if (pid1 == 0)
    {
        sleep(1);
        task2();
        printf("\n1号子进程读取数据成功!\n");
    }
    else if (pid1 > 0)
    {
        // 创建第二个子进程
        pid_t pid2 = fork();
        if (pid2 == 0)
        {
            sleep(2);
            task2();
            printf("\n2号子进程读取数据成功!\n");
        }
        else if (pid2 > 0)
        {
            // 父进程的操作
            task1();
            wait(0);
            wait(0);
        }
    }

    return 0;
}
2>运行结果

3.创建一个线程(结果为1个主线程和1个分支线程),主线程负责:输入三角形的三条边长;分支线程负责:计算三角形的面积(海伦公式)。

注意:使用sqrt函数编译的时候需要在编译的最后加上 -lm。

1>程序代码
cs 复制代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
#include <math.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

typedef struct Triangle
{
	double a;
	double b;
	double c;
}Tri,*TriPtr;

void* thread_main(void* arg)
{
	TriPtr tri = (TriPtr)arg;
	double s = (tri->a + tri->b + tri->c)/2;
	double area = sqrt(s*(s-(tri->a))*(s-(tri->b))*(s-(tri->c)));
	printf("这个三角形的面积为:%.2lf\n",area);
	free(tri);
}

int main(int argc, const char *argv[])
{
	TriPtr tri = (TriPtr)malloc(sizeof(Tri));
	printf("请输入三角形的三条边:");
	scanf("%lf%lf%lf",&(tri->a),&(tri->b),&(tri->c));
	pthread_t id;
	pthread_create(&id,0,thread_main,(void*) tri);
	pthread_join(id,NULL);
	free(tri);
	return 0;
}
2>运行结果
相关推荐
wj3055853781 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
abigriver1 小时前
打造 Linux 离线大模型级语音输入法:Whisper.cpp + 3090 显卡加速与 Rime 中英混输终极调优指南
linux·运维·whisper
wangqiaowq2 小时前
windows下nginx的安装
linux·服务器·前端
YYRAN_ZZU2 小时前
Petalinux新建自动脚本启动
linux
charlie1145141913 小时前
嵌入式Linux驱动开发pinctrl篇(1)——从寄存器到子系统:驱动演进之路
linux·运维·驱动开发
于小猿Sup3 小时前
VMware在Ubuntu22.04驱动Livox Mid360s
linux·c++·嵌入式硬件·自动驾驶
cen__y3 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
不仙5205 小时前
VMware Workstation 26.0.0 在 Ubuntu 24.04 (内核 6.17.0) 上的安装与内核模块编译问题
linux·ubuntu·elasticsearch
AI视觉网奇5 小时前
linux 检索库 判断库是否支持
java·linux·服务器
dapeng-大鹏5 小时前
KVM+LVM 零停机在线扩容 Ubuntu 根分区:从磁盘添加到逻辑卷扩展完整
linux·运维·ubuntu·磁盘空间扩展