Sei sulla pagina 1di 38

1: Program to illustrate task priorities #include<vxWorks.h> #include<stdio.

h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3; int main() { printf("i am in main\n"); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); printf("back in main\n"); return (0); } void my_fun1() { printf("i am in task 1\n"); } void my_fun2() { printf("i am in task 2\n"); } void my_fun3() { printf("i am in task 3\n"); }

/* -------------- output-------------- */ i am in main i am in task 1 i am in task 3 back in main i am in task 2

2:Program to demonstarte the task priorities


#include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority: %d",priority); } void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d",priority); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid1,&priority); printf("\n back in task 1 with priority: %d",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); } /*-----------output-------------------*/ i am in main with priority: 100 i am in task 1 with priority: 99 i am in task 3 with priority: 98 back in task 1 with priority: 99 back in main with priority: 100 i am in task 2 with priority: 199

3: Program to illustrate task delay #include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3; int main() { printf("i am in main\n"); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); printf("back in main\n"); return (0); }

void my_fun1() { printf("i am in task 1\n"); taskDelay(60); printf("i am in task 1\n"); } void my_fun2() { printf("i am in task 2\n"); } void my_fun3() { printf("i am in task 3\n"); } /* -------------- output-------------- */ i am in main i am in task 1 i am in task 3 back in main i am in task 2 back in task1

4:Program to illustrate task lock #include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskLock(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority: %d",priority); taskUnlock(); } void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); }

/* --------------------- output-------------------- */ i am in main with priority: 100 back in main with priority: 100 i am in task 3 with priority: 98 i am in task 1 with priority: 99 i am in task 2 with priority: 199

5:Program to illustrate the effect of taskdelay on tasklock. #include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskLock(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); taskDelay(60); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority: %d",priority); taskUnlock(); } void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); } /* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 i am in task 2 with priority: 199 back in main with priority: 100 i am in task 3 with priority: 98

6:Program to illustrate taskdelete #include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and no one killed me",priority); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and still iam alive",priority); } void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i will kill main",priority); taskDelete(tid_main); taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i killed main",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); }

/* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 and i will kill main i am in task 1 with priority: 99 and i killed main

7:Program to illustrate effect of tasksafe on taskdelete #include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskSafe(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and no one killed me",priority); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and still I am alive",priority); taskUnsafe(); } void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i will kill main",priority); taskDelete(tid_main); taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i killed main",priority); }

void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); }

/* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 and i will kill main i am in task 3 with priority: 98 back in main with priority : 100 and no one killed me back in main with priority : 100 and still I am alive i am in task 1 with priority: 99 and i killed main i am in task 2 with priority: 199

8:Program to illustrate effect of tasksafe on taskdelete #include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskSafe(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and no one killed me",priority); taskUnsafe(); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and still iam alive",priority); }

void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i will kill main",priority); taskDelete(tid_main); taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i killed main",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); } /* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 and i will kill main i am in task 3 with priority: 98 back in main with priority : 100 and no one killed me i am in task 1 with priority: 99 and i killed main i am in task 2 with priority: 199

9:Program to illustrate taskDelay,tasksafe and taskdelete #include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskSafe(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); taskDelay(10); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and no one killed me",priority); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskUnsafe(); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and still iam alive",priority); } void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i will kill main",priority); taskDelete(tid_main); taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i killed main",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); }

/* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 and i will kill main i am in task 2 with priority: 199 back in main with priority : 100 and no one killed me i am in task 3 with priority: 98 i am in task 1 with priority: 99 and i killed main

10:Program to illustrate taskDelay,tasksafe and taskdelete #include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskSafe(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n again in main with priority : %d and no one killed me",priority); taskDelay(10); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and no one killed me",priority); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskUnsafe(); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and still iam alive",priority); }

void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i will kill main",priority); taskDelay(10); taskDelete(tid_main); taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i killed main",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); taskDelete(tid1); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); } /* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 and i will kill main again in main with priority : 100 and no one killed me i am in task 2 with priority: 199 back in main with priority : 100 and no one killed me i am in task 3 with priority: 98 back in main with priority : 100 and still iam alive

#include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskSafe(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority);

tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n again in main with priority : %d and no one killed me",priority); taskDelete(tid_main); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and no one killed me",priority); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskUnsafe(); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and still iam alive",priority); } void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i will kill main",priority); taskDelete(tid_main); taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i killed main",priority); }

void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); } /* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 and i will kill main again in main with priority : 100 and no one killed me i am in task 1 with priority: 99 and i killed main i am in task 2 with priority: 199

#include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskSafe(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n again in main with priority : %d and no one killed me",priority); taskDelete(tid_main); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and no one killed me",priority); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskUnsafe(); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and still iam alive",priority); } void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i will kill main",priority); taskDelete(tid_main); taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i killed main",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); }

void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); }

/* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 and i will kill main again in main with priority : 100 and no one killed me i am in task 1 with priority: 99 and i killed main i am in task 2 with priority: 199

#include<vxWorks.h> #include<stdio.h>

void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskSafe(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and no one killed me",priority); taskUnsafe(); taskPriorityGet(tid_main,&priority); printf("\n back in main with priority : %d and still iam alive",priority); }

void my_fun1() { taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i will kill main",priority); taskDeleteForce(tid_main); taskPriorityGet(tid1,&priority); printf("\n i am in task 1 with priority: %d and i killed main",priority); } void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); } /* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 and i will kill main i am in task 1 with priority: 99 and i killed main

#include<vxWorks.h> #include<stdio.h> void my_fun1(); void my_fun2(); void my_fun3(); int tid1,tid2,tid3,tid_main,priority; main() { tid_main=taskIdSelf(); taskLock(); taskPriorityGet(tid_main,&priority); printf("\ni am in main with priority: %d",priority); tid1=taskSpawn("my_task1",99,0,1000,my_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",199,0,1000,my_fun2,0,0,0,0,0,0,0,0,0,0); taskDelay(10); tid3=taskSpawn("my_task3",98,0,1000,my_fun3,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid_main,&priority);

printf("\n back in main with priority: %d",priority); taskUnlock(); } void my_fun1() { taskPriorityGet(tid1,&priority);

taskDelete(tid_main); printf("\n i am in task 1 with priority: %d",priority);

} void my_fun2() { taskPriorityGet(tid2,&priority); printf("\n i am in task 2 with priority: %d",priority); } void my_fun3() { taskPriorityGet(tid3,&priority); printf("\n i am in task 3 with priority: %d",priority); } /* --------------------- output-------------------- */ i am in main with priority: 100 i am in task 1 with priority: 99 i am in task 2 with priority: 199

Message Queues and semaphores


1: Program to transmit and receive the message #include<stdio.h> #include<vxWorks.h> #include<semLib.h> #include<msgQLib.h> int rx_fun(); int tx_fun(); int tid1,tid2,tid3,tid_main,priority; MSG_Q_ID msg_que_id; int main() { msg_que_id=msgQCreate(3,25,1); printf("\n\ni am in main"); tid1=taskSpawn("my_task1",90,0,1000,rx_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again\n"); tid2=taskSpawn("my_task2",99,0,1000,tx_fun,0,0,0,0,0,0,0,0,0,0); printf("\n again in main again\n"); return(0); } int rx_fun() { char rx_buff[25]; taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d",priority); msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff); return(0); } int tx_fun() { char tx_buff[]={"hello, world"}; taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d",priority); msgQSend(msg_que_id,tx_buff,13,0,0); taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d",priority); return(0); }

/* --------------output-----------------------i am in main i am in receiver fun with priority: 90 back in main again i am in transmit fun with priority: 99 i am in receiver fun with priority: 90 and received message is: hello, world i am in transmit fun with priority: 99 again in main again -----------------------------------------------*/

2: Program to transmit and receive the message with same priority #include<stdio.h> #include<vxWorks.h> #include<semLib.h> #include<msgQLib.h> int rx_fun(); int tx_fun(); int tid1,tid2,tid3,tid_main,priority; MSG_Q_ID msg_que_id; int main() { msg_que_id=msgQCreate(3,25,1); printf("\n\ni am in main"); tid1=taskSpawn("my_task1",90,0,1000,rx_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again\n"); tid2=taskSpawn("my_task2",90,0,1000,tx_fun,0,0,0,0,0,0,0,0,0,0); printf("\n again in main again\n"); return(0); } int rx_fun() { char rx_buff[25]; taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d",priority); msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff);

return(0); }

