多线程例题练手
发布日期:2022-04-11 08:52:52
浏览次数:12
分类:技术文章
本文共 25458 字,大约阅读时间需要 84 分钟。
使用pthreads进行实时编程
需要模板代码critical.c和multithread.c。
/* * critical.c * * Demonstrate use of mutual exclusion using mutexes * * Upper case output indicates critical output * lower case output indicates non-critical output * * compile with gcc critical.c -o critical -lrt -lpthread * */#define _GNU_SOURCE#define _REENTRANT /* macro to ensure system calls are reentrant */#include/* header file for pthreads */#include /* header file for POSIX conformance */#include /* header file for POSIX time management */#include /* header file for POSIX scheduling */#include /* header file for standard input/outputlibrary */pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; /*define mutex */void *threadA(void *); /* predefine threadA routine */void *threadB(void *); /* predefine threadB routine */pthread_t threadA_id,threadB_id,main_id; /* thread identifiers */pthread_attr_t attrA,attrB; /* thread attribute structures */struct sched_param param; /* scheduling structure for thread attributes */int policy=SCHED_FIFO;int priority_min,priority_max; /* for range of priority levels *//* main routine */int main(){ int status; /* check that system calls return ok */ /* Set processor affinity */ cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0,&mask); /* use only 1 CPU core */ unsigned int len = sizeof(mask); status = sched_setaffinity(0, len, &mask); if (status < 0) perror("sched_setaffinity"); status = sched_getaffinity(0, len, &mask); if (status < 0) perror("sched_getaffinity"); /* Find priority limits */ priority_max = sched_get_priority_max(policy); priority_min = sched_get_priority_min(policy); /* Set priority and policy of main thread */ main_id = pthread_self(); param.sched_priority=priority_min; status = pthread_setschedparam(main_id, policy, ¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ /* Create threadA */ param.sched_priority = priority_min; pthread_attr_init(&attrA); status = pthread_attr_setschedpolicy(&attrA,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrA,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadA_id, &attrA, threadA, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadA_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Create threadB */ param.sched_priority = priority_min; pthread_attr_init(&attrB); status = pthread_attr_setschedpolicy(&attrB,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrB,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadB_id, &attrB, threadB, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadB_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Join threads - force main to wait for the thread to terminate */ status = pthread_join(threadA_id, NULL); if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */ status = pthread_join(threadB_id, NULL); if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */ status = pthread_mutex_destroy(&mtx); /* delete mutex */ if (status != 0) perror("pthread_mutex_destroy"); /* error check */ printf("\n"); return(0); } /* end of main */void *threadA(void *arg){ int j; int status; /* check that system calls return ok */ for(j=1;j<=5;j++){ printf("a"); /* non -critical */ } /* lock to enter critical region */ /* status = pthread_mutex_lock(&mtx); if (status != 0) perror("pthread_mutex_lock");*/ /* error check */ for(j=1;j<=5;j++){ printf("A"); /* critical */ } /* increment priority of threadB above threadA */ param.sched_priority++; status = pthread_setschedparam(threadB_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ for(j=1;j<=5;j++){ printf("A"); /* critical */ } /* unlock critical region */ /* status = pthread_mutex_unlock(&mtx); if (status != 0) perror("pthread_mutex_unlock");*/ /* error check */ for(j=1;j<=5;j++){ printf("a"); /* non -critical */ } return (NULL);}void *threadB(void *arg){ int j; int status; /* check that system calls return ok */ for(j=1;j<=5;j++){ printf("b"); /* non -critical */ } /* lock to enter critical region */ /* status = pthread_mutex_lock(&mtx); if (status != 0) perror("pthread_mutex_lock");*/ /* error check */ for(j=1;j<=5;j++){ printf("B"); /* critical */ } /* increment priority of threadA to above threadB*/ param.sched_priority++; status = pthread_setschedparam(threadA_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ for(j=1;j<=5;j++){ printf("B"); /* critical */ } /* unlock critical region */ /* status = pthread_mutex_unlock(&mtx); if (status != 0) perror("pthread_mutex_unlock");*/ /* error check */ for(j=1;j<=5;j++){ printf("b"); /* non -critical */ } return (NULL);}
/* * multithread.c * * Demonstrate use of a multi threading and scheduling using pthreads * * compile with gcc multithread.c -o multithread -lrt -lpthread * */#define _GNU_SOURCE#define _REENTRANT /* macro to ensure system calls are reentrant */#include/* header file for pthreads */#include /* header file for POSIX conformance */#include /* header file for POSIX time management */#include /* header file for POSIX scheduling */#include /* header file for standard input/outputlibrary */void *threadA(void *); /* predefine threadA routine */void *threadB(void *); /* predefine threadB routine */void *threadC(void *); /* predefine threadC routine */pthread_t threadA_id,threadB_id,threadC_id,main_id; /* thread identifiers */pthread_attr_t attrA,attrB,attrC; /* thread attribute structures */struct sched_param param; /* scheduling structure for thread attributes */int policy=SCHED_FIFO;int priority_min,priority_max; /* for range of priority levels *//* main routine */int main(){ int status; /* check that system calls return ok */ /* Set processor affinity */ cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0,&mask); /* use only 1 CPU core */ unsigned int len = sizeof(mask); status = sched_setaffinity(0, len, &mask); if (status < 0) perror("sched_setaffinity"); status = sched_getaffinity(0, len, &mask); if (status < 0) perror("sched_getaffinity"); /* Find priority limits */ priority_max = sched_get_priority_max(policy); priority_min = sched_get_priority_min(policy); /* Set priority and policy of main thread */ main_id = pthread_self(); param.sched_priority=priority_min; status = pthread_setschedparam(main_id, policy, ¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ /* Create threadA */ param.sched_priority = priority_min; pthread_attr_init(&attrA); status = pthread_attr_setschedpolicy(&attrA,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrA,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadA_id, &attrA, threadA, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadA_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Create threadB */ param.sched_priority = priority_min; pthread_attr_init(&attrB); status = pthread_attr_setschedpolicy(&attrB,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrB,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadB_id, &attrB, threadB, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadB_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Create threadC */ param.sched_priority = priority_min; pthread_attr_init(&attrC); status = pthread_attr_setschedpolicy(&attrC,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrC,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadC_id, &attrC, threadC, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadC_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Join threads - force main to wait for the thread to terminate */ status = pthread_join(threadA_id, NULL); if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */ status = pthread_join(threadB_id, NULL); if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */ status = pthread_join(threadC_id, NULL); if (status != 0) perror("pthread_join(threadC_id, NULL)"); /* error check */ printf("\n"); return(0); } /* end of main */void *threadA(void *arg){ int j; for(j=1;j<=10;j++){ printf("a"); } return (NULL);}void *threadB(void *arg){ int j; for(j=1;j<=10;j++){ printf("b"); } return (NULL);}void *threadC(void *arg){ int j; for(j=1;j<=10;j++){ printf("c"); } return (NULL);}
可以使用以下命令编译源代码:
GCC filename.c -o filename -lrt -lpthread 其中filename是源代码文件的名称。生成的可执行文件应该使用sudo来运行, 即,使用以下命令: sudo ./文件名 检查上面提供的源代码是否适用,并完成下面的练习。检查multithread.c程序的代码,确保您理解了它的功能。- 运行代码并报告输出。要运行这个程序,您需要使用sudo(即,使用 命令:sudo ./multithread)。简要解释这个程序做什么,以及调度是如何引起观察到的行为的。
- 程序包括三个线程A, B, C,但只使用一个CPU核心。线程的调度策略为FIFO(先到先出),优先级范围在min和max之间,status用来检查系统调用是否正常返回。 线程A、B、C按顺序创建。一个线程是for循环,输出10a, B输出10b, C输出10c。按照FIFO调度策略,输出为:aaaaaaaaaabbbbbbbbbbcccccccc。
- 修改threadA函数,在打印出一半的字母后,会调用以下两条指令:
param.sched_priority = priority_min+2;pthread_setschedparam(threadB id,policy,¶m);
运行修改后的程序并记录输出。解释这种变化对观察到的行为的影响。
- 修改两行代码的作用是增加2的for循环的线程优先级线程的输出一半,和进度执行线程B for循环输出B。在执行线程B,因为线程的优先级高于C,所以继续输出5,然后执行线程C。
- 使用nanosleep命令修改原始程序,使threadA在打印一半字母后休眠1毫秒。运行代码并报告输出。解释这种变化对观察到的行为的影响。
- nanosleep()函数将导致当前线程暂停执行,直到参数1指定的时间间隔。或者在指定的时间间隔内将信号传递给当前线程,这将导致当前线程调用信号捕获函数或终止线程。 因此,当ThreadA打印出一半的字母时,它会休眠一毫秒,这足够threadb和threadc在继续执行ThreadA之前完成执行。
- 编译并运行critical程序。报告输出。简要解释这个程序做什么,以及调度是如何引起观察到的行为的。
- critical.c包括线程a和线程b,调度策略为FIFO,其余均为多线程。 依次执行线程A、B,然后销毁 首先,线程A执行一个非关键操作,输出5个字母,然后执行一个关键操作,输出5个字母。 然后,线程B的优先级增加到线程a以上,线程B执行一个非关键操作输出5b个字母,执行一个关键操作输出5b个字母。 接下来,线程A的优先级增加到线程b以上,线程A的优先级更高,调度回线程A,执行关键操作,输出5个A,解锁关键区域,线程A执行非关键操作,输出A的5个字母,线程A结束。 CPU调度threadB,输出5b,增加threadA的优先级到更高的threadB,最后输出5b。
- 修改critical代码,使互斥对象不再被“注释掉”。运行程序并报告输出。简要解释修改后的程序的执行。
- 由于互斥操作的增加,线程会锁定进入临界区,并在输出5a之前进入临界区。此时,线程B基本上具有更高的优先级,无法进入临界区执行输出B的操作。 当threadA完成执行关键操作时,threadB将立即进入关键区段并执行输出B的关键操作。
以下是2,3,5题修改后的代码
/* * multithread2.c */#define _GNU_SOURCE#define _REENTRANT /* macro to ensure system calls are reentrant */#include/* header file for pthreads */#include /* header file for POSIX conformance */#include /* header file for POSIX time management */#include /* header file for POSIX scheduling */#include /* header file for standard input/outputlibrary */void *threadA(void *); /* predefine threadA routine */void *threadB(void *); /* predefine threadB routine */void *threadC(void *); /* predefine threadC routine */pthread_t threadA_id,threadB_id,threadC_id,main_id; /* thread identifiers */pthread_attr_t attrA,attrB,attrC; /* thread attribute structures */struct sched_param param; /* scheduling structure for thread attributes */int policy=SCHED_FIFO;int priority_min,priority_max; /* for range of priority levels *//* main routine */int main(){ int status; /* check that system calls return ok */ /* Set processor affinity */ cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0,&mask); /* use only 1 CPU core */ unsigned int len = sizeof(mask); status = sched_setaffinity(0, len, &mask); if (status < 0) perror("sched_setaffinity"); status = sched_getaffinity(0, len, &mask); if (status < 0) perror("sched_getaffinity"); /* Find priority limits */ priority_max = sched_get_priority_max(policy); priority_min = sched_get_priority_min(policy); /* Set priority and policy of main thread */ main_id = pthread_self(); param.sched_priority=priority_min; status = pthread_setschedparam(main_id, policy, ¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ /* Create threadA */ param.sched_priority = priority_min; pthread_attr_init(&attrA); status = pthread_attr_setschedpolicy(&attrA,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrA,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadA_id, &attrA, threadA, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadA_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Create threadB */ param.sched_priority = priority_min; pthread_attr_init(&attrB); status = pthread_attr_setschedpolicy(&attrB,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrB,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadB_id, &attrB, threadB, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadB_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Create threadC */ param.sched_priority = priority_min; pthread_attr_init(&attrC); status = pthread_attr_setschedpolicy(&attrC,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrC,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadC_id, &attrC, threadC, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadC_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Join threads - force main to wait for the thread to terminate */ status = pthread_join(threadA_id, NULL); if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */ status = pthread_join(threadB_id, NULL); if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */ status = pthread_join(threadC_id, NULL); if (status != 0) perror("pthread_join(threadC_id, NULL)"); /* error check */ printf("\n"); return(0); } /* end of main */void *threadA(void *arg){ int j; for(j=1;j<=10;j++){ printf("a"); if(j==5){ param.sched_priority = priority_min+2; pthread_setschedparam(threadB_id,policy,¶m); } } return (NULL);}void *threadB(void *arg){ int j; for(j=1;j<=10;j++){ printf("b"); } return (NULL);}void *threadC(void *arg){ int j; for(j=1;j<=10;j++){ printf("c"); } return (NULL);}
/* * multithread3.c */#define _GNU_SOURCE#define _REENTRANT /* macro to ensure system calls are reentrant */#include/* header file for pthreads */#include /* header file for POSIX conformance */#include /* header file for POSIX time management */#include /* header file for POSIX scheduling */#include /* header file for standard input/outputlibrary */void *threadA(void *); /* predefine threadA routine */void *threadB(void *); /* predefine threadB routine */void *threadC(void *); /* predefine threadC routine */pthread_t threadA_id,threadB_id,threadC_id,main_id; /* thread identifiers */pthread_attr_t attrA,attrB,attrC; /* thread attribute structures */struct sched_param param; /* scheduling structure for thread attributes */int policy=SCHED_FIFO;int priority_min,priority_max; /* for range of priority levels *//* main routine */int main(){ int status; /* check that system calls return ok */ /* Set processor affinity */ cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0,&mask); /* use only 1 CPU core */ unsigned int len = sizeof(mask); status = sched_setaffinity(0, len, &mask); if (status < 0) perror("sched_setaffinity"); status = sched_getaffinity(0, len, &mask); if (status < 0) perror("sched_getaffinity"); /* Find priority limits */ priority_max = sched_get_priority_max(policy); priority_min = sched_get_priority_min(policy); /* Set priority and policy of main thread */ main_id = pthread_self(); param.sched_priority=priority_min; status = pthread_setschedparam(main_id, policy, ¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ /* Create threadA */ param.sched_priority = priority_min; pthread_attr_init(&attrA); status = pthread_attr_setschedpolicy(&attrA,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrA,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadA_id, &attrA, threadA, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadA_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Create threadB */ param.sched_priority = priority_min; pthread_attr_init(&attrB); status = pthread_attr_setschedpolicy(&attrB,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrB,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadB_id, &attrB, threadB, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadB_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Create threadC */ param.sched_priority = priority_min; pthread_attr_init(&attrC); status = pthread_attr_setschedpolicy(&attrC,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrC,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadC_id, &attrC, threadC, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadC_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Join threads - force main to wait for the thread to terminate */ status = pthread_join(threadA_id, NULL); if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */ status = pthread_join(threadB_id, NULL); if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */ status = pthread_join(threadC_id, NULL); if (status != 0) perror("pthread_join(threadC_id, NULL)"); /* error check */ printf("\n"); return(0); } /* end of main */void *threadA(void *arg){ int j; for(j=1;j<=10;j++){ printf("a"); if(j==5){ struct timespec req = { 0 }, rem = { 0}; req.tv_sec = 0; req.tv_nsec = 1000000L; nanosleep (&req , &rem) ; } } return (NULL);}void *threadB(void *arg){ int j; for(j=1;j<=10;j++){ printf("b"); } return (NULL);}void *threadC(void *arg){ int j; for(j=1;j<=10;j++){ printf("c"); } return (NULL);}
/* * critical2.c */#define _GNU_SOURCE#define _REENTRANT /* macro to ensure system calls are reentrant */#include/* header file for pthreads */#include /* header file for POSIX conformance */#include /* header file for POSIX time management */#include /* header file for POSIX scheduling */#include /* header file for standard input/outputlibrary */pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; /*define mutex */void *threadA(void *); /* predefine threadA routine */void *threadB(void *); /* predefine threadB routine */pthread_t threadA_id,threadB_id,main_id; /* thread identifiers */pthread_attr_t attrA,attrB; /* thread attribute structures */struct sched_param param; /* scheduling structure for thread attributes */int policy=SCHED_FIFO;int priority_min,priority_max; /* for range of priority levels *//* main routine */int main(){ int status; /* check that system calls return ok */ /* Set processor affinity */ cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(0,&mask); /* use only 1 CPU core */ unsigned int len = sizeof(mask); status = sched_setaffinity(0, len, &mask); if (status < 0) perror("sched_setaffinity"); status = sched_getaffinity(0, len, &mask); if (status < 0) perror("sched_getaffinity"); /* Find priority limits */ priority_max = sched_get_priority_max(policy); priority_min = sched_get_priority_min(policy); /* Set priority and policy of main thread */ main_id = pthread_self(); param.sched_priority=priority_min; status = pthread_setschedparam(main_id, policy, ¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ /* Create threadA */ param.sched_priority = priority_min; pthread_attr_init(&attrA); status = pthread_attr_setschedpolicy(&attrA,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrA,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadA_id, &attrA, threadA, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadA_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Create threadB */ param.sched_priority = priority_min; pthread_attr_init(&attrB); status = pthread_attr_setschedpolicy(&attrB,policy); if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */ status = pthread_attr_setschedparam(&attrB,¶m); if (status != 0) perror("pthread_attr_setschedparam"); /* error check */ status = pthread_create(&threadB_id, &attrB, threadB, NULL); if (status != 0) perror("pthread_create"); /* error check */ status = pthread_setschedparam(threadB_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* Join threads - force main to wait for the thread to terminate */ status = pthread_join(threadA_id, NULL); if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */ status = pthread_join(threadB_id, NULL); if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */ status = pthread_mutex_destroy(&mtx); /* delete mutex */ if (status != 0) perror("pthread_mutex_destroy"); /* error check */ printf("\n"); return(0); } /* end of main */void *threadA(void *arg){ int j; int status; /* check that system calls return ok */ for(j=1;j<=5;j++){ printf("a"); /* non -critical */ } /* lock to enter critical region */ status = pthread_mutex_lock(&mtx); if (status != 0) perror("pthread_mutex_lock"); /* error check */ for(j=1;j<=5;j++){ printf("A"); /* critical */ } /* increment priority of threadB above threadA */ param.sched_priority++; status = pthread_setschedparam(threadB_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ for(j=1;j<=5;j++){ printf("A"); /* critical */ } /* unlock critical region */ status = pthread_mutex_unlock(&mtx); if (status != 0) perror("pthread_mutex_unlock"); /* error check */ for(j=1;j<=5;j++){ printf("a"); /* non -critical */ } return (NULL);}void *threadB(void *arg){ int j; int status; /* check that system calls return ok */ for(j=1;j<=5;j++){ printf("b"); /* non -critical */ } /* lock to enter critical region */ status = pthread_mutex_lock(&mtx); if (status != 0) perror("pthread_mutex_lock"); /* error check */ for(j=1;j<=5;j++){ printf("B"); /* critical */ } /* increment priority of threadA to above threadB*/ param.sched_priority++; status = pthread_setschedparam(threadA_id,policy,¶m); if (status != 0) perror("pthread_setschedparam"); /* error check */ for(j=1;j<=5;j++){ printf("B"); /* critical */ } /* unlock critical region */ status = pthread_mutex_unlock(&mtx); if (status != 0) perror("pthread_mutex_unlock"); /* error check */ for(j=1;j<=5;j++){ printf("b"); /* non -critical */ } return (NULL);}
转载地址:https://blog.csdn.net/weixin_42455006/article/details/123927552 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月09日 14时04分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
VTK:PolyData之Silhouette
2019-04-26
VTK:PolyData之ThresholdCells
2019-04-26
VTK:Qt之ImageDataToQImage
2019-04-26
VTK:Qt之RenderWindowUISingleInheritance
2019-04-26
VTK:Qt之ShowEvent
2019-04-26
VTK:直线网格之RGrid
2019-04-26
VTK:直线网格之RectilinearGrid
2019-04-26
VTK:Utilities之Screenshot
2019-04-26
VTK:Utilities之ShallowCopy
2019-04-26
VTK:Utilities之ShareCamera
2019-04-26
VTK:Utilities之ShepardMethod
2019-04-26
VTK:Utilities之SortDataArray
2019-04-26
VTK:Utilities之SparseArray
2019-04-26
VTK:Utilities之TimeStamp
2019-04-26
VTK:Utilities之Timer
2019-04-26
VTK:Utilities之TimerLog
2019-04-26
VTK:Utilities之UnknownLengthArray
2019-04-26
VTK:Utilities之Variant
2019-04-26
VTK:Utilities之Vector
2019-04-26
VTK:Utilities之VectorArrayKnownLength
2019-04-26