task switch and semaphore...big confusion

Status
Not open for further replies.

confusion

Junior Member level 3
Joined
Jan 6, 2012
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,516
hello!! am learning rtos in my curriculum . i came accross the code below and i dont understand the code flow..as in when the task switch will ocurr and when will the task perform the lcd functions. i don't understand the sequence in which the things will happen .plz help in interpreting what the output of this code will be.

code: priority inversion.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "config.h"
#include "stdlib.h"
#include<LPC21xx.H>
#include<key.h>
#include<lcd.h>
 
 
OS_STK Task1Stack[100];
void Task1(void *pdata);
 
OS_STK Task2Stack[256];
void Task2(void *pdata);
 
static OS_EVENT *semaphorea;
static OS_EVENT *semaphoreb;
//OS_TASK_CHANGE_PRIO_EN=1;
 
int  main (void)
{
    keyinit();
    LCDInit();
    lcdcmd(0x80);
    LCDInit();
    
    OSInit();
    semaphorea = OSSemCreate(1);     //counting semaphore ,1st semaphore
    semaphoreb = OSSemCreate(1);     //2nd semaphore
    if(semaphorea!=NULL)
    {
        DisplayLCD("Sema a create",13);
    }
    else
    {
        DisplayLCD("Sema a not create",17);
    }
        if(semaphoreb!=NULL)
    {
        DisplayLCD("Sema b create",13);
    }
    else
    {
        DisplayLCD("Sema b not create",17);
    }
    delay();
    clrscreen();
    delay();
    //DisplayLCD("Key:   LCD:");
    OSTaskCreate(Task1, (void *)0, &Task1Stack[99], 2);
    OSTaskCreate(Task2, (void *)0, &Task2Stack[255], 3);
 
    OSStart();
    return 0;
}
 
    
void Task1 (void *data)
{
 
  int n, i;
  char err;
  data = data;     
 
  while(1)
  {
        
   OSSemPend(semaphorea, 1, &err);  
   
    clrscreen();
  DisplayLCD("task1:sem a taken",17);
   for(i=0;i<900000;i++);
   for(i=0;i<900000;i++);
   for(i=0;i<900000;i++);
   OSTimeDly(100);
  
   OSSemPend(semaphoreb, 1, &err); 
 
    OSTimeDly(100);
  
   OSSemPost(semaphorea);
   OSSemPost(semaphoreb);           
   
   
  }
}
 
 
 
 
void Task2 (void *data)
{
 
  int n,i;
  char err;
  data = data;     
 
  while(1)
  {         
    
   OSSemPend(semaphoreb, 1, &err); 
   clrscreen();
  DisplayLCD("Task2:Sem b Taken",17);
   for(i=0;i<900000;i++); 
   for(i=0;i<900000;i++);
   for(i=0;i<900000;i++);
   
  // OSTimeDly(100);
   
    
 
   
    clrscreen();
        DisplayLCD("priority",8); 
         OSTaskChangePrio(3,1); 
        OSSemPend(semaphorea, 1, &err);
     clrscreen();
        DisplayLCD("task2:sem a take",17); 
   
  
   OSTimeDly(100);
   
   OSSemPost(semaphoreb);  
   OSSemPost(semaphorea);               
  }
}

 
Last edited by a moderator:

Task switch by
1) Interupt occur, most of RTOS using timer interrupt for schedule the task, when timer interrupt occur and others high priority task are ready to run it will make a task switch.
2) Calling OS API function, example call OSTimeDly currently running task will be block and kernel will find a new task for running next this will make task switch occur.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…