int tx_fun() { char tx_buff[]={"hello, world"}; taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d",priority); msgQSend(msg_que_id,tx_buff,13,0,0); taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d",priority); return(0); } /* --------------output-----------------------i am in main i am in receiver fun with priority: 90 back in main again i am in transmit fun with priority: 90 i am in transmit fun with priority: 90 i am in receiver fun with priority: 90 and received message is: hello, world again in main again -----------------------------------------------*/

3: Program to transmit and receive the message using FIFO type of message queue #include<stdio.h> #include<vxWorks.h> #include<semLib.h> #include<msgQLib.h> int rx_fun(); int tx_fun(); int tid1,tid2,tid3,tid_main,priority; MSG_Q_ID msg_que_id; int main() { msg_que_id=msgQCreate(3,25,1); printf("\n\ni am in main");

tid2=taskSpawn("my_task2",90,0,1000,tx_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again\n"); tid1=taskSpawn("my_task1",99,0,1000,rx_fun,0,0,0,0,0,0,0,0,0,0); printf("\n again in main again\n"); return(0); }

int rx_fun() { char rx_buff[25]; int i; taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d",priority); for(i=0;i<3;i++) { msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff); } return(0); } int tx_fun() { char tx_buff[3][25]={"hello, world","good morning","good evening"}; int i; for(i=0;i<3;i++) { taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d and transmitted message is: %s",priority,tx_buff[i]); msgQSend(msg_que_id,tx_buff[i],13,0,0); } taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d",priority); return(0); } /* --------------output-----------------------i am in main i am in transmit fun with priority: 90 and transmitted message is: hello, world i am in transmit fun with priority: 90 and transmitted message is: good morning i am in transmit fun with priority: 90 and transmitted message is: good evening i am in transmit fun with priority: 90 back in main again i am in receiver fun with priority: 99 i am in receiver fun with priority: 99 and received message is: hello, world i am in receiver fun with priority: 99 and received message is: good morning i am in receiver fun with priority: 99 and received message is: good evening again in main again -----------------------------------------------*/

4: Program to transmit and receive the message using priority based message queue #include<stdio.h> #include<vxWorks.h> #include<semLib.h> #include<msgQLib.h> int rx_fun(); int tx_fun(); int tid1,tid2,tid3,tid_main,priority; MSG_Q_ID msg_que_id; int main() { msg_que_id=msgQCreate(3,25,1); printf("\n\ni am in main"); tid2=taskSpawn("my_task2",90,0,1000,tx_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again\n"); tid1=taskSpawn("my_task1",99,0,1000,rx_fun,0,0,0,0,0,0,0,0,0,0); printf("\n again in main again\n"); return(0); } int rx_fun() { char rx_buff[25]; int i; taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d",priority); for(i=0;i<3;i++) { msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff); } return(0); } int tx_fun() { char tx_buff[3][25]={"hello, world","good morning","good evening"}; int i; for(i=0;i<2;i++) { taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d and transmitted message is: %s",priority,tx_buff[i]);

msgQSend(msg_que_id,tx_buff[i],13,0,0); } msgQSend(msg_que_id,tx_buff[i],13,0,1); taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d sending urgent message: %s",priority,tx_buff[2]); return(0); } /* --------------output-----------------------i am in main i am in transmit fun with priority: 90 and transmitted message is: hello, world i am in transmit fun with priority: 90 and transmitted message is: good morning i am in transmit fun with priority: 90 sending urgent message: good evening back in main again i am in receiver fun with priority: 99 i am in receiver fun with priority: 99 and received message is: good evening i am in receiver fun with priority: 99 and received message is: hello, world i am in receiver fun with priority: 99 and received message is: good morning again in main again -----------------------------------------------*/

5: Program to illustrate the effect of message queue size. #include<stdio.h> #include<vxWorks.h> #include<semLib.h> #include<msgQLib.h> int rx_fun(); int tx_fun(); int tid1,tid2,tid3,tid_main,priority; MSG_Q_ID msg_que_id; int main() { msg_que_id=msgQCreate(2,25,1); printf("\n i am in main"); tid2=taskSpawn("my_task2",90,0,1000,tx_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again\n"); tid1=taskSpawn("my_task1",99,0,1000,rx_fun,0,0,0,0,0,0,0,0,0,0); printf("\n again in main\n"); return(0); }

