604 lines
33 KiB
JavaScript
604 lines
33 KiB
JavaScript
const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell,
|
||
Header, Footer, AlignmentType, LevelFormat, BorderStyle,
|
||
WidthType, ShadingType, VerticalAlign, PageNumber, PageBreak } = require('docx');
|
||
const fs = require('fs');
|
||
|
||
// Border style
|
||
const border = { style: BorderStyle.SINGLE, size: 1, color: "000000" };
|
||
const borders = { top: border, bottom: border, left: border, right: border };
|
||
const noBorder = { style: BorderStyle.NONE, size: 0, color: "FFFFFF" };
|
||
const noBorders = { top: noBorder, bottom: noBorder, left: noBorder, right: noBorder };
|
||
|
||
// Helper function to create a paragraph with text
|
||
function createParagraph(text, options = {}) {
|
||
const { fontSize = 24, bold = false, alignment = AlignmentType.LEFT,
|
||
firstLine = 0, indent = 0, spaceBefore = 0, spaceAfter = 0,
|
||
font = "宋体", eastAsia = "宋体" } = options;
|
||
|
||
return new Paragraph({
|
||
alignment,
|
||
spacing: {
|
||
before: spaceBefore,
|
||
after: spaceAfter,
|
||
line: 480,
|
||
lineRule: "auto"
|
||
},
|
||
indent: {
|
||
firstLineChars: firstLine,
|
||
firstLine: firstLine > 0 ? firstLine * 10 : 0
|
||
},
|
||
children: [
|
||
new TextRun({
|
||
text,
|
||
font,
|
||
size: fontSize,
|
||
bold,
|
||
eastAsia
|
||
})
|
||
]
|
||
});
|
||
}
|
||
|
||
// Helper for section heading
|
||
function createSectionHeading(text) {
|
||
return new Paragraph({
|
||
spacing: { before: 240, after: 120 },
|
||
children: [
|
||
new TextRun({
|
||
text,
|
||
font: "宋体",
|
||
size: 28,
|
||
bold: true
|
||
})
|
||
]
|
||
});
|
||
}
|
||
|
||
// Helper for subsection heading
|
||
function createSubHeading(text) {
|
||
return new Paragraph({
|
||
spacing: { before: 180, after: 80 },
|
||
children: [
|
||
new TextRun({
|
||
text,
|
||
font: "宋体",
|
||
size: 24,
|
||
bold: true
|
||
})
|
||
]
|
||
});
|
||
}
|
||
|
||
// Helper for normal paragraph with indentation
|
||
function createNormalParagraph(text, indent = 480) {
|
||
return new Paragraph({
|
||
spacing: { line: 480, lineRule: "auto" },
|
||
indent: { firstLineChars: 200, firstLine: indent },
|
||
children: [
|
||
new TextRun({
|
||
text,
|
||
font: "宋体",
|
||
size: 24
|
||
})
|
||
]
|
||
});
|
||
}
|
||
|
||
// Create the document
|
||
const doc = new Document({
|
||
styles: {
|
||
default: {
|
||
document: {
|
||
run: { font: "宋体", size: 24 }
|
||
}
|
||
}
|
||
},
|
||
sections: [{
|
||
properties: {
|
||
page: {
|
||
size: { width: 11907, height: 16840 },
|
||
margin: { top: 1134, right: 567, bottom: 1134, left: 567 }
|
||
}
|
||
},
|
||
children: [
|
||
// Title
|
||
new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
spacing: { before: 0, after: 0 },
|
||
children: [
|
||
new TextRun({
|
||
text: "网络空间安全学院",
|
||
font: "华文中宋",
|
||
size: 32
|
||
})
|
||
]
|
||
}),
|
||
new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
spacing: { before: 0, after: 0 },
|
||
children: [
|
||
new TextRun({
|
||
text: "实验报告(电子版)",
|
||
font: "华文中宋",
|
||
size: 32
|
||
})
|
||
]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Info table
|
||
new Table({
|
||
width: { size: 10411, type: WidthType.DXA },
|
||
columnWidths: [2016, 1461, 1800, 2224, 1318, 1592],
|
||
rows: [
|
||
new TableRow({
|
||
children: [
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 2016, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [new TextRun({ text: "实验名称", font: "宋体", size: 24 })]
|
||
})]
|
||
}),
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 5485, type: WidthType.DXA },
|
||
columnSpan: 3,
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [
|
||
new TextRun({ text: "Linux", font: "宋体", size: 24 }),
|
||
new TextRun({ text: "多线程编程", font: "宋体", size: 24 })
|
||
]
|
||
})]
|
||
}),
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 1318, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [new TextRun({ text: "指导教师", font: "宋体", size: 24 })]
|
||
})]
|
||
}),
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 1592, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({ children: [] })]
|
||
})
|
||
]
|
||
}),
|
||
new TableRow({
|
||
children: [
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 2016, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [new TextRun({ text: "姓 名", font: "宋体", size: 24 })]
|
||
})]
|
||
}),
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 1461, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [new TextRun({ text: "吕锦中", font: "宋体", size: 24 })]
|
||
})]
|
||
}),
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 1800, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [new TextRun({ text: "学 号", font: "宋体", size: 24 })]
|
||
})]
|
||
}),
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 2224, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [new TextRun({ text: "2024414290124", font: "宋体", size: 24 })]
|
||
})]
|
||
}),
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 1318, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [new TextRun({ text: "班 级", font: "宋体", size: 24 })]
|
||
})]
|
||
}),
|
||
new TableCell({
|
||
borders,
|
||
width: { size: 1592, type: WidthType.DXA },
|
||
verticalAlign: VerticalAlign.CENTER,
|
||
children: [new Paragraph({
|
||
alignment: AlignmentType.CENTER,
|
||
children: [new TextRun({ text: "软件工程一班", font: "宋体", size: 24 })]
|
||
})]
|
||
})
|
||
]
|
||
})
|
||
]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Section 1: 实验目的
|
||
createSectionHeading("一、实验目的:"),
|
||
createNormalParagraph("1. 通过编程训练,掌握多线程编程、线程间互斥/同步编程基本方法;"),
|
||
createNormalParagraph("2. 通过应用编程,掌握多线程并行程序设计与性能分析方法;"),
|
||
createNormalParagraph("3. 编写进程管理和线程管理函数测时程序,巩固测试函数应用编程,通过用时比较,建立进程和线程管理性能概念;"),
|
||
createNormalParagraph("4. 编写动态线程管理程序,建立负载均衡管理的初步概念。"),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Section 2: 实验内容
|
||
createSectionHeading("二、实验内容:"),
|
||
createNormalParagraph("任务1(必做):编写程序task61.c,主线程创建3个对等线程T1、T2、T3,每个线程利用循环执行5次printf输出操作,两次循环间随机等待1-5s时间。主线程等待所有对等线程结束后终止进程。"),
|
||
createNormalParagraph("任务2(必做):编译、测试和运行badcount.c程序,用信号量方法改写程序实现对共享变量的安全访问。"),
|
||
createNormalParagraph("任务3(必做):编写多线程生产者/消费者程序task63.c,使用信号量实现同步。"),
|
||
createNormalParagraph("任务4(必做):编译、测试和运行psum64.c,实现并行计算平方和。"),
|
||
createNormalParagraph("任务5(选做):编写矩阵乘法并行程序matmult.c并验证正确性。"),
|
||
createNormalParagraph("任务6(任选):测量fork()与pthread_create()函数调用开销。"),
|
||
createNormalParagraph("任务7(任选):编写动态线程池管理程序task67.c。"),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Section 3: 相关情况介绍
|
||
createSectionHeading("三、相关情况介绍"),
|
||
createNormalParagraph("本实验使用Linux系统提供的POSIX线程库pthread进行多线程编程。实验中涉及线程创建、线程同步(信号量)、互斥锁、条件变量等概念。通过多个实际任务,涵盖了多线程编程的主要方面。"),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Section 4: 报告内容
|
||
createSectionHeading("四、报告内容"),
|
||
|
||
// Task 1
|
||
createSubHeading("任务1:多线程创建与基本同步"),
|
||
createSubHeading("1. 设计思想"),
|
||
createNormalParagraph("本程序创建三个对等线程T1、T2、T3,每个线程执行不同的输出任务。主线程使用pthread_join()等待所有对等线程结束。"),
|
||
createSubHeading("2. 源代码"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "#include <pthread.h>", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "#include <time.h>", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "// ... (完整源代码见task61.c)", font: "宋体", size: 22 })]
|
||
}),
|
||
createSubHeading("3. 编译过程"),
|
||
createNormalParagraph("gcc -o task61 task61.c -lpthread"),
|
||
createSubHeading("4. 测试数据"),
|
||
createNormalParagraph("程序无输入参数,直接运行。"),
|
||
createSubHeading("5. 运行结果"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "My name is Lvjinzhong", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "My student number is 2024414290124", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Current time Fri May 29 11:32:12 2026", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Task 2
|
||
createSubHeading("任务2:信号量实现互斥访问"),
|
||
createSubHeading("1. 设计思想"),
|
||
createNormalParagraph("原badcount.c程序存在竞态条件,多个线程同时对共享变量cnt进行增减操作,导致结果不正确。本程序使用二元信号量mutex实现对cnt变量的互斥访问保护。"),
|
||
createSubHeading("2. 源代码(核心部分)"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "sem_t mutex;", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "sem_init(&mutex, 0, 1);", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "// 在increase/decrease函数中:", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "sem_wait(&mutex); cnt++; sem_post(&mutex);", font: "Courier New", size: 20 })]
|
||
}),
|
||
createSubHeading("3. 编译过程"),
|
||
createNormalParagraph("gcc -o task62 task62.c -lpthread"),
|
||
createSubHeading("4. 测试数据"),
|
||
createNormalParagraph("运行时指定迭代次数,如:./task62 100000"),
|
||
createSubHeading("5. 运行结果"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Correct! cnt=0", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Task 3
|
||
createSubHeading("任务3:生产者-消费者问题"),
|
||
createSubHeading("1. 设计思想"),
|
||
createNormalParagraph("使用有界缓冲区实现生产者-消费者同步。slots信号量表示缓冲区空槽数,items信号量表示缓冲区产品数,mutex保护缓冲区的临界区操作。"),
|
||
createSubHeading("2. 源代码(核心数据结构)"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "typedef struct {", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " int *buf; int n; int inpos, outpos;", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " sem_t mutex, slots, items;", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "} sbuf_t;", font: "Courier New", size: 20 })]
|
||
}),
|
||
createSubHeading("3. 编译过程"),
|
||
createNormalParagraph("gcc -o task63 task63.c -lpthread"),
|
||
createSubHeading("4. 测试数据"),
|
||
createNormalParagraph("./task63 2 3 5 表示2个生产者、3个消费者、缓冲区大小为5"),
|
||
createSubHeading("5. 运行结果"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Produced sum: 2765", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Consumed sum: 2765", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Verification successful", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Task 4
|
||
createSubHeading("任务4:并行计算平方和"),
|
||
createSubHeading("1. 设计思想"),
|
||
createNormalParagraph("将0到n-1的平方和分配给多个线程并行计算。每个线程计算自己分配区间的平方和,最后将各线程的结果求和。"),
|
||
createSubHeading("2. 源代码(核心部分)"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "void *sum(void *vargp) {", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " unsigned long long begin = myid * nelems_per_thread;", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " for (i = begin; i < end; i++) lsum += i * i;", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "}", font: "Courier New", size: 20 })]
|
||
}),
|
||
createSubHeading("3. 编译过程"),
|
||
createNormalParagraph("gcc -o task64 task64.c -lpthread"),
|
||
createSubHeading("4. 测试数据与结果"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " Threads=1: Time=0.001945s, result=549755289600", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " Threads=2: Time=0.001522s", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " Threads=4: Time=0.001612s", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " Threads=8: Time=0.001587s", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " Threads=16: Time=0.002894s", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Task 5
|
||
createSubHeading("任务5:矩阵乘法并行化"),
|
||
createSubHeading("1. 设计思想"),
|
||
createNormalParagraph("将N×N矩阵按行分配给多个线程并行计算。每个线程计算其负责的行与其他列的乘积,最后汇总结果。"),
|
||
createSubHeading("2. 源代码(核心部分)"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "for (i = start_row; i < end_row; i++)", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " for (j = 0; j < N; j++)", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " C[i][j] = sum(A[i][k] * B[k][j]);", font: "Courier New", size: 20 })]
|
||
}),
|
||
createSubHeading("3. 编译过程"),
|
||
createNormalParagraph("gcc -o matmult matmult.c -lpthread"),
|
||
createSubHeading("4. 测试数据与结果(N=128, 4线程)"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Serial time: 0.010762s", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Parallel time: 0.004444s", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Speedup: 2.42, Efficiency: 0.605", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Task 6
|
||
createSubHeading("任务6:进程与线程创建开销比较"),
|
||
createSubHeading("1. 设计思想"),
|
||
createNormalParagraph("通过反复调用fork()和pthread_create()各1000次,计算平均每次调用的执行时间。fork需要复制进程资源,而pthread_create只创建轻量级线程。"),
|
||
createSubHeading("2. 源代码(核心部分)"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "// fork测量", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "pid_t pid = fork(); if(pid==0) _exit(0); waitpid(pid,NULL,0);", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "// pthread_create测量", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "pthread_create(&tid, NULL, func, NULL); pthread_join(tid, NULL);", font: "Courier New", size: 20 })]
|
||
}),
|
||
createSubHeading("3. 编译过程"),
|
||
createNormalParagraph("gcc -o task66 task66.c -lpthread"),
|
||
createSubHeading("4. 运行结果"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "pthread_create() + pthread_join(): 0.178 ms", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "fork() + waitpid(): 0.549 ms", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "fork比pthread_create慢约3倍", font: "宋体", size: 22 })]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Task 7
|
||
createSubHeading("任务7:动态线程池管理"),
|
||
createSubHeading("1. 设计思想"),
|
||
createNormalParagraph("主线程预先创建工作线程,通过有界缓冲区接收任务。当缓冲区满时,工作线程数翻倍;当缓冲区空时,工作线程数减半。"),
|
||
createSubHeading("2. 源代码(核心部分)"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "void *worker(void *arg) {", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: " while(1) { task = sbuf_remove(&buf); sleep(task); }", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "}", font: "Courier New", size: 20 })]
|
||
}),
|
||
createSubHeading("3. 编译过程"),
|
||
createNormalParagraph("gcc -o task67 task67.c -lpthread"),
|
||
createSubHeading("4. 测试数据"),
|
||
createNormalParagraph("运行后输入命令:10 5 表示创建10个任务,每个任务执行5秒"),
|
||
createSubHeading("5. 运行结果"),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Dynamic Thread Pool", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "Buffer size: 20, Initial threads: 5", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "> 10 5", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({
|
||
spacing: { line: 360, lineRule: "auto" },
|
||
indent: { firstLineChars: 100, firstLine: 200 },
|
||
children: [new TextRun({ text: "[Worker 0] executing task: sleep 5 seconds", font: "Courier New", size: 20 })]
|
||
}),
|
||
new Paragraph({ children: [] }),
|
||
|
||
// Section 5: 实验分析与总结
|
||
createSectionHeading("五、实验分析与总结"),
|
||
createSubHeading("任务2分析:互斥锁的正确性"),
|
||
createNormalParagraph("原badcount.c程序存在严重的竞态条件。当多个线程同时执行cnt++和cnt--时,操作不是原子的,可能导致计数错误。使用信号量实现互斥访问后,确保了同一时刻只有一个线程能修改共享变量,程序输出cnt=0,说明同步机制正确有效。"),
|
||
createSubHeading("任务3分析:生产者-消费者同步"),
|
||
createNormalParagraph("本程序使用三个信号量实现完整同步:slots计数空槽数,items计数产品数,mutex保护缓冲区。验证方案通过比较生产者产生的随机数之和与消费者接收的随机数之和来验证正确性。实验结果显示两者相等,证明同步机制工作正常。"),
|
||
createSubHeading("任务4分析:并行计算加速比"),
|
||
createNormalParagraph("从测试结果看,线程数从1增加到2时,执行时间从0.001945s降至0.001522s,有明显加速。但线程数继续增加时,加速效果不明显,甚至在16线程时时间增加。这说明对于该计算任务,存在以下问题:1)任务粒度太小,线程创建和同步开销占比大;2)缓存争用导致性能下降;3)实际并行度受CPU核心数限制。"),
|
||
createSubHeading("任务5分析:矩阵乘法并行效率"),
|
||
createNormalParagraph("N=128的矩阵乘法,使用4线程获得了2.42的加速比,效率为0.605。这说明并行化有一定效果,但由于矩阵不大,线程通信和同步开销仍占一定比例。随着矩阵规模增大和线程数增加,效率会进一步提升。"),
|
||
createSubHeading("任务6分析:进程与线程创建开销"),
|
||
createNormalParagraph("fork()创建进程需要复制整个进程空间,包括内存描述符、文件描述符等资源,而pthread_create()只需创建一个轻量级线程,共享进程资源。实验结果显示fork()的开销约为pthread_create()的3倍,符合理论预期。"),
|
||
createSubHeading("任务7分析:动态线程池"),
|
||
createNormalParagraph("动态线程池通过根据负载动态调整工作线程数来提高资源利用率。缓冲区满时增加线程,缓冲区空时减少线程。但实际测试中发现线程数调整可能导致任务重新分配的问题,需要更精细的同步机制。"),
|
||
createSubHeading("总结"),
|
||
createNormalParagraph("本次实验系统地练习了Linux多线程编程的各个方面,包括线程创建与同步、互斥访问、生产者-消费者问题、并行计算性能分析等。通过实际编程和测试,加深了对线程同步机制和并行程序性能特征的理解。")
|
||
]
|
||
}]
|
||
});
|
||
|
||
// Generate the document
|
||
Packer.toBuffer(doc).then(buffer => {
|
||
fs.writeFileSync("/home/cho/C/C-exp-collection/exp1/Linux多线程编程实验(2)_completed.docx", buffer);
|
||
console.log("Document created successfully!");
|
||
}).catch(err => {
|
||
console.error("Error creating document:", err);
|
||
});
|