操作系统实验 C++实现进程与线程
实验目的
理解进程的异步执行,加深对进程概念的理解
理解程序、进程、线程的区别
了解进程控制函数:fork、wait/waitpid函数
了解线程控制函数:·pthread_create、pthread_join。
创建进程
1、编写一个c语言程序: 、
初始化一个count变量为1;
使用fork函数创建两个子进程,每个子进程对count加1之后,显示“I am son, count=?”或“I am daughter, count=?”,?使用count值代替。 父进程对count加1之后,显示“I am father, count=?”,?使用count值替代。
最后,父进程使用waitpid等待两个子进程结束后退出
2、编译后,运行该程序,观察
屏幕上显示结果的顺序性,直至出现不一样的情况为止;
观察每行打印结果中count的值。
3、要点提示
(1)fork()函数:创建子进程,该函数调用一次,却返回两次,可能有三种返回值:
在父进程中,fork返回新创建的子进程的ID
在子进程中,fork返回0
如果出现错误,fork返回一个负值
(2)wait/waitpid函数:等待子进程返回
pid_t wait(int *status):阻塞等待一个子进程退出,返回该子进程的ID,status保存该进程退出的状态
pid_t waitpid(pid_t pid, int* status, int options):阻塞(或不阻塞,通过options设置)等待一个子进程退出,返回结束子进程ID。如果pid>0,则是需要等待的pid进程,如pid=0,则是等待任何一个子进程返回
4、实验代码
子进程:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int count = 1;
HANDLE hEventDaughter = OpenEvent(EVENT_ALL_ACCESS, FALSE, "EventDaughter");
count++;
printf("i am daughter,count=%d\n", count);
SetEvent(hEventDaughter);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int count = 1;
HANDLE hEventSon = OpenEvent(EVENT_ALL_ACCESS, FALSE, "EventSon");
count++;
printf("i am son,count=%d\n", count);
SetEvent(hEventSon);
return 0;
}
父进程:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int count = 1;
// 用于存储子进程信息
STARTUPINFO si;
PROCESS_INFORMATION pi1, pi2;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi1, sizeof(pi1));
ZeroMemory(&pi2, sizeof(pi2));
// 创建第一个子进程(模拟儿子进程)
if (!CreateProcess(NULL, "sonProcess.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi1)) {
DWORD errorCode = GetLastError();
fprintf(stderr, "Create Process Failed for sonProcess: Error %lu\n", errorCode);
return -1;
}
// 创建第二个子进程(模拟女儿进程)
if (!CreateProcess(NULL, "daughterProcess.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi2)) {
DWORD errorCode = GetLastError();
fprintf(stderr, "Create Process Failed for daughterProcess: Error %lu\n", errorCode);
return -1;
}
// 等待子进程结束
WaitForSingleObject(pi1.hProcess, INFINITE);
WaitForSingleObject(pi2.hProcess, INFINITE);
// 父进程操作
count++;
printf("i am father,count=%d\n", count);
// 关闭句柄
CloseHandle(pi1.hProcess);
CloseHandle(pi1.hThread);
CloseHandle(pi2.hProcess);
CloseHandle(pi2.hThread);
return 0;
}
5、实验结果

创建线程
1、编写一个c语言程序:
初始化一个count变量为1;
使用pthread_create函数创建两个线程,每个线程对count加1之后,显示“I am son, count=?”或“I am daughter, count=?”,?使用count值代替;
父进程对count加之后,显示“I am father, count=?”,?使用count值替代。最后,父进程使用pthread_join等待两个线程结束后退出。
2、编译后,运行该程序,观察
屏幕上显示结果的顺序性,直至出现不一样的情况为止;
观察每行打印结果中count的值。
3、要点提示
(1)int pthread_create(pthread_t *tid, pthread_attr_t *attr, void* (*start_routine)(void *), void* arg)
返回值:成功返回0,出错返回-1
tid:返回线程标识符
attr:线程属性设置
start_routine:线程函数指针
arg:传递给start_routine的参数
(2)线程函数: void * thread_fun(void* arg)
(3)int pthread_join(pthread_t tid, void** tret):阻塞等待tid结束,tret是线程返回的值。
4、实验代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// 线程函数1
void *son_thread(void *arg) {
int *count = (int *)arg;
(*count)++;
printf("I am son, count = %d\n", *count);
pthread_exit(NULL);
}
// 线程函数2
void *daughter_thread(void *arg) {
int *count = (int *)arg;
(*count)++;
printf("I am daughter, count = %d\n", *count);
pthread_exit(NULL);
}
int main() {
int count = 1;
pthread_t son_tid, daughter_tid;
// 创建第一个线程
if (pthread_create(&son_tid, NULL, son_thread, &count)) {
perror("pthread_create son error");
return -1;
}
// 创建第二个线程
if (pthread_create(&daughter_tid, NULL, daughter_thread, &count)) {
perror("pthread_create daughter error");
return -1;
}
// 父进程操作
count++;
printf("I am father, count = %d\n", count);
// 等待线程结束
pthread_join(son_tid, NULL);
pthread_join(daughter_tid, NULL);
return 0;
}
5、实验结果

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)