int rx_fun() { char rx_buff[25]; int i; taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d",priority); for(i=0;i<5;i++) { printf("\n i am in receiver task"); msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff); } return(0); } int tx_fun() { char tx_buff[][25]={"hello, world","good morning","good evening","message queues","semaphores"}; int i; for(i=0;i<5;i++) { taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d and transmitted message is: %s",priority,tx_buff[i]); msgQSend(msg_que_id,tx_buff[i],13,1,0); } taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d",priority); return(0); } /* --------------output-----------------------i am in main i am in transmit fun with priority: 90 and transmitted message is: hello, world i am in transmit fun with priority: 90 and transmitted message is: good morning i am in transmit fun with priority: 90 and transmitted message is: good evening back in main again i am in receiver fun with priority: 99 i am in receiver task i am in transmit fun with priority: 90 and transmitted message is: message queues i am in receiver fun with priority: 99 and received message is: hello, world i am in receiver task i am in transmit fun with priority: 90 and transmitted message is: semaphores i am in receiver fun with priority: 99 and received message is: good morning i am in receiver task i am in transmit fun with priority: 90 i am in receiver fun with priority: 99 and received message is: good evening i am in receiver task

i am in receiver fun with priority: 99 and received message is: message queue i am in receiver task i am in receiver fun with priority: 99 and received message is: semaphores again in main -----------------------------------------------*/ 6: Program to illustrate intertask communication by spawning the task from host shell. #include<stdio.h> #include<vxWorks.h> #include<semLib.h> #include<msgQLib.h> int rx_fun(); int tx_fun(); int tid1,tid2,tid3,tid_main,priority; MSG_Q_ID msg_que_id; int main() { msg_que_id=msgQCreate(3,25,1); printf("\n\ni am in main"); tid1=taskSpawn("my_task1",99,0,1000,rx_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again, spawn the task from host shell \n"); return(0); } int rx_fun() { char rx_buff[25]; int i; taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d",priority); for(i=0;i<3;i++) { msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff); } return(0); } int tx_fun() { char tx_buff[25]={"hello"}; taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d and transmitted message is: %s",priority,tx_buff); msgQSend(msg_que_id,tx_buff,13,0,0); return(0); }

/* --------------output-----------------------i am in main i am in receiver fun with priority: 99 back in main again, spawn the task from host shell i am in transmit fun with priority: 100 and transmitted message is: hello i am in receiver fun with priority: 99 and received message is: hello i am in transmit fun with priority: 100 and transmitted message is: hello i am in receiver fun with priority: 99 and received message is: hello i am in transmit fun with priority: 100 and transmitted message is: hello i am in receiver fun with priority: 99 and received message is: hello -----------------------------------------------*/

6: Program to illustrate ITC and task priorities by spawning the task from host shell #include<stdio.h> #include<vxWorks.h> #include<semLib.h> #include<msgQLib.h> int rx_fun1(); int rx_fun2(); int rx_fun3(); int tx_fun(); int tid,tid1,tid2,tid3,tid_main,priority; MSG_Q_ID msg_que_id; int main() { msg_que_id=msgQCreate(3,25,1); printf("\n\ni am in main"); tid1=taskSpawn("my_task1",90,0,1000,rx_fun1,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",99,0,1000,rx_fun2,0,0,0,0,0,0,0,0,0,0); tid3=taskSpawn("my_task3",98,0,1000,rx_fun3,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again, spawn the task from host shell \n"); return(0); }

int rx_fun1() { char rx_buff[25]; msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff); return(0); }

int rx_fun2() { char rx_buff[25]; msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid2,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff); return(0); }

