多线程例题练手(c入门)

使用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 <pthread.h>    /* header file for pthreads */
#include <unistd.h>     /* header file for POSIX conformance */
#include <time.h>       /* header file for POSIX time management */
#include <sched.h>      /* header file for POSIX scheduling */
#include <stdio.h>      /* 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, &param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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 <pthread.h>    /* header file for pthreads */
#include <unistd.h>     /* header file for POSIX conformance */
#include <time.h>       /* header file for POSIX time management */
#include <sched.h>      /* header file for POSIX scheduling */
#include <stdio.h>      /* 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, &param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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程序的代码,确保您理解了它的功能。

  1. 运行代码并报告输出。要运行这个程序,您需要使用sudo(即,使用
    命令:sudo ./multithread)。简要解释这个程序做什么,以及调度是如何引起观察到的行为的。
    在这里插入图片描述
  • 程序包括三个线程A, B, C,但只使用一个CPU核心。线程的调度策略为FIFO(先到先出),优先级范围在min和max之间,status用来检查系统调用是否正常返回。
    线程A、B、C按顺序创建。一个线程是for循环,输出10a, B输出10b, C输出10c。按照FIFO调度策略,输出为:aaaaaaaaaabbbbbbbbbbcccccccc。
  1. 修改threadA函数,在打印出一半的字母后,会调用以下两条指令:
param.sched_priority = priority_min+2;
pthread_setschedparam(threadB id,policy,&param);

运行修改后的程序并记录输出。解释这种变化对观察到的行为的影响。
在这里插入图片描述

  • 修改两行代码的作用是增加2的for循环的线程优先级线程的输出一半,和进度执行线程B for循环输出B。在执行线程B,因为线程的优先级高于C,所以继续输出5,然后执行线程C。
  1. 使用nanosleep命令修改原始程序,使threadA在打印一半字母后休眠1毫秒。运行代码并报告输出。解释这种变化对观察到的行为的影响。
    在这里插入图片描述
  • nanosleep()函数将导致当前线程暂停执行,直到参数1指定的时间间隔。或者在指定的时间间隔内将信号传递给当前线程,这将导致当前线程调用信号捕获函数或终止线程。
    因此,当ThreadA打印出一半的字母时,它会休眠一毫秒,这足够threadb和threadc在继续执行ThreadA之前完成执行。
  1. 编译并运行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。
  1. 修改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 <pthread.h>    /* header file for pthreads */
#include <unistd.h>     /* header file for POSIX conformance */
#include <time.h>       /* header file for POSIX time management */
#include <sched.h>      /* header file for POSIX scheduling */
#include <stdio.h>      /* 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, &param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
	}	
  }
  
  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 <pthread.h>    /* header file for pthreads */
#include <unistd.h>     /* header file for POSIX conformance */
#include <time.h>       /* header file for POSIX time management */
#include <sched.h>      /* header file for POSIX scheduling */
#include <stdio.h>      /* 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, &param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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 <pthread.h>    /* header file for pthreads */
#include <unistd.h>     /* header file for POSIX conformance */
#include <time.h>       /* header file for POSIX time management */
#include <sched.h>      /* header file for POSIX scheduling */
#include <stdio.h>      /* 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, &param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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,&param);
  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);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值