[新增] 信号量测试Demo

This commit is contained in:
gaoyang3513
2023-07-11 20:04:17 +08:00
commit 649283de15
5 changed files with 148 additions and 0 deletions

37
.editorconfig Normal file
View File

@ -0,0 +1,37 @@
# EditorConfig helps developers define and maintain consistent
# EditorConfig帮助开发人员定义和维护一致性
# coding styles between different editors and IDEs
# 不同编辑器和ide之间的编码样式
# 打开需要格式化的文件并手动格式化代码Mac OS shift+option+f  Windows shift+alt+f
# editorconfig.org
# editorconfig顶级配置文件,停止向上寻找配置文件
root = true
[*]
# change these settings to your own preference
# 将这些设置更改为您自己的首选项
# 缩进样式=空格
indent_style = tab
# 缩进大小=2
indent_size = 8
# we recommend you to keep these unchanged
# 我们建议你保持这些不变
# 换行符类型 = lf
end_of_line = lf
# 字符集=utf-8
charset = utf-8
# 删除行尾空格 = 是
trim_trailing_whitespace = true
# 插入最后一行=真
insert_final_newline = true
[*.md]
# 删除行尾空格 = 否
trim_trailing_whitespace = false
[package.json]
# 缩进样式=空格
indent_style = space
# 缩进大小=2
indent_size = 2

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
output/

14
CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.18)
set(TARGET_EXE demo_sem)
project(${TARGET_EXE})
add_executable(${TARGET_EXE})
target_sources(${TARGET_EXE}
PRIVATE
test_sem.c
)
target_link_libraries(${TARGET_EXE} pthread)

9
ReadMe.md Normal file
View File

@ -0,0 +1,9 @@
## Install
```shell
# Top
cmake -S . -B output -G "Unix Makefiles"
```

86
test_sem.c Normal file
View File

@ -0,0 +1,86 @@
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<semaphore.h>
#include<sys/sem.h>
#include <sys/stat.h>
#include<fcntl.h>
int main(){
int pid1,pid2;
sem_t *resource1;
sem_t *resource2;
char inpipe[200],outpipe1[100],outpipe2[100];
int fd[2]; //0为读出段,1为写入端
pipe(fd); //建立一个无名管道
pid1 = fork();
if(pid1<0){
printf("error in the first fork!");
}
else if(pid1==0){ //子进程1
printf("[%s|%u]Current: %u\n", __FUNCTION__, __LINE__, getpid());
printf("Pid of PID[1]: %u\n", getpid());
resource1=sem_open("name_sem1",O_CREAT,0666,0); // 有名信号量打开name_sem1
close(fd[0]); //关掉读出端
lockf(fd[1],1,0); // 管道持锁, 锁定从当前偏移量到文件结尾的区域
sprintf(outpipe1,"Child process 1 is sending a message!\n");
write(fd[1],outpipe1, strlen(outpipe1));
lockf(fd[1],0,0); // 管道解锁
sem_post(resource1); // 信号量开锁name_sem1
sem_close(resource1); // 有名信号量关闭name_sem1
exit(0); // 线程退出
}
pid2 = fork();
if(pid2<0){
printf("error in the second fork!\n");
}else if(pid2==0){ // 子进程2
printf("[%s|%u]Current: %u\n", __FUNCTION__, __LINE__, getpid());
resource1=sem_open("name_sem1",O_CREAT,0666,0);
resource2=sem_open("name_sem2",O_CREAT,0666,0);
sem_wait(resource1);
printf("Pid of PID[2]: %u\n", getpid());
close(fd[0]);
lockf(fd[1],1,0);
sprintf(outpipe2,"Child process 2 is sending a message!\n");
write(fd[1],outpipe2, strlen(outpipe2));
lockf(fd[1],0,0);
sem_post(resource2);
sem_close(resource1);
sem_close(resource2);
exit(0);
}
if(pid1 > 0 && pid2 >0){
printf("[%s|%u]Current: %u\n", __FUNCTION__, __LINE__, getpid());
resource2=sem_open("name_sem2",O_CREAT,0666,0);
sem_wait(resource2); // 信号量持锁source2
printf("Current: %u\n", getpid());
waitpid(pid1,NULL,0); // 等待子线程1状态变化(退出)
waitpid(pid2,NULL,0); // 等待子线程2状态变化(退出)
close(fd[1]); //关掉写端
read(fd[0],inpipe,200);
printf("%s\n",inpipe);
sem_close(resource2);
exit(0); // 进程退出,正常
}
sem_unlink("name_sem1"); // 信号量销毁,
sem_unlink("name_sem2");
return 0;
}