int rx_fun3() { char rx_buff[25]; msgQReceive(msg_que_id,rx_buff,13,WAIT_FOREVER); taskPriorityGet(tid3,&priority); printf("\n i am in receiver fun with priority: %d and received message is: %s",priority,rx_buff); return(0); } int tx_fun() { char tx_buff[25]={"hello"}; taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d and transmitted message is: %s",priority,tx_buff); msgQSend(msg_que_id,tx_buff,13,0,0); return(0); } /* ----------------------------------------output--------------------------------------> sp main Task spawned: id = 0x103d7c58, name = t2 value = 272464984 = 0x103d7c58 = 'X' -> i am in main back in main again, spawn the task from host shell -> sp tx_fun Task spawned: id = 0x103d7c58, name = t3 value = 272464984 = 0x103d7c58 = 'X' -> i am in transmit fun with priority: 99 and transmitted message is: hello i am in receiver fun with priority: 90 and received message is: hello -> sp tx_fun Task spawned: id = 0x103d7c58, name = t4 value = 272464984 = 0x103d7c58 = 'X'

-> i am in transmit fun with priority: 99 and transmitted message is: hello i am in receiver fun with priority: 98 and received message is: hello -> sp tx_fun Task spawned: id = 0x103d7c58, name = t5 value = 272464984 = 0x103d7c58 = 'X' -> i am in transmit fun with priority: 99 and transmitted message is: hello i am in receiver fun with priority: 99 and received message is: hello ------------------------------------------------------------------------*/

#include<stdio.h> #include<vxWorks.h> #include<semLib.h> #include<msgQLib.h> #define MSG_SIZE 50 int rx_fun(); int tx_fun(); int tid,tid1,tid2,tid3,tid_main,priority; MSG_Q_ID msg_que_id; int main() { msg_que_id=msgQCreate(3,MSG_SIZE,1); printf("\n\ni am in main"); tid1=taskSpawn("my_task1",99,0,1000,rx_fun,0,0,0,0,0,0,0,0,0,0); tid2=taskSpawn("my_task2",90,0,1000,tx_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again, spawn the task from host shell \n"); return(0); }

int rx_fun() { char rx_buff[50],n; printf("\n i am in receiver fun"); n=msgQReceive(msg_que_id,rx_buff,MSG_SIZE,WAIT_FOREVER); taskPriorityGet(tid1,&priority); printf("\n i am in receiver fun with priority: %d and received message is: ''%s'' with size: %d",priority,rx_buff,n); return(0); }

int tx_fun() { char tx_buff[25]={"good morning india"}; int n; n=msgQSend(msg_que_id,tx_buff,strlen(tx_buff),0,0); taskPriorityGet(tid2,&priority); printf("\n i am in transmit fun with priority: %d and transmitted message is: ''%s'' with size: %d",priority,tx_buff,n); return(0); } /* ----------------------------------------output-------------------------------------i am in main i am in receiver fun i am in transmit fun with priority: 90 and transmitted message is: ''good morning india'' with size: 0 i am in receiver fun with priority: 99 and received message is: ''good morning india'' with size: 18 back in main again, spawn the task from host shell NOTE: 1> if the size of the data to be transmitted is greater then the message size then the message will not be sent and the fun(msgQsend) returns -1 or else data will be transmitted successfully and it returns 0. 2> the msgQreceive fun receives the message and returns the number of bytes it has received( nothing but the size of message which was transmitted).... ------------------------------------------------------------------------*/

Semaphores
1: Proram to illustrate priority inversion #include<stdio.h> #include<vxWorks.h> #include<semLib.h> int low_pri_fun(); int med_pri_fun(); int high_pri_fun(); int tid1,tid2,tid3,tid_main,priority; SEM_ID sem_one_id, sem_two_id; MSG_Q_ID msg_que_id; int main() { sem_one_id=semMCreate(SEM_Q_PRIORITY); sem_two_id=semMCreate(SEM_Q_PRIORITY); printf("\n\ni am in main"); tid1=taskSpawn("low_pri_task",99,0,1000,low_pri_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again\n"); return(0); }

int low_pri_fun() { taskPriorityGet(tid1,&priority); printf("\n i am low_pri_task with priority: %d",priority); semTake(sem_one_id,WAIT_FOREVER); tid2=taskSpawn("high_pri_task",90,0,1000,high_pri_fun,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid1,&priority); printf("\n back in low_pri_task with priority: %d",priority); tid3=taskSpawn("med_pri_task",95,0,1000,med_pri_fun,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid1,&priority); printf("\n again in low_pri_task with priority: %d",priority); semGive(sem_one_id); taskPriorityGet(tid1,&priority); printf("\n again in low_pri_task with priority: %d",priority); return(0); }

int med_pri_fun() { taskPriorityGet(tid3,&priority); printf("\n i am med_pri_task with priority: %d",priority); semTake(sem_two_id,WAIT_FOREVER); taskPriorityGet(tid3,&priority); printf("\n still in med_pri_task with priority: %d",priority); semGive(sem_two_id); taskPriorityGet(tid3,&priority); printf("\n finally in med_pri_task with priority: %d",priority); return(0); } int high_pri_fun() { taskPriorityGet(tid2,&priority); printf("\n i am high_pri_task with priority: %d",priority); semTake(sem_one_id,WAIT_FOREVER); taskPriorityGet(tid2,&priority); printf("\n still in high_pri_task with priority: %d",priority); semGive(sem_one_id); taskPriorityGet(tid2,&priority); printf("\n finally in high_pri_task with priority: %d",priority); return(0); } /* --------------output-----------------------i am in main i am low_pri_task with priority: 99 i am high_pri_task with priority: 90 back in low_pri_task with priority: 99 i am med_pri_task with priority: 95 still in med_pri_task with priority: 95 finally in med_pri_task with priority: 95 again in low_pri_task with priority: 99 still in high_pri_task with priority: 90 finally in high_pri_task with priority: 90 again in low_pri_task with priority: 99 back in main again -----------------------------------------------*/

2: Proram to illustrate priority inheritance #include<stdio.h> #include<vxWorks.h> #include<semLib.h> int low_pri_fun(); int med_pri_fun(); int high_pri_fun(); int tid1,tid2,tid3,tid_main,priority; SEM_ID sem_one_id,sem_two_id; MSG_Q_ID msg_que_id; int main() { sem_one_id=semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE); sem_two_id=semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE); printf("\n\ni am in main"); tid1=taskSpawn("low_pri_task",99,0,1000,low_pri_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again\n"); return(0); }

int low_pri_fun() { taskPriorityGet(tid1,&priority); printf("\n i am low_pri_task with priority: %d",priority); semTake(sem_one_id,WAIT_FOREVER); tid2=taskSpawn("high_pri_task",90,0,1000,high_pri_fun,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid1,&priority); printf("\n back in low_pri_task with priority: %d",priority); tid3=taskSpawn("med_pri_task",95,0,1000,med_pri_fun,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid1,&priority); printf("\n again in low_pri_task with priority: %d",priority); semGive(sem_one_id); taskPriorityGet(tid1,&priority); printf("\n finally in low_pri_task with priority: %d",priority); return(0); }

int med_pri_fun() { taskPriorityGet(tid3,&priority); printf("\n i am med_pri_task with priority: %d",priority); semTake(sem_two_id,WAIT_FOREVER); taskPriorityGet(tid3,&priority); printf("\n still in med_pri_task with priority: %d",priority); semGive(sem_two_id); taskPriorityGet(tid3,&priority); printf("\n finally in med_pri_task with priority: %d",priority); return(0); } int high_pri_fun() { taskPriorityGet(tid2,&priority); printf("\n i am high_pri_task with priority: %d",priority); semTake(sem_one_id,WAIT_FOREVER); taskPriorityGet(tid2,&priority); printf("\n still in high_pri_task with priority: %d",priority); semGive(sem_one_id); taskPriorityGet(tid2,&priority); printf("\n finally in high_pri_task with priority: %d",priority); return(0); } /* --------------output-----------------------i am in main i am low_pri_task with priority: 99 i am high_pri_task with priority: 90 back in low_pri_task with priority: 90 again in low_pri_task with priority: 90 still in high_pri_task with priority: 90 finally in high_pri_task with priority: 90 i am med_pri_task with priority: 95 still in med_pri_task with priority: 95 finally in med_pri_task with priority: 95 finally in low_pri_task with priority: 99 back in main again -----------------------------------------------*/

#include<stdio.h> #include<vxWorks.h> #include<semLib.h> int low_pri_fun(); int med_pri_fun(); int high_pri_fun(); int highest_pri_fun(); int tid1,tid2,tid3,tid4,tid_main,priority; SEM_ID sem_one_id,sem_two_id; MSG_Q_ID msg_que_id; int main() { sem_one_id=semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE); sem_two_id=semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE); printf("\n\ni am in main"); tid1=taskSpawn("low_pri_task",99,0,1000,low_pri_fun,0,0,0,0,0,0,0,0,0,0); printf("\nback in main again\n"); return(0); }

int low_pri_fun() { taskPriorityGet(tid1,&priority); printf("\n i am low_pri_task with priority: %d",priority); semTake(sem_one_id,WAIT_FOREVER); tid2=taskSpawn("high_pri_task",90,0,1000,high_pri_fun,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid1,&priority); printf("\n back in low_pri_task with priority: %d",priority); tid3=taskSpawn("med_pri_task",95,0,1000,med_pri_fun,0,0,0,0,0,0,0,0,0,0); tid4=taskSpawn("highest_pri_task",80,0,1000,highest_pri_fun,0,0,0,0,0,0,0,0,0,0); taskPriorityGet(tid1,&priority); printf("\n again in low_pri_task with priority: %d",priority); semGive(sem_one_id); taskPriorityGet(tid1,&priority); printf("\n finally in low_pri_task with priority: %d",priority); return(0); }

int med_pri_fun() { taskPriorityGet(tid3,&priority); printf("\n i am med_pri_task with priority: %d",priority); semTake(sem_two_id,WAIT_FOREVER); taskPriorityGet(tid3,&priority); printf("\n still in med_pri_task with priority: %d",priority); semGive(sem_two_id); taskPriorityGet(tid3,&priority); printf("\n finally in med_pri_task with priority: %d",priority); return(0); } int high_pri_fun() { taskPriorityGet(tid2,&priority); printf("\n i am high_pri_task with priority: %d",priority); semTake(sem_one_id,WAIT_FOREVER); taskPriorityGet(tid2,&priority); printf("\n still in high_pri_task with priority: %d",priority); semGive(sem_one_id); taskPriorityGet(tid2,&priority); printf("\n finally in high_pri_task with priority: %d",priority); return(0); } int highest_pri_fun() { taskPriorityGet(tid4,&priority); printf("\n i am highest_pri_task with priority: %d",priority); semTake(sem_one_id,WAIT_FOREVER); taskPriorityGet(tid4,&priority); printf("\n still in highest_pri_task with priority: %d",priority);

semGive(sem_one_id); taskPriorityGet(tid4,&priority); printf("\n finally in highest_pri_task with priority: %d",priority); return(0); }

/* --------------output------------------------

i am in main i am low_pri_task with priority: 99 i am high_pri_task with priority: 90 back in low_pri_task with priority: 90 i am highest_pri_task with priority: 80 again in low_pri_task with priority: 80 still in highest_pri_task with priority: 80 finally in highest_pri_task with priority: 80 still in high_pri_task with priority: 90 finally in high_pri_task with priority: 90 i am med_pri_task with priority: 95 still in med_pri_task with priority: 95 finally in med_pri_task with priority: 95 finally in low_pri_task with priority: 99 back in main again

#include "vxWorks.h" #include "taskLib.h" #include "semLib.h" #include "stdio.h" /* function prototypes */ void taskOne(void); void taskTwo(void); /* globals */ #define ITER 5 SEM_ID semBinary; int global = 0; void main(void) { int taskIdOne, taskIdTwo; /* create semaphore with semaphore available and queue tasks on FIFO basis */ semBinary = semBCreate(SEM_Q_FIFO, SEM_FULL); /* Note 1: lock the semaphore for scheduling purposes */ semTake(semBinary,WAIT_FOREVER); /* spawn the two tasks */ taskIdOne = taskSpawn("t1",90,0x100,2000,(FUNCPTR)taskOne,0,0,0,0,0,0,0,0,0,0); taskIdTwo = taskSpawn("t2",90,0x100,2000,(FUNCPTR)taskTwo,0,0,0,0,0,0,0,0,0,0); }

void taskOne(void) { int i; for (i=0; i < ITER; i++) { semTake(semBinary,WAIT_FOREVER); /* wait indefinitely for semaphore */ printf("I am taskOne and global = %d......................\n", ++global); semGive(semBinary); /* give up semaphore */ } } void taskTwo(void) { int i; semGive(semBinary); /* Note 2: give up semaphore(a scheduling fix) */ for (i=0; i < ITER; i++) { semTake(semBinary,WAIT_FOREVER); /* wait indefinitely for semaphore */ printf("I am taskTwo and global = %d----------------------\n", --global);

semGive(semBinary); /* give up semaphore */ } }

/* -----------------------output------------------------*/

I am taskOne and global = 1...................... I am taskTwo and global = 0---------------------I am taskOne and global = 1...................... I am taskTwo and global = 0---------------------I am taskOne and global = 1...................... I am taskTwo and global = 0---------------------I am taskOne and global = 1...................... I am taskTwo and global = 0---------------------I am taskOne and global = 1...................... I am taskTwo and global = 0----------------------

Potrebbero piacerti anche