diff --git a/signal/gjb_S0100401GN_1.c b/signal/gjb_S0100401GN_1.c new file mode 100644 index 0000000000000000000000000000000000000000..4dee8533eb33e0bb85d763fd26f53630693d4781 --- /dev/null +++ b/signal/gjb_S0100401GN_1.c @@ -0,0 +1,201 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100401GN_1.c +** +** 文件创建日期: 2021 年 1 月 11 日 +** +** 描 述: 二值信号量创建测试 +*********************************************************************************************************/ + +/******************************************************************* +**报错:执行此程序,报EPERM:operation not permitted(权限不够,操作不被允许) +**解决方法:1:赋予vscode管理员权限;2.给予报错的文件夹管理员权限;3.运行命令前加sudo +*******************************************************************/ +// 编译:gcc -g gjb_S0100401GN_1.c -o S0100401GN_1 -lpthread +// 运行:sudo ./S0100401GN_1 + +// #include 错误码 +// 前1-34的错误信息定义在/usr/include/asm-generic/errno-base.h中; +// 后35-133的错误信息定义在/usr/include/asm-generic/errno.h中。 + +#define _GUN_SOURCE + +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +void *task1_fsemaphore401(void *arg); +void *task2_fsemaphore401(void *arg); +void binding(pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, const cpu_set_t *__cpuset); + +sem_t sem_sem_4011;//信号量 +int status_sem_4011; +int ret1_sem_4011; +int ret2_sem_4011; +int sval_sem_4011; + +pthread_attr_t thread_attr1_sem_4011; +pthread_attr_t thread_attr2_sem_4011; + +int main(int argc, char **argv) +{ + pthread_t task1_t; // unsigned long int + pthread_t task2_t; + + struct sched_param sched1; // 线程的调度参数,优先级数值0-99 + struct sched_param sched2; + sched1.sched_priority = 40; // 40 + sched2.sched_priority = 30; // 30 + // struct sched_param sched1={40}; + // struct sched_param sched2={30}; + // 对线程属性变量的初始化。 + pthread_attr_init(&thread_attr1_sem_4011); + pthread_attr_init(&thread_attr2_sem_4011); + + int res1 = 0; + int res2 = 0; + int res3 = 0; + + res1 = pthread_attr_setschedpolicy(&thread_attr1_sem_4011, SCHED_RR); // 线程的调度策略:实时、轮转法;返回为0,成功 + // printf("res1=%d\n",res1); + res2 = pthread_attr_setinheritsched(&thread_attr1_sem_4011, PTHREAD_EXPLICIT_SCHED); // 线程调度策略的继承性:显示指定自己的调度策略和调度参数 + // printf("res2=%d\n",res2); + res3 = pthread_attr_setschedparam(&thread_attr1_sem_4011, &sched1); // 线程的调度参数 + // printf("res3=%d\n",res3); + + pthread_attr_setschedpolicy(&thread_attr2_sem_4011, SCHED_RR); + pthread_attr_setinheritsched(&thread_attr2_sem_4011, PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedparam(&thread_attr2_sem_4011, &sched2); + /*thread_attr1_sem_4011.schedpolicy = SCHED_RR; + thread_attr1_sem_4011.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1_sem_4011.schedparam.sched_priority = 40; + thread_attr2_sem_4011.schedpolicy = SCHED_RR; + thread_attr2_sem_4011.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2_sem_4011.schedparam.sched_priority = 30;*/ + + status_sem_4011 = sem_init(&sem_sem_4011, 0, 1); // 初始化一个匿名信号量值为 1 + if (status_sem_4011 != 0) + { + GJB_PRT_ERROR_INFO("sem_init() is failed!\n"); + goto __errno_handle; + } + else + { + // 成功 + GJB_PRT_INFO("creat sem_bin success!\n"); + } + + // ret1_sem_4011 = pthread_create (&task1_t,&thread_attr1_sem_4011,(void*)task1_fsemaphore401,NULL);// + ret1_sem_4011 = pthread_create(&task1_t, &thread_attr1_sem_4011, (void *)task1_fsemaphore401, NULL); + //printf("ret1_sem_4011=%d\n",ret1_sem_4011);//0 + + if (ret1_sem_4011 == 0) + { + GJB_PRT_INFO("task1_f_create success!\n"); + } + else + { + // 发生错误 + GJB_PRT_ERROR_INFO("task1_f_create fail!\n"); + goto __errno_handle; + } + + binding(task1_t, 0); + //sleep(2); + + ret2_sem_4011 = pthread_create(&task2_t, &thread_attr2_sem_4011, (void *)task2_fsemaphore401, NULL); + //printf("ret2_sem_4011=%d\n",ret2_sem_4011); + if (ret2_sem_4011 == 0) + { + GJB_PRT_INFO("task2_f_create success!\n"); + } + else + { + GJB_PRT_ERROR_INFO("task2_f_create fail!\n"); + goto __errno_handle; + } + //sleep(2); + + //printf("binding before\n"); + binding(task2_t, 0); + + pthread_join(task1_t, NULL); + pthread_join(task2_t, NULL); + printf("pass\n"); + return (GJB_PRI_PASS); + + +__errno_handle: + return (GJB_PRI_FAIL); + //printf("pass\n"); +} + +void *task1_fsemaphore401(void *arg) +{ + GJB_PRT_INFO("task1_f gain binary sem success!\n"); + sem_wait(&sem_sem_4011);//等待信号量,将信号量减一;信号量为0时,将后来线程放入阻塞队列 + sem_post(&sem_sem_4011);//信号量加一,释放信号量守护的资源 + sem_getvalue(&sem_sem_4011, &sval_sem_4011); // 获取信号量值 + GJB_PRT_INFO("sval = %d\n", sval_sem_4011);//1 + return (NULL); + + /*int a=99,b=99,c=99; + GJB_PRT_INFO("task1_f gain binary sem success!\n"); + a=sem_wait(&sem_sem_4011);//等待信号量,将信号量减一;信号量为0时,将后来线程放入阻塞队列 + printf("sem_wait=%d\n",a); + b=sem_post(&sem_sem_4011);//信号量加一,释放信号量守护的资源 + printf("sem_post=%d\n",b); + c=sem_getvalue(&sem_sem_4011, &sval_sem_4011); // 获取信号量值 + printf("sem_getvalue=%d\n",c); + GJB_PRT_INFO("sval = %d\n", sval_sem_4011);//1 + return (NULL);*/ +} + +void *task2_fsemaphore401(void *arg) +{ + GJB_PRT_INFO("task2_f gain binary sem success!\n"); + sem_wait(&sem_sem_4011); + sem_getvalue(&sem_sem_4011, &sval_sem_4011); // 获取信号量值 + GJB_PRT_INFO("sval = %d\n", sval_sem_4011); + return (NULL); + + /*int a=99,b=99; + GJB_PRT_INFO("task2_f gain binary sem success!\n"); + a=sem_wait(&sem_sem_4011); + printf("sem_wait=%d\n",a); + b=sem_getvalue(&sem_sem_4011, &sval_sem_4011); // 获取信号量值 + printf("sem_getvalue=%d\n",b); + GJB_PRT_INFO("sval = %d\n", sval_sem_4011); + return (NULL);*/ +} + +/* + * 将指定的线程 BIND 到指定CPU + */ +void binding(pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + //printf("binding pass\n"); + pthread_setaffinity_np(tid, sizeof(set), &set);//成功返回0 +} + +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, const cpu_set_t *__cpuset) +{ + return 0; +} diff --git a/signal/gjb_S0100401GN_2.c b/signal/gjb_S0100401GN_2.c new file mode 100644 index 0000000000000000000000000000000000000000..6da85061eb9192156b45995dd06eb9c9dbce063f --- /dev/null +++ b/signal/gjb_S0100401GN_2.c @@ -0,0 +1,84 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100401GN_2.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 11 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷值锟脚猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100401GN_2.c -o S0100401GN_2 -lpthread +//锟斤拷锟斤拷 ./S0100401GN_2 + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +int main(int argc, char **argv) +{ + sem_t *mysemp; + char semname[50]; + int ret; + int sval; + + /* 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟脚猴拷锟斤拷 */ + mysemp = sem_open(semname, O_CREAT, 0777, 1, SEM_BINARY, PTHREAD_WAITQ_PRIO); + if (( mysemp == SEM_FAILED ) || (mysemp == NULL )) { + GJB_PRT_ERROR_INFO( "open sem_binary error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "open sem_binary success!\n"); + } + + /* 锟斤拷取锟脚猴拷锟斤拷锟斤拷值 */ + if (sem_getvalue(mysemp, &sval) == -1) { + GJB_PRT_ERROR_INFO( "sem_getvalue error!\n"); + sem_close(mysemp); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "sem_getvalue success!\n"); + GJB_PRT_INFO("sval = %d\n", sval); + } + + /* 锟截憋拷锟斤拷锟斤拷锟脚猴拷锟斤拷 */ + ret = sem_close(mysemp); + if(ret != 0 ) { + GJB_PRT_ERROR_INFO( "close sem_bin error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "close sem_bin success!\n"); + } + + /* 删锟斤拷锟脚猴拷锟斤拷 */ + ret = sem_unlink(semname); + if(ret != 0 ) { + GJB_PRT_ERROR_INFO( "unlink sem_bin error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "unlink sem_bin success!\n"); + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100402GN_1.c b/signal/gjb_S0100402GN_1.c new file mode 100644 index 0000000000000000000000000000000000000000..12354857101b8c73ace0c11fb8c83004683d21f0 --- /dev/null +++ b/signal/gjb_S0100402GN_1.c @@ -0,0 +1,152 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100402GN_1.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 11 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷值锟脚猴拷锟斤拷删锟斤拷锟斤拷锟斤拷 +*********************************************************************************************************/ +//缂栬瘧锛歡cc -g gjb_S0100402GN_1.c -o S0100402GN_1 -lpthread +//杩愶拷?? sudo ./S0100402GN_1 + +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +void *task1_fsemaphore402 (void *arg); +void *task2_fsemaphore402 (void *arg); +void binding (pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset); + +sem_t sem_sem_4021; +int status_sem_4021; +int ret_sem_4021; +int ret1_sem_4021; +int ret2_sem_4021; +int sval_sem_4021; + +pthread_attr_t thread_attr1_sem_4021; +pthread_attr_t thread_attr2_sem_4021; + +int main(int argc, char **argv) +{ + pthread_t task1_t; + pthread_t task2_t; + + struct sched_param sched1={40}; + struct sched_param sched2={30}; + + pthread_attr_init(&thread_attr1_sem_4021); + pthread_attr_init(&thread_attr2_sem_4021); + + pthread_attr_setschedpolicy(&thread_attr1_sem_4021,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆锟?锟? + pthread_attr_setinheritsched(&thread_attr1_sem_4021,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾锟?宸辩殑璋冨害绛栫暐鍜岃皟搴﹀弬锟? + pthread_attr_setschedparam(&thread_attr1_sem_4021,&sched1);//绾跨▼鐨勮皟搴﹀弬锟? + + pthread_attr_setschedpolicy(&thread_attr2_sem_4021,SCHED_RR); + pthread_attr_setinheritsched(&thread_attr2_sem_4021,PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedparam(&thread_attr2_sem_4021,&sched2); + + status_sem_4021=sem_init (&sem_sem_4021,0,1); + if(status_sem_4021 != 0) { + GJB_PRT_ERROR_INFO("sem_init() is failed!\n"); + goto __errno_handle; + } else { + GJB_PRT_INFO( "creat sem_bin success!\n"); + } + + ret1_sem_4021=pthread_create (&task1_t,&thread_attr1_sem_4021,task1_fsemaphore402,NULL); + if(ret1_sem_4021 == 0) { + GJB_PRT_INFO( "task1_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task1_f_create fail!\n"); + goto __errno_handle; + } + + binding(task1_t, 0); + + ret2_sem_4021=pthread_create (&task2_t,&thread_attr2_sem_4021,task2_fsemaphore402,NULL); + if(ret2_sem_4021 == 0) { + GJB_PRT_INFO( "task2_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task2_f_create fail!\n"); + goto __errno_handle; + } + binding(task2_t, 0); + + if(pthread_join(task1_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join1()\n"); + goto __errno_handle; + } + + /* 锟斤拷锟斤拷pthread_join锟饺达拷锟斤拷锟斤拷锟斤拷止 */ + if(pthread_join(task2_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join2()\n"); + goto __errno_handle; + } + + ret_sem_4021=sem_destroy (&sem_sem_4021); + if(ret_sem_4021 != 0) { + GJB_PRT_ERROR_INFO("delete sem_bin failed!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "delete sem_bin success!\n"); + } + + //printf("pass\n"); + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} + +void *task1_fsemaphore402 (void *arg) +{ + GJB_PRT_INFO("task1_f gain binary sem success!\n"); + sem_wait(&sem_sem_4021); + sem_post(&sem_sem_4021); + sem_getvalue(&sem_sem_4021,&sval_sem_4021); + GJB_PRT_INFO("sval = %d\n", sval_sem_4021); + sleep(1); + + return (NULL); +} + +void *task2_fsemaphore402 (void *arg) +{ + GJB_PRT_INFO("task2_f gain binary sem success!\n"); + sem_wait(&sem_sem_4021); + sem_getvalue(&sem_sem_4021,&sval_sem_4021); + GJB_PRT_INFO("sval = %d\n", sval_sem_4021); + sleep(1); + + return (NULL); +} + +void binding (pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + pthread_setaffinity_np(tid, sizeof(set), &set); +} + +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, + const cpu_set_t *__cpuset) +{ + return 0; +} \ No newline at end of file diff --git a/signal/gjb_S0100402GN_2.c b/signal/gjb_S0100402GN_2.c new file mode 100644 index 0000000000000000000000000000000000000000..0755e662c622c63c2a9627bde80b250fdddc08c4 --- /dev/null +++ b/signal/gjb_S0100402GN_2.c @@ -0,0 +1,87 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100402GN_2.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 11 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷值锟脚猴拷锟斤拷删锟斤拷锟斤拷锟斤拷 +*********************************************************************************************************/ +//锟斤拷锟诫:gcc -g gjb_S0100402GN_2.c -o S0100402GN_2 -lpthread +//锟斤拷锟叫o拷./S0100402GN_2 + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +int main(int argc, char **argv) +{ + sem_t *mysemp; + char semname[50]; + int ret; + int sval; + + /* 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟脚猴拷锟斤拷 */ + mysemp = sem_open(semname, O_CREAT, 0777, 1,SEM_BINARY,PTHREAD_WAITQ_PRIO); + if (( mysemp == SEM_FAILED ) || (mysemp == NULL )) { + GJB_PRT_ERROR_INFO( "open sem_binary error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "open sem_binary success!\n"); + + } + /* 锟斤拷取锟脚猴拷锟斤拷锟斤拷值 */ + if( sem_getvalue(mysemp, &sval) == -1 ) { + GJB_PRT_ERROR_INFO( "sem_getvalue error!\n"); + sem_close(mysemp); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "sem_getvalue success!\n"); + GJB_PRT_INFO("sval = %d\n", sval); + } + + + /* 锟截憋拷锟斤拷锟斤拷锟脚猴拷锟斤拷 */ + ret = sem_close(mysemp); + if(ret != 0 ) { + GJB_PRT_ERROR_INFO( "close sem_bin error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "close sem_bin success!\n"); + + } + + /* 删锟斤拷锟脚猴拷锟斤拷 */ + ret = sem_unlink(semname); + if(ret != 0 ) { + GJB_PRT_ERROR_INFO( "unlink sem_bin error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "unlink sem_bin success!\n"); + } + + return (GJB_PRI_PASS); + + +__errno_handle: + return (GJB_PRI_FAIL); + +} diff --git a/signal/gjb_S0100403GN_1.c b/signal/gjb_S0100403GN_1.c new file mode 100644 index 0000000000000000000000000000000000000000..8f8e0d32e2bb7c5e0294a95e227ca4e2d824bc46 --- /dev/null +++ b/signal/gjb_S0100403GN_1.c @@ -0,0 +1,204 @@ +/********************************************************************************************************* +** +** GJB ???????? +** +** Copyright All Rights Reserved +** +**--------------??????-------------------------------------------------------------------------------- +** +** ?? ?? ??: gjb_S0100403GN_1.c +** +** ???????????: 2021 ?? 1 ?? 12 ?? +** +** ?? ??: ????????????????? +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100403GN_1.c -o S0100403GN_1 -lpthread +//锟斤拷锟斤拷 sudo ./S0100403GN_1 + +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +void *task1_fsemaphore403 (void *arg); +void *task2_fsemaphore403 (void *arg); +void *task3_fsemaphore403 (void *arg); +void binding (pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset); +//int pthread_delay(int ticks); + +sem_t sem_sem_4031; +int status_sem_4031; +int ret1_sem_4031; +int ret2_sem_4031; +int ret3_sem_4031; +int sval_sem_4031; + +pthread_attr_t thread_attr1_sem_4031; +pthread_attr_t thread_attr2_sem_4031; +pthread_attr_t thread_attr3_sem_4031; + +int main(int argc, char **argv) +{ + pthread_t task1_t; + pthread_t task2_t; + pthread_t task3_t; + + struct sched_param sched1={50}; + struct sched_param sched2={40}; + struct sched_param sched3={30}; + + pthread_attr_init(&thread_attr1_sem_4031); + pthread_attr_init(&thread_attr2_sem_4031); + pthread_attr_init(&thread_attr3_sem_4031); + + pthread_attr_setschedpolicy(&thread_attr1_sem_4031,SCHED_RR);//锟竭程的碉拷锟饺诧拷锟皆o拷实时锟斤拷锟斤拷转锟斤拷 + pthread_attr_setinheritsched(&thread_attr1_sem_4031,PTHREAD_EXPLICIT_SCHED);//锟竭程碉拷锟饺诧拷锟皆的继筹拷锟皆o拷锟斤拷示指锟斤拷锟皆硷拷锟侥碉拷锟饺诧拷锟皆和碉拷锟饺诧拷锟斤拷 + pthread_attr_setschedparam(&thread_attr1_sem_4031,&sched1);//锟竭程的碉拷锟饺诧拷锟斤拷 + pthread_attr_setschedpolicy(&thread_attr2_sem_4031,SCHED_RR);//锟竭程的碉拷锟饺诧拷锟皆o拷实时锟斤拷锟斤拷转锟斤拷 + pthread_attr_setinheritsched(&thread_attr2_sem_4031,PTHREAD_EXPLICIT_SCHED);//锟竭程碉拷锟饺诧拷锟皆的继筹拷锟皆o拷锟斤拷示指锟斤拷锟皆硷拷锟侥碉拷锟饺诧拷锟皆和碉拷锟饺诧拷锟斤拷 + pthread_attr_setschedparam(&thread_attr2_sem_4031,&sched2);//锟竭程的碉拷锟饺诧拷锟斤拷 + pthread_attr_setschedpolicy(&thread_attr3_sem_4031,SCHED_RR);//锟竭程的碉拷锟饺诧拷锟皆o拷实时锟斤拷锟斤拷转锟斤拷 + pthread_attr_setinheritsched(&thread_attr3_sem_4031,PTHREAD_EXPLICIT_SCHED);//锟竭程碉拷锟饺诧拷锟皆的继筹拷锟皆o拷锟斤拷示指锟斤拷锟皆硷拷锟侥碉拷锟饺诧拷锟皆和碉拷锟饺诧拷锟斤拷 + pthread_attr_setschedparam(&thread_attr3_sem_4031,&sched3);//锟竭程的碉拷锟饺诧拷锟斤拷 + + /*thread_attr1_sem_4031.schedpolicy = SCHED_RR; + thread_attr1_sem_4031.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1_sem_4031.schedparam.sched_priority = 50; + thread_attr2_sem_4031.schedpolicy = SCHED_RR; + thread_attr2_sem_4031.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2_sem_4031.schedparam.sched_priority = 40; + thread_attr3_sem_4031.schedpolicy = SCHED_RR; + thread_attr3_sem_4031.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr3_sem_4031.schedparam.sched_priority = 30;*/ + + status_sem_4031 = sem_init (&sem_sem_4031, 0, 2); + if (status_sem_4031 != 0) { + GJB_PRT_INFO("creat sem_cnt isfailed!\n"); + goto __errno_handle; + } else { + GJB_PRT_ERROR_INFO( "creat sem_cnt success!\n"); + } + + ret1_sem_4031 = pthread_create (&task1_t, &thread_attr1_sem_4031, task1_fsemaphore403, NULL); + if (ret1_sem_4031 == 0) { + GJB_PRT_INFO( "task1_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task1_f_create fail!\n"); + goto __errno_handle; + } + + binding(task1_t, 0); + + ret2_sem_4031 = pthread_create (&task2_t, &thread_attr2_sem_4031, task2_fsemaphore403, NULL); + if (ret2_sem_4031 == 0) { + GJB_PRT_INFO( "task2_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task2_f_create fail!\n"); + goto __errno_handle; + } + + binding(task2_t, 0); + + ret3_sem_4031 = pthread_create (&task3_t,&thread_attr3_sem_4031,(void*)task3_fsemaphore403,NULL); + if (ret3_sem_4031 == 0) { + GJB_PRT_INFO( "task3_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task3_f_create fail!\n"); + goto __errno_handle; + } + + binding(task3_t, 0); + + if (pthread_join(task1_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join1()\n"); + goto __errno_handle; + } + + /* ????pthread_join?????????? */ + if (pthread_join(task2_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join2()\n"); + goto __errno_handle; + } + + if (pthread_join(task3_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join3()\n"); + goto __errno_handle; + } + printf("pass\n"); + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} + +void *task1_fsemaphore403 (void *arg) +{ + GJB_PRT_INFO("task1_f gain sem success!\n"); + sem_wait(&sem_sem_4031); + //pthread_delay(100); + sem_post(&sem_sem_4031); + sem_getvalue(&sem_sem_4031,&sval_sem_4031); + GJB_PRT_INFO("sval = %d\n", sval_sem_4031); + + return (NULL); +} + +void *task2_fsemaphore403 (void *arg) +{ + GJB_PRT_INFO("task2_f gain sem success!\n"); + sem_wait(&sem_sem_4031); + sem_getvalue(&sem_sem_4031,&sval_sem_4031); + GJB_PRT_INFO("sval = %d\n", sval_sem_4031); + + return (NULL); +} + +void *task3_fsemaphore403 (void *arg) +{ + GJB_PRT_INFO("task3_f gain sem success!\n"); + sem_wait(&sem_sem_4031); + sem_getvalue(&sem_sem_4031,&sval_sem_4031); + GJB_PRT_INFO("sval = %d\n", sval_sem_4031); + + return (NULL); +} + +void binding (pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + pthread_setaffinity_np(tid, sizeof(set), &set); +} + +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, + const cpu_set_t *__cpuset) +{ + return 0; +} + +/*pthread_delay.c + * Created on: Jan 25, 2022 + * Author: jian*/ +/*int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } +}*/ \ No newline at end of file diff --git a/signal/gjb_S0100403GN_2.c b/signal/gjb_S0100403GN_2.c new file mode 100644 index 0000000000000000000000000000000000000000..2c6e916d2425a0ed08a597b3626209986ce8b67f --- /dev/null +++ b/signal/gjb_S0100403GN_2.c @@ -0,0 +1,72 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100403GN_2.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟脚猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 +*********************************************************************************************************/ + +//缂栬瘧锛歡cc -g gjb_S0100403GN_2.c -o S0100403GN_2 -lpthread +//杩愯 ./S0100403GN_2 + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +int main(int argc, char **argv) +{ + sem_t *mysemp; + char semname[50]; + int ret; + + /* sem_open锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷募锟斤拷锟斤拷藕锟斤拷锟 */ + mysemp = sem_open(semname, O_CREAT, 0777, 1,SEM_COUNTING,PTHREAD_WAITQ_PRIO); + if (( mysemp == SEM_FAILED ) || (mysemp == NULL )) { + GJB_PRT_ERROR_INFO( "open sem_cnt fail!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "open sem_cnt success!\n"); + } + + /* 锟截憋拷锟斤拷锟斤拷锟脚猴拷锟斤拷 */ + ret = sem_close(mysemp); + if(ret != 0 ) { + GJB_PRT_ERROR_INFO( "close sem_cnt fail!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "close sem_cnt success!\n"); + + } + /* 删锟斤拷锟脚猴拷锟斤拷 */ + ret = sem_unlink(semname); + if(ret != 0 ) { + GJB_PRT_ERROR_INFO( "unlink sem_cnt fail!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "unlink sem_cnt success!\n"); + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100404GN_1.c b/signal/gjb_S0100404GN_1.c new file mode 100644 index 0000000000000000000000000000000000000000..846ed4b6c8bc31d784ceeb5decc501a704b79b9e --- /dev/null +++ b/signal/gjb_S0100404GN_1.c @@ -0,0 +1,197 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100404GN_1.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟脚猴拷锟斤拷删锟斤拷锟斤拷锟斤拷 +*********************************************************************************************************/ + +//缂栬瘧锛歡cc -g gjb_S0100404GN_1.c -o S0100404GN_1 -lpthread +//杩愯 sudo ./S0100404GN_1 + +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +void *task1_fsemaphore404 (void *arg); +void *task2_fsemaphore404 (void *arg); +void *task3_fsemaphore404 (void *arg); +void binding (pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset); + +sem_t sem_sem_4041; +int status_sem_4041; +int ret_sem_4041; +int ret1_sem_4041; +int ret2_sem_4041; +int ret3_sem_4041; +int sval_sem_4041; + +pthread_attr_t thread_attr1_sem_4041; +pthread_attr_t thread_attr2_sem_4041; +pthread_attr_t thread_attr3_sem_4041; + +int main(int argc, char **argv) +{ + pthread_t task1_t; + pthread_t task2_t; + pthread_t task3_t; + + struct sched_param sched1={50}; + struct sched_param sched2={40}; + struct sched_param sched3={30}; + + + pthread_attr_init(&thread_attr1_sem_4041); + pthread_attr_init(&thread_attr2_sem_4041); + pthread_attr_init(&thread_attr3_sem_4041); + + pthread_attr_setschedpolicy(&thread_attr1_sem_4041,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr1_sem_4041,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr1_sem_4041,&sched1);//绾跨▼鐨勮皟搴﹀弬鏁 + pthread_attr_setschedpolicy(&thread_attr2_sem_4041,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr2_sem_4041,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr2_sem_4041,&sched2);//绾跨▼鐨勮皟搴﹀弬鏁 + pthread_attr_setschedpolicy(&thread_attr3_sem_4041,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr3_sem_4041,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr3_sem_4041,&sched3);//绾跨▼鐨勮皟搴﹀弬鏁 + + /*thread_attr1_sem_4041.schedpolicy = SCHED_RR; + thread_attr1_sem_4041.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1_sem_4041.schedparam.sched_priority = 50; + thread_attr2_sem_4041.schedpolicy = SCHED_RR; + thread_attr2_sem_4041.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2_sem_4041.schedparam.sched_priority = 40; + thread_attr3_sem_4041.schedpolicy = SCHED_RR; + thread_attr3_sem_4041.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr3_sem_4041.schedparam.sched_priority = 30;*/ + + status_sem_4041 = sem_init (&sem_sem_4041,0,2); + if(status_sem_4041 != 0) { + GJB_PRT_ERROR_INFO("sem_init() is failed errno = %d!\n", errno); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "creat sem_cnt success!\n"); + } + + ret1_sem_4041 = pthread_create (&task1_t,&thread_attr1_sem_4041,task1_fsemaphore404,NULL); + if (ret1_sem_4041 == 0) { + GJB_PRT_INFO( "task1_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task1_f_create fail errno = %d!\n", errno); + goto __errno_handle; + } + + binding(task1_t, 0); + + ret2_sem_4041 = pthread_create (&task2_t,&thread_attr2_sem_4041,task2_fsemaphore404,NULL); + if (ret2_sem_4041 == 0) { + GJB_PRT_INFO( "task2_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task2_f_create fail errno = %d!\n", errno); + goto __errno_handle; + } + + binding(task2_t, 0); + + ret3_sem_4041 = pthread_create (&task3_t,&thread_attr3_sem_4041,task3_fsemaphore404,NULL); + if (ret3_sem_4041 == 0) { + GJB_PRT_INFO( "task3_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task3_f_create fail errno = %d!\n", errno); + goto __errno_handle; + } + + binding(task3_t, 0); + + if (pthread_join(task1_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join1()\n"); + } + + if (pthread_join(task2_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join2()\n"); + goto __errno_handle; + } + + if (pthread_join(task3_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join3()\n"); + goto __errno_handle; + } + + ret_sem_4041 = sem_destroy (&sem_sem_4041); + if (ret_sem_4041 != 0) { + GJB_PRT_ERROR_INFO("delete sem_cnt failed!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "delete sem_cnt success!\n"); + } + //printf("pass\n"); + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} + +void *task1_fsemaphore404 (void *arg) +{ + GJB_PRT_INFO("task1_f gain sem success!\n"); + sem_wait(&sem_sem_4041); + + //pthread_delay( 100 ); + sem_post(&sem_sem_4041); + sem_getvalue(&sem_sem_4041,&sval_sem_4041); + GJB_PRT_INFO("sval = %d\n", sval_sem_4041); + + return (NULL); +} + +void *task2_fsemaphore404 (void *arg) +{ + GJB_PRT_INFO("task2_f gain sem success!\n"); + sem_wait(&sem_sem_4041); + sem_getvalue(&sem_sem_4041,&sval_sem_4041); + GJB_PRT_INFO("sval = %d\n", sval_sem_4041); + + return (NULL); +} + +void *task3_fsemaphore404 (void *arg) +{ + GJB_PRT_INFO("task3_f gain sem success!\n"); + sem_wait(&sem_sem_4041); + sem_getvalue(&sem_sem_4041,&sval_sem_4041); + GJB_PRT_INFO("sval = %d\n", sval_sem_4041); + + return (NULL); +} + +void binding (pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + pthread_setaffinity_np(tid, sizeof(set), &set); +} + +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, + const cpu_set_t *__cpuset) +{ + return 0; +} \ No newline at end of file diff --git a/signal/gjb_S0100404GN_2.c b/signal/gjb_S0100404GN_2.c new file mode 100644 index 0000000000000000000000000000000000000000..09af8c6bf229ef305ef4e125091df14d87c8065d --- /dev/null +++ b/signal/gjb_S0100404GN_2.c @@ -0,0 +1,76 @@ +/********************************************************************************************************* +** +** GJB ???????? +** +** Copyright All Rights Reserved +** +**--------------??????-------------------------------------------------------------------------------- +** +** ?? ?? ??: gjb_S0100404GN_2.c +** +** ???????????: 2021 ?? 1 ?? 12 ?? +** +** ?? ??: ???????????????? +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100404GN_2.c -o S0100404GN_2 -lpthread +//锟斤拷锟斤拷 ./S0100404GN_2 + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +int main(int argc, char **argv) +{ + sem_t *mysemp; + char semname[50]; + int ret; + + /* sem_open????????????????????? */ + mysemp = sem_open(semname, O_CREAT, 0777, 1, SEM_COUNTING, PTHREAD_WAITQ_PRIO); + if (( mysemp == SEM_FAILED ) || (mysemp == NULL )) { + GJB_PRT_ERROR_INFO( "open sem_cnt fail!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "open sem_cnt success!\n"); + + } + + /* ???????????? */ + ret = sem_close(mysemp); + if(ret != 0 ) { + GJB_PRT_ERROR_INFO( "close sem_cnt fail!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "close sem_cnt success!\n"); + + } + /* ???????? */ + ret = sem_unlink(semname); + if(ret != 0 ) { + GJB_PRT_ERROR_INFO( "unlink sem_cnt fail!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "unlink sem_cnt success!\n"); + + } + + return (GJB_PRI_PASS); + + +__errno_handle: + return (GJB_PRI_FAIL); + +} diff --git a/signal/gjb_S0100405GN.c b/signal/gjb_S0100405GN.c new file mode 100644 index 0000000000000000000000000000000000000000..f6da5754e86ff209b79b1efa2a597bb2aa9d62f0 --- /dev/null +++ b/signal/gjb_S0100405GN.c @@ -0,0 +1,227 @@ +/********************************************************************************************************* +** +** GJB ???????? +** +** Copyright All Rights Reserved +** +**--------------??????-------------------------------------------------------------------------------- +** +** ?? ?? ??: gjb_S0100405GN.c +** +** ???????????: 2021 ?? 1 ?? 12 ?? +** +** ?? ??: ?????????????????????? +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100405GN.c -o S0100405GN -lpthread +//锟斤拷锟斤拷 sudo ./S0100405GN + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +int err_count_406 = 0; +sem_t sem_406; +static int flag_406 = 0; +pthread_attr_t thread_attr1_406; +pthread_attr_t thread_attr2_406; + +void *task_Asemaphore405(void *arg); +void *task_Bsemaphore405(void *arg); +void binding (pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset); + +int main(int argc, char **argv) +{ + int status; + int ret; + + pthread_t task1_t, task2_t; + + struct sched_param sched1={30}; + struct sched_param sched2={40}; + + pthread_attr_init(&thread_attr1_406); + pthread_attr_init(&thread_attr2_406); + + pthread_attr_setschedpolicy(&thread_attr1_406,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr1_406,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr1_406,&sched1);//绾跨▼鐨勮皟搴﹀弬鏁 + pthread_attr_setschedpolicy(&thread_attr2_406,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr2_406,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr2_406,&sched2);//绾跨▼鐨勮皟搴﹀弬鏁 + + /*thread_attr1_406.schedpolicy = SCHED_RR; + thread_attr1_406.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1_406.schedparam.sched_priority = 30; + thread_attr2_406.schedpolicy = SCHED_RR; + thread_attr2_406.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2_406.schedparam.sched_priority = 40;*/ + + status=sem_init (&sem_406,0,0); + if (status != 0) { + GJB_PRT_ERROR_INFO("sem_init() isfailed! errno = %d\n", errno); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "creat sem_bin success!\n"); + } + + /* ????????A */ + if (pthread_create (&task1_t,&thread_attr1_406, task_Asemaphore405, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create task_A error = %d!\n", errno); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "pthread_createA success!\n"); + } + + binding(task1_t, 0); + + /* ????????B */ + if (pthread_create(&task2_t,&thread_attr2_406, task_Bsemaphore405, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create2 task_B error = %d!\n", errno); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "pthread_createB success!\n"); + } + + binding(task2_t, 0); + + sleep(2); + + /*?????????sem_post*/ + ret = sem_post(&sem_406); + if (ret != 0) { + GJB_PRT_ERROR_INFO( "sem_post fail errno = %d!\n", errno); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "sem_post success!\n"); + } + + /* ????pthread_join?????????? */ + if (pthread_join(task1_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join1() errno = %d\n", errno); + goto __errno_handle; + } + + /* ????pthread_join?????????? */ + if (pthread_join(task2_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join2() errno = %d\n", errno); + goto __errno_handle; + } + + /* ???????? */ + ret = sem_close(&sem_406); + ret = sem_destroy(&sem_406); + if (ret != 0) { + GJB_PRT_ERROR_INFO( "sem_destroy fail!\n"); + GJB_PRT_ERROR_INFO("sem_destroy"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "sem_destroy success!\n"); + } + + if (flag_406 != 2) { + GJB_PRT_ERROR_INFO("task did not get semaphore as PTHREAD_WAITQ_PRIO!\n "); + goto __errno_handle; + } + + if (err_count_406) { + goto __errno_handle; + } + + //printf("pass\n"); + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); + +} +void *task_Asemaphore405(void *arg) +{ + int ret = -2; + int ret1 = -2; + /* ????????? */ + ret = sem_wait(&sem_406); + if( ret == -1 ) { + GJB_PRT_ERROR_INFO("sem_waitA error!\n"); + err_count_406++; + } else { + GJB_PRT_INFO( "sem_waitA success!\n"); + } + + if(flag_406 == 1) { + flag_406 = 2; + } else { + GJB_PRT_ERROR_INFO("task_A get semaphore before task_B - error!\n"); + err_count_406++; + } + + ret1=sem_post(&sem_406); + if( ret1 == -1 ) { + GJB_PRT_ERROR_INFO("sem_postA error!\n"); + err_count_406++; + } else { + GJB_PRT_INFO( "sem_postA success!\n"); + } + + pthread_exit(0); + + return (NULL); +} + +void *task_Bsemaphore405(void *arg) +{ + int ret2 = -2; + int ret3 = -2; + /* ????????? */ + ret2 = sem_wait(&sem_406); + if( ret2 == -1 ) { + GJB_PRT_ERROR_INFO("sem_waitB error!\n"); + err_count_406++; + } else { + GJB_PRT_INFO( "sem_waitB success!\n"); + } + + flag_406 = 1; + + ret3 = sem_post(&sem_406); + if( ret3 == -1 ) { + GJB_PRT_ERROR_INFO("sem_postB error!\n"); + err_count_406++; + } else { + GJB_PRT_INFO( "sem_postB success!\n"); + } + + pthread_exit(0); + + return (NULL); +} + +void binding (pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + pthread_setaffinity_np(tid, sizeof(set), &set); +} + +//鍑芥暟鏈疄鐜 +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, + const cpu_set_t *__cpuset) +{ + return 0; +} \ No newline at end of file diff --git a/signal/gjb_S0100406GN.c b/signal/gjb_S0100406GN.c new file mode 100644 index 0000000000000000000000000000000000000000..3c8e57e2011662646b84d680d2e562ae0c10b153 --- /dev/null +++ b/signal/gjb_S0100406GN.c @@ -0,0 +1,231 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100406GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟脚猴拷锟斤拷锟斤拷锟饺斤拷锟饺筹拷锟斤拷锟饺诧拷锟皆诧拷锟斤拷 +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100406GN.c -o S0100406GN -lpthread +//锟斤拷锟斤拷 sudo ./S0100406GN + +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +int err_count_405 = 0; +sem_t sem_405; +static int flag_405 = 0; +pthread_attr_t thread_attr1_405; +pthread_attr_t thread_attr2_405; + +void *task_Asemaphore406(void *arg); +void *task_Bsemaphore406(void *arg); +void binding (pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset); + +int main(int argc, char **argv) +{ + int status; + int ret; + + struct sched_param sched1={30}; + struct sched_param sched2={30}; + + pthread_attr_init(&thread_attr1_405); + pthread_attr_init(&thread_attr2_405); + + + pthread_attr_setschedpolicy(&thread_attr1_405,SCHED_FIFO);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佸厛鍏ュ厛鍑 + pthread_attr_setinheritsched(&thread_attr1_405,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr1_405,&sched1);//绾跨▼鐨勮皟搴﹀弬鏁 + pthread_attr_setschedpolicy(&thread_attr2_405,SCHED_FIFO);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佸厛鍏ュ厛鍑 + pthread_attr_setinheritsched(&thread_attr2_405,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr2_405,&sched2);//绾跨▼鐨勮皟搴﹀弬鏁 + + /*thread_attr1_405.schedpolicy = SCHED_FIFO; + thread_attr1_405.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1_405.schedparam.sched_priority = 30; + thread_attr2_405.schedpolicy = SCHED_FIFO; + thread_attr2_405.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2_405.schedparam.sched_priority = 30;*/ + + pthread_t task1_t, task2_t; + + status = sem_init (&sem_405,0,0); + if (status != 0) { + GJB_PRT_ERROR_INFO("sem_init() isfailed!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "creat sem_bin success!\n"); + } + + /* 锟斤拷锟斤拷锟斤拷锟斤拷B */ + if (pthread_create(&task2_t,&thread_attr2_405, task_Bsemaphore406, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create task_B error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "pthread_createB success!\n"); + } + + binding(task2_t, 0); + sleep(1); + + /* 锟斤拷锟斤拷锟斤拷锟斤拷A */ + if (pthread_create(&task1_t,&thread_attr1_405, task_Asemaphore406, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create task_A error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "pthread_createA success!\n"); + } + + binding(task1_t, 0); + sleep(1); + + /*锟斤拷锟斤拷锟斤拷锟斤拷锟絪em_post*/ + ret = sem_post(&sem_405); + if(ret != 0) { + GJB_PRT_ERROR_INFO( "sem_post error!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "sem_post success!\n"); + } + + /* 锟斤拷锟斤拷pthread_join锟饺达拷锟斤拷锟斤拷锟斤拷止 */ + if (pthread_join(task1_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join1()\n"); + goto __errno_handle; + } + + /* 锟斤拷锟斤拷pthread_join锟饺达拷锟斤拷锟斤拷锟斤拷止 */ + if(pthread_join(task2_t, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join2()\n"); + goto __errno_handle; + } + + sem_wait(&sem_405); + /* 锟截憋拷锟脚猴拷锟斤拷 */ + ret = sem_close(&sem_405); + ret = sem_destroy(&sem_405); + if( ret != 0) { + GJB_PRT_ERROR_INFO( "sem_destroy error%d!\n",errno); + goto __errno_handle; + + } else { + GJB_PRT_INFO( "sem_destroy success!\n"); + } + + if(flag_405 != 2) { + GJB_PRT_ERROR_INFO("task did not get semaphore as PTHREAD_WAITQ_FIFO!\n "); + goto __errno_handle; + } + + if (err_count_405) { + goto __errno_handle; + } + + //printf("pass\n"); + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} +void *task_Asemaphore406(void *arg) +{ + int ret = -2; + int ret1 = -2; + /* 锟斤拷锟斤拷锟脚猴拷锟斤拷 */ + ret = sem_wait(&sem_405); + if( ret == -1 ) { + GJB_PRT_ERROR_INFO("sem_waitA error!\n"); + err_count_405++; + + } else { + GJB_PRT_INFO( "sem_waitA success!\n"); + } + + if(flag_405 == 1) { + flag_405 = 2; + + } else { + GJB_PRT_ERROR_INFO("task_A get semaphore before task_B - error!\n"); + } + + ret1=sem_post(&sem_405); + if( ret1 == -1 ) { + GJB_PRT_ERROR_INFO("sem_postA error!\n"); + err_count_405++; + + } else { + GJB_PRT_INFO( "sem_postA success!\n"); + } + + pthread_exit(0); + + return (NULL); +} + +void *task_Bsemaphore406(void *arg) +{ + int ret2 = -2; + int ret3 = -2; + /* 锟斤拷锟斤拷锟脚猴拷锟斤拷 */ + ret2 = sem_wait(&sem_405); + if( ret2 == -1 ) { + GJB_PRT_ERROR_INFO("sem_waitB error!\n"); + err_count_405++; + + } else { + GJB_PRT_INFO( "sem_waitB success!\n"); + } + + flag_405 = 1; + + ret3 = sem_post(&sem_405); + if( ret3 == -1 ) { + GJB_PRT_ERROR_INFO("sem_postB error!\n"); + err_count_405++; + + } else { + GJB_PRT_INFO( "sem_postB success!\n"); + } + + pthread_exit(0); + + return (NULL); +} + +void binding (pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + pthread_setaffinity_np(tid, sizeof(set), &set); +} + +//鍑芥暟鏈疄鐜 +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, + const cpu_set_t *__cpuset) +{ + return 0; +} + diff --git a/signal/gjb_S0100407GN.c b/signal/gjb_S0100407GN.c new file mode 100644 index 0000000000000000000000000000000000000000..968daa65f0935b7ec50c3bae192c0aaf2ccb2277 --- /dev/null +++ b/signal/gjb_S0100407GN.c @@ -0,0 +1,171 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100407GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟斤拷式锟脚猴拷锟斤拷锟斤拷取锟斤拷锟斤拷 sem_trywait +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100407GN.c -o S0100407GN -lpthread +//锟斤拷锟斤拷 sudo ./S0100407GN + +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +void *task1_fsemaphore407 (void *arg); +void *task2_fsemaphore407 (void *arg); +void binding (pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset); + +int err_count_407 = 0; +sem_t sem_407; +int running_407=1; +int status_407; +int ret_407; +int ret1_407; +int ret2_407; +int ret3_407; +int ret4_407; +int sval_407; + +pthread_attr_t thread_attr1_407; +pthread_attr_t thread_attr2_407; + +int main(int argc, char **argv) +{ + pthread_t task1_t; + pthread_t task2_t; + + struct sched_param sched1={40}; + struct sched_param sched2={30}; + + pthread_attr_init(&thread_attr1_407); + pthread_attr_init(&thread_attr2_407); + + + + pthread_attr_setschedpolicy(&thread_attr1_407,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr1_407,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr1_407,&sched1);//绾跨▼鐨勮皟搴﹀弬鏁 + pthread_attr_setschedpolicy(&thread_attr2_407,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr2_407,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr2_407,&sched2);//绾跨▼鐨勮皟搴﹀弬鏁 + + /*thread_attr1_407.schedpolicy = SCHED_RR; + thread_attr1_407.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1_407.schedparam.sched_priority = 40; + thread_attr2_407.schedpolicy = SCHED_RR; + thread_attr2_407.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2_407.schedparam.sched_priority = 30;*/ + + status_407 = sem_init (&sem_407,0,1); + if (status_407 != 0) { + GJB_PRT_ERROR_INFO("sem_init() failed!\n"); + goto __errno_handle; + } else { + GJB_PRT_INFO( "creat sem_bin success!\n"); + } + + ret_407 = sem_getvalue(&sem_407,&sval_407); + if (ret_407 != 0) { + GJB_PRT_ERROR_INFO("sem_getvalue failed!\n"); + goto __errno_handle; + } else { + GJB_PRT_INFO( "sem_getvalue success!\n"); + } + + ret1_407 = pthread_create (&task1_t,&thread_attr1_407,task1_fsemaphore407,NULL); + if (ret1_407 != 0) { + //鍙戠敓閿欒 + GJB_PRT_ERROR_INFO("task1_f failed!\n"); + goto __errno_handle; + } else { + GJB_PRT_INFO( "task1_f success!\n"); + } + sleep(1); + binding(task1_t, 0); + + ret2_407 = pthread_create (&task2_t,&thread_attr2_407,task2_fsemaphore407,NULL); + if (ret2_407 != 0) { + GJB_PRT_ERROR_INFO("task2_f failed!\n"); + goto __errno_handle; + } else { + GJB_PRT_INFO( "task2_f success!\n"); + } + binding(task2_t, 0); + + pthread_join(task1_t, NULL); + pthread_join(task2_t, NULL); + + if (err_count_407) { + goto __errno_handle; + } + + //printf("pass\n"); + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} +void *task1_fsemaphore407 (void *arg) +{ + ret3_407 = sem_trywait(&sem_407); + if(ret3_407 != 0) { + GJB_PRT_ERROR_INFO("task1_f sem_trywait failed!\n"); + err_count_407++; + } else { + GJB_PRT_INFO( "task1_f sem_trywait success!\n"); + } + + sem_getvalue(&sem_407,&sval_407); + GJB_PRT_INFO("sval = %d\n", sval_407); + sem_post(&sem_407); + + return (NULL); +} + +void *task2_fsemaphore407 (void *arg) +{ + ret4_407 = sem_trywait(&sem_407); + if(ret4_407 != 0) { + GJB_PRT_ERROR_INFO("task2_f sem_trywait failed!\n"); + err_count_407++; + } else { + GJB_PRT_INFO( "task2_f sem_trywait success!\n"); + } + + sem_getvalue(&sem_407,&sval_407); + GJB_PRT_INFO("sval = %d\n", sval_407); + + return (NULL); +} + +void binding (pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + pthread_setaffinity_np(tid, sizeof(set), &set); +} + +//鍑芥暟鏈疄鐜 +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, + const cpu_set_t *__cpuset) +{ + return 0; +} \ No newline at end of file diff --git a/signal/gjb_S0100408GN.c b/signal/gjb_S0100408GN.c new file mode 100644 index 0000000000000000000000000000000000000000..e693d72a517221871561ebcd0e658301cb98144f --- /dev/null +++ b/signal/gjb_S0100408GN.c @@ -0,0 +1,161 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100408GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟矫等达拷锟斤拷式锟斤拷取锟脚猴拷锟斤拷锟斤拷锟斤拷 sem_wait +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100408GN.c -o S0100408GN -lpthread +//锟斤拷锟斤拷 sudo ./S0100408GN + +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +void *task1_fsemaphore408 (void *arg); +void *task2_fsemaphore408 (void *arg); +void binding (pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset); + +static int err_count_408 = 0; +sem_t sem_408; +int status_408; +int ret_408; +int ret1_408; +int ret2_408; +int ret3_408; +int ret4_408; +int sval_408; + +pthread_attr_t thread_attr1_408; +pthread_attr_t thread_attr2_408; + +int main(int argc, char **argv) +{ + pthread_t task1_t; + pthread_t task2_t; + + struct sched_param sched1={40}; + struct sched_param sched2={30}; + + pthread_attr_init(&thread_attr1_408); + pthread_attr_init(&thread_attr2_408); + + pthread_attr_setschedpolicy(&thread_attr1_408,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr1_408,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr1_408,&sched1);//绾跨▼鐨勮皟搴﹀弬鏁 + pthread_attr_setschedpolicy(&thread_attr2_408,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr2_408,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr2_408,&sched2);//绾跨▼鐨勮皟搴﹀弬鏁 + + /*thread_attr1_408.schedpolicy = SCHED_RR; + thread_attr1_408.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1_408.schedparam.sched_priority = 40; + thread_attr2_408.schedpolicy = SCHED_RR; + thread_attr2_408.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2_408.schedparam.sched_priority = 30;*/ + + status_408 = sem_init (&sem_408,0,1); + if(status_408 != 0) { + GJB_PRT_ERROR_INFO("sem_init() is failed!\n"); + goto __errno_handle; + } else { + GJB_PRT_INFO( "creat sem_bin success!\n"); + } + + ret1_408 = pthread_create (&task1_t,&thread_attr1_408,task1_fsemaphore408,NULL); + if( ret1_408 == 0 ) { + GJB_PRT_INFO( "task1_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task1_f_create fail!\n"); + goto __errno_handle; + } + binding(task1_t, 0); + sleep(1); + + ret2_408 = pthread_create (&task2_t,&thread_attr2_408,task2_fsemaphore408,NULL); + if( ret2_408 == 0) { + GJB_PRT_INFO( "task2_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task2_f_create fail!\n"); + goto __errno_handle; + } + binding(task2_t, 0); + + pthread_join(task1_t, NULL); + pthread_join(task2_t, NULL); + + if (err_count_408) { + goto __errno_handle; + } + + printf("pass\n"); + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} + +void *task1_fsemaphore408 (void *arg) +{ + ret3_408 = sem_wait(&sem_408); + if(ret3_408!=0) { + GJB_PRT_ERROR_INFO("task1_f sem_wait failed!\n"); + err_count_408++; + } else { + GJB_PRT_INFO( "task1_f sem_wait success!\n"); + } + + sem_getvalue(&sem_408,&sval_408); + GJB_PRT_INFO("sval = %d\n", sval_408); + + sem_post(&sem_408); + + return (NULL); +} + +void *task2_fsemaphore408 (void *arg) +{ + ret4_408 = sem_wait(&sem_408); + if(ret4_408 != 0) { + GJB_PRT_ERROR_INFO("task2_f sem_wait failed!\n"); + err_count_408++; + } else { + GJB_PRT_INFO( "task2_f sem_wait success!\n"); + } + + sem_getvalue(&sem_408,&sval_408); + GJB_PRT_INFO("sval = %d\n", sval_408); + + return (NULL); +} + +void binding (pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + pthread_setaffinity_np(tid, sizeof(set), &set); +} + +//鍑芥暟鏈疄鐜 +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, + const cpu_set_t *__cpuset) +{ + return 0; +} \ No newline at end of file diff --git a/signal/gjb_S0100409GN.c b/signal/gjb_S0100409GN.c new file mode 100644 index 0000000000000000000000000000000000000000..ec6c4cbe0a82a6d1366e61fbcde1032ed9f1f6ca --- /dev/null +++ b/signal/gjb_S0100409GN.c @@ -0,0 +1,128 @@ +/********************************************************************************************************* +** +** GJB ???????? +** +** Copyright All Rights Reserved +** +**--------------??????-------------------------------------------------------------------------------- +** +** ?? ?? ??: gjb_S0100409GN.c +** +** ???????????: 2021 ?? 1 ?? 12 ?? +** +** ?? ??: ??????????????????????? sem_timedwait +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100409GN.c -o S0100409GN -lpthread +//锟斤拷锟斤拷 ./S0100409GN + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +#define TIMEOUT_S 5 +#define TIMEOUT_MS 1500 + +static int count_409; +sem_t sem_409; + +void *pthread (void *arg); + +int main(int argc, char **argv) +{ + int iThread; + pthread_t tid; + + sem_init(&sem_409, 0, 0); // ??3????????0???????????????????????????????锟斤拷????(?????sem_post ????)????????? + sem_post(&sem_409); + + iThread = pthread_create(&tid, NULL, &pthread, NULL); + if (iThread != ERROR_NONE) { + GJB_PRT_ERROR_INFO("create thread failed.\n"); + goto __errno_handle; + } + + pthread_join(tid, NULL); + + sem_destroy(&sem_409); + + if (count_409 != 2) { + goto __errno_handle; + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} +/* + * ??? time ??? ms ???? + * void time_add_ms(struct timeval *time, uint ms) + */ +void time_add_ms(struct timeval *time, UINT ms) +{ + time->tv_usec += ms * 1000; /* ??? = ???? * 1000*/ + if(time->tv_usec >= 1000000) { /* ??锟斤拷??1000 000 ??? = 1 ?? */ + time->tv_sec += time->tv_usec / 1000000; + time->tv_usec %= 1000000; + } +} + +void *pthread(void *arg) +{ + struct timespec t; + +/* + * ?????? + */ + struct timeval time; + //源锟斤拷锟诫:lib_gettimeofday(&time, NULL); 头锟侥硷拷锟斤拷锟斤拷lib_gettimeofday锟斤拷锟斤拷 + gettimeofday(&time, NULL); + time_add_ms(&time, TIMEOUT_MS); + t.tv_sec = time.tv_sec; + t.tv_nsec = time.tv_usec * 1000; + + +/* ???? + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + t.tv_sec = time(NULL) + TIMEOUT_S; + t.tv_nsec = 0; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + + GJB_PRT_INFO("t.tv_sec = %lld\n", t.tv_sec); + GJB_PRT_INFO("t.tv_nsec = %ld\n", t.tv_nsec); + + while (1) { + int semvalue = -1; + sem_getvalue(&sem_409, &semvalue); + GJB_PRT_INFO("will call sem_timedwait, semvalue = %d\n", semvalue); + + /* + * ???sem ??????>0????sem_timedwait ????????????sem ????????0???? sem_timedwait ??????? TIMEOUT????????? + */ + int ret = sem_timedwait(&sem_409, &t); + GJB_PRT_INFO("over call sem_timedwait, ret = %d\n", ret); + if (ret == -1) { + sem_getvalue(&sem_409, &semvalue); + GJB_PRT_INFO("pthread() will return\n"); + count_409++; + return NULL; + + } else { + count_409++; + GJB_PRT_INFO("sem_timedwait success!\n"); + } + } + + return (NULL); +} diff --git a/signal/gjb_S0100410GN.c b/signal/gjb_S0100410GN.c new file mode 100644 index 0000000000000000000000000000000000000000..90f577620528970e603794b0182d5fc18d0a5349 --- /dev/null +++ b/signal/gjb_S0100410GN.c @@ -0,0 +1,191 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100410GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟脚猴拷锟酵放癸拷锟杰诧拷锟斤拷 sem_post +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100410GN.c -o S0100410GN -lpthread +//锟斤拷锟斤拷 sudo ./S0100410GN + +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +#define TIME 100 + +void *task1_fsemaphore410 (void *arg); +void *task2_fsemaphore410 (void *arg); +void *task3_fsemaphore410 (void *arg); +void binding (pthread_t tid, int cpu); +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset); + +int err_count_410 = 0; +sem_t sem_410; +int status_410; +int ret_410; +int ret1_410; +int ret2_410; +int ret3_410; +int sval_410; + +pthread_attr_t thread_attr1_410; +pthread_attr_t thread_attr2_410; +pthread_attr_t thread_attr3_410; + +int main(int argc, char **argv) +{ + pthread_t task1_t; + pthread_t task2_t; + pthread_t task3_t; + + struct sched_param sched1={50}; + struct sched_param sched2={40}; + struct sched_param sched3={30}; + + pthread_attr_init(&thread_attr1_410); + pthread_attr_init(&thread_attr2_410); + pthread_attr_init(&thread_attr3_410); + + pthread_attr_setschedpolicy(&thread_attr1_410,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr1_410,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr1_410,&sched1);//绾跨▼鐨勮皟搴﹀弬鏁 + pthread_attr_setschedpolicy(&thread_attr2_410,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr2_410,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr2_410,&sched2);//绾跨▼鐨勮皟搴﹀弬鏁 + pthread_attr_setschedpolicy(&thread_attr3_410,SCHED_RR);//绾跨▼鐨勮皟搴︾瓥鐣ワ細瀹炴椂銆佽疆杞硶 + pthread_attr_setinheritsched(&thread_attr3_410,PTHREAD_EXPLICIT_SCHED);//绾跨▼璋冨害绛栫暐鐨勭户鎵挎э細鏄剧ず鎸囧畾鑷繁鐨勮皟搴︾瓥鐣ュ拰璋冨害鍙傛暟 + pthread_attr_setschedparam(&thread_attr3_410,&sched3);//绾跨▼鐨勮皟搴﹀弬鏁 + + + /*thread_attr1_410.schedpolicy = SCHED_RR; + thread_attr1_410.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1_410.schedparam.sched_priority = 50; + thread_attr2_410.schedpolicy = SCHED_RR; + thread_attr2_410.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2_410.schedparam.sched_priority = 40; + thread_attr3_410.schedpolicy = SCHED_RR; + thread_attr3_410.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr3_410.schedparam.sched_priority = 30;*/ + + status_410 = sem_init (&sem_410,0,2); + if (status_410 != 0) { + GJB_PRT_ERROR_INFO("sem_init() isfailed!\n"); + goto __errno_handle; + } else { + GJB_PRT_INFO( "creat sem_cnt succes!\n"); + } + + ret1_410 = pthread_create (&task1_t,&thread_attr1_410,task1_fsemaphore410,NULL); + if (ret1_410 == 0) { + GJB_PRT_INFO( "task1_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task1_f_create fail!\n"); + printf("%d\n",ret1_410); + goto __errno_handle; + } + binding(task1_t, 0); + + ret2_410 = pthread_create (&task2_t,&thread_attr2_410,task2_fsemaphore410,NULL); + if (ret2_410 == 0) { + GJB_PRT_INFO( "task2_f_create success!\n"); + } else { + GJB_PRT_ERROR_INFO( "task2_f_create fail!\n"); + goto __errno_handle; + } + binding(task2_t, 0); + + ret3_410 = pthread_create (&task3_t,&thread_attr3_410,task3_fsemaphore410,NULL); + if (ret3_410 == 0) { + GJB_PRT_INFO( "task3_f_create success!\n"); + + } else { + GJB_PRT_ERROR_INFO( "task3_f_create fail!\n"); + goto __errno_handle; + } + + binding(task3_t, 0); + + if (err_count_410) { + goto __errno_handle; + } + + //printf("pass\n"); + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); + +} +void *task1_fsemaphore410 (void *arg) +{ + GJB_PRT_INFO("task1_f gain sem success!\n"); + sem_wait(&sem_410); + sem_getvalue(&sem_410,&sval_410); + GJB_PRT_INFO("sval = %d\n", sval_410); + //GJB_PRT_INFO("sval1 = %d\n", sval_410); + ret_410 = sem_post(&sem_410); + if(ret_410 != 0) { + GJB_PRT_ERROR_INFO("sem_post failed!\n"); + err_count_410++; + + } else { + GJB_PRT_INFO( "sem_post success!\n"); + } + + sem_getvalue(&sem_410,&sval_410); + GJB_PRT_INFO("sval = %d\n", sval_410); + //GJB_PRT_INFO("sval11 = %d\n", sval_410); + return (NULL); +} + +void *task2_fsemaphore410 (void *arg) +{ + GJB_PRT_INFO("task2_f gain sem success!\n"); + sem_wait(&sem_410); + sem_getvalue(&sem_410,&sval_410); + GJB_PRT_INFO("sval = %d\n", sval_410); + //GJB_PRT_INFO("sval2 = %d\n", sval_410); + return (NULL); +} + +void *task3_fsemaphore410 (void *arg) +{ + GJB_PRT_INFO("task3_f gain sem success!\n"); + sem_wait(&sem_410); + sem_getvalue(&sem_410,&sval_410); + GJB_PRT_INFO("sval = %d\n", sval_410); + //GJB_PRT_INFO("sval3 = %d\n", sval_410); + + return (NULL); +} + +void binding (pthread_t tid, int cpu) +{ + cpu_set_t set; + CPU_ZERO(&set); + CPU_SET(cpu, &set); + pthread_setaffinity_np(tid, sizeof(set), &set); +} + +//鍑芥暟鏈疄鐜 +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize,const cpu_set_t *__cpuset) +{ + return 0; +} + diff --git a/signal/gjb_S0100601GN.c b/signal/gjb_S0100601GN.c new file mode 100644 index 0000000000000000000000000000000000000000..72e4e8f354229c96faae1f38e5cbc64895d57516 --- /dev/null +++ b/signal/gjb_S0100601GN.c @@ -0,0 +1,83 @@ +/********************************************************************************************************* +** +** GJB 鏍囧噯娴嬭瘯闆 +** +** Copyright All Rights Reserved +** +**--------------鏂囦欢淇℃伅-------------------------------------------------------------------------------- +** +** 鏂 浠 鍚: gjb_S0100601GN.c +** +** 鏂囦欢鍒涘缓鏃ユ湡: 2021 骞 1 鏈 15 鏃 +** +** 鎻 杩: 娑堟伅闃熷垪鍒涘缓鍜屽垹闄ゅ姛鑳芥祴璇 +*********************************************************************************************************/ + +//缂栬瘧锛歡cc -g gjb_S0100601GN.c -o S0100601GN -lrt +//杩愯锛./S0100601GN + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "7714types.h" + + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 +//#define MQ_PRIO_MAX 32768 + +int main(int argc, char **argv) +{ + char qname[50]; + mqd_t queue; + + //婧愪唬鐮侊細sprintf(qname, "FUNCTION_S0100501GN_TEST_%lu", pthread_self());鍛藉悕鏂瑰紡閿欒 + sprintf(qname, "/FUNCTION_S0100501GN_TEST_%lu", pthread_self());//灏嗗璞″悕鍐欏叆qname涓紝pthread_self锛氳幏鍙栧綋鍓嶈繘绋婭D + //printf("qname=%s\n",qname); + + /*struct mq_attr mq_attr_param = { + .mq_flags = O_NONBLOCK, + .mq_maxmsg = 10, + .mq_msgsize = 100 + }; // O_NONBLOCK 璁剧疆涓洪潪闃诲, 鏈澶ф秷鎭暟涓10锛 姣忎釜娑堟伅鏈澶т负100瀛楄妭 + queue = mq_open("/qname", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &mq_attr_param);*/ + + //杩斿洖娑堟伅闃熷垪鎻忚堪绗︼紝澶辫触杩斿洖-1 + queue = mq_open(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);//瀵硅薄鍚嶏紝娑堟伅闃熷垪閫夐」锛堜互璇诲啓妯″紡鍒涘缓娑堟伅闃熷垪锛夛紝鏂囦欢璁块棶鏉冮檺锛屾秷鎭槦鍒楀睘鎬э紙榛樿锛 + //printf("queue=%d.\n",queue); + + if (queue == (mqd_t)-1) + { + //鎵撳紑鏂囦欢鍙戠敓閿欒锛宔rrno锛氭棤鏁堝弬鏁 + GJB_PRT_ERROR_INFO("mq_open()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + + } else { + GJB_PRT_INFO("mq_open()success.\n"); + } + + if (mq_close(queue) != 0) { + GJB_PRT_ERROR_INFO("mq_close()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + if (mq_unlink(qname) != 0) { + GJB_PRT_ERROR_INFO("mq_unlink() failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } else { + GJB_PRT_INFO("mq_unlink() success.TEST PASS!\n"); + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100602GN.c b/signal/gjb_S0100602GN.c new file mode 100644 index 0000000000000000000000000000000000000000..1cff07b40b9bad23a6f0ba906d544bb9fd62b832 --- /dev/null +++ b/signal/gjb_S0100602GN.c @@ -0,0 +1,146 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100602GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 15 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷息锟斤拷锟斤拷同锟斤拷通锟脚伙拷锟狡诧拷锟斤拷 +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100602GN.c -o S0100602GN -lrt -lpthread +// ./S0100602GN + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +#define BUFFER 40 //鏈澶ч檺鍒朵负8192 +//#define BUFFER1 10 //绯荤粺鏈澶ф秷鎭暟闄愬埗涓10 +#define BUFFER1 40 //鏈澶ф秷鎭暟闄愬埗涓10锛岃鍙傛暟涓嬬▼搴忔棤娉曢氳繃 +#define NAMESIZE 50 + +static int err_count_602; +static pthread_t new_th; +static mqd_t queue; +static int rc; +static char msgrv[BUFFER]; + +void *MQ_Task_02GN() +{ + /* + * 锟斤拷锟斤拷息锟斤拷锟斤拷锟叫斤拷锟斤拷锟斤拷息 + */ + rc = mq_receive(queue, msgrv, BUFFER, NULL); + if (rc == -1) { + GJB_PRT_ERROR_INFO("mq_receive()failed.errno=%d. TEST FAILED!\n",errno); + err_count_602++; + return (void *)-1; + } + + pthread_exit(0); + return NULL; +} + +int main(int argc, char **argv) +{ + const char *msgptr = "test message 1"; + struct mq_attr attr; + char qname[NAMESIZE]; + + memset(&attr, 0, sizeof(attr));//涓篴ttr鍐呯殑鍙傛暟璧嬪 + + sprintf(qname, "/S0100502GN_%lu", pthread_self()); + //printf("qname=%s\n",qname); + + /*struct mq_attr mq_attr_param = { + //.mq_flags = O_NONBLOCK, + .mq_flags = 0, + .mq_maxmsg = 10, + .mq_msgsize = 100 + }; */ + + attr.mq_maxmsg = BUFFER1;//10锛氱郴缁熼檺鍒剁殑鏈澶ф秷鎭暟 + attr.mq_msgsize = BUFFER;//40 + attr.mq_curmsgs = 0;//褰撳墠鎺掗槦鐨勬秷鎭暟锛岃灞炴у彧鑳借鑾峰彇锛屼笉鑳借璁剧疆 + /* + * 锟斤拷锟斤拷息锟斤拷锟斤拷 + */ + queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr); + //printf("queue=%d.\n",queue); + + if (queue == (mqd_t)-1) { + GJB_PRT_ERROR_INFO("mq_open() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle;//转锟斤拷锟斤拷锟斤拷末 + } + /* + * 锟斤拷锟斤拷锟斤拷息锟斤拷锟斤拷锟竭筹拷 + */ + if (pthread_create(&new_th, NULL, MQ_Task_02GN, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create() failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + sleep(1); + + /* + * 锟斤拷锟斤拷息锟斤拷锟斤拷锟叫凤拷锟斤拷锟斤拷息 + */ + if (mq_send(queue, msgptr, strlen(msgptr), 1) != 0) { + GJB_PRT_ERROR_INFO("mq_send()failed.errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + sleep(1); + + if (strncmp(msgptr, msgrv, strlen(msgptr)) != 0) { + GJB_PRT_ERROR_INFO("mq_receive didn't receive the correct message.TEST FAILED!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO("mq_receive receive the correct message.TEST PASS!\n"); + } + + if (pthread_join(new_th, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join().errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + rc = mq_close(queue); + if (rc != 0) { + GJB_PRT_ERROR_INFO("mq_close()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + rc = mq_unlink(qname); + if (rc != 0) { + GJB_PRT_ERROR_INFO("mq_unlink()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + if (err_count_602) { + goto __errno_handle; + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100603GN.c b/signal/gjb_S0100603GN.c new file mode 100644 index 0000000000000000000000000000000000000000..d19eeedb6e7d9d3e851f406bcdc7fd42f5f609dc --- /dev/null +++ b/signal/gjb_S0100603GN.c @@ -0,0 +1,192 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100603GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 15 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷息锟斤拷锟斤拷锟届步通锟脚伙拷锟狡诧拷锟斤拷 +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100603GN.c -o S0100603GN -lrt -lpthread +//锟斤拷锟叫o拷./S0100603GN + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +//#include "gjb.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +#define BUFFER 40 //鏈澶ч檺鍒朵负8192 +//#define BUFFER1 10 //鏈澶ф秷鎭暟闄愬埗涓10 +#define BUFFER1 40 //鏈澶ф秷鎭暟闄愬埗涓10锛岃鍙傛暟涓嬬▼搴忔棤娉曢氳繃 +#define NAMESIZE 50 + +static int err_count_603 = 0; +static int rc; +static int flag_03GN = 0; +static pthread_t new_th1; +static pthread_t new_th2; +static mqd_t queue; +static const char *msgptr_03GN = "test message 15"; +static char msgrv[BUFFER]; + +void *MQ_Task_B () +{ + int i; + + /* + * 锟斤拷锟斤拷息锟斤拷锟斤拷锟叫斤拷锟斤拷锟斤拷息 + */ + for (i = 0; i < BUFFER1; i++){ //鏈澶ф秷鎭暟 + //for (i = 0; i < BUFFER; i++){ //鏈澶ф秷鎭暟 + rc = mq_receive(queue, msgrv, BUFFER, NULL); + if (rc == -1) { + GJB_PRT_ERROR_INFO("mq_receive() failed. errno=%d. TEST FAILED!\n", errno); + err_count_603++; + return (void *)-1; + } + } + + if (flag_03GN == 1) { + flag_03GN = 2; + } + + pthread_exit(0); + + return NULL; +} + +void *MQ_Task_A() +{ + int i; + + for (i = 0; i < BUFFER1; i++){ + //for (i = 0; i < BUFFER; i++){ + if (mq_send(queue, msgptr_03GN, strlen(msgptr_03GN), 1) != 0) { + GJB_PRT_ERROR_INFO("mq_send() failed. errno=%d. TEST FAILED!\n",errno); + err_count_603++; + return (void *)-1; + } + } + + if (pthread_create(&new_th2, NULL, MQ_Task_B, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create() MQ_Task_B failed. errno=%d. TEST FAILED!\n",errno); + err_count_603++; + return (void *)-1; + } + + //sleep(2); + flag_03GN = 1; + + pthread_exit(0); + return NULL; +} + +int main(int argc, char **argv) +{ + char qname[NAMESIZE]; + struct mq_attr attr; + struct mq_attr mq_info; + + sprintf(qname, "/S0100503GN_%lu", pthread_self()); + + attr.mq_msgsize = BUFFER; + attr.mq_maxmsg = BUFFER1; //10 + attr.mq_curmsgs = 0; + attr.mq_flags = 0; + + queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr); + //printf("queue=%d.\n",queue); + + if (queue == (mqd_t)-1) { + //锟斤拷锟斤拷锟斤拷锟斤拷 + GJB_PRT_ERROR_INFO("mq_open() failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + //printf("queue\n"); + + if (pthread_create(&new_th1, NULL, MQ_Task_A, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create() MQ_Task_A failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + //printf("new_th1=%lx.\n", new_th1); + //printf("pthread_creat\n"); + + sleep(2); + + if (pthread_join(new_th1, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join().errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + //printf("pthread_join1\n"); + + if (pthread_join(new_th2, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join().errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + //printf("pthread_join2\n"); + + //Get the current number of messages attr + if (mq_getattr(queue, &mq_info) != 0) { + GJB_PRT_ERROR_INFO("Error in mq_getattr(). errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + //printf("mq_getattr\n"); + + if ((rc = mq_info.mq_curmsgs) == 0) { + GJB_PRT_INFO("The current number of messages in the message queue is 0. Test PASS!\n"); + } + else { + GJB_PRT_ERROR_INFO("The current number of messages in the message " + "queue is not 0. the number is %d. Test failed!\n", rc); + goto __errno_handle; + } + + rc = mq_close(queue); + if (rc != 0) { + GJB_PRT_ERROR_INFO("mq_close()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + rc = mq_unlink(qname); + if (rc != 0) { + GJB_PRT_ERROR_INFO("mq_unlink()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + if (flag_03GN == 2) { + GJB_PRT_INFO("flag_03GN = %d. Test PASS!\n", flag_03GN); + + } else { + GJB_PRT_ERROR_INFO("flag_03GN = %d.Test failed!\n", flag_03GN); + goto __errno_handle; + } + + if (err_count_603) { + goto __errno_handle; + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100604GN.c b/signal/gjb_S0100604GN.c new file mode 100644 index 0000000000000000000000000000000000000000..68a505fa1218856ef3ec5d3448e39f164552f6b7 --- /dev/null +++ b/signal/gjb_S0100604GN.c @@ -0,0 +1,179 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100604GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 15 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟斤拷息锟斤拷锟叫o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷/锟斤拷锟斤拷之锟斤拷通锟斤拷锟斤拷息锟斤拷锟斤拷通锟脚o拷锟斤拷锟斤拷锟斤拷息锟斤拷锟酵和斤拷锟斤拷锟劫度o拷 +** 锟皆硷拷锟斤拷息锟斤拷锟酵凤拷式锟斤拷锟斤拷证锟斤拷锟斤拷锟斤拷式锟角凤拷锟斤拷效锟斤拷锟斤拷锟斤拷锟较拷锟斤拷锟斤拷锟斤拷锟斤拷锟 +** 锟斤拷么锟斤拷息锟斤拷锟斤拷锟斤拷锟斤拷/锟斤拷锟教斤拷锟斤拷锟斤拷锟斤拷锟斤拷直锟斤拷锟斤拷息锟斤拷锟叫空硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞斤拷锟斤拷锟斤拷锟较拷锟斤拷泄锟斤拷懿锟斤拷锟 +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100604GN.c -o S0100604GN -lrt -lpthread +//锟斤拷锟叫o拷./S0100604GN + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +#define BUFFER 40 +#define BUFFER1 40 +//#define BUFFER1 10 +#define NAMESIZE 50 + +static int err_count_604; +static int rc; +static int flag_04GN = 0; +static pthread_t new_th1; +static pthread_t new_th2; +static mqd_t queue; +static char msgrv[BUFFER]; +static const char *msgptr_04GN = "test message 11"; + +void *MQ_Recv_Task() +{ + if (mq_send(queue, msgptr_04GN, strlen(msgptr_04GN), 1) != 0) { + GJB_PRT_ERROR_INFO("mq_send() failed. errno=%d. TEST FAILED!\n", errno); + err_count_604++; + return (void *)-1; + } + + if (flag_04GN == 1) { + flag_04GN = 2; + } + + pthread_exit(0); + return NULL; +} + +void *MQ_Send_Task() +{ + int i; + + for (i = 0;i < BUFFER1; i++) { + //for (i = 0;i < BUFFER; i++) { + if (mq_send(queue, msgptr_04GN, strlen(msgptr_04GN), 1) != 0) { + GJB_PRT_ERROR_INFO("mq_send() failed. errno=%d. TEST FAILED!\n", errno); + err_count_604++; + return (void *)-1; + } + } + + if (pthread_create(&new_th2, NULL, MQ_Recv_Task, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create() MQ_Recv_Task failed. errno=%d. TEST FAILED!\n",errno); + + err_count_604++; + return (void *)-1; + } + + sleep(1); + + rc = mq_receive(queue, msgrv, BUFFER, NULL); + if ( rc == -1) { + GJB_PRT_ERROR_INFO("mq_receive() failed. errno=%d. TEST FAILED!\n",errno); + err_count_604++; + return (void *)-1; + } + + flag_04GN = 1; + pthread_exit(0); + return NULL; +} + +int main(int argc, char **argv) +{ + char qname[NAMESIZE]; + struct mq_attr attr; + struct mq_attr mq_info; + + sprintf(qname, "/Mq_Func_10_%lu", pthread_self()); + + memset(&attr, 0, sizeof(attr)); + + attr.mq_msgsize = BUFFER; + attr.mq_maxmsg = BUFFER1; + attr.mq_curmsgs = 0; + attr.mq_flags = 0; + + queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr); + if (queue == (mqd_t)-1) { + GJB_PRT_ERROR_INFO("mq_open()failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (pthread_create(&new_th1, NULL, MQ_Send_Task, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create() MQ_Send_Task failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + sleep(2); + + if (pthread_join(new_th1, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join() new_th1. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (pthread_join(new_th2, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join() new_th2. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + ///Get the current number of messages + if (mq_getattr(queue,&mq_info) != 0) { + GJB_PRT_ERROR_INFO("Error in mq_getattr(). errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + //if ((rc = mq_info.mq_curmsgs) == 40){ + if ((rc = mq_info.mq_curmsgs) == BUFFER1) { + GJB_PRT_INFO("The current number of messages in the message queue is %d. Test PASS!\n",BUFFER1); + + } else { + GJB_PRT_ERROR_INFO("The current number of messages in " + "the message queue is not 40. the number is %d. Test failed!\n", rc); + goto __errno_handle; + } + + rc = mq_close(queue); + if (rc != 0) { + GJB_PRT_ERROR_INFO("mq_close()failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + rc = mq_unlink(qname); + if (rc != 0) { + GJB_PRT_ERROR_INFO("mq_unlink()failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (flag_04GN == 2) { + GJB_PRT_INFO("flag_04GN = %d. Test PASS!\n", flag_04GN); + + } else { + GJB_PRT_ERROR_INFO("flag_04GN = %d.Test failed!\n", flag_04GN); + goto __errno_handle; + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100605GN.c b/signal/gjb_S0100605GN.c new file mode 100644 index 0000000000000000000000000000000000000000..2c7ac91ee4c613205a441cb3e9e6bfd904b05946 --- /dev/null +++ b/signal/gjb_S0100605GN.c @@ -0,0 +1,113 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100605GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 15 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷息锟斤拷锟叫癸拷锟杰诧拷锟斤拷 +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100605GN.c -o S0100605GN -lrt -lpthread +//锟斤拷锟叫o拷./S0100605GN + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 +#define MQ_PRIO_MAX 32768 + +int main(int argc, char **argv) +{ + int i; + int spri = 1; + int maxreached = 0; + char qname[50]; + char msgrcd[40]; + char msgptr[50]; + mqd_t queue; + struct mq_attr attr; + unsigned pri; + + sprintf(qname, "/mq_send_7-1_%lu", pthread_self()); + + attr.mq_msgsize = 40; + attr.mq_maxmsg = 10; + attr.mq_curmsgs = 0; + attr.mq_flags = 0; + + /* + * 锟斤拷锟斤拷息锟斤拷锟斤拷 (锟斤拷锟斤拷), 锟斤拷锟斤拷锟较拷锟斤拷锟斤拷锟叫∥40, 锟斤拷锟斤拷锟较拷锟轿10, 锟斤拷锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷息锟斤拷锟斤拷 + */ + queue = mq_open(qname, O_CREAT | O_RDWR | O_NONBLOCK, S_IRUSR | S_IWUSR, &attr); + if (queue == (mqd_t)-1) { + GJB_PRT_ERROR_INFO("mq_open() failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + for (i = 0; i < MQ_PRIO_MAX; i++) { + sprintf(msgptr, "message %d", i); + + /* + * 锟斤拷锟斤拷锟斤拷息锟斤拷指锟斤拷锟斤拷息锟斤拷锟斤拷锟饺硷拷 + */ + if (mq_send(queue, msgptr, strlen(msgptr), spri++) == -1) { + maxreached = 1; + if (errno != EAGAIN) { //Verify Whether the error code is correct when execution fails? + GJB_PRT_ERROR_INFO("mq_send() failed. errno=%d!=EAGAIN. TEST FAILED!\n",errno); + mq_close(queue); + goto __errno_handle; + } else { + GJB_PRT_INFO("mq_send():[%d] time did not return success,error is EAGAIN. TEST PASS\n",i+1); + break; + } + + } else { + GJB_PRT_INFO("mq_send():[%d] time return success.\n",i+1); + } + } + + if (mq_receive(queue, msgrcd, 40, &pri) == -1) { + GJB_PRT_ERROR_INFO("mq_receive() failed. errno=%d. TEST FAILED!\n",errno); + mq_close(queue); + goto __errno_handle; + } + + if ((strcmp(msgptr, msgrcd) == 0) && (maxreached != 0)) { //Check that the message queue is indeed full + GJB_PRT_ERROR_INFO("Error: Received message that caused EAGAIN. TEST FAILED!\n"); + mq_close(queue); + goto __errno_handle; + } + + if (mq_close(queue) != 0) { + GJB_PRT_ERROR_INFO("mq_close() failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (mq_unlink(qname) != 0) { + GJB_PRT_ERROR_INFO("mq_unlink() failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100606GN.c b/signal/gjb_S0100606GN.c new file mode 100644 index 0000000000000000000000000000000000000000..97b661640f9bb91c310d9df594f2a242b2415cb2 --- /dev/null +++ b/signal/gjb_S0100606GN.c @@ -0,0 +1,133 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100606GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 15 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟斤拷息锟斤拷锟斤拷为锟秸筹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷/锟斤拷锟教从匡拷锟斤拷息锟斤拷锟斤拷锟叫斤拷锟斤拷锟斤拷息. +** 锟斤拷锟秸凤拷式为锟斤拷锟斤拷模式锟斤拷锟斤拷锟斤拷锟较拷锟斤拷锟轿拷锟, +** 锟斤拷锟斤拷锟斤拷息锟斤拷锟斤拷/锟斤拷锟教憋拷锟斤拷锟斤拷锟斤拷直锟斤拷锟斤拷息锟斤拷锟叫斤拷锟秸碉拷一锟斤拷锟斤拷息. +** +** 锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷息锟斤拷锟杰诧拷锟斤拷 +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100606GN.c -o S0100606GN -lrt -lpthread +//锟斤拷锟叫o拷./S0100606GN + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +#define BUFFER 40 +//#define BUFFER1 10 +#define BUFFER1 40 +#define NAMESIZE 50 + +static int err_count_606; +static int rc; +static pthread_t new_th; +static mqd_t queue; +static char msgrv[BUFFER]; + +void *MQ_Task_06GN() +{ + GJB_PRT_INFO("mqueue is empty will block.\n"); + rc = mq_receive(queue, msgrv, BUFFER, NULL); + if (rc == -1) { + GJB_PRT_ERROR_INFO("mq_receive()failed.errno=%d. TEST FAILED!\n",errno); + err_count_606++; + return (void *)-1; + } + + pthread_exit(0); + return NULL; +} + +int main(int argc, char **argv) +{ + const char *msgptr = "test message 1"; + struct mq_attr attr; + char qname[NAMESIZE]; + + sprintf(qname,"/S0100502GN_%lu", pthread_self()); + + attr.mq_msgsize = BUFFER; + attr.mq_maxmsg = BUFFER1; + //attr.mq_maxmsg = BUFFER; + attr.mq_curmsgs = 0; + attr.mq_flags = 0; + + queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr); + if (queue == (mqd_t)-1) { + //锟斤拷锟斤拷锟斤拷锟斤拷 + GJB_PRT_ERROR_INFO("mq_open() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (pthread_create(&new_th, NULL, MQ_Task_06GN, NULL) != 0) { + GJB_PRT_ERROR_INFO("pthread_create() failed. errno=%d. TEST FAILED!",errno); + goto __errno_handle; + } + + sleep(2); + + if (mq_send(queue, msgptr, strlen(msgptr), 1) != 0) { + GJB_PRT_ERROR_INFO("mq_send()failed.errno=%d. TEST FAILED!",errno); + goto __errno_handle; + } + + sleep(1); + + if (strncmp(msgptr, msgrv, strlen(msgptr)) != 0) { + GJB_PRT_ERROR_INFO("mq_receive didn't receive the correct message.TEST FAILED!\n"); + goto __errno_handle; + + } else { + GJB_PRT_INFO("mq_receive receive the correct message.TEST PASS!\n"); + } + + if (pthread_join(new_th, NULL) != 0) { + GJB_PRT_ERROR_INFO("Error in pthread_join().errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + rc = mq_close(queue); + if (rc != 0) { + GJB_PRT_ERROR_INFO("mq_close()failed. errno=%d. TEST FAILED!",errno); + return -1; + } + + rc = mq_unlink(qname); + if (rc != 0) { + GJB_PRT_ERROR_INFO("mq_unlink()failed. errno=%d. TEST FAILED!",errno); + goto __errno_handle; + } + + if (err_count_606) { + goto __errno_handle; + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100607GN.c b/signal/gjb_S0100607GN.c new file mode 100644 index 0000000000000000000000000000000000000000..328db4679956ce097d15996d662343e192e0ede3 --- /dev/null +++ b/signal/gjb_S0100607GN.c @@ -0,0 +1,94 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100602GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 15 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟皆凤拷锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷息锟斤拷锟杰诧拷锟斤拷 +*********************************************************************************************************/ + +//锟斤拷锟诫:gcc -g gjb_S0100607GN.c -o S0100607GN -lrt -lpthread +//锟斤拷锟叫o拷./S0100607GN + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include "7714types.h" + +#define BUFFER 40 +//#define BUFFER1 10 +#define BUFFER1 40 + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +int main(int argc, char **argv) +{ + char mqname[50]; + char msgrv[40]; + mqd_t mqdes; + struct mq_attr attr; + + sprintf(mqname, "/FUNCTION_TEST_%lu", pthread_self()); + + attr.mq_msgsize = BUFFER; + attr.mq_maxmsg = BUFFER1; + attr.mq_curmsgs = 0; + + /* + * 锟皆凤拷锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷息锟斤拷锟斤拷 + */ + mqdes = mq_open(mqname, O_CREAT | O_NONBLOCK | O_RDWR, S_IRUSR | S_IWUSR, &attr); + if (mqdes == (mqd_t)-1) { + GJB_PRT_ERROR_INFO("mq_open() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + /* + * 锟斤拷锟斤拷锟斤拷息锟斤拷锟斤拷 + */ + if (mq_receive(mqdes, msgrv, 40, NULL) != -1) { + GJB_PRT_ERROR_INFO("mq_receive() succeed unexpectly. TEST FAILED!\n"); + mq_close(mqdes); + goto __errno_handle; + + } else { + if (EAGAIN != errno) { + GJB_PRT_ERROR_INFO("mq_receive fail when message queue is empty.but errno is not EAGAIN,errno = %d.TEST FAILED!\n", errno); + goto __errno_handle; + + } else { + GJB_PRT_INFO("mq_receive fail [%d] when message queue is empty.errno is EAGAIN.TEST PASS!\n", errno); + } + } + + if (mq_close(mqdes) != 0) { + GJB_PRT_ERROR_INFO("mq_close() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (mq_unlink(mqname) != 0) { + GJB_PRT_ERROR_INFO("mq_unlink() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + return (GJB_PRI_PASS); + +__errno_handle: + return (GJB_PRI_FAIL); +} diff --git a/signal/gjb_S0100801GN.c b/signal/gjb_S0100801GN.c new file mode 100644 index 0000000000000000000000000000000000000000..a7f2b823b9a71a99a923ef77829448ce582c37dd --- /dev/null +++ b/signal/gjb_S0100801GN.c @@ -0,0 +1,129 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100801GN.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 向指定任务发送特定信号的功能 +*********************************************************************************************************/ + +//编译gcc -g gjb_S0100801GN.c -o S0100801GN -lpthread +//运行 ./S0100801GN +//gcc -g gjb_S0100801GN.c -o S0100801GN -lpthread && ./S0100801GN + +#define _XOPEN_SOURCE 600 +#define _XOPEN_REALTIME 1 +#define __USE_GNU +#define SIGTOTEST SIGRTMIN//具有最高优先级的可用实时信号的编号[34-64] +#define NUMCALLS 5//5 + +#include +#include +#include +#include +#include +#include +#include + +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + + +#ifdef SYLIXOS +#define static +#endif + +volatile int counter_sig_801 = 0; + +void myhandler_01GN(int signo, siginfo_t *info, void *context) { + counter_sig_801++; +} + +void *t_gjb_S0100801GN_xx (void *arg) +{ + struct sigaction act; + long sig = (long)arg;//源码未加long + + //printf("sig=%ld\n",sig);// 34-38 + GJB_PRT_INFO("thread %lx running.\n", pthread_self()); //获取调用线程的ID + + act.sa_flags = SA_SIGINFO; //4 + act.sa_sigaction = myhandler_01GN; + sigemptyset(&act.sa_mask); + int sigaction1=sigaction(sig, &act, 0); // 注册信号处理函数,检查或修改指定信号的设置(信号编号,新的信号处理函数指针) + //printf("sigaction=%d\n",sigaction1); + + //等待接收信号 + sleep(5); + return NULL; +} + +int S0100601GN() +{ + int sig = SIGTOTEST, i; //具有最高优先级的可用实时信号的编号 + union sigval value; + //pthread_t tid[NUMCALLS]; //5 + pthread_t tid[NUMCALLS]; + //printf("SIGTOTEST=%d\n",SIGTOTEST); + + for (i = 0; i < NUMCALLS; ++i) { + //printf("SIGTOTEST=%d\n",SIGTOTEST); + //创建线程(存放任务标识的指针,指向任务属性对象的指针,任务执行函数,任务执行函数的参数) + int err; + err=pthread_create(&tid[i], NULL, t_gjb_S0100801GN_xx, (void *)(long)(SIGTOTEST + i));//源码未加long + //err=pthread_create(&tid[i], NULL, t_gjb_S0100801GN_xx, NULL); + if (err != 0) printf("can't create thread\n"); + //else printf("pthread_create;tid[%d]=%lx\n",i,tid[i]); + } + // 保证所有线程运行 + sleep(1); + + for (i = 0; i < NUMCALLS; ++i) { + value.sival_int = i + 1; + sig = SIGTOTEST + i; + //printf("tid[%d]=%lx,value=%d,sig=%d\n",i,tid[i],value.sival_int,sig);s + + //用于给指定进程发送实时信号;(接收信号的进程id,发送的信号,传递的信号参数) + //if (sigqueue(tid[i], sig, value) != 0) { //返回值为0,函数执行成功 + if (pthread_sigqueue(tid[i], sig, value) != 0) { + GJB_PRT_ERROR_INFO("Test UNRESOLVED: call to sigqueue did not return success\n"); + //printf("sigqueue;tid[%d]=%lx\n",i,tid[i]); + //printf("sigqueue()failed%d. errno=%d. TEST FAILED!\n", i,errno);//任务ID所对应的任务不存在。 + return -1; //此处返回 + } + } + + sleep(1); + + if (NUMCALLS != counter_sig_801) { + GJB_PRT_ERROR_INFO("Test FAILED: signal was queued %d time(s) even though sigqueue was called %d time(s)\n", counter_sig_801, NUMCALLS); + return -1; + } + + GJB_PRT_INFO("test pass.\n"); + return 0; +} + +int main(int argc, char **argv) +{ + int result; + + result = S0100601GN(); + if (result != 0) { + printf("test fail.\n"); + return (GJB_PRI_FAIL); + } + + return (GJB_PRI_PASS); + +} diff --git a/signal/gjb_S0100802GN.c b/signal/gjb_S0100802GN.c new file mode 100644 index 0000000000000000000000000000000000000000..7a9fe5a3dff2bfa2983f2bdf0f53d58026c296f6 --- /dev/null +++ b/signal/gjb_S0100802GN.c @@ -0,0 +1,169 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100802GN.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 以阻塞方式等待特定信号发生功能测试 +*********************************************************************************************************/ + +//编译gcc -g gjb_S0100802GN.c -o S0100802GN -lpthread +//运行 ./S0100802GN + +//出现报错:"message": "不允许使用不完整的类类型 \"struct sigaction\"".添加以下宏定义 +#define _XOPEN_SOURCE 600 +#define _XOPEN_REALTIME 1 +#define __USE_GNU + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +static int flag = 0; +static int SIGUSR1_called = 0; +static int SIGUSR2_called = 0; + +void handler_02GN_signal_1(int signo)//信号编号 +{ + if (signo == SIGUSR1) + { + GJB_PRT_INFO("SIGUSR1 called. Inside handler_02GN\n"); + SIGUSR1_called = 1; + flag = SIGUSR1; + } + + else if (signo == SIGUSR2) + { + GJB_PRT_INFO("SIGUSR2 called. Inside handler_02GN\n"); + SIGUSR2_called = 1; + flag = SIGUSR2; + } +} + +void *threaded_signal_1(void *arg) //该函数运行均成功 +{ + sigset_t tempmask, originalmask; + struct sigaction act; + + act.sa_handler = handler_02GN_signal_1; //void (*__sighandler_t) (int); + act.sa_flags=0; + + //printf("thread %lx running.\n", pthread_self()); + + sigemptyset(&act.sa_mask);//初始化由act.sa_mask指向的信号集,清除其中所有的信号即初始化一个空信号集 + //printf("sigemptyset=%d,",sigemptyset(&act.sa_mask)); + sigemptyset(&tempmask); + //printf("sigemptyset=%d,",sigemptyset(&tempmask)); + sigaddset(&tempmask, SIGUSR2);//将信号SIGUSR2加入到信号集合tempmask之中 + //printf("sigaddset=%d\n",sigaddset(&tempmask, SIGUSR2)); + + if (sigaction(SIGUSR1, &act, 0) == -1) // 注册信号处理函数 + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to pre-conditions, test unsolved!"); + return (void *)-1; + } + + if (sigaction(SIGUSR2, &act, 0) == -1) // 注册信号处理函数 + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to pre-conditions, test unsolved!"); + return (void *)-1; + } + + sigemptyset(&originalmask); + sigaddset(&originalmask, SIGUSR1);//将信号SIGUSR1加入到信号集合originalmask之中 + pthread_sigmask(SIG_SETMASK, &originalmask, NULL); + //sigprocmask(SIG_SETMASK, &originalmask, NULL); // 设置信号的掩码,取消阻塞 + + GJB_PRT_INFO("suspending child\n"); + if (sigsuspend(&tempmask) != -1) + { + GJB_PRT_ERROR_INFO("sigsuspend error,errno=%d,test unsolved.",errno); + } + + sleep(1); + return 0; +} + +int S0100602GN() +{ + pthread_t child; + + if (pthread_create(&child, NULL, threaded_signal_1, NULL)!=0) + { + GJB_PRT_ERROR_INFO("Unable to create child thread, errno=%d\n",errno); + return -1; + } + //printf("child=%ld,\n",child); + /* parent */ + sleep(3); + GJB_PRT_INFO("parent sending child a SIGUSR2 signal\n"); + + //kill (child, SIGUSR2);// 向child线程发送SIGUSR2信号 + int b=pthread_kill(child, SIGUSR2); + //int b=kill (child, SIGUSR2); //返回值为-1,errno=3. + if(b!=0){ + printf("kill()failed. errno=%d. TEST FAILED!\n", errno);}//errno=3,任务id调用的任务不存在 + //printf("child=%lx,kill=%d\n",child,b); + + if (SIGUSR2_called == 1) + { + GJB_PRT_ERROR_INFO("Test FAILED: sigsuspend did not add SIGUSR2 to the temporary mask.test failed.\n"); + return -1; + } + + GJB_PRT_INFO("parent sending child a SIGUSR1 signal\n"); + + //if(kill (child, SIGUSR1)!=0){ + //printf("kill()failed. errno=%d. TEST FAILED!\n", errno);}//errno=3,任务id调用的任务不存在 + int a=pthread_kill(child, SIGUSR1); + //int a=kill (child, SIGUSR1); // child>0,向child线程发送SIGUSR1信号 + //printf("child=%lx,kill=%d\n",child,a); + + sleep(2); + if(flag == SIGUSR2) + { + GJB_PRT_INFO("Test PASSED.\n"); + return 0; + } + else + { + //发生错误 + GJB_PRT_ERROR_INFO("Test failed.\n"); + return -1; + } +} + +int main(int argc, char **argv) +{ + int result; + + result = S0100602GN(); + if (result != 0) { + return (GJB_PRI_FAIL); + } + + return (GJB_PRI_PASS); +} diff --git a/signal/gjb_S0100803GN.c b/signal/gjb_S0100803GN.c new file mode 100644 index 0000000000000000000000000000000000000000..ffddac32a11e3b5cefc299c9564cc34f8489adbd --- /dev/null +++ b/signal/gjb_S0100803GN.c @@ -0,0 +1,191 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100803GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 系统锟结供锟脚号碉拷默锟较达拷锟斤拷锟斤拷式锟斤拷锟杰诧拷锟斤拷 +*********************************************************************************************************/ +//锟斤拷锟诫:gcc -g gjb_S0100803GN.c -o S0100803GN -lpthread +//锟斤拷锟叫o拷./S0100803GN +//gcc -g gjb_S0100803GN.c -o S0100803GN -lpthread && ./S0100803GN + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "7714types.h" + +#define _XOPEN_SOURCE 600 +#define _XOPEN_REALTIME 1 +#define __USE_GNU + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +#ifdef SYLIXOS +#define static +#endif + +#define SIG_THREAD_NUM 64 + +static int handler_called_03GN = 0; + +static void myhandler_03GN(int signo) +{ + GJB_PRT_INFO("SIGCHLD called. Inside handler\n"); + handler_called_03GN = 1; +} + +void *t_gjb_S0100803GN_xxx (void *arg) +{ + int sig = (int)(long)arg; + + if (signal(sig, myhandler_03GN) == SIG_ERR) + { + GJB_PRT_ERROR_INFO("Unexpected error while using signal()\n"); + return (void *)-1; + } + + if (signal(sig,SIG_DFL) != myhandler_03GN) + { + GJB_PRT_ERROR_INFO("Unexpected error while using signal()\n"); + return (void *)-1; + } + + sleep(3); + + return NULL; +} + +int S0100603GN() +{ + pthread_t tid[SIG_THREAD_NUM]; + + pthread_create(&tid[1], NULL, t_gjb_S0100803GN_xxx, (void *)(long)1); + //printf("tid[1]=%ld\n",tid[1]); + pthread_create(&tid[2], NULL, t_gjb_S0100803GN_xxx, (void *)(long)2); + pthread_create(&tid[3], NULL, t_gjb_S0100803GN_xxx, (void *)(long)3); + pthread_create(&tid[4], NULL, t_gjb_S0100803GN_xxx, (void *)(long)51); + pthread_create(&tid[5], NULL, t_gjb_S0100803GN_xxx, (void *)(long)5); + pthread_create(&tid[6], NULL, t_gjb_S0100803GN_xxx, (void *)(long)49); + pthread_create(&tid[7], NULL, t_gjb_S0100803GN_xxx, (void *)(long)7); + pthread_create(&tid[8], NULL, t_gjb_S0100803GN_xxx, (void *)(long)52); + pthread_create(&tid[9], NULL, t_gjb_S0100803GN_xxx, (void *)(long)21); + pthread_create(&tid[10], NULL, t_gjb_S0100803GN_xxx,(void *)(long)48); + pthread_create(&tid[11], NULL, t_gjb_S0100803GN_xxx,(void *)(long)50); + pthread_create(&tid[12], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 12); + pthread_create(&tid[13], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 13); + pthread_create(&tid[14], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 14); + pthread_create(&tid[15], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 15); + pthread_create(&tid[16], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 16); + pthread_create(&tid[17], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 22); + pthread_create(&tid[18], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 23); + pthread_create(&tid[19], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 19); + pthread_create(&tid[20], NULL, t_gjb_S0100803GN_xxx,(void *)(long) 20); + + /*pthread_create(&tid[1], NULL, t_gjb_S0100803GN_xxx, (void *)(long)1); + //printf("tid[1]=%ld\n",tid[1]); + pthread_create(&tid[2], NULL, t_gjb_S0100803GN_xxx, 2); + pthread_create(&tid[3], NULL, t_gjb_S0100803GN_xxx, 3); + pthread_create(&tid[4], NULL, t_gjb_S0100803GN_xxx, 51); + pthread_create(&tid[5], NULL, t_gjb_S0100803GN_xxx, 5); + pthread_create(&tid[6], NULL, t_gjb_S0100803GN_xxx, 49); + pthread_create(&tid[7], NULL, t_gjb_S0100803GN_xxx, 7); + pthread_create(&tid[8], NULL, t_gjb_S0100803GN_xxx, 52); + pthread_create(&tid[9], NULL, t_gjb_S0100803GN_xxx, 21); + pthread_create(&tid[10], NULL, t_gjb_S0100803GN_xxx, 48); + pthread_create(&tid[11], NULL, t_gjb_S0100803GN_xxx, 50); + pthread_create(&tid[12], NULL, t_gjb_S0100803GN_xxx, 12); + pthread_create(&tid[13], NULL, t_gjb_S0100803GN_xxx, 13); + pthread_create(&tid[14], NULL, t_gjb_S0100803GN_xxx, 14); + pthread_create(&tid[15], NULL, t_gjb_S0100803GN_xxx, 15); + pthread_create(&tid[16], NULL, t_gjb_S0100803GN_xxx, 16); + pthread_create(&tid[17], NULL, t_gjb_S0100803GN_xxx, 22); + pthread_create(&tid[18], NULL, t_gjb_S0100803GN_xxx, 23); + pthread_create(&tid[19], NULL, t_gjb_S0100803GN_xxx, 19); + pthread_create(&tid[20], NULL, t_gjb_S0100803GN_xxx, 20);*/ + + sleep(1); + kill(tid[1], 1); + kill(tid[2], 2); + kill(tid[3], 3); + kill(tid[4], 51); + kill(tid[5], 5); + kill(tid[6], 49); + kill(tid[7], 7); + kill(tid[8], 52); + kill(tid[9], 21); + kill(tid[10], 48); + kill(tid[11], 50); + kill(tid[12], 12); + kill(tid[13], 13); + kill(tid[14], 14); + kill(tid[15], 15); + kill(tid[16], 16); + kill(tid[17], 22); + kill(tid[18], 23); + kill(tid[19], 19); + kill(tid[20], 20); + /*pthread_kill(tid[1], 1); + pthread_kill(tid[2], 2); + pthread_kill(tid[3], 3); + pthread_kill(tid[4], 51); + pthread_kill(tid[5], 5); + pthread_kill(tid[6], 49); + pthread_kill(tid[7], 7); + pthread_kill(tid[8], 52); + pthread_kill(tid[9], 21); + pthread_kill(tid[10], 48); + pthread_kill(tid[11], 50); + pthread_kill(tid[12], 12); + pthread_kill(tid[13], 13); + pthread_kill(tid[14], 14); + pthread_kill(tid[15], 15); + pthread_kill(tid[16], 16); + pthread_kill(tid[17], 22); + pthread_kill(tid[18], 23); + pthread_kill(tid[19], 19); + pthread_kill(tid[20], 20);*/ + + sleep(1); + + if (handler_called_03GN == 1) + { + GJB_PRT_ERROR_INFO("Test FAILED: handler was called even though default was expected\n"); + return -1; + } + else + { + GJB_PRT_INFO("The system provides the default signal processing function and passes the test.TEST PASS!.\n"); + } + return 0; +} + +int main(int argc, char **argv) +{ + int result; + + result = S0100603GN(); + if (result != 0) { + return (GJB_PRI_FAIL); + } + + return (GJB_PRI_PASS); +} diff --git a/signal/gjb_S0100804GN.c b/signal/gjb_S0100804GN.c new file mode 100644 index 0000000000000000000000000000000000000000..5246b30bb51a17987bc62368f47b1ec502a17382 --- /dev/null +++ b/signal/gjb_S0100804GN.c @@ -0,0 +1,172 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100804GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟脚号达拷锟斤拷锟斤拷锟斤拷锟斤拷展锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 +*********************************************************************************************************/ + //gcc -g gjb_S0100804GN.c -o S0100804GN -lpthread + // ./S0100804GN + +#define _XOPEN_SOURCE 600 +#define _XOPEN_REALTIME 1 +#define __USE_GNU + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "7714types.h" + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + +#ifdef SYLIXOS +#define static +#endif + +pthread_t gjb_S0100804GN_tid[2]; +volatile int gjb_S0100804GN_pass_flag1 = 0; +volatile int gjb_S0100804GN_pass_flag2 = 0; + +static void handler_04GN1(int signo, siginfo_t *si, void *arg) +//static void handler_04GN1(int signo, struct siginfo *si, void *arg) +{ + union sigval sv; + + if (si->si_value.sival_int == 100) { + gjb_S0100804GN_pass_flag1 = 1; + } + GJB_PRT_INFO("signal(%d) ext data: %d\n", signo, si->si_value.sival_int); + sv.sival_int = 200; + + //int a=sigqueue(gjb_S0100804GN_tid[1], SIGUSR2, sv); + pthread_sigqueue(gjb_S0100804GN_tid[1], SIGUSR2, sv); +} + +static void handler_04GN2(int signo, siginfo_t *si, void *arg) +//源锟斤拷锟诫:static void handler_04GN2(int signo, struct siginfo *si, void *arg) +{ + if (si->si_value.sival_int == 200) { + gjb_S0100804GN_pass_flag2 = 1; + } + + GJB_PRT_INFO("signal(%d) ext data: %d\n", signo, si->si_value.sival_int); +} + +void *t_gjb_S0100804GN_xx1 (void *arg) +{ + struct sigaction act; + + act.sa_sigaction = handler_04GN1;//淇″彿澶勭悊鍑芥暟 + act.sa_flags = SA_SIGINFO;//褰卞搷淇″彿鐨勮涓猴紝SA_SIGINFO琛ㄧず鑳藉鎺ュ彈鏂版暟鎹 + + //int a=sigemptyset(&act.sa_mask); + sigemptyset(&act.sa_mask); + + if (sigaction(SIGUSR1, &act, 0) == -1) //淇″彿鏈彂閫佹垚鍔 + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to setup test " "pre-conditions"); + return (void *)-1; + } + sleep(2); + //printf("t_gjb_S0100804GN_xx1 pass\n"); + + return NULL; +} + +void *t_gjb_S0100804GN_xx2 (void *arg) +{ + struct sigaction act; + union sigval sv; + + act.sa_sigaction = handler_04GN2; + act.sa_flags = SA_SIGINFO; + + //int a=sigemptyset(&act.sa_mask); + sigemptyset(&act.sa_mask); + + if (sigaction(SIGUSR2, &act, 0) == -1) + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to setup test ""pre-conditions"); + return (void *)-1; + } + + sv.sival_int = 100; + + //int b=sigqueue(gjb_S0100804GN_tid[0], SIGUSR1, sv);//杩斿洖鍊间负-1 + pthread_sigqueue(gjb_S0100804GN_tid[0], SIGUSR1, sv);//杩斿洖鍊间负-1 + + /*if(0!=b) + { + printf("t_gjb_S0100804GN_xx2 sigqueue fail.errno=%d\n",errno); + }*/ + + sleep(2); + //printf("t_gjb_S0100804GN_xx2 pass\n"); + return NULL; +} + +int S0100604GN() +{ + /*int a=pthread_create(&gjb_S0100804GN_tid[0], NULL, t_gjb_S0100804GN_xx1, NULL); + printf("pthread_create1 pass\n"); + int b=pthread_create(&gjb_S0100804GN_tid[1], NULL, t_gjb_S0100804GN_xx2, NULL); + + sleep(1); + printf("pthread_create2 pass\n"); + + int c=pthread_join(gjb_S0100804GN_tid[0], NULL); + printf("pthread_join1 pass\n"); + int d=pthread_join(gjb_S0100804GN_tid[1], NULL); + printf("pthread_join2 pass\n");*/ + + pthread_create(&gjb_S0100804GN_tid[0], NULL, t_gjb_S0100804GN_xx1, NULL); + pthread_create(&gjb_S0100804GN_tid[1], NULL, t_gjb_S0100804GN_xx2, NULL); + + sleep(1); + + pthread_join(gjb_S0100804GN_tid[0], NULL); + pthread_join(gjb_S0100804GN_tid[1], NULL); + + printf("gjb_S0100804GN_pass_flag2=%d\n",gjb_S0100804GN_pass_flag2);//0 + printf("gjb_S0100804GN_pass_flag1=%d\n",gjb_S0100804GN_pass_flag1);//0 + + if (gjb_S0100804GN_pass_flag2 == 1 && gjb_S0100804GN_pass_flag1 == 1) + { + GJB_PRT_INFO("test PASS.\n"); + return 0; + } + + GJB_PRT_ERROR_INFO("test FAIL.\n"); + return -1; +} + +int main(int argc, char **argv) +{ + int result; + + result = S0100604GN(); + if (result != 0) { + return (GJB_PRI_FAIL); + } + + return (GJB_PRI_PASS); +} diff --git a/signal/gjb_S0100805GN.c b/signal/gjb_S0100805GN.c new file mode 100644 index 0000000000000000000000000000000000000000..a043313ffbec591cf09d968f57ea950eac5be11f --- /dev/null +++ b/signal/gjb_S0100805GN.c @@ -0,0 +1,185 @@ +/********************************************************************************************************* +** +** GJB 锟斤拷准锟斤拷锟皆硷拷 +** +** Copyright All Rights Reserved +** +**--------------锟侥硷拷锟斤拷息-------------------------------------------------------------------------------- +** +** 锟斤拷 锟斤拷 锟斤拷: gjb_S0100805GN.c +** +** 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷: 2021 锟斤拷 1 锟斤拷 12 锟斤拷 +** +** 锟斤拷 锟斤拷: 锟斤拷锟斤拷锟脚猴拷锟斤拷锟诫功锟杰诧拷锟斤拷 +*********************************************************************************************************/ + + //gcc -g gjb_S0100805GN.c -o S0100805GN -lpthread + // ./S0100805GN + +#define _XOPEN_SOURCE 600 +#define _XOPEN_REALTIME 1 +#define __USE_GNU + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "7714types.h" + + +#define GJB_PRT_INFO printf +#define GJB_PRT_ERROR_INFO printf +#define GJB_PRI_FAIL -1 +#define GJB_PRI_PASS 0 + + +#ifdef SYLIXOS +#define static +#endif + +static int sigprocmask_1_handler_called = 0; +pthread_t tid_main_gjb_S0100805GN; + +volatile int gjb_S0100805GN_flag = 0; + +static void sigprocmask_1_handler(int signo) +{ + sigprocmask_1_handler_called = 1; +} + +void *t_gjb_S0100805GN (void *arg) +{ + while (!gjb_S0100805GN_flag) ; + + if ((pthread_kill(tid_main_gjb_S0100805GN, SIGUSR1) == -1) | (pthread_kill(tid_main_gjb_S0100805GN, SIGUSR2) == -1)) + { + //发生错误 + GJB_PRT_ERROR_INFO("Unexpected error while attempting to setup test pre-conditions1\n"); + //printf("kill()failed. errno=%d. TEST FAILED!\n", errno);//errno=3,任务id调用的任务不存在 + return (void *)-1; + } + + GJB_PRT_INFO("thread send signal SIGUSR1 AND SIGUSR2 to main thread.\n"); + + return NULL; +} + +void *t_gjb_S0100805GN_1 (void *arg) +{ + struct sigaction act; + sigset_t blocked_set1, blocked_set2, pending_set; + + sigemptyset(&blocked_set1); + sigemptyset(&blocked_set2); + sigaddset(&blocked_set1, SIGUSR1); + sigaddset(&blocked_set2, SIGUSR2); + + //printf("thread %lx running.\n", pthread_self()); + + act.sa_handler = sigprocmask_1_handler; + act.sa_flags = 0; + + sigemptyset(&act.sa_mask); + + if (sigaction(SIGUSR1, &act, 0) == -1) + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to setup test pre-conditions2\n"); + return (void *)-1; + } + + if (sigaction(SIGUSR2, &act, 0) == -1) + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to setup test pre-conditions3\n"); + return (void *)-1; + } + + if (pthread_sigmask(SIG_SETMASK, &blocked_set1, NULL) == -1) + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to use sigprocmask.\n"); + return (void *)-1; + } + + if (pthread_sigmask(SIG_BLOCK, &blocked_set2, NULL) == -1) + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to use sigprocmask.\n"); + return (void *)-1; + } + + gjb_S0100805GN_flag = 1; + + sleep(1); + + if (sigprocmask_1_handler_called) + { + GJB_PRT_ERROR_INFO("FAIL: Signal was not blocked\n"); + return (void *)-1; + } + else + { + //成功输出 + GJB_PRT_INFO("The signal was successfully blocked.\n"); + } + + if (sigpending(&pending_set) == -1) + { + GJB_PRT_ERROR_INFO("Unexpected error while attempting to use sigpending\n"); + return (void *)-1; + } + //printf("sigismember_SIGUSR1=%d,sigismember_SIGUSR1=%d\n",sigismember(&pending_set, SIGUSR1) ,sigismember(&pending_set, SIGUSR2)); + + if ((sigismember(&pending_set, SIGUSR1) != 1) | (sigismember(&pending_set, SIGUSR2) != 1))//返回值为0:函数执行成功,SIGUSR1和SIGUSR2不是pending_set中的成员; + { + //发生错误 + GJB_PRT_ERROR_INFO("The blocking signal set of tasks does not contain SIGUSR1 and SIGUSR2 signals.test failed.\n"); + return (void *)-1; + } + else + { + GJB_PRT_INFO("The blocking signal set of tasks contain SIGUSR1 and SIGUSR2 signals.test pass.\n"); + + } + GJB_PRT_INFO("Test PASSED: signal was added to the process's signal mask\n"); + + return (NULL); +} + +int S0100605GN() +{ + pthread_t tid; + void *status; + + int a=pthread_create(&tid_main_gjb_S0100805GN, NULL, t_gjb_S0100805GN_1, NULL); + //printf("create thread;a=%d\n",a); + + int b=pthread_create(&tid, NULL, t_gjb_S0100805GN, NULL); + //printf("create thread;b=%d\n",b); + + pthread_join(tid_main_gjb_S0100805GN, &status); + pthread_join(tid, NULL); + + if ((long)status == -1) { + return (-1); + } + + return 0; +} + +int main(int argc, char **argv) +{ + int result; + + result = S0100605GN(); + if (result != 0) { + return (GJB_PRI_FAIL); + } + + return (GJB_PRI_PASS); +}