diff --git a/conditional_variable/gjb_S0100901GN_1.c b/conditional_variable/gjb_S0100901GN_1.c new file mode 100644 index 0000000000000000000000000000000000000000..5b07c9b001b62ef74eecb7a08250a46944962192 --- /dev/null +++ b/conditional_variable/gjb_S0100901GN_1.c @@ -0,0 +1,69 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100901GN_1.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 初始化、销毁条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include + +//#include "gjb.h" + +int gjb_S0100901GN_1(int argc, char **argv) +{ + int rc; + + pthread_condattr_t condattr; + pthread_cond_t cond1; + pthread_cond_t cond2; + + /* + * Initialize a condition variable attributes object + */ + if ((rc=pthread_condattr_init(&condattr)) != 0) { + printf("Error at pthread_condattr_init(), rc=%d\n",rc); + goto __errno_handle; + } + + /* + * Initialize cond1 with the default condition variable attributes + */ + if((rc=pthread_cond_init(&cond1,&condattr)) != 0) { + printf("Fail to initialize cond1, rc=%d\n",rc); + printf("Test FAILED\n"); + goto __errno_handle; + } + + /* + * Initialize cond2 with NULL attributes + */ + if((rc=pthread_cond_init(&cond2,NULL)) != 0) { + printf("Fail to initialize cond2, rc=%d\n",rc); + printf("Test FAILED。\n"); + goto __errno_handle; + } + printf("..................................................[(0)]\n \n \n"); + + return (0); + + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); + + +} + + + + + diff --git a/conditional_variable/gjb_S0100901GN_2.c b/conditional_variable/gjb_S0100901GN_2.c new file mode 100644 index 0000000000000000000000000000000000000000..250a0d9f3cb04139a6675d3f098229e4cb05dcd1 --- /dev/null +++ b/conditional_variable/gjb_S0100901GN_2.c @@ -0,0 +1,43 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100901GN_2.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 初始化、销毁条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include + +//#include "gjb.h" + +int gjb_S0100901GN_2(int argc, char **argv) +{ + int rc = 123; + + pthread_condattr_t condattr; + pthread_cond_t cond1; + + /* + * Initialize cond1 with the default condition variable attributes + */ + if ((rc = pthread_cond_init(&cond1,&condattr)) != EINVAL) { + printf("Fail to initialize cond1, rc=%d\n",rc); + printf("Test FAILED\n"); + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} diff --git a/conditional_variable/gjb_S0100901GN_3.c b/conditional_variable/gjb_S0100901GN_3.c new file mode 100644 index 0000000000000000000000000000000000000000..7b00b090b68930ab02034e3ffb960a04885ac575 --- /dev/null +++ b/conditional_variable/gjb_S0100901GN_3.c @@ -0,0 +1,81 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100901GN_3.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 初始化、销毁条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +//#include "gjb.h" + +#define TEST_UINT_MAX 3100 +pthread_cond_t cond_cv_9013[3100]; + + +int gjb_S0100901GN_3(int argc, char **argv) +{ + int rc; + int i; + + /* + * 初始化cond数组 + */ + memset(cond_cv_9013, 0, 3100*sizeof(pthread_cond_t)); + + for (i = 0; ; i++) { + + /* + * 条件变量初始化 + */ + rc = pthread_cond_init(&(cond_cv_9013[i]),NULL); + + if (rc != 0) { + if (rc == EAGAIN){ + printf("Test pass。 times %d\n",i); + break; + } else{ + printf("test failed.pthread_cond_init should return EAGAIN,but return %d,times %d",rc,i); + for(;i>=0;i--) { + pthread_cond_destroy(&(cond_cv_9013[i])); + } + goto __errno_handle; + } + + } else { + if(i>TEST_UINT_MAX) { + printf("pthread_cond_init sill return successful even i>UINT_MAX,i=%d. test failed.\n",i); + for(;i>=0;i--) { + pthread_cond_destroy(&(cond_cv_9013[i])); + } + goto __errno_handle; + } + } + } + + for(;i>=0;i--) { + if (rc != 0) { + pthread_cond_destroy(&(cond_cv_9013[i])); /* 条件变量的删除 */ + } + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} + + + diff --git a/conditional_variable/gjb_S0100901GN_4.c b/conditional_variable/gjb_S0100901GN_4.c new file mode 100644 index 0000000000000000000000000000000000000000..274222bc7f0cff0875b2e0e4a5d10453909b9a52 --- /dev/null +++ b/conditional_variable/gjb_S0100901GN_4.c @@ -0,0 +1,99 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100901GN_4.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 初始化、销毁条件变量功能测试 +*********************************************************************************************************/ +#include +#include + +//#include "gjb.h" + +pthread_cond_t cond1_cv_9014, cond2_cv_9014; +pthread_cond_t cond3_cv_9014 = PTHREAD_COND_INITIALIZER; + +int gjb_S0100901GN_4(int argc, char **argv) +{ + int rc; + pthread_condattr_t condattr; + + + /* + * Initialize a condition variable attribute object + */ + if((rc=pthread_condattr_init(&condattr)) != 0) { + printf("Error at pthread_condattr_init(), rc=%d\n",rc); + goto __errno_handle; + } + + /* + * Initialize cond1 with the default condition variable attribute + */ + if((rc=pthread_cond_init(&cond1_cv_9014,&condattr)) != 0) { + printf("Fail to initialize cond1, rc=%d\n",rc); + goto __errno_handle; + } + + /* + * Initialize cond2 with NULL attributes + */ + if((rc=pthread_cond_init(&cond2_cv_9014,NULL)) != 0) { + printf("Fail to initialize cond2, rc=%d\n",rc); + goto __errno_handle; + } + + /* + * Destroy the condition variable attribute object + */ + if((rc=pthread_condattr_destroy(&condattr)) != 0) { + printf("Error at pthread_condattr_destroy(), rc=%d\n",rc); + goto __errno_handle; + } + + /* + * Destroy cond1 + */ + if((rc=pthread_cond_destroy(&cond1_cv_9014)) != 0) { + printf("Fail to destroy cond1, rc=%d\n",rc); + printf("Test FAILED\n"); + goto __errno_handle; + } + + /* + * Destroy cond2 + */ + if((rc=pthread_cond_destroy(&cond2_cv_9014)) != 0) { + printf("Fail to destroy cond2, rc=%d\n",rc); + printf("Test FAILED\n"); + goto __errno_handle; + } + + /* + * Destroy cond3 + */ + if((rc=pthread_cond_destroy(&cond3_cv_9014)) != 0) { + printf("Fail to destroy cond3, rc=%d\n",rc); + printf("Test FAILED\n"); + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} + + + + diff --git a/conditional_variable/gjb_S0100901GN_5.c b/conditional_variable/gjb_S0100901GN_5.c new file mode 100644 index 0000000000000000000000000000000000000000..1f48df815247c8de816ffe734aa8b2d33f3ff8cb --- /dev/null +++ b/conditional_variable/gjb_S0100901GN_5.c @@ -0,0 +1,118 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100901GN_5.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 初始化、销毁条件变量功能测试 +*********************************************************************************************************/ +#define __SYLIXOS_KERNEL +#include +#include +#include +#include +#include +#include + +//#include "gjb.h" + +#define TEST "-2" +#define FUNCTION "pthread_cond_destroy" +#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " + +static int err_count_cv_9015; +/* cond used by the two threads */ +pthread_cond_t cond_cv_9015 = PTHREAD_COND_INITIALIZER; + +/* cond used by the two threads */ +pthread_mutex_t mutex_cv_9015 = PTHREAD_MUTEX_INITIALIZER; + +void * thread_cv_9015(void *tmp) +{ + int rc = 0; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex_cv_9015); + if(rc != 0) { + printf(ERROR_PREFIX "pthread_mutex_lock\n"); + err_count_cv_9015++; + return (void *)-1; + } + + /* + * Wait on the cond var. This will not return, as nobody signals + */ + rc = pthread_cond_wait(&cond_cv_9015, &mutex_cv_9015); + if(rc != 0) { + printf(ERROR_PREFIX "pthread_cond_wait\n"); + err_count_cv_9015++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex_cv_9015); + if(rc != 0) { + printf(ERROR_PREFIX "pthread_mutex_unlock\n"); + err_count_cv_9015++; + return (void *)-1; + } + + return NULL; +} + +int gjb_S0100901GN_5(int argc, char **argv) +{ + pthread_t low_id; + int rc = 0; + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread_cv_9015, NULL); + if(rc != 0) { + printf(ERROR_PREFIX "pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(5); + + + /* + * Try to destroy the cond var. This should return an error + */ + rc = pthread_cond_destroy(&cond_cv_9015); + if(rc != EBUSY) { + printf(ERROR_PREFIX "Test failed: Expected %d(EBUSY) got %d.\n", EBUSY, rc); + goto __errno_handle; + } + + //GJB_PRT_INFO("Test PASS\n"); + printf("Test PASS\n"); + + rc = pthread_cond_signal(&cond_cv_9015); + if(rc != 0) { + printf("pthread_cond_signal\n"); + goto __errno_handle; + } + + if (err_count_cv_9015) { + goto __errno_handle; + } + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} diff --git a/conditional_variable/gjb_S0100901GN_6.c b/conditional_variable/gjb_S0100901GN_6.c new file mode 100644 index 0000000000000000000000000000000000000000..01ca18983a9983c0d5b8826a49ba0b19dc0559d6 --- /dev/null +++ b/conditional_variable/gjb_S0100901GN_6.c @@ -0,0 +1,76 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100901GN_6.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 初始化、销毁条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include + +//#include "gjb.h" + +int gjb_S0100901GN_6(int argc, char **argv) +{ + + pthread_cond_t cond; + int rc; + + /* + * Initialize cond2 with NULL attributes + */ + if((rc=pthread_cond_init(&cond,NULL)) != 0) { + printf("Fail to initialize cond, rc=%d\n",rc); + goto __errno_handle; + } + + if(pthread_cond_destroy(&cond) != 0) { + printf("Fail to destroy cond\n"); + printf("Test FAILED\n"); + + goto __errno_handle; + } + + /* + * Try to destroy a NULL condition variable attributes object using pthread_cond_destroy() + * It should return EINVAL + */ + if((rc=pthread_cond_destroy(&cond)) == EINVAL) { + printf("Test PASSED\n"); + + } else { + printf("Test FAILED: *NOTE: Expect %d(EINVAL), but return %d.\n", EINVAL, rc); + goto __errno_handle; + } + + + if((rc=pthread_cond_destroy(NULL)) == EINVAL) { + printf("Test PASSED when para is null.\n"); + + } else { + printf("Test FAILED: when para is null.*NOTE: Expect %d(EINVAL), but return %d.\n", EINVAL, rc); + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} + + + + diff --git a/conditional_variable/gjb_S0100902GN_1.c b/conditional_variable/gjb_S0100902GN_1.c new file mode 100644 index 0000000000000000000000000000000000000000..1394d405b356646ca07367a091bd983ebb0cd977 --- /dev/null +++ b/conditional_variable/gjb_S0100902GN_1.c @@ -0,0 +1,107 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100902GN_1.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 以永久阻塞方式等待条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include + +//#include "gjb.h" + +static int err_count_cv_9021; + +pthread_cond_t cond_cv_9021 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ + + +pthread_mutex_t mutex_cv_9021 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread_cv_9021(void *tmp) +{ + int rc = 0; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex_cv_9021); + if(rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + err_count_cv_9021++; + return (void *)-1; + } + + /* + * Wait on the cond var. This will not return, as nobody signals + */ + rc = pthread_cond_wait(&cond_cv_9021, &mutex_cv_9021); + if(rc == 0) { + printf("test pass,pthread_cond_wait return 0\n"); + + } else { + printf("test failed.pthread_cond_wait should return 0 , but return %d\n",rc); + err_count_cv_9021++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex_cv_9021); + if(rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + err_count_cv_9021++; + return (void *)-1; + } + + return NULL; +} + +// int gjb_S0100902GN_1(int argc, char **argv) +int gjb_S0100902GN_1(int argc, char **argv) +{ + pthread_t low_id; + int rc = 0; + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread_cv_9021, NULL); + if(rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(2); + + + // pthread_kill(low_id, SIGTERM); + // pthread_kill(low_id, SIGKILL); + pthread_join(low_id, NULL); + if (err_count_cv_9021) { + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} + + + + diff --git a/conditional_variable/gjb_S0100902GN_2.c b/conditional_variable/gjb_S0100902GN_2.c new file mode 100644 index 0000000000000000000000000000000000000000..fa871fb72198d853d59e8f80d4a3df2eadc00d9c --- /dev/null +++ b/conditional_variable/gjb_S0100902GN_2.c @@ -0,0 +1,158 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100902GN_1.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 以永久阻塞方式等待条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include + +//#include "gjb.h" + +static int err_count_cv_9022; + +pthread_cond_t cond1_cv_9022 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ + +pthread_mutex_t mutex1_cv_9022 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread1_cv_9022(void *tmp) +{ + int rc = 0; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex1_cv_9022); + if(rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + err_count_cv_9022++; + return (void *)-1; + } + + + rc = pthread_cond_wait(&cond1_cv_9022, &cond1_cv_9022); + if (rc == EINVAL) { + printf("test 2 pass,pthread_cond_wait return EINVAL \n"); + + } else { + printf("test 2 failed.pthread_cond_wait should return EINVAL , but return %d\n",rc); + err_count_cv_9022++; + return (void *)-1; + } + + rc = pthread_cond_wait(NULL, &mutex1_cv_9022); + if (rc == EINVAL ) { + printf("test 3 pass,pthread_cond_wait return EINVAL \n"); + + } else { + printf("test 3 failed.pthread_cond_wait should return EINVAL , but return %d\n",rc); + err_count_cv_9022++; + return (void *)-1; + } + + rc = pthread_cond_wait(&cond1_cv_9022,NULL); + if(rc == EINVAL ) { + printf("test 4 pass,pthread_cond_wait return EINVAL \n"); + + } else { + printf("test 4 failed.pthread_cond_wait should return EINVAL , but return %d\n",rc); + err_count_cv_9022++; + return (void *)-1; + } + + rc = pthread_cond_wait(&cond1_cv_9022,&mutex1_cv_9022); + if(rc != 0 ) { + printf("test 5 failed.pthread_cond_wait should return 0 , but return %d\n",rc); + err_count_cv_9022++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex1_cv_9022); + if(rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + err_count_cv_9022++; + return (void *)-1; + } + + return NULL; +} + +int gjb_S0100902GN_2(int argc, char **argv) +{ + pthread_t low_id; + pthread_mutex_t mutex2; + int rc = 0; + + pthread_mutex_init(&mutex2,NULL); + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread1_cv_9022, NULL); + if(rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(2); + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex2); + if(rc != 0) { + printf("test failed.pthread_mutex_lock(2)\n"); + goto __errno_handle; + } + + rc = pthread_cond_wait(&cond1_cv_9022,&mutex2); + if(rc == EINVAL ) { + printf("test 5 pass,pthread_cond_wait return EINVAL \n"); + + } else { + printf("test 5 failed.pthread_cond_wait should return EINVAL , but return %d\n",rc); + goto __errno_handle; + } + + rc = pthread_mutex_unlock(&mutex2); + if(rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + goto __errno_handle; + } + + + + sleep(1); + pthread_kill(low_id, SIGTERM); + + if (err_count_cv_9022) { + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} + + + + diff --git a/conditional_variable/gjb_S0100902GN_3.c b/conditional_variable/gjb_S0100902GN_3.c new file mode 100644 index 0000000000000000000000000000000000000000..41ca6a01d083c7c13a9fe7e0a5de0b7c1906e9d2 --- /dev/null +++ b/conditional_variable/gjb_S0100902GN_3.c @@ -0,0 +1,81 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100902GN_3.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 以永久阻塞方式等待条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include + +//#include "gjb.h" + +static int err_count_cv_9023; + +pthread_cond_t cond2_cv_9023 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ +pthread_mutex_t mutex3_cv_9023 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread2_cv_9023(void *tmp) +{ + int rc = 0; + + /* + * Wait on the cond var. This will not return, as nobody signals + */ + rc = pthread_cond_wait(&cond2_cv_9023, &mutex3_cv_9023); + if (rc == EPERM) { + printf("test pass,pthread_cond_wait return EPERM \n"); + + } else { + printf("test failed.pthread_cond_wait should return EPERM , but return %d\n",rc); + err_count_cv_9023++; + return (void *)-1; + } + + return NULL; +} + +int main(int argc, char **argv) +{ + pthread_t low_id; + int rc = 0; + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread2_cv_9023, NULL); + if(rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(2); + + // pthread_kill(low_id, SIGTERM); + pthread_join(low_id, SIGTERM); + if (err_count_cv_9023) { + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} diff --git a/conditional_variable/gjb_S0100903GN_1.c b/conditional_variable/gjb_S0100903GN_1.c new file mode 100644 index 0000000000000000000000000000000000000000..660c4a684954d9918779aa2237d6b61cfff873fe --- /dev/null +++ b/conditional_variable/gjb_S0100903GN_1.c @@ -0,0 +1,123 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100903GN_1.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 以限时阻塞方式等待条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// #include +// #include +// #include + +// #include "gjb.h" + +#define TIMEOUT 5 + +static int err_count_cv_9031; + +pthread_cond_t cond_cv_9031 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ + +pthread_mutex_t mutex_cv_9031 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread_cv_9031(void *tmp) +{ + int rc = 0; + struct timespec timeout; + struct timespec curtime; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex_cv_9031); + if(rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + err_count_cv_9031++; + return (void *)-1; + } + + + if (clock_gettime(CLOCK_REALTIME, &curtime) != 0) { + printf("Fail to get current time\n"); + err_count_cv_9031++; + return (void *)-1; + } + + timeout.tv_sec = curtime.tv_sec; + + timeout.tv_nsec = curtime.tv_nsec; + + timeout.tv_sec += TIMEOUT; + + rc = pthread_cond_timedwait(&cond_cv_9031, &mutex_cv_9031, &timeout); + if(rc == 0) { + printf("test pass.pthread_cond_timedwait return 0\n"); + + } else { + printf("test failed,pthread_cond_timedwait return %d",rc); + err_count_cv_9031++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex_cv_9031); + if(rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + err_count_cv_9031++; + return (void *)-1; + } + + return NULL; +} + +int gjb_S0100903GN_1(int argc, char **argv) +{ + pthread_t low_id; + int rc = 0; + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread_cv_9031, NULL); + if(rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(2); + + pthread_cond_signal(&cond_cv_9031); + + if (err_count_cv_9031) { + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); + +} diff --git a/conditional_variable/gjb_S0100903GN_2.c b/conditional_variable/gjb_S0100903GN_2.c new file mode 100644 index 0000000000000000000000000000000000000000..c4d84c7069b466080318d503e2a2508d919fc7dd --- /dev/null +++ b/conditional_variable/gjb_S0100903GN_2.c @@ -0,0 +1,201 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100903GN_2.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 以限时阻塞方式等待条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// #include "gjb.h" + +#define TIMEOUT 5 + +static int err_count_cv_9032; + +pthread_cond_t cond1_cv_9032 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ +pthread_mutex_t mutex1_cv_9032 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread1_cv_9032(void *tmp) +{ + int rc = 0; + struct timespec timeout; + struct timespec curtime; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex1_cv_9032); + if(rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + err_count_cv_9032++; + return (void *)-1; + } + + if (clock_gettime(CLOCK_REALTIME, &curtime) != 0) { + printf("Fail to get current time\n"); + err_count_cv_9032++; + return (void *)-1; + } + + timeout.tv_sec = curtime.tv_sec; + + timeout.tv_nsec = curtime.tv_nsec; + + timeout.tv_sec += TIMEOUT; + + /* + * 传入无效的参数类型 + */ + rc = pthread_cond_timedwait(&mutex1_cv_9032, &mutex1_cv_9032, &timeout); + if(rc == EINVAL ) { + printf("test 1 pass,pthread_cond_timedwait return EINVAL \n"); + + } else { + printf("test 1 failed.pthread_cond_timedwait should return EINVAL , but return %d\n",rc); + err_count_cv_9032++; + return (void *)-1; + } + + /* + * 传入无效的参数类型 + */ + rc = pthread_cond_timedwait(&cond1_cv_9032, &cond1_cv_9032 , &timeout); + if(rc == EINVAL ) { + printf("test 2 pass,pthread_cond_timedwait return EINVAL \n"); + + } else { + printf("test 2 failed.pthread_cond_timedwait should return EINVAL , but return %d\n",rc); + err_count_cv_9032++; + return (void *)-1; + } + + rc = pthread_cond_timedwait(NULL, &mutex1_cv_9032, &timeout); + if(rc == EINVAL ) { + printf("test 3 pass,pthread_cond_timedwait return EINVAL \n"); + + } else { + printf("test 3 failed.pthread_cond_timedwait should return EINVAL , but return %d\n",rc); + err_count_cv_9032++; + return (void *)-1; + } + + rc = pthread_cond_timedwait(&cond1_cv_9032, NULL, &timeout); + if(rc == EINVAL ) { + printf("test 4 pass,pthread_cond_timedwait return EINVAL \n"); + + } else { + printf("test 4 failed.pthread_cond_timedwait should return EINVAL , but return %d\n",rc); + err_count_cv_9032++; + return (void *)-1; + } + + rc = pthread_cond_timedwait(&cond1_cv_9032, &mutex1_cv_9032, &timeout); + if(rc != 0 ) { + printf("test failed.pthread_cond_timedwait should return 0 , but return %d\n",rc); + err_count_cv_9032++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex1_cv_9032); + if(rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + err_count_cv_9032++; + return (void *)-1; + } + sleep(2); + + pthread_cond_signal(&cond1_cv_9032); + + return NULL; +} + +int gjb_S0100903GN_2(int argc, char **argv) +{ + pthread_t low_id; + pthread_mutex_t mutex2; + struct timespec timeout; + + struct timespec curtime; + int rc = 0; + + pthread_mutex_init(&mutex2,NULL); + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread1_cv_9032, NULL); + if(rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(3); + + pthread_cond_signal(&cond1_cv_9032); + + if (clock_gettime(CLOCK_REALTIME, &curtime) != 0) { + printf("Fail to get current time\n"); + goto __errno_handle; + } + + timeout.tv_sec = curtime.tv_sec; + + timeout.tv_nsec = curtime.tv_nsec; + + timeout.tv_sec += TIMEOUT; + + rc = pthread_mutex_lock(&mutex2); + if(rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + goto __errno_handle; + } + + rc = pthread_cond_timedwait(&cond1_cv_9032, &mutex2, &timeout); + if(rc == EINVAL ) { + printf("test 5 pass,pthread_cond_timedwait return EINVAL \n"); + + } else { + printf("test 5 failed.pthread_cond_timedwait should return EINVAL , but return %d\n",rc); + goto __errno_handle; + } + + rc = pthread_mutex_unlock(&mutex2); + if(rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + goto __errno_handle; + } + + if (err_count_cv_9032) { + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} diff --git a/conditional_variable/gjb_S0100903GN_3.c b/conditional_variable/gjb_S0100903GN_3.c new file mode 100644 index 0000000000000000000000000000000000000000..cadd7624038f2459ebefc8fdec384799cf514ec9 --- /dev/null +++ b/conditional_variable/gjb_S0100903GN_3.c @@ -0,0 +1,94 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100903GN_3.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 以限时阻塞方式等待条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//#include "gjb.h" + +#define TIMEOUT 5 + +static int err_count_cv_9033 = 0; +pthread_cond_t cond2_cv_9033 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ +pthread_mutex_t mutex3_cv_9033 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread2_cv_9033 (void *tmp) +{ + int rc = 0; + struct timespec timeout; + struct timespec curtime; + + if (clock_gettime(CLOCK_REALTIME, &curtime) != 0) { + printf("Fail to get current time\n"); + return (void *)-1; + } + + timeout.tv_sec = curtime.tv_sec; + + timeout.tv_nsec = curtime.tv_nsec; + + timeout.tv_sec += TIMEOUT; + + rc = pthread_cond_timedwait(&cond2_cv_9033, &mutex3_cv_9033, &timeout); + if (rc == EPERM) { + printf("test pass.pthread_cond_timedwait return EPERM \n"); + } else { + printf("test failed.pthread_cond_timedwait should return EPERM , but return %d\n",rc); + err_count_cv_9033++; + return (void *)-1; + } + + return NULL; +} + +int gjb_S0100903GN_3(int argc, char **argv) +{ + pthread_t low_id; + int rc = 0; + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread2_cv_9033, NULL); + if (rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(2); + + if (err_count_cv_9033) { + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} diff --git a/conditional_variable/gjb_S0100903GN_4.c b/conditional_variable/gjb_S0100903GN_4.c new file mode 100644 index 0000000000000000000000000000000000000000..0179e958928cdec59ea2d3e29d9d89d28935fd2a --- /dev/null +++ b/conditional_variable/gjb_S0100903GN_4.c @@ -0,0 +1,116 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100903GN_4.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 以限时阻塞方式等待条件变量功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// #include "gjb.h" + +#define TIMEOUT 5 + + +static int err_count_cv_9034; + +pthread_cond_t cond3_cv_9034 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ + +pthread_mutex_t mutex4_cv_9034 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread3_cv_9034(void *tmp) +{ + int rc = 0; + struct timespec timeout; + struct timespec curtime; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex4_cv_9034); + if(rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + err_count_cv_9034++; + return (void *)-1; + } + + if (clock_gettime(CLOCK_REALTIME, &curtime) != 0) { + printf("Fail to get current time\n"); + err_count_cv_9034++; + return (void *)-1; + } + + timeout.tv_sec = curtime.tv_sec; + + timeout.tv_nsec = curtime.tv_nsec; + + timeout.tv_sec += TIMEOUT; + + + rc = pthread_cond_timedwait(&cond3_cv_9034, &mutex4_cv_9034, &timeout); + if(rc == ETIMEDOUT) { + printf("test pass.pthread_cond_timedwait returned ETIMEDOUT\n"); + + } else { + printf("test failed.pthread_cond_timedwait should return ETIMEDOUT ,but return %d\n",rc); + err_count_cv_9034++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex4_cv_9034); + if(rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + err_count_cv_9034++; + return (void *)-1; + } + + return NULL; +} + +int gjb_S0100903GN_4(int argc, char **argv) +{ + pthread_t low_id; + int rc = 0; + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread3_cv_9034, NULL); + if(rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(10); + + if (err_count_cv_9034) { + goto __errno_handle; + } + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} diff --git a/conditional_variable/gjb_S0100904GN_1.c b/conditional_variable/gjb_S0100904GN_1.c new file mode 100644 index 0000000000000000000000000000000000000000..a71f14f8d8795a52f612f8919ac1734d1a3282bc --- /dev/null +++ b/conditional_variable/gjb_S0100904GN_1.c @@ -0,0 +1,100 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100904GN_1.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 使用条件变量同时唤醒一个或者多个等待任务/进程功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include + +// #include "gjb.h" + +static int err_count_cv_9041; +pthread_cond_t cond_cv_9041 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ +pthread_mutex_t mutex_cv_9041 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread_cv_9041 (void *tmp) +{ + int rc = 0; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex_cv_9041); + if (rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + err_count_cv_9041++; + return (void *)-1; + } + + /* + * Wait on the cond var. This will not return, as nobody signals + */ + rc = pthread_cond_wait(&cond_cv_9041, &mutex_cv_9041); + if (rc != 0) { + printf("test failed.pthread_cond_wait\n"); + err_count_cv_9041++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex_cv_9041); + if (rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + err_count_cv_9041++; + return (void *)-1; + } + + return NULL; +} + +int gjb_S0100904GN_1(int argc, char **argv) +{ + pthread_t low_id; + int rc = 0; + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread_cv_9041, NULL); + if (rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(2); + + rc = pthread_cond_signal(&cond_cv_9041); + if (rc==0) { + printf("test pass.pthread_cond_signal return 0\n"); + + } else { + printf("test failed:pthread_cond_signal should return 0,but return %d",rc); + goto __errno_handle; + } + + if (err_count_cv_9041) { + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} diff --git a/conditional_variable/gjb_S0100904GN_2.c b/conditional_variable/gjb_S0100904GN_2.c new file mode 100644 index 0000000000000000000000000000000000000000..c4fe601b765a8db2fda58c75434bfd5e11fc9d9f --- /dev/null +++ b/conditional_variable/gjb_S0100904GN_2.c @@ -0,0 +1,99 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100904GN_2.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 使用条件变量同时唤醒一个或者多个等待任务/进程功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include + +// #include "gjb.h" + +// int gjb_S0100904GN_2(int argc, char **argv) +int main(int argc, char **argv) +{ + pthread_condattr_t condattr; + pthread_cond_t cond; + int rc,fail=0; + + /* + * Initialize a condition variable attribute object + */ + if ((rc = pthread_condattr_init(&condattr)) != 0) { + printf("Error at pthread_condattr_init(), rc=%d\n",rc); + goto __errno_handle; + } + + /* + * Initialize cond with the default condition variable attribute + */ + if ((rc = pthread_cond_init(&cond,&condattr)) != 0) { + printf("Fail to initialize cond, rc=%d\n",rc); + goto __errno_handle; + } + + /* + * Destroy the condition variable attribute object + */ + if ((rc = pthread_condattr_destroy(&condattr)) != 0) { + printf("Error at pthread_condattr_destroy(), rc=%d\n",rc); + goto __errno_handle; + } + + /* + * Destroy cond + */ + if ((rc = pthread_cond_destroy(&cond)) != 0) { + printf("Fail to destroy cond, rc=%d\n",rc); + printf("Test FAILED\n"); + + goto __errno_handle; + } + + rc = pthread_cond_signal(&cond); + if (rc == EINVAL) { + printf("test pass when the cond is not exist .pthread_cond_signal return EINVAL\n"); + + } else { + printf("test failed when the cond is not exist.pthread_cond_signal should return EINVAL,but return %d\n",rc); + fail = 1; + } + + + rc = pthread_cond_signal(NULL); + if (rc == EINVAL) { + printf("test NULL pass.pthread_cond_signal return EINVAL\n"); + + } else { + printf("test failed:pthread_cond_signal should return EINVAL,but return %d",rc); + fail = 1; + } + + if (fail == 1) { + printf("Test failed.\n"); + goto __errno_handle; + + } else { + printf("Test pass\n"); + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); + +} diff --git a/conditional_variable/gjb_S0100904GN_3.c b/conditional_variable/gjb_S0100904GN_3.c new file mode 100644 index 0000000000000000000000000000000000000000..3d504b1ce5215f3419827579df93aed7001d95ec --- /dev/null +++ b/conditional_variable/gjb_S0100904GN_3.c @@ -0,0 +1,188 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0100904GN_3.c +** +** 文件创建日期: 2021 年 1 月 22 日 +** +** 描 述: 使用条件变量同时唤醒一个或者多个等待任务/进程功能测试 +*********************************************************************************************************/ +#include +#include +#include +#include +#include + +// #include "gjb.h" + +static int err_count_cv_9043 = 0; +static pthread_cond_t cond_cv_9043 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ +static pthread_mutex_t mutex_cv_9043; //= PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread_cv_9043 (void *tmp) +{ + int rc = 0; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex_cv_9043); + if (rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + err_count_cv_9043++; + return (void *)-1; + } + + /* + * Wait on the cond var. This will not return, as nobody signals + */ + rc = pthread_cond_wait(&cond_cv_9043, &mutex_cv_9043); + if (rc != 0) { + printf("test 1.1 failed.pthread_cond_wait\n"); + err_count_cv_9043++; + return (void *)-1; + } + + rc = pthread_cond_wait(&cond_cv_9043, &mutex_cv_9043); + if (rc != 0) { + printf("test 1.2 failed.pthread_cond_wait\n,should return 0,but return %d\n",rc); + err_count_cv_9043++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex_cv_9043); + if (rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + err_count_cv_9043++; + return (void *)-1; + } + + return NULL; +} + +void * thread1_cv_9043(void *tmp) +{ + int rc = 0; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex_cv_9043/*&mutex1*/); + if(rc != 0) { + printf("test failed.pthread_mutex_lock(1)\n"); + err_count_cv_9043++; + return (void *)-1; + } + + /* + * Wait on the cond var. This will not return, as nobody signals* + */ + rc = pthread_cond_wait(&cond_cv_9043, &mutex_cv_9043/*&mutex1*/); + if(rc != 0) { + printf( "test 2.1 failed.pthread_cond_wait, rc = %d\n",rc); + err_count_cv_9043++; + return (void *)-1; + } + + rc = pthread_cond_wait(&cond_cv_9043, &mutex_cv_9043/*&mutex1*/); + if (rc != 0) { + printf( "test 2.2 failed.pthread_cond_wait\n,should return 0,but return %d\n",rc); + err_count_cv_9043++; + return (void *)-1; + } + + rc = pthread_mutex_unlock(&mutex_cv_9043); + if(rc != 0) { + printf( "test failed.pthread_mutex_unlock\n"); + err_count_cv_9043++; + return (void *)-1; + } + + return NULL; +} + +int gjb_S0100904GN_3(int argc, char **argv) +{ + pthread_t low_id,low_id1; + int rc = 0; + + if (pthread_mutex_init(&mutex_cv_9043, NULL) != 0) { + printf("Fail to initialize mutex\n"); + goto __errno_handle; + } + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread_cv_9043, NULL); + if (rc != 0) { + printf( "pthread_create\n"); + goto __errno_handle; + } + + rc = pthread_create(&low_id1, NULL, thread1_cv_9043, NULL); + if (rc != 0) { + printf( "pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(2); + + rc = pthread_cond_broadcast(&cond_cv_9043); + if (rc == 0) { + printf( "1.1test pass.pthread_cond_broadcast return 0\n"); + + } else { + printf("1.1test failed:pthread_cond_broadcast should return 0,but return %d\n",rc); + goto __errno_handle; + } + + sleep(1); + rc = pthread_cond_broadcast(&cond_cv_9043); + if (rc == 0) { + printf( "1.2test pass.pthread_cond_broadcast return 0\n"); + + } else { + printf("1.2test failed:pthread_cond_broadcast should return 0,but return %d\n",rc); + goto __errno_handle; + } + + sleep(1); + + /* + * Try to destroy the cond var. This should return 0 + * because pthread_cond_broadcast should exit all the pthread_cond_wait + */ + rc = pthread_cond_destroy(&cond_cv_9043); + if (rc == 0) { + printf( "Test pass. pthread_cond_destroy should return 0\n"); + } else { + printf ("Test failed. pthread_cond_destroy should return 0,but return %d\n",rc); + goto __errno_handle; + } + + if (pthread_mutex_destroy(&mutex_cv_9043) != 0) { + printf("Fail to pthread_mutex_destroy mutex\n"); + goto __errno_handle; + } + + if (err_count_cv_9043) { + goto __errno_handle; + } + + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); +} diff --git a/conditional_variable/gjb_S0100904GN_4.c b/conditional_variable/gjb_S0100904GN_4.c new file mode 100644 index 0000000000000000000000000000000000000000..1db9ebf3aa33cc049363a3e3046013d2a127e712 --- /dev/null +++ b/conditional_variable/gjb_S0100904GN_4.c @@ -0,0 +1,111 @@ +/********************************************************************************************************* +** +** GJB ��׼���Լ� +** +** Copyright All Rights Reserved +** +**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +** +** �� �� ��: gjb_S0100904GN_4.c +** +** �ļ���������: 2021 �� 1 �� 22 �� +** +** �� ��: ʹ����������ͬʱ����һ�����߶���ȴ�����?/���̹��ܲ��� +*********************************************************************************************************/ +#include +#include +#include +#include +#include + +// #include "gjb.h" + +static int err_count_cv_9044 = 0; +static pthread_cond_t cond1_cv_9044 = PTHREAD_COND_INITIALIZER; /* cond used by the two threads */ +static pthread_mutex_t mutex2_cv_9044 = PTHREAD_MUTEX_INITIALIZER; /* cond used by the two threads */ + +void * thread2_cv_9044(void *tmp) +{ + int rc = 0; + + /* + * acquire the mutex + */ + rc = pthread_mutex_lock(&mutex2_cv_9044); + if (rc != 0) { + printf("test failed.pthread_mutex_lock\n"); + err_count_cv_9044++; + return NULL; + } + + /* + * Wait on the cond var. This will not return, as nobody signals + */ + rc = pthread_cond_wait(&cond1_cv_9044, &mutex2_cv_9044); + if (rc != 0) { + printf("test failed.pthread_cond_wait\n"); + err_count_cv_9044++; + return NULL; + } + + + rc = pthread_mutex_unlock(&mutex2_cv_9044); + if (rc != 0) { + printf("test failed.pthread_mutex_unlock\n"); + err_count_cv_9044++; + return NULL; + } + + return NULL; +} + +// int gjb_S0100904GN_4(int argc, char **argv) +int main(int argc, char **argv) +{ + pthread_t low_id; + int rc = 0; + + /* + * Create a new thread with default attributes + */ + rc = pthread_create(&low_id, NULL, thread2_cv_9044, NULL); + if (rc != 0) { + printf("pthread_create\n"); + goto __errno_handle; + } + + /* + * Let the other thread run + */ + sleep(2); + + rc = pthread_cond_broadcast(&cond1_cv_9044); + if (rc == 0) { + printf("test illegal pass.pthread_cond_broadcast return EINVAL\n"); + + } else { + printf("test failed:pthread_cond_broadcast should return EINVAL,but return %d\n",rc); + goto __errno_handle; + } + + rc = pthread_cond_broadcast(NULL); + if (rc == EINVAL) { + printf("test NULL pass.pthread_cond_broadcast return EINVAL\n"); + + } else { + printf("test failed:pthread_cond_broadcast should return EINVAL,but return %d\n",rc); + goto __errno_handle; + } + + if (err_count_cv_9044) { + goto __errno_handle; + } + printf("..................................................[(0)]\n \n \n"); + + return (0); + +__errno_handle: + printf("..................................................[(-1)]\n \n \n"); + return (-1); + +} diff --git "a/conditional_variable/\346\235\241\344\273\266\345\217\230\351\207\217\344\277\256\346\224\271\345\207\275\346\225\260\350\256\260\345\275\225.docx" "b/conditional_variable/\346\235\241\344\273\266\345\217\230\351\207\217\344\277\256\346\224\271\345\207\275\346\225\260\350\256\260\345\275\225.docx" new file mode 100644 index 0000000000000000000000000000000000000000..70493ff932ed7416d66cc1e024a6688ea8379721 Binary files /dev/null and "b/conditional_variable/\346\235\241\344\273\266\345\217\230\351\207\217\344\277\256\346\224\271\345\207\275\346\225\260\350\256\260\345\275\225.docx" differ