From 812eb182a354c196f4f9c8db3d9a5f9db1f165da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Fri, 17 Nov 2023 07:22:30 +0000 Subject: [PATCH 01/34] signal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庡笀 <13383954+li-shi-xiao@user.noreply.gitee.com> --- signal/gjb_S0100801GN.c | 129 +++++++++++++++++++++++++++ signal/gjb_S0100802GN.c | 169 +++++++++++++++++++++++++++++++++++ signal/gjb_S0100803GN.c | 191 ++++++++++++++++++++++++++++++++++++++++ signal/gjb_S0100804GN.c | 172 ++++++++++++++++++++++++++++++++++++ signal/gjb_S0100805GN.c | 185 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 846 insertions(+) create mode 100644 signal/gjb_S0100801GN.c create mode 100644 signal/gjb_S0100802GN.c create mode 100644 signal/gjb_S0100803GN.c create mode 100644 signal/gjb_S0100804GN.c create mode 100644 signal/gjb_S0100805GN.c diff --git a/signal/gjb_S0100801GN.c b/signal/gjb_S0100801GN.c new file mode 100644 index 0000000..a7f2b82 --- /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 0000000..7a9fe5a --- /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 0000000..ffddac3 --- /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 0000000..5246b30 --- /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 0000000..a043313 --- /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); +} -- Gitee From 1cfab6c85303014c8d8acbfa81f0592dc4919a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Fri, 17 Nov 2023 07:26:23 +0000 Subject: [PATCH 02/34] messge_queue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庡笀 <13383954+li-shi-xiao@user.noreply.gitee.com> --- signal/gjb_S0100601GN.c | 83 +++++++++++++++++ signal/gjb_S0100602GN.c | 146 ++++++++++++++++++++++++++++++ signal/gjb_S0100603GN.c | 192 ++++++++++++++++++++++++++++++++++++++++ signal/gjb_S0100604GN.c | 179 +++++++++++++++++++++++++++++++++++++ signal/gjb_S0100605GN.c | 113 +++++++++++++++++++++++ signal/gjb_S0100606GN.c | 133 ++++++++++++++++++++++++++++ signal/gjb_S0100607GN.c | 94 ++++++++++++++++++++ 7 files changed, 940 insertions(+) create mode 100644 signal/gjb_S0100601GN.c create mode 100644 signal/gjb_S0100602GN.c create mode 100644 signal/gjb_S0100603GN.c create mode 100644 signal/gjb_S0100604GN.c create mode 100644 signal/gjb_S0100605GN.c create mode 100644 signal/gjb_S0100606GN.c create mode 100644 signal/gjb_S0100607GN.c diff --git a/signal/gjb_S0100601GN.c b/signal/gjb_S0100601GN.c new file mode 100644 index 0000000..72e4e8f --- /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 0000000..1cff07b --- /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 0000000..d19eeed --- /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 0000000..68a505f --- /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 0000000..2c7ac91 --- /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 0000000..97b6616 --- /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 0000000..328db46 --- /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); +} -- Gitee From 5a5cf8e6143504c1e13278cf9f5c123be1eb0601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Fri, 17 Nov 2023 07:28:03 +0000 Subject: [PATCH 03/34] semaphore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庡笀 <13383954+li-shi-xiao@user.noreply.gitee.com> --- signal/gjb_S0100401GN_1.c | 201 +++++++++++++++++++++++++++++++++ signal/gjb_S0100401GN_2.c | 84 ++++++++++++++ signal/gjb_S0100402GN_1.c | 152 +++++++++++++++++++++++++ signal/gjb_S0100402GN_2.c | 87 ++++++++++++++ signal/gjb_S0100403GN_1.c | 204 +++++++++++++++++++++++++++++++++ signal/gjb_S0100403GN_2.c | 72 ++++++++++++ signal/gjb_S0100404GN_1.c | 197 ++++++++++++++++++++++++++++++++ signal/gjb_S0100404GN_2.c | 76 +++++++++++++ signal/gjb_S0100405GN.c | 227 +++++++++++++++++++++++++++++++++++++ signal/gjb_S0100406GN.c | 231 ++++++++++++++++++++++++++++++++++++++ signal/gjb_S0100407GN.c | 171 ++++++++++++++++++++++++++++ signal/gjb_S0100408GN.c | 161 ++++++++++++++++++++++++++ signal/gjb_S0100409GN.c | 128 +++++++++++++++++++++ signal/gjb_S0100410GN.c | 191 +++++++++++++++++++++++++++++++ 14 files changed, 2182 insertions(+) create mode 100644 signal/gjb_S0100401GN_1.c create mode 100644 signal/gjb_S0100401GN_2.c create mode 100644 signal/gjb_S0100402GN_1.c create mode 100644 signal/gjb_S0100402GN_2.c create mode 100644 signal/gjb_S0100403GN_1.c create mode 100644 signal/gjb_S0100403GN_2.c create mode 100644 signal/gjb_S0100404GN_1.c create mode 100644 signal/gjb_S0100404GN_2.c create mode 100644 signal/gjb_S0100405GN.c create mode 100644 signal/gjb_S0100406GN.c create mode 100644 signal/gjb_S0100407GN.c create mode 100644 signal/gjb_S0100408GN.c create mode 100644 signal/gjb_S0100409GN.c create mode 100644 signal/gjb_S0100410GN.c diff --git a/signal/gjb_S0100401GN_1.c b/signal/gjb_S0100401GN_1.c new file mode 100644 index 0000000..4dee853 --- /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 0000000..6da8506 --- /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 0000000..1235485 --- /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 0000000..0755e66 --- /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 0000000..8f8e0d3 --- /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 0000000..2c6e916 --- /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 0000000..846ed4b --- /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 0000000..09af8c6 --- /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 0000000..f6da575 --- /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 0000000..3c8e57e --- /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 0000000..968daa6 --- /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 0000000..e693d72 --- /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 0000000..ec6c4cb --- /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 0000000..90f5776 --- /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; +} + -- Gitee From a3d0a627535cda59ed496d56e8d2d6da3edf3091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:07:43 +0000 Subject: [PATCH 04/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100401GN=5F1.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100401GN_1.c | 201 -------------------------------------- 1 file changed, 201 deletions(-) delete mode 100644 signal/gjb_S0100401GN_1.c diff --git a/signal/gjb_S0100401GN_1.c b/signal/gjb_S0100401GN_1.c deleted file mode 100644 index 4dee853..0000000 --- a/signal/gjb_S0100401GN_1.c +++ /dev/null @@ -1,201 +0,0 @@ -/********************************************************************************************************* -** -** 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; -} -- Gitee From 832cd9cded381611d6501d225e32e36c7dd4be3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:07:56 +0000 Subject: [PATCH 05/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100401GN=5F2.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100401GN_2.c | 84 --------------------------------------- 1 file changed, 84 deletions(-) delete mode 100644 signal/gjb_S0100401GN_2.c diff --git a/signal/gjb_S0100401GN_2.c b/signal/gjb_S0100401GN_2.c deleted file mode 100644 index 6da8506..0000000 --- a/signal/gjb_S0100401GN_2.c +++ /dev/null @@ -1,84 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From 7575da91a2c70bcefe16f85298cd7cd3aeead77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:08:16 +0000 Subject: [PATCH 06/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100402GN=5F1.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100402GN_1.c | 152 -------------------------------------- 1 file changed, 152 deletions(-) delete mode 100644 signal/gjb_S0100402GN_1.c diff --git a/signal/gjb_S0100402GN_1.c b/signal/gjb_S0100402GN_1.c deleted file mode 100644 index 1235485..0000000 --- a/signal/gjb_S0100402GN_1.c +++ /dev/null @@ -1,152 +0,0 @@ -/********************************************************************************************************* -** -** 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 -- Gitee From 9b4706db2bc2af3ad8189629ed0d8037ecae7ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:08:27 +0000 Subject: [PATCH 07/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100402GN=5F2.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100402GN_2.c | 87 --------------------------------------- 1 file changed, 87 deletions(-) delete mode 100644 signal/gjb_S0100402GN_2.c diff --git a/signal/gjb_S0100402GN_2.c b/signal/gjb_S0100402GN_2.c deleted file mode 100644 index 0755e66..0000000 --- a/signal/gjb_S0100402GN_2.c +++ /dev/null @@ -1,87 +0,0 @@ -/********************************************************************************************************* -** -** 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); - -} -- Gitee From 4cdcd2fd1807c1f6a9c31c1793eab6b5ade5046d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:08:36 +0000 Subject: [PATCH 08/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100403GN=5F1.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100403GN_1.c | 204 -------------------------------------- 1 file changed, 204 deletions(-) delete mode 100644 signal/gjb_S0100403GN_1.c diff --git a/signal/gjb_S0100403GN_1.c b/signal/gjb_S0100403GN_1.c deleted file mode 100644 index 8f8e0d3..0000000 --- a/signal/gjb_S0100403GN_1.c +++ /dev/null @@ -1,204 +0,0 @@ -/********************************************************************************************************* -** -** 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 -- Gitee From 2529761689a66d1a2170ac5c519ab541049ec161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:08:44 +0000 Subject: [PATCH 09/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100403GN=5F2.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100403GN_2.c | 72 --------------------------------------- 1 file changed, 72 deletions(-) delete mode 100644 signal/gjb_S0100403GN_2.c diff --git a/signal/gjb_S0100403GN_2.c b/signal/gjb_S0100403GN_2.c deleted file mode 100644 index 2c6e916..0000000 --- a/signal/gjb_S0100403GN_2.c +++ /dev/null @@ -1,72 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From d0c374c21ce1c95cc8fa30b69c856c09c53690cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:08:50 +0000 Subject: [PATCH 10/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100404GN=5F1.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100404GN_1.c | 197 -------------------------------------- 1 file changed, 197 deletions(-) delete mode 100644 signal/gjb_S0100404GN_1.c diff --git a/signal/gjb_S0100404GN_1.c b/signal/gjb_S0100404GN_1.c deleted file mode 100644 index 846ed4b..0000000 --- a/signal/gjb_S0100404GN_1.c +++ /dev/null @@ -1,197 +0,0 @@ -/********************************************************************************************************* -** -** 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 -- Gitee From f0561e5b8af2a598e6e2f0761b77f1257baab41a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:08:57 +0000 Subject: [PATCH 11/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100405GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100405GN.c | 227 ---------------------------------------- 1 file changed, 227 deletions(-) delete mode 100644 signal/gjb_S0100405GN.c diff --git a/signal/gjb_S0100405GN.c b/signal/gjb_S0100405GN.c deleted file mode 100644 index f6da575..0000000 --- a/signal/gjb_S0100405GN.c +++ /dev/null @@ -1,227 +0,0 @@ -/********************************************************************************************************* -** -** 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 -- Gitee From a2f6de4242f4ea4dbaa30d3c8f942090a036cd44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:09:03 +0000 Subject: [PATCH 12/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100404GN=5F2.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100404GN_2.c | 76 --------------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 signal/gjb_S0100404GN_2.c diff --git a/signal/gjb_S0100404GN_2.c b/signal/gjb_S0100404GN_2.c deleted file mode 100644 index 09af8c6..0000000 --- a/signal/gjb_S0100404GN_2.c +++ /dev/null @@ -1,76 +0,0 @@ -/********************************************************************************************************* -** -** 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); - -} -- Gitee From 86c5dc95e49d24dd96cb04d3987e6e48cddbb90c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:09:12 +0000 Subject: [PATCH 13/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100406GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100406GN.c | 231 ---------------------------------------- 1 file changed, 231 deletions(-) delete mode 100644 signal/gjb_S0100406GN.c diff --git a/signal/gjb_S0100406GN.c b/signal/gjb_S0100406GN.c deleted file mode 100644 index 3c8e57e..0000000 --- a/signal/gjb_S0100406GN.c +++ /dev/null @@ -1,231 +0,0 @@ -/********************************************************************************************************* -** -** 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; -} - -- Gitee From d86c990ce162ed590e561140d11591853ab0b946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:09:22 +0000 Subject: [PATCH 14/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100409GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100409GN.c | 128 ---------------------------------------- 1 file changed, 128 deletions(-) delete mode 100644 signal/gjb_S0100409GN.c diff --git a/signal/gjb_S0100409GN.c b/signal/gjb_S0100409GN.c deleted file mode 100644 index ec6c4cb..0000000 --- a/signal/gjb_S0100409GN.c +++ /dev/null @@ -1,128 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From d6b6527d16dc6cfe773e50e3d6b54e479fba187c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:09:26 +0000 Subject: [PATCH 15/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100407GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100407GN.c | 171 ---------------------------------------- 1 file changed, 171 deletions(-) delete mode 100644 signal/gjb_S0100407GN.c diff --git a/signal/gjb_S0100407GN.c b/signal/gjb_S0100407GN.c deleted file mode 100644 index 968daa6..0000000 --- a/signal/gjb_S0100407GN.c +++ /dev/null @@ -1,171 +0,0 @@ -/********************************************************************************************************* -** -** 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 -- Gitee From 6a49d09d77c7d5dfd6860bfa86c985ceb7a5a5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:09:39 +0000 Subject: [PATCH 16/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100408GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100408GN.c | 161 ---------------------------------------- 1 file changed, 161 deletions(-) delete mode 100644 signal/gjb_S0100408GN.c diff --git a/signal/gjb_S0100408GN.c b/signal/gjb_S0100408GN.c deleted file mode 100644 index e693d72..0000000 --- a/signal/gjb_S0100408GN.c +++ /dev/null @@ -1,161 +0,0 @@ -/********************************************************************************************************* -** -** 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 -- Gitee From 395696ad27e04998eefa80049f77f5233373dd7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:09:47 +0000 Subject: [PATCH 17/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100410GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100410GN.c | 191 ---------------------------------------- 1 file changed, 191 deletions(-) delete mode 100644 signal/gjb_S0100410GN.c diff --git a/signal/gjb_S0100410GN.c b/signal/gjb_S0100410GN.c deleted file mode 100644 index 90f5776..0000000 --- a/signal/gjb_S0100410GN.c +++ /dev/null @@ -1,191 +0,0 @@ -/********************************************************************************************************* -** -** 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; -} - -- Gitee From 5ec14b91d322bd01d705ab0690b88d699555cc9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:09:52 +0000 Subject: [PATCH 18/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100602GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100602GN.c | 146 ---------------------------------------- 1 file changed, 146 deletions(-) delete mode 100644 signal/gjb_S0100602GN.c diff --git a/signal/gjb_S0100602GN.c b/signal/gjb_S0100602GN.c deleted file mode 100644 index 1cff07b..0000000 --- a/signal/gjb_S0100602GN.c +++ /dev/null @@ -1,146 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From 7c0eb4258408024856a1c84cc3be40cefbb31581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:09:57 +0000 Subject: [PATCH 19/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100601GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100601GN.c | 83 ----------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 signal/gjb_S0100601GN.c diff --git a/signal/gjb_S0100601GN.c b/signal/gjb_S0100601GN.c deleted file mode 100644 index 72e4e8f..0000000 --- a/signal/gjb_S0100601GN.c +++ /dev/null @@ -1,83 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From d7911e838793b177a136512fe577bd03df653bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:10:00 +0000 Subject: [PATCH 20/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100603GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100603GN.c | 192 ---------------------------------------- 1 file changed, 192 deletions(-) delete mode 100644 signal/gjb_S0100603GN.c diff --git a/signal/gjb_S0100603GN.c b/signal/gjb_S0100603GN.c deleted file mode 100644 index d19eeed..0000000 --- a/signal/gjb_S0100603GN.c +++ /dev/null @@ -1,192 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From daee9375d3cbe6c22fadfbe11d171678343e4c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:10:07 +0000 Subject: [PATCH 21/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100604GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100604GN.c | 179 ---------------------------------------- 1 file changed, 179 deletions(-) delete mode 100644 signal/gjb_S0100604GN.c diff --git a/signal/gjb_S0100604GN.c b/signal/gjb_S0100604GN.c deleted file mode 100644 index 68a505f..0000000 --- a/signal/gjb_S0100604GN.c +++ /dev/null @@ -1,179 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From b4e8249ccf2421f245a7e0b14171f66db0d4dab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:10:14 +0000 Subject: [PATCH 22/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100801GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100801GN.c | 129 ---------------------------------------- 1 file changed, 129 deletions(-) delete mode 100644 signal/gjb_S0100801GN.c diff --git a/signal/gjb_S0100801GN.c b/signal/gjb_S0100801GN.c deleted file mode 100644 index a7f2b82..0000000 --- a/signal/gjb_S0100801GN.c +++ /dev/null @@ -1,129 +0,0 @@ -/********************************************************************************************************* -** -** 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); - -} -- Gitee From 03ab440ac6cbe3650e1d1475dfd5d11e041494aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:10:23 +0000 Subject: [PATCH 23/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100605GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100605GN.c | 113 ---------------------------------------- 1 file changed, 113 deletions(-) delete mode 100644 signal/gjb_S0100605GN.c diff --git a/signal/gjb_S0100605GN.c b/signal/gjb_S0100605GN.c deleted file mode 100644 index 2c7ac91..0000000 --- a/signal/gjb_S0100605GN.c +++ /dev/null @@ -1,113 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From 4b7b678e908c82dde6c0618bbe3eaff0539a6070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:10:31 +0000 Subject: [PATCH 24/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100606GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100606GN.c | 133 ---------------------------------------- 1 file changed, 133 deletions(-) delete mode 100644 signal/gjb_S0100606GN.c diff --git a/signal/gjb_S0100606GN.c b/signal/gjb_S0100606GN.c deleted file mode 100644 index 97b6616..0000000 --- a/signal/gjb_S0100606GN.c +++ /dev/null @@ -1,133 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From c69e453e9dcd0b8cafb81084519a7e3d48b036a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:10:37 +0000 Subject: [PATCH 25/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100805GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100805GN.c | 185 ---------------------------------------- 1 file changed, 185 deletions(-) delete mode 100644 signal/gjb_S0100805GN.c diff --git a/signal/gjb_S0100805GN.c b/signal/gjb_S0100805GN.c deleted file mode 100644 index a043313..0000000 --- a/signal/gjb_S0100805GN.c +++ /dev/null @@ -1,185 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From fc2cea290849cab8ebcb73af4b8a976041e6de7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:10:49 +0000 Subject: [PATCH 26/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100802GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100802GN.c | 169 ---------------------------------------- 1 file changed, 169 deletions(-) delete mode 100644 signal/gjb_S0100802GN.c diff --git a/signal/gjb_S0100802GN.c b/signal/gjb_S0100802GN.c deleted file mode 100644 index 7a9fe5a..0000000 --- a/signal/gjb_S0100802GN.c +++ /dev/null @@ -1,169 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From b2fa1f895b0ad98f9d6ea47f45e2bfd4db3487d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:10:55 +0000 Subject: [PATCH 27/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100804GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100804GN.c | 172 ---------------------------------------- 1 file changed, 172 deletions(-) delete mode 100644 signal/gjb_S0100804GN.c diff --git a/signal/gjb_S0100804GN.c b/signal/gjb_S0100804GN.c deleted file mode 100644 index 5246b30..0000000 --- a/signal/gjb_S0100804GN.c +++ /dev/null @@ -1,172 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From 1fb56546b61f01751a655fc88b62b084bfa18592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:11:01 +0000 Subject: [PATCH 28/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100607GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100607GN.c | 94 ----------------------------------------- 1 file changed, 94 deletions(-) delete mode 100644 signal/gjb_S0100607GN.c diff --git a/signal/gjb_S0100607GN.c b/signal/gjb_S0100607GN.c deleted file mode 100644 index 328db46..0000000 --- a/signal/gjb_S0100607GN.c +++ /dev/null @@ -1,94 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From e63db3bf5e07ce2d388c2fbe93941cddc82cbfc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:11:06 +0000 Subject: [PATCH 29/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20si?= =?UTF-8?q?gnal/gjb=5FS0100803GN.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- signal/gjb_S0100803GN.c | 191 ---------------------------------------- 1 file changed, 191 deletions(-) delete mode 100644 signal/gjb_S0100803GN.c diff --git a/signal/gjb_S0100803GN.c b/signal/gjb_S0100803GN.c deleted file mode 100644 index ffddac3..0000000 --- a/signal/gjb_S0100803GN.c +++ /dev/null @@ -1,191 +0,0 @@ -/********************************************************************************************************* -** -** 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); -} -- Gitee From a5bc3c0119d21c05ce67b9f9a81cc1ec4bbd4495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:12:31 +0000 Subject: [PATCH 30/34] signal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庡笀 <13383954+li-shi-xiao@user.noreply.gitee.com> --- signal/gjb_S0100801GN.c | 119 +++++++++++++++++++++++++++ signal/gjb_S0100802GN.c | 161 ++++++++++++++++++++++++++++++++++++ signal/gjb_S0100803GN.c | 146 +++++++++++++++++++++++++++++++++ signal/gjb_S0100804GN.c | 148 +++++++++++++++++++++++++++++++++ signal/gjb_S0100805GN.c | 177 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 751 insertions(+) create mode 100644 signal/gjb_S0100801GN.c create mode 100644 signal/gjb_S0100802GN.c create mode 100644 signal/gjb_S0100803GN.c create mode 100644 signal/gjb_S0100804GN.c create mode 100644 signal/gjb_S0100805GN.c diff --git a/signal/gjb_S0100801GN.c b/signal/gjb_S0100801GN.c new file mode 100644 index 0000000..6d6fba0 --- /dev/null +++ b/signal/gjb_S0100801GN.c @@ -0,0 +1,119 @@ +/********************************************************************************************************* +** +** 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 + +#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 + printf("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]; + //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 + 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); + + //用于给指定进程发送实时信号;(接收信号的进程id,发送的信号,传递的信号参数) + //if (sigqueue(tid[i], sig, value) != 0) { //返回值为0,函数执行成功 + if (pthread_sigqueue(tid[i], sig, value) != 0) { + printf("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) { + printf("Test FAILED: signal was queued %d time(s) even though sigqueue was called %d time(s)\n", counter_sig_801, NUMCALLS); + return -1; + } + + printf("test pass.\n"); + return 0; +} + +int main(int argc, char **argv) +{ + int result; + + result = S0100601GN(); + if (result != 0) { + printf("test fail.\n"); + return (-1); + } + + return (0); + +} diff --git a/signal/gjb_S0100802GN.c b/signal/gjb_S0100802GN.c new file mode 100644 index 0000000..24a2236 --- /dev/null +++ b/signal/gjb_S0100802GN.c @@ -0,0 +1,161 @@ +/********************************************************************************************************* +** +** 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 + +static int flag = 0; +static int SIGUSR1_called = 0; +static int SIGUSR2_called = 0; + +void handler_02GN_signal_1(int signo)//信号编号 +{ + if (signo == SIGUSR1) + { + printf("SIGUSR1 called. Inside handler_02GN\n"); + SIGUSR1_called = 1; + flag = SIGUSR1; + } + + else if (signo == SIGUSR2) + { + printf("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) // 注册信号处理函数 + { + printf("Unexpected error while attempting to pre-conditions, test unsolved!"); + return (void *)-1; + } + + if (sigaction(SIGUSR2, &act, 0) == -1) // 注册信号处理函数 + { + printf("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); // 设置信号的掩码,取消阻塞 + + printf("suspending child\n"); + if (sigsuspend(&tempmask) != -1) + { + printf("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) + { + printf("Unable to create child thread, errno=%d\n",errno); + return -1; + } + /* parent */ + sleep(3); + printf("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) + { + printf("Test FAILED: sigsuspend did not add SIGUSR2 to the temporary mask.test failed.\n"); + return -1; + } + + printf("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) + { + printf("Test PASSED.\n"); + return 0; + } + else + { + //发生错误 + printf("Test failed.\n"); + return -1; + } +} + +int main(int argc, char **argv) +{ + int result; + + result = S0100602GN(); + if (result != 0) { + return (-1); + } + + return (0); +} diff --git a/signal/gjb_S0100803GN.c b/signal/gjb_S0100803GN.c new file mode 100644 index 0000000..502ef44 --- /dev/null +++ b/signal/gjb_S0100803GN.c @@ -0,0 +1,146 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100803GN.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 系统提供信号的默认处理方式功能测试 +*********************************************************************************************************/ +//编译:gcc -g gjb_S0100803GN.c -o S0100803GN -lpthread +//运行:./S0100803GN +//gcc -g gjb_S0100803GN.c -o S0100803GN -lpthread && ./S0100803GN + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define _XOPEN_SOURCE 600 +#define _XOPEN_REALTIME 1 +#define __USE_GNU + + +#ifdef SYLIXOS +#define static +#endif + +#define SIG_THREAD_NUM 64 + +static int handler_called_03GN = 0; + +static void myhandler_03GN(int signo) +{ + printf("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) + { + printf("Unexpected error while using signal()\n"); + return (void *)-1; + } + + if (signal(sig,SIG_DFL) != myhandler_03GN) + { + printf("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); + + + 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); + + sleep(1); + + if (handler_called_03GN == 1) + { + printf("Test FAILED: handler was called even though default was expected\n"); + return -1; + } + else + { + printf("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 (-1); + } + + return (0); +} diff --git a/signal/gjb_S0100804GN.c b/signal/gjb_S0100804GN.c new file mode 100644 index 0000000..fc87865 --- /dev/null +++ b/signal/gjb_S0100804GN.c @@ -0,0 +1,148 @@ +/********************************************************************************************************* +** +** 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 + +#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; + } + printf("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; + } + + printf("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; + + //int a=sigemptyset(&act.sa_mask); + sigemptyset(&act.sa_mask); + + if (sigaction(SIGUSR1, &act, 0) == -1) + { + printf("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) + { + printf("Unexpected error while attempting to setup test ""pre-conditions"); + return (void *)-1; + } + + sv.sival_int = 100; + + pthread_sigqueue(gjb_S0100804GN_tid[0], SIGUSR1, sv); + + sleep(2); + //printf("t_gjb_S0100804GN_xx2 pass\n"); + return NULL; +} + +int S0100604GN() +{ + 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) + { + printf("test PASS.\n"); + return 0; + } + + printf("test FAIL.\n"); + return -1; +} + +int main(int argc, char **argv) +{ + int result; + + result = S0100604GN(); + if (result != 0) { + return (-1); + } + + return (0); +} diff --git a/signal/gjb_S0100805GN.c b/signal/gjb_S0100805GN.c new file mode 100644 index 0000000..8d29907 --- /dev/null +++ b/signal/gjb_S0100805GN.c @@ -0,0 +1,177 @@ +/********************************************************************************************************* +** +** 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 + +#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)) + { + //发生错误 + printf("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; + } + + printf("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) + { + printf("Unexpected error while attempting to setup test pre-conditions2\n"); + return (void *)-1; + } + + if (sigaction(SIGUSR2, &act, 0) == -1) + { + printf("Unexpected error while attempting to setup test pre-conditions3\n"); + return (void *)-1; + } + + if (pthread_sigmask(SIG_SETMASK, &blocked_set1, NULL) == -1) + { + printf("Unexpected error while attempting to use sigprocmask.\n"); + return (void *)-1; + } + + if (pthread_sigmask(SIG_BLOCK, &blocked_set2, NULL) == -1) + { + printf("Unexpected error while attempting to use sigprocmask.\n"); + return (void *)-1; + } + + gjb_S0100805GN_flag = 1; + + sleep(1); + + if (sigprocmask_1_handler_called) + { + printf("FAIL: Signal was not blocked\n"); + return (void *)-1; + } + else + { + //成功输出 + printf("The signal was successfully blocked.\n"); + } + + if (sigpending(&pending_set) == -1) + { + printf("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中的成员; + { + //发生错误 + printf("The blocking signal set of tasks does not contain SIGUSR1 and SIGUSR2 signals.test failed.\n"); + return (void *)-1; + } + else + { + printf("The blocking signal set of tasks contain SIGUSR1 and SIGUSR2 signals.test pass.\n"); + + } + printf("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 (-1); + } + + return (0); +} -- Gitee From 91bddb0164c8cf2e054a388795d52032d00e0fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:17:36 +0000 Subject: [PATCH 31/34] messge_queue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庡笀 <13383954+li-shi-xiao@user.noreply.gitee.com> --- messge_queue/gjb_S0100601GN.c | 81 +++++++++++++++ messge_queue/gjb_S0100602GN.c | 143 ++++++++++++++++++++++++++ messge_queue/gjb_S0100603GN.c | 186 ++++++++++++++++++++++++++++++++++ messge_queue/gjb_S0100604GN.c | 178 ++++++++++++++++++++++++++++++++ messge_queue/gjb_S0100605GN.c | 109 ++++++++++++++++++++ messge_queue/gjb_S0100606GN.c | 128 +++++++++++++++++++++++ messge_queue/gjb_S0100607GN.c | 90 ++++++++++++++++ 7 files changed, 915 insertions(+) create mode 100644 messge_queue/gjb_S0100601GN.c create mode 100644 messge_queue/gjb_S0100602GN.c create mode 100644 messge_queue/gjb_S0100603GN.c create mode 100644 messge_queue/gjb_S0100604GN.c create mode 100644 messge_queue/gjb_S0100605GN.c create mode 100644 messge_queue/gjb_S0100606GN.c create mode 100644 messge_queue/gjb_S0100607GN.c diff --git a/messge_queue/gjb_S0100601GN.c b/messge_queue/gjb_S0100601GN.c new file mode 100644 index 0000000..2f5bdf2 --- /dev/null +++ b/messge_queue/gjb_S0100601GN.c @@ -0,0 +1,81 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100601GN.c +** +** 文件创建日期: 2021 年 1 月 15 日 +** +** 描 述: 消息队列创建和删除功能测试 +*********************************************************************************************************/ + +//编译:gcc -g gjb_S0100601GN.c -o S0100601GN -lrt +//运行:./S0100601GN + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#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 qname[50]; + mqd_t queue; + + //源代码:sprintf(qname, "FUNCTION_S0100501GN_TEST_%lu", pthread_self()); + sprintf(qname, "/FUNCTION_S0100501GN_TEST_%lu", pthread_self());// + //printf("qname=%s\n",qname); + + /*struct mq_attr mq_attr_param = { + .mq_flags = O_NONBLOCK, + .mq_maxmsg = 10, + .mq_msgsize = 100 + }; // O_NONBLOCK + queue = mq_open("/qname", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &mq_attr_param);*/ + + + queue = mq_open(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL); + //printf("queue=%d.\n",queue); + + if (queue == (mqd_t)-1) + { + printf("mq_open()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + + } else { + printf("mq_open()success.\n"); + } + + if (mq_close(queue) != 0) { + printf("mq_close()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + if (mq_unlink(qname) != 0) { + printf("mq_unlink() failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } else { + printf("mq_unlink() success.TEST PASS!\n"); + } + + return (0); + +__errno_handle: + return (-1); +} diff --git a/messge_queue/gjb_S0100602GN.c b/messge_queue/gjb_S0100602GN.c new file mode 100644 index 0000000..cfda7ec --- /dev/null +++ b/messge_queue/gjb_S0100602GN.c @@ -0,0 +1,143 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100602GN.c +** +** 文件创建日期: 2021 年 1 月 15 日 +** +** 描 述: 消息队列同步通信机制测试 +*********************************************************************************************************/ + +//编译:gcc -g gjb_S0100602GN.c -o S0100602GN -lrt -lpthread +//运行: ./S0100602GN +//注:由于系统限制,mq_maxmsg 和 mq_msgsize 参数分别被限制为10和8129,超过该值程序无法运行通过 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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) { + printf("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));//为attr内的参数赋值 + + 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;//系统限制的最大消息数 + 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) { + printf("mq_open() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + /* + * 创建消息接收线程 + */ + if (pthread_create(&new_th, NULL, MQ_Task_02GN, NULL) != 0) { + printf("pthread_create() failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + sleep(1); + + /* + * 向消息队列中发送消息 + */ + if (mq_send(queue, msgptr, strlen(msgptr), 1) != 0) { + printf("mq_send()failed.errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + sleep(1); + + if (strncmp(msgptr, msgrv, strlen(msgptr)) != 0) { + printf("mq_receive didn't receive the correct message.TEST FAILED!\n"); + goto __errno_handle; + + } else { + printf("mq_receive receive the correct message.TEST PASS!\n"); + } + + if (pthread_join(new_th, NULL) != 0) { + printf("Error in pthread_join().errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + rc = mq_close(queue); + if (rc != 0) { + printf("mq_close()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + rc = mq_unlink(qname); + if (rc != 0) { + printf("mq_unlink()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + if (err_count_602) { + goto __errno_handle; + } + + return (0); + +__errno_handle: + return (-1); +} diff --git a/messge_queue/gjb_S0100603GN.c b/messge_queue/gjb_S0100603GN.c new file mode 100644 index 0000000..0cc7eae --- /dev/null +++ b/messge_queue/gjb_S0100603GN.c @@ -0,0 +1,186 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100603GN.c +** +** 文件创建日期: 2021 年 1 月 15 日 +** +** 描 述: 消息队列异步通信机制测试 +*********************************************************************************************************/ + +//编译:gcc -g gjb_S0100603GN.c -o S0100603GN -lrt -lpthread +//运行:./S0100603GN +//注:由于系统限制,mq_maxmsg 和 mq_msgsize 参数分别被限制为10和8129,超过该值程序无法运行通过 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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) { + printf("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) { + printf("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) { + printf("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) { + printf("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) { + printf("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) { + printf("Error in pthread_join().errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + //printf("pthread_join1\n"); + + if (pthread_join(new_th2, NULL) != 0) { + printf("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) { + printf("Error in mq_getattr(). errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + //printf("mq_getattr\n"); + + if ((rc = mq_info.mq_curmsgs) == 0) { + printf("The current number of messages in the message queue is 0. Test PASS!\n"); + } + else { + printf("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) { + printf("mq_close()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + rc = mq_unlink(qname); + if (rc != 0) { + printf("mq_unlink()failed. errno=%d. TEST FAILED!\n", errno); + goto __errno_handle; + } + + if (flag_03GN == 2) { + printf("flag_03GN = %d. Test PASS!\n", flag_03GN); + + } else { + printf("flag_03GN = %d.Test failed!\n", flag_03GN); + goto __errno_handle; + } + + if (err_count_603) { + goto __errno_handle; + } + + return (0); + +__errno_handle: + return (-1); +} diff --git a/messge_queue/gjb_S0100604GN.c b/messge_queue/gjb_S0100604GN.c new file mode 100644 index 0000000..baa671f --- /dev/null +++ b/messge_queue/gjb_S0100604GN.c @@ -0,0 +1,178 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100604GN.c +** +** 文件创建日期: 2021 年 1 月 15 日 +** +** 描 述: 创建消息队列,在两个任务/进程之间通过消息队列通信,调整消息发送和接收速度, +** 以及消息发送方式,验证阻塞方式是否有效;如果消息队列已满, +** 那么消息发送任务/进程将被阻塞,直到消息队列空间可用 +** +** 以阻塞方式发送消息队列功能测试 +********************************************************************************************************/ + +//gcc -g gjb_S0100604GN.c -o S0100604GN -lrt -lpthread +//./S0100604GN +//注:由于系统限制,mq_maxmsg 和 mq_msgsize 参数分别被限制为10和8129,超过该值程序无法运行通过 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFER 40 //最大限制为8192 +//#define BUFFER1 10 //系统限制的最大消息数为10 +#define BUFFER1 40 //系统限制的最大消息数为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) { + printf("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) { + printf("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) { + printf("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) { + printf("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) { + printf("mq_open()failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (pthread_create(&new_th1, NULL, MQ_Send_Task, NULL) != 0) { + printf("pthread_create() MQ_Send_Task failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + sleep(2); + + if (pthread_join(new_th1, NULL) != 0) { + printf("Error in pthread_join() new_th1. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (pthread_join(new_th2, NULL) != 0) { + printf("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) { + printf("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) { + printf("The current number of messages in the message queue is %d. Test PASS!\n",BUFFER1); + + } else { + printf("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) { + printf("mq_close()failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + rc = mq_unlink(qname); + if (rc != 0) { + printf("mq_unlink()failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (flag_04GN == 2) { + printf("flag_04GN = %d. Test PASS!\n", flag_04GN); + + } else { + printf("flag_04GN = %d.Test failed!\n", flag_04GN); + goto __errno_handle; + } + + return (0); + +__errno_handle: + return (-1); +} diff --git a/messge_queue/gjb_S0100605GN.c b/messge_queue/gjb_S0100605GN.c new file mode 100644 index 0000000..0bfd0dd --- /dev/null +++ b/messge_queue/gjb_S0100605GN.c @@ -0,0 +1,109 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100605GN.c +** +** 文件创建日期: 2021 年 1 月 15 日 +** +** 描 述: 以阻塞方式发送消息队列功能测试 +*********************************************************************************************************/ + +//gcc -g gjb_S0100605GN.c -o S0100605GN -lrt -lpthread +//./S0100605GN + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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) { + printf("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? + printf("mq_send() failed. errno=%d!=EAGAIN. TEST FAILED!\n",errno); + mq_close(queue); + goto __errno_handle; + } else { + printf("mq_send():[%d] time did not return success,error is EAGAIN. TEST PASS\n",i+1); + break; + } + + } else { + printf("mq_send():[%d] time return success.\n",i+1); + } + } + + if (mq_receive(queue, msgrcd, 40, &pri) == -1) { + printf("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 + printf("Error: Received message that caused EAGAIN. TEST FAILED!\n"); + mq_close(queue); + goto __errno_handle; + } + + if (mq_close(queue) != 0) { + printf("mq_close() failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (mq_unlink(qname) != 0) { + printf("mq_unlink() failed. errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + return (0); + +__errno_handle: + return (-1); +} diff --git a/messge_queue/gjb_S0100606GN.c b/messge_queue/gjb_S0100606GN.c new file mode 100644 index 0000000..6b14ea1 --- /dev/null +++ b/messge_queue/gjb_S0100606GN.c @@ -0,0 +1,128 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100606GN.c +** +** 文件创建日期: 2021 年 1 月 15 日 +** +** 描 述: 构造消息队列为空场景,调用任务/进程从空消息队列中接收消息. +** 接收方式为阻塞模式;如果消息队列为空, +** 接收消息任务/进程被阻塞,直到消息队列接收到一个消息. +** +** 阻塞式接收消息功能测试 +*********************************************************************************************************/ + +//gcc -g gjb_S0100606GN.c -o S0100606GN -lrt -lpthread +//./S0100606GN +//注:由于系统限制,mq_maxmsg 和 mq_msgsize 参数分别被限制为10和8129,超过该值程序无法运行通过 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFER 40 //最大限制为8192 +//#define BUFFER1 10 //系统限制的最大消息数为10 +#define BUFFER1 40 //系统限制的最大消息数为10,该参数下程序无法通过 +#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() +{ + printf("mqueue is empty will block.\n"); + rc = mq_receive(queue, msgrv, BUFFER, NULL); + if (rc == -1) { + printf("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) { + printf("mq_open() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (pthread_create(&new_th, NULL, MQ_Task_06GN, NULL) != 0) { + printf("pthread_create() failed. errno=%d. TEST FAILED!",errno); + goto __errno_handle; + } + + sleep(2); + + if (mq_send(queue, msgptr, strlen(msgptr), 1) != 0) { + printf("mq_send()failed.errno=%d. TEST FAILED!",errno); + goto __errno_handle; + } + + sleep(1); + + if (strncmp(msgptr, msgrv, strlen(msgptr)) != 0) { + printf("mq_receive didn't receive the correct message.TEST FAILED!\n"); + goto __errno_handle; + + } else { + printf("mq_receive receive the correct message.TEST PASS!\n"); + } + + if (pthread_join(new_th, NULL) != 0) { + printf("Error in pthread_join().errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + rc = mq_close(queue); + if (rc != 0) { + printf("mq_close()failed. errno=%d. TEST FAILED!",errno); + return -1; + } + + rc = mq_unlink(qname); + if (rc != 0) { + printf("mq_unlink()failed. errno=%d. TEST FAILED!",errno); + goto __errno_handle; + } + + if (err_count_606) { + goto __errno_handle; + } + + return (0); + +__errno_handle: + return (-1); +} diff --git a/messge_queue/gjb_S0100607GN.c b/messge_queue/gjb_S0100607GN.c new file mode 100644 index 0000000..ea2f3a4 --- /dev/null +++ b/messge_queue/gjb_S0100607GN.c @@ -0,0 +1,90 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100602GN.c +** +** 文件创建日期: 2021 年 1 月 15 日 +** +** 描 述: 以非阻塞方式接收消息功能测试 +*********************************************************************************************************/ + +//gcc -g gjb_S0100607GN.c -o S0100607GN -lrt -lpthread +//./S0100607GN +//注:由于系统限制,mq_maxmsg 和 mq_msgsize 参数分别被限制为10和8129,超过该值程序无法运行通过 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFER 40 //最大限制为8192 +//#define BUFFER1 10 //系统限制的最大消息数为10 +#define BUFFER1 40 //系统限制的最大消息数为10,该参数下程序无法通过 + +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) { + printf("mq_open() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + /* + * 接收消息队列 + */ + if (mq_receive(mqdes, msgrv, 40, NULL) != -1) { + printf("mq_receive() succeed unexpectly. TEST FAILED!\n"); + mq_close(mqdes); + goto __errno_handle; + + } else { + if (EAGAIN != errno) { + printf("mq_receive fail when message queue is empty.but errno is not EAGAIN,errno = %d.TEST FAILED!\n", errno); + goto __errno_handle; + + } else { + printf("mq_receive fail [%d] when message queue is empty.errno is EAGAIN.TEST PASS!\n", errno); + } + } + + if (mq_close(mqdes) != 0) { + printf("mq_close() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + if (mq_unlink(mqname) != 0) { + printf("mq_unlink() failed.errno=%d. TEST FAILED!\n",errno); + goto __errno_handle; + } + + return (0); + +__errno_handle: + return (-1); +} -- Gitee From 889aa43347bac3ccb6a215e7744b33e8b1cc2159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 06:18:30 +0000 Subject: [PATCH 32/34] semaphore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庡笀 <13383954+li-shi-xiao@user.noreply.gitee.com> --- semaphore/gjb_S0100401GN_1.c | 157 ++++++++++++++++++++++++ semaphore/gjb_S0100401GN_2.c | 81 +++++++++++++ semaphore/gjb_S0100402GN_1.c | 155 ++++++++++++++++++++++++ semaphore/gjb_S0100402GN_2.c | 84 +++++++++++++ semaphore/gjb_S0100403GN_1.c | 171 ++++++++++++++++++++++++++ semaphore/gjb_S0100403GN_2.c | 73 ++++++++++++ semaphore/gjb_S0100404GN_1.c | 183 ++++++++++++++++++++++++++++ semaphore/gjb_S0100404GN_2.c | 73 ++++++++++++ semaphore/gjb_S0100405GN.c | 218 ++++++++++++++++++++++++++++++++++ semaphore/gjb_S0100406GN.c | 224 +++++++++++++++++++++++++++++++++++ semaphore/gjb_S0100407GN.c | 159 +++++++++++++++++++++++++ semaphore/gjb_S0100408GN.c | 152 ++++++++++++++++++++++++ semaphore/gjb_S0100409GN.c | 121 +++++++++++++++++++ semaphore/gjb_S0100410GN.c | 172 +++++++++++++++++++++++++++ 14 files changed, 2023 insertions(+) create mode 100644 semaphore/gjb_S0100401GN_1.c create mode 100644 semaphore/gjb_S0100401GN_2.c create mode 100644 semaphore/gjb_S0100402GN_1.c create mode 100644 semaphore/gjb_S0100402GN_2.c create mode 100644 semaphore/gjb_S0100403GN_1.c create mode 100644 semaphore/gjb_S0100403GN_2.c create mode 100644 semaphore/gjb_S0100404GN_1.c create mode 100644 semaphore/gjb_S0100404GN_2.c create mode 100644 semaphore/gjb_S0100405GN.c create mode 100644 semaphore/gjb_S0100406GN.c create mode 100644 semaphore/gjb_S0100407GN.c create mode 100644 semaphore/gjb_S0100408GN.c create mode 100644 semaphore/gjb_S0100409GN.c create mode 100644 semaphore/gjb_S0100410GN.c diff --git a/semaphore/gjb_S0100401GN_1.c b/semaphore/gjb_S0100401GN_1.c new file mode 100644 index 0000000..84cf2a8 --- /dev/null +++ b/semaphore/gjb_S0100401GN_1.c @@ -0,0 +1,157 @@ +/********************************************************************************************************* +** +** 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中。 + +#include +#include +#include + +#define __USE_GNU +#include +#include + +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); + + pthread_attr_setschedpolicy(&thread_attr1_sem_4011, SCHED_RR); // 程的调度策略:实时、轮转法;返回为0,成功 + pthread_attr_setinheritsched(&thread_attr1_sem_4011, PTHREAD_EXPLICIT_SCHED); // 线程调度策略的继承性:显示指定自己的调度策略和调度参数 + pthread_attr_setschedparam(&thread_attr1_sem_4011, &sched1); // 线程的调度参数 + + 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); + + status_sem_4011 = sem_init(&sem_sem_4011, 0, 1); // 初始化一个匿名信号量值为1 + if (status_sem_4011 != 0) + { + printf("sem_init() is failed!\n"); + goto __errno_handle; + } + else + { + printf("creat sem_bin success!\n"); + } + + ret1_sem_4011 = pthread_create(&task1_t, &thread_attr1_sem_4011, (void *)task1_fsemaphore401, NULL); + + if (ret1_sem_4011 == 0) + { + printf("task1_f_create success!\n"); + } + else + { + printf("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); + if (ret2_sem_4011 == 0) + { + printf("task2_f_create success!\n"); + } + else + { + printf("task2_f_create fail!\n"); + goto __errno_handle; + } + //sleep(2); + binding(task2_t, 0); + + pthread_join(task1_t, NULL); + pthread_join(task2_t, NULL); + printf("pass\n"); + return (0); + + +__errno_handle: + return (-1); +} + +void *task1_fsemaphore401(void *arg) +{ + printf("task1_f gain binary sem success!\n"); + sem_wait(&sem_sem_4011); + sem_post(&sem_sem_4011); + sem_getvalue(&sem_sem_4011, &sval_sem_4011); + printf("sval = %d\n", sval_sem_4011);//1 + return (NULL); + +} + +void *task2_fsemaphore401(void *arg) +{ + printf("task2_f gain binary sem success!\n"); + sem_wait(&sem_sem_4011); + sem_getvalue(&sem_sem_4011, &sval_sem_4011); + printf("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); +} + +int pthread_setaffinity_np(pthread_t __th, size_t __cpusetsize, const cpu_set_t *__cpuset) +{ + return 0; +} diff --git a/semaphore/gjb_S0100401GN_2.c b/semaphore/gjb_S0100401GN_2.c new file mode 100644 index 0000000..83e6487 --- /dev/null +++ b/semaphore/gjb_S0100401GN_2.c @@ -0,0 +1,81 @@ +/********************************************************************************************************* +** +** 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 + +#define SEM_BINARY 1 +#define PTHREAD_WAITQ_PRIO 1 + +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 )) { + printf( "open sem_binary error!\n"); + goto __errno_handle; + + } else { + printf( "open sem_binary success!\n"); + } + + /* 获取信号量的值 */ + if (sem_getvalue(mysemp, &sval) == -1) { + printf( "sem_getvalue error!\n"); + sem_close(mysemp); + goto __errno_handle; + + } else { + printf( "sem_getvalue success!\n"); + printf("sval = %d\n", sval); + } + + /* 关闭命名信号量 */ + ret = sem_close(mysemp); + if(ret != 0 ) { + printf( "close sem_bin error!\n"); + goto __errno_handle; + + } else { + printf( "close sem_bin success!\n"); + } + + /* 删除信号量 */ + ret = sem_unlink(semname); + if(ret != 0 ) { + printf( "unlink sem_bin error!\n"); + goto __errno_handle; + + } else { + printf( "unlink sem_bin success!\n"); + } + + return (0); + +__errno_handle: + return (-1); +} diff --git a/semaphore/gjb_S0100402GN_1.c b/semaphore/gjb_S0100402GN_1.c new file mode 100644 index 0000000..1bac1fa --- /dev/null +++ b/semaphore/gjb_S0100402GN_1.c @@ -0,0 +1,155 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100402GN_1.c +** +** 文件创建日期: 2021 年 1 月 11 日 +** +** 描 述: 二值信号量删除测试 +*********************************************************************************************************/ +//编译:gcc -g gjb_S0100402GN_1.c -o S0100402GN_1 -lpthread +//运行:sudo ./S0100402GN_1 + +#include +#include +#include +#include + +#define __USE_GNU +#include +#include + +#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) { + printf("sem_init() is failed!\n"); + goto __errno_handle; + } else { + printf( "creat sem_bin success!\n"); + } + + ret1_sem_4021=pthread_create (&task1_t,&thread_attr1_sem_4021,task1_fsemaphore402,NULL); + if(ret1_sem_4021 == 0) { + printf( "task1_f_create success!\n"); + } else { + printf( "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) { + printf( "task2_f_create success!\n"); + } else { + printf( "task2_f_create fail!\n"); + goto __errno_handle; + } + binding(task2_t, 0); + + if(pthread_join(task1_t, NULL) != 0) { + printf("Error in pthread_join1()\n"); + goto __errno_handle; + } + + /* 调用pthread_join等待任务终止 */ + if(pthread_join(task2_t, NULL) != 0) { + printf("Error in pthread_join2()\n"); + goto __errno_handle; + } + + ret_sem_4021=sem_destroy (&sem_sem_4021); + if(ret_sem_4021 != 0) { + printf("delete sem_bin failed!\n"); + goto __errno_handle; + + } else { + printf( "delete sem_bin success!\n"); + } + + //printf("pass\n"); + return (0); + +__errno_handle: + return (-1); +} + +void *task1_fsemaphore402 (void *arg) +{ + printf("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); + printf("sval = %d\n", sval_sem_4021); + sleep(1); + + return (NULL); +} + +void *task2_fsemaphore402 (void *arg) +{ + printf("task2_f gain binary sem success!\n"); + sem_wait(&sem_sem_4021); + sem_getvalue(&sem_sem_4021,&sval_sem_4021); + printf("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/semaphore/gjb_S0100402GN_2.c b/semaphore/gjb_S0100402GN_2.c new file mode 100644 index 0000000..4c8bb61 --- /dev/null +++ b/semaphore/gjb_S0100402GN_2.c @@ -0,0 +1,84 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100402GN_2.c +** +** 文件创建日期: 2021 年 1 月 11 日 +** +** 描 述: 二值信号量删除测试 +*********************************************************************************************************/ +//编译:gcc -g gjb_S0100402GN_2.c -o S0100402GN_2 -lpthread +//运行:./S0100402GN_2 + +#include +#include +#include +#include +#include +#include + +#define SEM_BINARY 1 +#define PTHREAD_WAITQ_PRIO 1 + +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 )) { + printf( "open sem_binary error!\n"); + goto __errno_handle; + + } else { + printf( "open sem_binary success!\n"); + + } + /* 获取信号量的值 */ + if( sem_getvalue(mysemp, &sval) == -1 ) { + printf( "sem_getvalue error!\n"); + sem_close(mysemp); + goto __errno_handle; + + } else { + printf( "sem_getvalue success!\n"); + printf("sval = %d\n", sval); + } + + + /* 关闭命名信号量 */ + ret = sem_close(mysemp); + if(ret != 0 ) { + printf( "close sem_bin error!\n"); + goto __errno_handle; + + } else { + printf( "close sem_bin success!\n"); + + } + + /* 删除信号量 */ + ret = sem_unlink(semname); + if(ret != 0 ) { + printf( "unlink sem_bin error!\n"); + goto __errno_handle; + + } else { + printf( "unlink sem_bin success!\n"); + } + + return (0); + + +__errno_handle: + return (-1); + +} diff --git a/semaphore/gjb_S0100403GN_1.c b/semaphore/gjb_S0100403GN_1.c new file mode 100644 index 0000000..11bd843 --- /dev/null +++ b/semaphore/gjb_S0100403GN_1.c @@ -0,0 +1,171 @@ +/********************************************************************************************************* +** +** 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 + +#define __USE_GNU +#include +#include + +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); + +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); + pthread_attr_setinheritsched(&thread_attr1_sem_4031,PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedparam(&thread_attr1_sem_4031,&sched1); + pthread_attr_setschedpolicy(&thread_attr2_sem_4031,SCHED_RR); + pthread_attr_setinheritsched(&thread_attr2_sem_4031,PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedparam(&thread_attr2_sem_4031,&sched2); + pthread_attr_setschedpolicy(&thread_attr3_sem_4031,SCHED_RR); + pthread_attr_setinheritsched(&thread_attr3_sem_4031,PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedparam(&thread_attr3_sem_4031,&sched3); + + status_sem_4031 = sem_init (&sem_sem_4031, 0, 2); + if (status_sem_4031 != 0) { + printf("creat sem_cnt isfailed!\n"); + goto __errno_handle; + } else { + printf( "creat sem_cnt success!\n"); + } + + ret1_sem_4031 = pthread_create (&task1_t, &thread_attr1_sem_4031, task1_fsemaphore403, NULL); + if (ret1_sem_4031 == 0) { + printf( "task1_f_create success!\n"); + } else { + printf( "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) { + printf( "task2_f_create success!\n"); + } else { + printf( "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) { + printf( "task3_f_create success!\n"); + } else { + printf( "task3_f_create fail!\n"); + goto __errno_handle; + } + + binding(task3_t, 0); + + if (pthread_join(task1_t, NULL) != 0) { + printf("Error in pthread_join1()\n"); + goto __errno_handle; + } + + if (pthread_join(task2_t, NULL) != 0) { + printf("Error in pthread_join2()\n"); + goto __errno_handle; + } + + if (pthread_join(task3_t, NULL) != 0) { + printf("Error in pthread_join3()\n"); + goto __errno_handle; + } + printf("pass\n"); + + return (0); + +__errno_handle: + return (-1); +} + +void *task1_fsemaphore403 (void *arg) +{ + printf("task1_f gain sem success!\n"); + sem_wait(&sem_sem_4031); + sem_post(&sem_sem_4031); + sem_getvalue(&sem_sem_4031,&sval_sem_4031); + printf("sval = %d\n", sval_sem_4031); + + return (NULL); +} + +void *task2_fsemaphore403 (void *arg) +{ + printf("task2_f gain sem success!\n"); + sem_wait(&sem_sem_4031); + sem_getvalue(&sem_sem_4031,&sval_sem_4031); + printf("sval = %d\n", sval_sem_4031); + + return (NULL); +} + +void *task3_fsemaphore403 (void *arg) +{ + printf("task3_f gain sem success!\n"); + sem_wait(&sem_sem_4031); + sem_getvalue(&sem_sem_4031,&sval_sem_4031); + printf("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; +} \ No newline at end of file diff --git a/semaphore/gjb_S0100403GN_2.c b/semaphore/gjb_S0100403GN_2.c new file mode 100644 index 0000000..522ea6d --- /dev/null +++ b/semaphore/gjb_S0100403GN_2.c @@ -0,0 +1,73 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100403GN_2.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 计数信号量创建测试 +*********************************************************************************************************/ + +//gcc -g gjb_S0100403GN_2.c -o S0100403GN_2 -lpthread +//./S0100403GN_2 + +#include +#include +#include +#include +#include +#include +#include "7714types.h" +#include + +#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 )) { + printf( "open sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "open sem_cnt success!\n"); + } + + /* 关闭命名信号量 */ + ret = sem_close(mysemp); + if(ret != 0 ) { + printf( "close sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "close sem_cnt success!\n"); + + } + /* 删除信号量 */ + ret = sem_unlink(semname); + if(ret != 0 ) { + printf( "unlink sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "unlink sem_cnt success!\n"); + } + + return (0); + +__errno_handle: + return (-1); +} diff --git a/semaphore/gjb_S0100404GN_1.c b/semaphore/gjb_S0100404GN_1.c new file mode 100644 index 0000000..c19e4a9 --- /dev/null +++ b/semaphore/gjb_S0100404GN_1.c @@ -0,0 +1,183 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100404GN_1.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 计数信号量删除测试 +*********************************************************************************************************/ + +//编译:gcc -g gjb_S0100404GN_1.c -o S0100404GN_1 -lpthread +//运行: sudo ./S0100404GN_1 + +#include +#include +#include +#include + +#define __USE_GNU +#include +#include + +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);//程的调度策略:实时、轮转法;返回为0,成功 + 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);//程的调度策略:实时、轮转法;返回为0,成功 + 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);//程的调度策略:实时、轮转法;返回为0,成功 + pthread_attr_setinheritsched(&thread_attr3_sem_4041,PTHREAD_EXPLICIT_SCHED);//线程调度策略的继承性:显示指定自己的调度策略和调度参数 + pthread_attr_setschedparam(&thread_attr3_sem_4041,&sched3);//线程的调度参数 + + status_sem_4041 = sem_init (&sem_sem_4041,0,2);//初始化一个匿名信号量值为1 + if(status_sem_4041 != 0) { + printf("sem_init() is failed errno = %d!\n", errno); + goto __errno_handle; + + } else { + printf( "creat sem_cnt success!\n"); + } + + ret1_sem_4041 = pthread_create (&task1_t,&thread_attr1_sem_4041,task1_fsemaphore404,NULL); + if (ret1_sem_4041 == 0) { + printf( "task1_f_create success!\n"); + } else { + printf( "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) { + printf( "task2_f_create success!\n"); + } else { + printf( "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) { + printf( "task3_f_create success!\n"); + } else { + printf( "task3_f_create fail errno = %d!\n", errno); + goto __errno_handle; + } + + binding(task3_t, 0); + + if (pthread_join(task1_t, NULL) != 0) { + printf("Error in pthread_join1()\n"); + } + + if (pthread_join(task2_t, NULL) != 0) { + printf("Error in pthread_join2()\n"); + goto __errno_handle; + } + + if (pthread_join(task3_t, NULL) != 0) { + printf("Error in pthread_join3()\n"); + goto __errno_handle; + } + + ret_sem_4041 = sem_destroy (&sem_sem_4041); + if (ret_sem_4041 != 0) { + printf("delete sem_cnt failed!\n"); + goto __errno_handle; + + } else { + printf( "delete sem_cnt success!\n"); + } + //printf("pass\n"); + + return (0); + +__errno_handle: + return (-1); +} + +void *task1_fsemaphore404 (void *arg) +{ + printf("task1_f gain sem success!\n"); + sem_wait(&sem_sem_4041);//等待信号量,将信号量减一;信号量为0时,将后来线程放入阻塞队列 + sem_post(&sem_sem_4041);//信号量加一,释放信号量守护的资源 + sem_getvalue(&sem_sem_4041,&sval_sem_4041);//获取信号量值 + printf("sval = %d\n", sval_sem_4041);//1 + + return (NULL); +} + +void *task2_fsemaphore404 (void *arg) +{ + printf("task2_f gain sem success!\n"); + sem_wait(&sem_sem_4041); + sem_getvalue(&sem_sem_4041,&sval_sem_4041); + printf("sval = %d\n", sval_sem_4041); + + return (NULL); +} + +void *task3_fsemaphore404 (void *arg) +{ + printf("task3_f gain sem success!\n"); + sem_wait(&sem_sem_4041); + sem_getvalue(&sem_sem_4041,&sval_sem_4041); + printf("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);//成功返回0 +} + +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/semaphore/gjb_S0100404GN_2.c b/semaphore/gjb_S0100404GN_2.c new file mode 100644 index 0000000..2e19889 --- /dev/null +++ b/semaphore/gjb_S0100404GN_2.c @@ -0,0 +1,73 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100404GN_1.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 计数信号量删除测试 +*********************************************************************************************************/ + +//gcc -g gjb_S0100404GN_2.c -o S0100404GN_2 -lpthread +//./S0100404GN_2 + +#include +#include +#include +#include +#include +#include + +#define SEM_COUNTING 1 +#define PTHREAD_WAITQ_PRIO 1 + +int main(int argc, char **argv) +{ + sem_t *mysemp; + char semname[50]; + int ret; + + /* sem_opensem_open创建或打开命名的计数信号量 */ + mysemp = sem_open(semname, O_CREAT, 0777, 1, SEM_COUNTING, PTHREAD_WAITQ_PRIO); + if (( mysemp == SEM_FAILED ) || (mysemp == NULL )) { + printf( "open sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "open sem_cnt success!\n"); + + } + + /* 关闭命名信号量 */ + ret = sem_close(mysemp); + if(ret != 0 ) { + printf( "close sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "close sem_cnt success!\n"); + + } + /* 删除信号量 */ + ret = sem_unlink(semname); + if(ret != 0 ) { + printf( "unlink sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "unlink sem_cnt success!\n"); + + } + + return (0); + + +__errno_handle: + return (-1); + +} diff --git a/semaphore/gjb_S0100405GN.c b/semaphore/gjb_S0100405GN.c new file mode 100644 index 0000000..cb45b5c --- /dev/null +++ b/semaphore/gjb_S0100405GN.c @@ -0,0 +1,218 @@ +/********************************************************************************************************* +** +** 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 + + +#define __USE_GNU +#include +#include + +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); + + status=sem_init (&sem_406,0,0); + if (status != 0) { + printf("sem_init() isfailed! errno = %d\n", errno); + goto __errno_handle; + + } else { + printf( "creat sem_bin success!\n"); + } + + /* 创建任务A */ + if (pthread_create (&task1_t,&thread_attr1_406, task_Asemaphore405, NULL) != 0) { + printf("pthread_create task_A error = %d!\n", errno); + goto __errno_handle; + + } else { + printf( "pthread_createA success!\n"); + } + + binding(task1_t, 0); + + /* 创建任务B */ + if (pthread_create(&task2_t,&thread_attr2_406, task_Bsemaphore405, NULL) != 0) { + printf("pthread_create2 task_B error = %d!\n", errno); + goto __errno_handle; + + } else { + printf( "pthread_createB success!\n"); + } + + binding(task2_t, 0); + + sleep(2); + + /*主任务调用sem_post*/ + ret = sem_post(&sem_406); + if (ret != 0) { + printf( "sem_post fail errno = %d!\n", errno); + goto __errno_handle; + + } else { + printf( "sem_post success!\n"); + } + + /* 调用pthread_join等待任务终止 */ + if (pthread_join(task1_t, NULL) != 0) { + printf("Error in pthread_join1() errno = %d\n", errno); + goto __errno_handle; + } + + /* 调用pthread_join等待任务终止*/ + if (pthread_join(task2_t, NULL) != 0) { + printf("Error in pthread_join2() errno = %d\n", errno); + goto __errno_handle; + } + + /* 关闭信号量 */ + ret = sem_close(&sem_406); + ret = sem_destroy(&sem_406); + if (ret != 0) { + printf( "sem_destroy fail!\n"); + printf("sem_destroy"); + goto __errno_handle; + + } else { + printf( "sem_destroy success!\n"); + } + + if (flag_406 != 2) { + printf("task did not get semaphore as PTHREAD_WAITQ_PRIO!\n "); + goto __errno_handle; + } + + if (err_count_406) { + goto __errno_handle; + } + + //printf("pass\n"); + return (0); + +__errno_handle: + return (-1); + +} +void *task_Asemaphore405(void *arg) +{ + int ret = -2; + int ret1 = -2; + /* 锁定信号量 */ + ret = sem_wait(&sem_406); + if( ret == -1 ) { + printf("sem_waitA error!\n"); + err_count_406++; + } else { + printf( "sem_waitA success!\n"); + } + + if(flag_406 == 1) { + flag_406 = 2; + } else { + printf("task_A get semaphore before task_B - error!\n"); + err_count_406++; + } + + ret1=sem_post(&sem_406); + if( ret1 == -1 ) { + printf("sem_postA error!\n"); + err_count_406++; + } else { + printf( "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 ) { + printf("sem_waitB error!\n"); + err_count_406++; + } else { + printf( "sem_waitB success!\n"); + } + + flag_406 = 1; + + ret3 = sem_post(&sem_406); + if( ret3 == -1 ) { + printf("sem_postB error!\n"); + err_count_406++; + } else { + printf( "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/semaphore/gjb_S0100406GN.c b/semaphore/gjb_S0100406GN.c new file mode 100644 index 0000000..92e886c --- /dev/null +++ b/semaphore/gjb_S0100406GN.c @@ -0,0 +1,224 @@ +/********************************************************************************************************* +** +** 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 + +#define __USE_GNU +#include +#include + + +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); + + pthread_t task1_t, task2_t; + + status = sem_init (&sem_405,0,0); + if (status != 0) { + printf("sem_init() isfailed!\n"); + goto __errno_handle; + + } else { + printf( "creat sem_bin success!\n"); + } + + /* 创建任务B */ + if (pthread_create(&task2_t,&thread_attr2_405, task_Bsemaphore406, NULL) != 0) { + printf("pthread_create task_B error!\n"); + goto __errno_handle; + + } else { + printf( "pthread_createB success!\n"); + } + + binding(task2_t, 0); + sleep(1); + + /* 创建任务A */ + if (pthread_create(&task1_t,&thread_attr1_405, task_Asemaphore406, NULL) != 0) { + printf("pthread_create task_A error!\n"); + goto __errno_handle; + + } else { + printf( "pthread_createA success!\n"); + } + + binding(task1_t, 0); + sleep(1); + + /*主任务调用sem_post*/ + ret = sem_post(&sem_405); + if(ret != 0) { + printf( "sem_post error!\n"); + goto __errno_handle; + + } else { + printf( "sem_post success!\n"); + } + + /* 调用pthread_join等待任务终止 */ + if (pthread_join(task1_t, NULL) != 0) { + printf("Error in pthread_join1()\n"); + goto __errno_handle; + } + + /* 调用pthread_join等待任务终止 */ + if(pthread_join(task2_t, NULL) != 0) { + printf("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) { + printf( "sem_destroy error%d!\n",errno); + goto __errno_handle; + + } else { + printf( "sem_destroy success!\n"); + } + + if(flag_405 != 2) { + printf("task did not get semaphore as PTHREAD_WAITQ_FIFO!\n "); + goto __errno_handle; + } + + if (err_count_405) { + goto __errno_handle; + } + + //printf("pass\n"); + return (0); + +__errno_handle: + return (-1); +} +void *task_Asemaphore406(void *arg) +{ + int ret = -2; + int ret1 = -2; + /* 锟斤拷锟斤拷锟脚猴拷锟斤拷 */ + ret = sem_wait(&sem_405); + if( ret == -1 ) { + printf("sem_waitA error!\n"); + err_count_405++; + + } else { + printf( "sem_waitA success!\n"); + } + + if(flag_405 == 1) { + flag_405 = 2; + + } else { + printf("task_A get semaphore before task_B - error!\n"); + } + + ret1=sem_post(&sem_405); + if( ret1 == -1 ) { + printf("sem_postA error!\n"); + err_count_405++; + + } else { + printf( "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 ) { + printf("sem_waitB error!\n"); + err_count_405++; + + } else { + printf( "sem_waitB success!\n"); + } + + flag_405 = 1; + + ret3 = sem_post(&sem_405); + if( ret3 == -1 ) { + printf("sem_postB error!\n"); + err_count_405++; + + } else { + printf( "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/semaphore/gjb_S0100407GN.c b/semaphore/gjb_S0100407GN.c new file mode 100644 index 0000000..e04839e --- /dev/null +++ b/semaphore/gjb_S0100407GN.c @@ -0,0 +1,159 @@ +/********************************************************************************************************* +** +** 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 + +#define __USE_GNU +#include +#include + +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); + + status_407 = sem_init (&sem_407,0,1); + if (status_407 != 0) { + printf("sem_init() failed!\n"); + goto __errno_handle; + } else { + printf( "creat sem_bin success!\n"); + } + + ret_407 = sem_getvalue(&sem_407,&sval_407); + if (ret_407 != 0) { + printf("sem_getvalue failed!\n"); + goto __errno_handle; + } else { + printf( "sem_getvalue success!\n"); + } + + ret1_407 = pthread_create (&task1_t,&thread_attr1_407,task1_fsemaphore407,NULL); + if (ret1_407 != 0) { + printf("task1_f failed!\n"); + goto __errno_handle; + } else { + printf( "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) { + printf("task2_f failed!\n"); + goto __errno_handle; + } else { + printf( "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 (0); + +__errno_handle: + return (-1); +} +void *task1_fsemaphore407 (void *arg) +{ + ret3_407 = sem_trywait(&sem_407); + if(ret3_407 != 0) { + printf("task1_f sem_trywait failed!\n"); + err_count_407++; + } else { + printf( "task1_f sem_trywait success!\n"); + } + + sem_getvalue(&sem_407,&sval_407); + printf("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) { + printf("task2_f sem_trywait failed!\n"); + err_count_407++; + } else { + printf( "task2_f sem_trywait success!\n"); + } + + sem_getvalue(&sem_407,&sval_407); + printf("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/semaphore/gjb_S0100408GN.c b/semaphore/gjb_S0100408GN.c new file mode 100644 index 0000000..1532509 --- /dev/null +++ b/semaphore/gjb_S0100408GN.c @@ -0,0 +1,152 @@ +/********************************************************************************************************* +** +** 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 + +#define __USE_GNU +#include +#include + +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); + + status_408 = sem_init (&sem_408,0,1); + if(status_408 != 0) { + printf("sem_init() is failed!\n"); + goto __errno_handle; + } else { + printf( "creat sem_bin success!\n"); + } + + ret1_408 = pthread_create (&task1_t,&thread_attr1_408,task1_fsemaphore408,NULL); + if( ret1_408 == 0 ) { + printf( "task1_f_create success!\n"); + } else { + printf( "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) { + printf( "task2_f_create success!\n"); + } else { + printf( "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 (0); + +__errno_handle: + return (-1); +} + +void *task1_fsemaphore408 (void *arg) +{ + ret3_408 = sem_wait(&sem_408); + if(ret3_408!=0) { + printf("task1_f sem_wait failed!\n"); + err_count_408++; + } else { + printf( "task1_f sem_wait success!\n"); + } + + sem_getvalue(&sem_408,&sval_408); + printf("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) { + printf("task2_f sem_wait failed!\n"); + err_count_408++; + } else { + printf( "task2_f sem_wait success!\n"); + } + + sem_getvalue(&sem_408,&sval_408); + printf("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/semaphore/gjb_S0100409GN.c b/semaphore/gjb_S0100409GN.c new file mode 100644 index 0000000..e0cada0 --- /dev/null +++ b/semaphore/gjb_S0100409GN.c @@ -0,0 +1,121 @@ +/********************************************************************************************************* +** +** 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 + +#define ERROR_NONE 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) { + printf("create thread failed.\n"); + goto __errno_handle; + } + + pthread_join(tid, NULL); + + sem_destroy(&sem_409); + + if (count_409 != 2) { + goto __errno_handle; + } + + return (0); + +__errno_handle: + return (-1); +} +/* + * 时间 time 自加 ms 毫秒 + * void time_add_ms(struct timeval *time, uint ms) + */ +void time_add_ms(struct timeval *time, unsigned int 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); 头文件 + 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; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + printf("t.tv_sec = %ld\n", t.tv_sec); + printf("t.tv_nsec = %ld\n", t.tv_nsec); + + while (1) { + int semvalue = -1; + sem_getvalue(&sem_409, &semvalue); + printf("will call sem_timedwait, semvalue = %d\n", semvalue); + + /* + * 如果sem 信号量值>0,则sem_timedwait 立即返回;如果sem 信号量值≤0,则 sem_timedwait 阻塞等待 TIMEOUT秒后再返回。 + */ + int ret = sem_timedwait(&sem_409, &t); + printf("over call sem_timedwait, ret = %d\n", ret); + if (ret == -1) { + sem_getvalue(&sem_409, &semvalue); + printf("pthread() will return\n"); + count_409++; + return NULL; + + } else { + count_409++; + printf("sem_timedwait success!\n"); + } + } + + return (NULL); +} diff --git a/semaphore/gjb_S0100410GN.c b/semaphore/gjb_S0100410GN.c new file mode 100644 index 0000000..c4d8c5e --- /dev/null +++ b/semaphore/gjb_S0100410GN.c @@ -0,0 +1,172 @@ +/********************************************************************************************************* +** +** 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 + +#define __USE_GNU +#include +#include + +#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); + + status_410 = sem_init (&sem_410,0,2); + if (status_410 != 0) { + printf("sem_init() isfailed!\n"); + goto __errno_handle; + } else { + printf( "creat sem_cnt succes!\n"); + } + + ret1_410 = pthread_create (&task1_t,&thread_attr1_410,task1_fsemaphore410,NULL); + if (ret1_410 == 0) { + printf( "task1_f_create success!\n"); + } else { + printf( "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) { + printf( "task2_f_create success!\n"); + } else { + printf( "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) { + printf( "task3_f_create success!\n"); + + } else { + printf( "task3_f_create fail!\n"); + goto __errno_handle; + } + + binding(task3_t, 0); + + if (err_count_410) { + goto __errno_handle; + } + + //printf("pass\n"); + return (0); + +__errno_handle: + return (-1); + +} +void *task1_fsemaphore410 (void *arg) +{ + printf("task1_f gain sem success!\n"); + sem_wait(&sem_410); + sem_getvalue(&sem_410,&sval_410); + printf("sval = %d\n", sval_410); + ret_410 = sem_post(&sem_410); + if(ret_410 != 0) { + printf("sem_post failed!\n"); + err_count_410++; + + } else { + printf( "sem_post success!\n"); + } + + sem_getvalue(&sem_410,&sval_410); + printf("sval = %d\n", sval_410); + return (NULL); +} + +void *task2_fsemaphore410 (void *arg) +{ + printf("task2_f gain sem success!\n"); + sem_wait(&sem_410); + sem_getvalue(&sem_410,&sval_410); + printf("sval = %d\n", sval_410); + return (NULL); +} + +void *task3_fsemaphore410 (void *arg) +{ + printf("task3_f gain sem success!\n"); + sem_wait(&sem_410); + sem_getvalue(&sem_410,&sval_410); + printf("sval = %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; +} + -- Gitee From a36c14286de537fa92cd18fc19372240047983b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 12:46:53 +0000 Subject: [PATCH 33/34] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20se?= =?UTF-8?q?maphore/gjb=5FS0100403GN=5F2.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- semaphore/gjb_S0100403GN_2.c | 73 ------------------------------------ 1 file changed, 73 deletions(-) delete mode 100644 semaphore/gjb_S0100403GN_2.c diff --git a/semaphore/gjb_S0100403GN_2.c b/semaphore/gjb_S0100403GN_2.c deleted file mode 100644 index 522ea6d..0000000 --- a/semaphore/gjb_S0100403GN_2.c +++ /dev/null @@ -1,73 +0,0 @@ -/********************************************************************************************************* -** -** GJB 标准测试集 -** -** Copyright All Rights Reserved -** -**--------------文件信息-------------------------------------------------------------------------------- -** -** 文 件 名: gjb_S0100403GN_2.c -** -** 文件创建日期: 2021 年 1 月 12 日 -** -** 描 述: 计数信号量创建测试 -*********************************************************************************************************/ - -//gcc -g gjb_S0100403GN_2.c -o S0100403GN_2 -lpthread -//./S0100403GN_2 - -#include -#include -#include -#include -#include -#include -#include "7714types.h" -#include - -#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 )) { - printf( "open sem_cnt fail!\n"); - goto __errno_handle; - - } else { - printf( "open sem_cnt success!\n"); - } - - /* 关闭命名信号量 */ - ret = sem_close(mysemp); - if(ret != 0 ) { - printf( "close sem_cnt fail!\n"); - goto __errno_handle; - - } else { - printf( "close sem_cnt success!\n"); - - } - /* 删除信号量 */ - ret = sem_unlink(semname); - if(ret != 0 ) { - printf( "unlink sem_cnt fail!\n"); - goto __errno_handle; - - } else { - printf( "unlink sem_cnt success!\n"); - } - - return (0); - -__errno_handle: - return (-1); -} -- Gitee From cd24153e3d7f9726f217dfe966b19fcb12db126b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=88?= <13383954+li-shi-xiao@user.noreply.gitee.com> Date: Mon, 20 Nov 2023 12:48:09 +0000 Subject: [PATCH 34/34] semaphore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 鏉庡笀 <13383954+li-shi-xiao@user.noreply.gitee.com> --- semaphore/gjb_S0100403GN_2.c | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 semaphore/gjb_S0100403GN_2.c diff --git a/semaphore/gjb_S0100403GN_2.c b/semaphore/gjb_S0100403GN_2.c new file mode 100644 index 0000000..81c7e2e --- /dev/null +++ b/semaphore/gjb_S0100403GN_2.c @@ -0,0 +1,70 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100403GN_2.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 计数信号量创建测试 +*********************************************************************************************************/ + +//gcc -g gjb_S0100403GN_2.c -o S0100403GN_2 -lpthread +//./S0100403GN_2 + +#include +#include +#include +#include +#include +#include +#include + +#define SEM_COUNTING 1 +#define PTHREAD_WAITQ_PRIO 1 + +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 )) { + printf( "open sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "open sem_cnt success!\n"); + } + + /* 关闭命名信号量 */ + ret = sem_close(mysemp); + if(ret != 0 ) { + printf( "close sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "close sem_cnt success!\n"); + + } + /* 删除信号量 */ + ret = sem_unlink(semname); + if(ret != 0 ) { + printf( "unlink sem_cnt fail!\n"); + goto __errno_handle; + + } else { + printf( "unlink sem_cnt success!\n"); + } + + return (0); + +__errno_handle: + return (-1); +} -- Gitee