void taskh_func(long tid) { RTIME time; unsigned int msg = 0, wait; rt_send(&taskm, msg); rt_send(&taskl, msg); while (1) { rt_receive(&taskm, &msg); time = rt_get_time_ns(); if (MUTEX_LOCK(&mutex) <= 1) { if ((wait = (int)(rt_get_time_ns() - time)) > 250000) { rt_printk("PRIORITY INVERSION, WAITED FOR %d us\n", wait/1000); } else { rt_printk("NO PRIORITY INVERSION, WAITED FOR %d us\n", wait/1000); } if (SemType) { MUTEX_LOCK(&mutex); MUTEX_LOCK(&mutex); rt_busy_sleep(100000); MUTEX_LOCK(&mutex); } rt_busy_sleep(100000); if (SemType) { rt_sem_signal(&mutex); rt_busy_sleep(100000); rt_sem_signal(&mutex); rt_sem_signal(&mutex); } rt_sem_signal(&mutex); } else { rt_task_suspend(0); } } }
static void driver(int t) { RT_TASK *thread[NTASKS]; int i, l; unsigned int msg = 0; RTIME now; for (i = 1; i < NTASKS; i++) { thread[0] = rt_receive(0, &msg); thread[msg] = thread[0]; } for (i = 1; i < NTASKS; i++) { rt_return(thread[i], i); } now = rt_get_time(); rt_task_make_periodic(rt_whoami(), now + NTASKS*tick_period, tick_period); msg = 0; l = LOOPS; while(l--) { for (i = 1; i < NTASKS; i++) { cpu_used[hard_cpu_id()]++; if (i%2) { rt_rpc(thread[i], msg, &msg); } else { rt_send(thread[i], msg); msg = 1 - msg; } rt_task_wait_period(); } } for (i = 1; i < NTASKS; i++) { rt_send(thread[i], END); } }
static void ClockChrono_Read(long t) { char ch; unsigned int run = 0; while(1) { cpu_used[hard_cpu_id()]++; rt_sem_wait(&keybrd_sem); rtf_get(Keyboard, &ch, 1); ch = toupper(ch); switch(ch) { case 'T': case 'R': case 'H': case 'M': case 'S': CommandClock_Put(ch); break; case 'C': case 'I': case 'E': CommandChrono_Put(ch); break; case 'N': hide = ~hide; break; case 'P': pause = TRUE; rt_fractionated_sleep(nano2count(FIVE_SECONDS)); pause = FALSE; break; case 'K': case 'D': run += ch; if (run == ('K' + 'D')) { rt_send(&clock, run); rt_send(&chrono, run); } break; } } }
void taskm_func(long tid) { unsigned int msg = 0; rt_receive(0, &msg); rt_send(&taskl, msg); while (1) { rt_receive(&taskl, &msg); rt_send(&taskh, msg); rt_busy_sleep(5000000); } }
static void *logMsg_fun(void *args) { RT_TASK *buddy; struct { int nch; FILE *fs; } type; char msg[MAX_MSG_SIZE]; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); if (!(buddy = rt_task_init_schmod(nam2num("LOGSRV"), 1, 0, 0, SCHED_FIFO, 0xFFFFFFFF))) { printf("CANNOT INIT LOG MSG SERVER BUDDY TASK %p.\n", buddy); return (void *)1; } rt_send(*(RT_TASK **)args, (unsigned int)buddy); while (1) { if (!rt_mbx_receive(logmbx, &type, sizeof(type)) && !rt_mbx_receive(logmbx, msg, type.nch)) { if (type.fs) { fprintf(type.fs, msg); } else { fprintf(stderr, msg); fprintf(logfile, msg); } } else { while(!rt_mbx_receive_wp(logmbx, msg, MAX_MSG_SIZE)); } } return (void *)0; }
static void Display_task(long t) { unsigned long command; int ackn = 0; RT_TASK *get = (RT_TASK *)0, *tput = (RT_TASK *)0, *hput = (RT_TASK *)0, *task; while (ackn != ('g' + 'p' + 'P')) { task = rt_receive((RT_TASK *)0, &command); switch (command) { case 'g': get = task; ackn += command; break; case 'p': tput = task; ackn += command; break; case 'P': hput = task; ackn += command; break; } } rt_return(get, command); rt_return(tput, command); rt_return(hput, command); while(1) { cpu_used[hard_cpu_id()]++; task = rt_receive(0, &command); if (task == tput || task == hput) { rt_send(get, command); } } }
void *CommandClock_task(void *args) { RT_TASK *mytask; unsigned long command; char R = 'R'; int ackn = 0; RT_TASK *get = (RT_TASK *)0, *put = (RT_TASK *)0, *task; if (!(mytask = rt_thread_init(nam2num("CLKTSK"), 1, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT TASK CommandClock_task\n"); exit(1); } printf("INIT TASK CommandClock_task %p.\n", mytask); mlockall(MCL_CURRENT | MCL_FUTURE); Clockstatus = stopped; while (ackn != ('a' + 'b')) { task = rt_receive((RT_TASK *)0, &command); switch (command) { case 'b': get = task; ackn += command; break; case 'a': put = task; ackn += command; break; } } rt_return(put, command); rt_return(get, command); while(1) { switch (Clockstatus) { case stopped: rt_receive(put, &command); if (command == 'R') { Clockstatus = running; } break; case running: if (rt_receive_if(put, &command)) { if (command == 'T') { Clockstatus = stopped; } } else { command = R; } break; } rt_send(get, command); if (command == 'F') { goto end; } } end: rt_task_delete(mytask); printf("END TASK CommandClock_task %p.\n", mytask); return 0; }
void *thread_fun(void *arg) { funtask = rt_task_init_schmod(0xcaccb, 0, 0, 0, SCHED_FIFO, 0x1); rt_printk("FUN INIT\n"); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); if (!SNDBRCV) { rt_sleep(nano2count(100000000)); } if (USE_RPC) { unsigned long msg; rt_printk("FUN RPC\n"); rt_rpc(maintask, 0, &msg); } else { rt_printk("FUN SEND\n"); rt_send(maintask, 0); rt_printk("FUN SUSP\n"); rt_task_suspend(funtask); } rt_printk("FUN DONE\n"); rt_task_delete(funtask); rt_printk("FUN END\n"); return 0; }
static void CommandChrono_task(long t) { RTIME fiveSeconds = nano2count(FIVE_SECONDS); unsigned long command; unsigned int buffered = 0; unsigned int C = 'C'; unsigned int R = 'R'; int ackn = 0; RT_TASK *get = (RT_TASK *)0, *put = (RT_TASK *)0, *task; Chronostatus = stoppedInitial; while (ackn != ('c' + 'd')) { task = rt_receive((RT_TASK *)0, &command); switch (command) { case 'd': get = task; ackn += command; break; case 'c': put = task; ackn += command; break; } } rt_return(put, command); rt_return(get, command); while(1) { cpu_used[hard_cpu_id()]++; switch (Chronostatus) { case stoppedInitial: if (buffered) { command = buffered; buffered = 0; } else { rt_receive(put, &command); } Chronostatus = running; break; case running: if (rt_receive_if(put, &command)) { if (command == 'E') { Chronostatus = stoppedFinal; } } else { command = C; } break; case stoppedFinal: Chronostatus = stoppedInitial; if (rt_receive_timed(put, &command, fiveSeconds) > 0) { buffered = command; } command = R; break; } rt_send(get, command); } }
int main(void) { int i, indx[NTASKS]; unsigned long mytask_name = nam2num("MASTER"); signal(SIGINT, endme); if (!(mytask = rt_task_init(mytask_name, 1, 0, 0))) { printf("CANNOT INIT TASK %lu\n", mytask_name); exit(1); } printf("MASTER INIT: name = %lu, address = %p.\n", mytask_name, mytask); sem = rt_sem_init(10000, 0); rt_set_oneshot_mode(); // rt_set_periodic_mode(); start_rt_timer(0); for (i = 0; i < ntasks; i++) { indx[i] = i; if (!(task[i] = rt_thread_create(thread_fun, &indx[i], 10000))) { printf("ERROR IN CREATING THREAD %d\n", indx[i]); exit(1); } } for (i = 0; i < ntasks; i++) { while (!rt_get_adr(taskname(i))) { rt_sleep(nano2count(20000000)); } } for (i = 0; i < ntasks; i++) { rt_send(rt_get_adr(taskname(i)), (unsigned long)sem); } for (i = 0; i < ntasks; i++) { rt_sem_wait(sem); } for (i = 0; i < ntasks; i++) { while (rt_get_adr(taskname(i))) { rt_sleep(nano2count(20000000)); } } for (i = 0; i < ntasks; i++) { rt_thread_join(task[i]); } rt_sem_delete(sem); stop_rt_timer(); rt_task_delete(mytask); printf("MASTER %lu %p ENDS\n", mytask_name, mytask); return 0; }
void CommandChrono_Put(char command) { static RT_TASK *ackn = 0; unsigned int put = 'c'; unsigned long msg; if (ackn != &Chrono) { ackn = rt_rpc(&Chrono, put, &msg); } if ((Chronostatus == running) != (command == 'C')) { rt_send(&Chrono, (unsigned int)command); } }
void CommandClock_Put(char command) { static RT_TASK *ackn = 0; unsigned int put = 'a'; unsigned long msg; if (ackn != rt_get_adr(nam2num("CLKTSK"))) { ackn = rt_rpc(rt_get_adr(nam2num("CLKTSK")), put, &msg); } if (((Clockstatus == running) == (command == 'T')) || command == 'F') { rt_send(rt_get_adr(nam2num("CLKTSK")), (unsigned int)command); } }
void *Display_task(void *args) { RT_TASK *mytask; unsigned long command; int ackn = 0; RT_TASK *get = (RT_TASK *)0, *tput = (RT_TASK *)0, *hput = (RT_TASK *)0, *task; if (!(mytask = rt_thread_init(nam2num("DSPTSK"), 1, 0, SCHED_FIFO, 0xF))) { printf("CANNOT INIT TASK Display_task\n"); exit(1); } printf("INIT TASK Display_task %p.\n", mytask); mlockall(MCL_CURRENT | MCL_FUTURE); while (ackn != ('g' + 'p' + 'P')) { task = rt_receive((RT_TASK *)0, &command); switch (command) { case 'g': get = task; ackn += command; break; case 'p': tput = task; ackn += command; break; case 'P': hput = task; ackn += command; break; } } rt_return(get, command); rt_return(tput, command); rt_return(hput, command); while(1) { task = rt_receive(0, &command); if (task == tput || task == hput) { rt_send(get, command); if (((char *)command)[1] == 101) { goto end; } } } end: rt_task_delete(mytask); printf("END TASK Display_task %p.\n", mytask); return 0; }
int main(void) { RT_TASK *receiving_task; RT_TASK *agentask; int i, *shm; unsigned int msg, chksum; struct sched_param mysched; mysched.sched_priority = 99; if( sched_setscheduler( 0, SCHED_FIFO, &mysched ) == -1 ) { puts(" ERROR IN SETTING THE SCHEDULER UP"); perror( "errno" ); exit( 0 ); } mlockall(MCL_CURRENT | MCL_FUTURE); receiving_task = rt_task_init(nam2num("RTSK"), 0, 0, 0); agentask = rt_get_adr(nam2num("ATSK")); shm = rtai_malloc(nam2num("MEM"), 1); while(1) { printf("RECEIVING TASK RPCS TO AGENT TASK %x\n", 0xaaaaaaaa); rt_rpc(agentask, 0xaaaaaaaa, &msg); printf("AGENT TASK RETURNED %x\n", msg); if (msg != 0xeeeeeeee) { chksum = 0; for (i = 1; i <= shm[0]; i++) { chksum += shm[i]; } printf("RECEIVING TASK: CHECKSUM = %x\n", chksum); if (chksum != shm[shm[0] + 1]) { printf("RECEIVING TASK: WRONG SHMEM CHECKSUM\n"); } printf("RECEIVING TASK SENDS TO AGENT TASK %x\n", 0xaaaaaaaa); rt_send(agentask, 0xaaaaaaaa); } else { printf("RECEIVING TASK DELETES ITSELF\n"); rt_task_delete(receiving_task); printf("END RECEIVING TASK\n"); exit(1); } } return 0; }
void taskl_func(long tid) { unsigned int msg = 0; rt_receive(0, &msg); rt_receive(0, &msg); while (MUTEX_LOCK(&mutex) <= 1) { if (SemType) { MUTEX_LOCK(&mutex); rt_busy_sleep(100000); MUTEX_LOCK(&mutex); } rt_send(&taskm, msg); rt_busy_sleep(100000); if (SemType) { rt_sem_signal(&mutex); rt_busy_sleep(100000); rt_sem_signal(&mutex); } rt_sem_signal(&mutex); rt_sleep(nano2count(500000000)); } rt_task_suspend(0); }
void *tskfun(void *tasknr) { int tsknr, k, prio, bprio; tsknr = (int)tasknr; task[tsknr] = rt_task_init_schmod(0, NTASKS - tsknr - 1, 0, 0, SCHED_FIFO, CPUS_ALLOWED); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); rt_sem_wait(sem[tsknr]); rt_send(task[0], 0); rt_sem_wait(sem[tsknr - 1]); rt_sem_signal(sem[tsknr]); rt_sem_signal(sem[tsknr - 1]); rt_printk("AT TSKNR EXIT %d > (TSKNR-PRI):\n", tsknr); for (k = 0; k < tsknr; k++) { rt_get_priorities(task[k], &prio, &bprio); rt_printk("%d-%d|", k, prio); } rt_get_priorities(task[tsknr], &prio, &bprio); rt_printk("%d-%d\n\n", tsknr, prio); return NULL; }
int main(int argc, char **argv) { int n, i, size, prflag; int pos = 3; int maxfd; int rt_sockfd,recv_ping_sockfd; int send_mulcst_fd, recv_mulcst_fd; const int on = 1; char buff_recv[BUFFSIZE]; char tour_array[TOURNUM][IPLEN]; char *ptr; fd_set rset; struct timeval tval, tval1, tval2, tval3, tval4; struct sockaddr_ll saddrll; Signal(SIGALRM, sig_alrm); //Signal(SIGCHLD, sig_chld); uname(&myname); if (argc == 1){ fputs("This is NOT source node\n", stdout); //_is_src_node = 0; source_flag = 0; }else{ fputs("I'm source node, start to send tour infomation \n", stdout); //_is_src_node = 1; source_flag = 1; } //if (uname(&myname) < 0) //err_sys("uname error"); for ( hwa = Get_hw_addrs(); hwa != NULL; hwa = hwa->hwa_next) { if(strcmp(hwa->if_name,"eth0")==0){ /* in eth0 we create pfsocket and bind it to hw addr, this pf socket is for sned ping info */ //pf_sockfd = socket(PF_PACKET, SOCK_RAW,htons(ETH_P_IP)); send_ping_pf_sockfd = socket(PF_PACKET, SOCK_RAW,htons(ETH_P_IP)); //send_ping_pf_sockfd use to send ping to source node saddrll.sll_family = PF_PACKET; saddrll.sll_ifindex = hwa->if_index; saddrll.sll_protocol = htons(ETH_P_IP); bind(send_ping_pf_sockfd, (struct sockaddr *) &saddrll, sizeof(saddrll)); printf("%s :%s", hwa->if_name, ((hwa->ip_alias) == IP_ALIAS) ? " (alias)\n" : "\n"); if ( (sa = hwa->ip_addr) != NULL) printf("IP addr = %s\n", Sock_ntop_host(sa, sizeof(*sa))); prflag = 0; i = 0; do { if (hwa->if_haddr[i] != '\0') { prflag = 1; break; } } while (++i < IF_HADDR); if (prflag) { printf(" HW addr = "); ptr = hwa->if_haddr; i = IF_HADDR; do { printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":"); } while (--i > 0); } printf("\n Interface Index = %d\n\n", hwa->if_index); break; } } /* creat two raw sockets for route travesal and receive ping*/ rt_sockfd = Socket(AF_INET, SOCK_RAW, 254); setsockopt(rt_sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)); recv_ping_sockfd = Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); setuid(getuid()); /* don't need special permissions any more */ size = 60 * 1024; /* OK if setsockopt fails */ setsockopt (recv_ping_sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)); if (source_flag == 1) {//this node is source struct icmp *icmp; struct ip* ip; int node_num; node_num = argc + 3; strcpy(tour_array[0], Sock_ntop_host(sa, sizeof(*sa)));//first one is source code sprintf(tour_array[1],"%d",argc - 1); sprintf(tour_array[2],"%d",node_num); sprintf(tour_array[3],"%d",4); for (i = 4; i < node_num; i++) strcpy(tour_array[i], getipbyvm(argv[i-3])); //////////////////////////before send we let the source node join the multicast send_mulcst_fd = Udp_client(MC_ADDR_LIN, MC_PORT_LIN, (void **) &sasend, &salen); recv_mulcst_fd = Socket(AF_INET, SOCK_DGRAM, 0); Setsockopt(recv_mulcst_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); sarecv = Malloc(salen); memcpy(sarecv, sasend, salen); Bind(recv_mulcst_fd, sarecv, salen); Mcast_join(recv_mulcst_fd, sasend, salen, NULL, 0); Mcast_set_loop(send_mulcst_fd, 0); /////////////////////////////////////////////// this is the send tour part rt_send(rt_sockfd, tour_array); for(;;){ if(first_mulcst_flag == 1){ printf("wait for first multicast info\n"); recv_all(recv_mulcst_fd, salen); first_mulcst_flag = 0; char buf[MAXLINE]; //sleep(1); snprintf(buf, sizeof(buf), "<<<<< Node: %s I am a member of the group.>>>>>\n", myname.nodename); send_all(send_mulcst_fd, sasend, salen, buf); }else{ for(;;){ recv_all(recv_mulcst_fd, salen); printf("Waiting for 5 seconds and exit\n"); } } } }else{//not source node pthread_t tid, tid2; char source_name[IPLEN]; for( ;; ) { FD_ZERO(&rset); FD_SET(rt_sockfd, &rset); maxfd = rt_sockfd; if(rt_recved_flag == 1){ FD_SET(recv_ping_sockfd, &rset); maxfd = max(recv_ping_sockfd, maxfd); } if (ns_first_flag == 0) { FD_SET(recv_mulcst_fd,&rset); //maxfd = (maxfd > recv_mulcst_fd) ? maxfd : recv_mulcst_fd; maxfd = max(maxfd, recv_mulcst_fd); } //printf("before select\n"); select(maxfd + 1, &rset, NULL, NULL, NULL); if (FD_ISSET(rt_sockfd, &rset)) { printf("receive route travelsal paket\n"); n = rt_recv(rt_sockfd, tour_array); memcpy(dest_ip_addr, tour_array[0], IPLEN); if (n < 0) { if (errno == EINTR) continue; else err_sys("receive tour packet error"); } get_vmname(tour_array[0], source_name); if (ns_first_flag == 1) { ns_first_flag = 0; rt_recved_flag = 1; // join the multicast first send_mulcst_fd = Udp_client(MC_ADDR_LIN, MC_PORT_LIN, (void **) &sasend, &salen); recv_mulcst_fd = Socket(AF_INET, SOCK_DGRAM, 0); Setsockopt(recv_mulcst_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); sarecv = Malloc(salen); memcpy(sarecv, sasend, salen); Bind(recv_mulcst_fd, sarecv, salen); Mcast_join(recv_mulcst_fd, sasend, salen, NULL, 0); Mcast_set_loop(send_mulcst_fd, 0); //create a thread for ping Pthread_create(&tid, NULL, &ping, NULL); } if(last_node_flag == 0){ rt_send(rt_sockfd, tour_array); }else{ //create a thread to handle last operations Pthread_create(&tid2, NULL, &ls_send_mul, &send_mulcst_fd); } } if (FD_ISSET(recv_mulcst_fd, &rset)) {//recv multicast info if (first_mulcst_flag == 1 ) { first_mulcst_flag = 0; ping_over_flag = 1;// //printf("ping_over_flag is %d\n", ping_over_flag); recv_all(recv_mulcst_fd, salen); char buf[MAXLINE]; snprintf(buf, sizeof(buf), "<<<<< Node: %s I am a member of the group.>>>>>\n", myname.nodename); send_all(send_mulcst_fd, sasend, salen, buf); //printf("gonna go to alarm with pof changed\n"); alarm(0); }else{ for(;;){ recv_all(recv_mulcst_fd, salen); } printf("Waiting for 5 seconds and exit\n"); } } if (FD_ISSET(recv_ping_sockfd, &rset)) {//recv ping reply //printf("received ping reply\n"); recvfrom(recv_ping_sockfd, buff_recv, MAXLINE, 0, NULL, NULL); Gettimeofday (&tval, NULL); proc_v4 (buff_recv, n, &tval); if (ping_over_flag == 1) alarm(0); } } } }
int main(int argc, char* argv[]) { unsigned long mtsk_name = nam2num("MTSK"); unsigned long btsk_name = nam2num("BTSK"); unsigned long sem_name = nam2num("SEM"); unsigned long smbx_name = nam2num("SMBX"); unsigned long rmbx_name = nam2num("RMBX"); unsigned long msg; long long mbx_msg; long long llmsg = 0xaaaaaaaaaaaaaaaaLL; RT_TASK *mtsk, *rcvd_from; SEM *sem; MBX *smbx, *rmbx; int pid, count; if (!(mtsk = rt_task_init_schmod(mtsk_name, 0, 0, 0, SCHED_FIFO, 0x1))) { printf("CANNOT INIT MASTER TASK\n"); exit(1); } printf("MASTER TASK INIT: name = %lx, address = %p.\n", mtsk_name, mtsk); printf("MASTER TASK STARTS THE ONESHOT TIMER\n"); rt_set_oneshot_mode(); start_rt_timer(nano2count(10000000)); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); rt_sleep(1000000); printf("MASTER TASK MAKES ITSELF PERIODIC WITH A PERIOD OF 1 ms\n"); rt_task_make_periodic(mtsk, rt_get_time(), nano2count(PERIOD)); rt_sleep(nano2count(1000000000)); count = PERIODIC_LOOPS; printf("MASTER TASK LOOPS ON WAIT_PERIOD FOR %d PERIODS\n", count); while(count--) { printf("PERIOD %d\n", count); rt_task_wait_period(); } count = SLEEP_LOOPS; printf("MASTER TASK LOOPS ON SLEEP 0.1 s FOR %d PERIODS\n", count); while(count--) { printf("SLEEPING %d\n", count); rt_sleep(nano2count(DELAY)); } printf("MASTER TASK YIELDS ITSELF\n"); rt_task_yield(); printf("MASTER TASK CREATES BUDDY TASK\n"); pid = fork(); if (!pid) { execl("./slave", "./slave", NULL); } printf("MASTER TASK SUSPENDS ITSELF, TO BE RESUMED BY BUDDY TASK\n"); rt_task_suspend(mtsk); printf("MASTER TASK RESUMED BY BUDDY TASK\n"); if (!(sem = rt_sem_init(sem_name, 0))) { printf("CANNOT CREATE SEMAPHORE %lx\n", sem_name); exit(1); } printf("MASTER TASK CREATES SEM: name = %lx, address = %p.\n", sem_name, sem); printf("MASTER TASK WAIT_IF ON SEM\n"); rt_sem_wait_if(sem); printf("MASTER STEP BLOCKS WAITING ON SEM\n"); rt_sem_wait(sem); printf("MASTER TASK SIGNALLED BY BUDDY TASK WAKES UP AND BLOCKS WAIT TIMED 1 s ON SEM\n"); rt_sem_wait_timed(sem, nano2count(1000000000)); printf("MASTER TASK DELETES SEM\n"); rt_sem_delete(sem); printf("MASTER TASK BLOCKS RECEIVING FROM ANY\n"); rcvd_from = rt_receive(0, (void *)&msg); printf("MASTER TASK RECEIVED MESSAGE %lx FROM BUDDY TASK\n", msg); printf("MASTER TASK RPCS TO BUDDY TASK THE MESSAGE %lx\n", 0xabcdefL); rcvd_from = rt_rpc(rcvd_from, 0xabcdef, (void *)&msg); printf("MASTER TASK RECEIVED THE MESSAGE %lx RETURNED BY BUDDY TASK\n", msg); //exit(1); if (!(smbx = rt_mbx_init(smbx_name, 1))) { printf("CANNOT CREATE MAILBOX %lx\n", smbx_name); exit(1); } if (!(rmbx = rt_mbx_init(rmbx_name, 1))) { printf("CANNOT CREATE MAILBOX %lx\n", rmbx_name); exit(1); } printf("MASTER TASK CREATED TWO MAILBOXES %p %p %p %p \n", smbx, rmbx, &mtsk_name, &msg); count = MBX_LOOPS; while(count--) { rt_mbx_send(smbx, &llmsg, sizeof(llmsg)); printf("%d MASTER TASK SENDS THE MESSAGE %llx MBX\n", count, llmsg); mbx_msg = 0; rt_mbx_receive_timed(rmbx, &mbx_msg, sizeof(mbx_msg), nano2count(MSG_DELAY)); printf("%d MASTER TASK RECEIVED THE MESSAGE %llx FROM MBX\n", count, mbx_msg); rt_sleep(nano2count(DELAY)); } printf("MASTER TASK SENDS THE MESSAGE %lx TO BUDDY TO ALLOW ITS END\n", 0xeeeeeeeeL); rt_send(rcvd_from, 0xeeeeeeee); printf("MASTER TASK WAITS FOR BUDDY TASK END\n"); while (rt_get_adr(btsk_name)) { rt_sleep(nano2count(1000000000)); } printf("MASTER TASK STOPS THE PERIODIC TIMER\n"); stop_rt_timer(); printf("MASTER TASK DELETES MAILBOX %p\n", smbx); rt_mbx_delete(smbx); printf("MASTER TASK DELETES MAILBOX %p\n", rmbx); rt_mbx_delete(rmbx); printf("MASTER TASK DELETES ITSELF\n"); rt_task_delete(mtsk); printf("END MASTER TASK\n"); return 0; }
static int modify_vnet_link(int cmd, const char* if_name, const char* peer_name, int ns_pid) { int llen; int fd = 0; unsigned int seq; struct sockaddr_nl loc_addr; rt_link_request_t r; int ret = 0; printf("modifyLink: begin. \n"); memset(&r, 0, sizeof(r)); r.ifi.ifi_family = PF_UNSPEC; r.ifi.ifi_index = 0; r.ifi.ifi_flags = 0; if (cmd == RTM_NEWLINK) { rt_link_init(&r.n, RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE); /*unsigned char mac[6] = { 0xa, 1, 2, 3, 4, 5}; rt_addAttr_data(&r.n, IFA_ADDRESS, mac, 6);*/ llen = strlen(if_name) + 1; rt_addAttr_data(&r.n, IFLA_IFNAME, if_name, llen); char* if_kind = "veth"; struct rtattr *rta_linkinfo = rt_addAttr_hdr(&r.n, IFLA_LINKINFO); rt_addAttr_data(&r.n, IFLA_INFO_KIND, if_kind, strlen(if_kind) + 1); struct rtattr *rta_datainfo = rt_addAttr_hdr(&r.n, IFLA_INFO_DATA); struct ifinfomsg ifi_p; ifi_p.ifi_family = PF_UNSPEC; ifi_p.ifi_index = 0; ifi_p.ifi_flags = 0; struct rtattr *rta_peerinfo = rt_addAttr_data(&r.n, VETH_INFO_PEER, (void*)&ifi_p, sizeof(struct ifinfomsg)); llen = strlen(peer_name) + 1; rt_addAttr_data(&r.n, IFLA_IFNAME, peer_name, llen); if (ns_pid > 0) { unsigned int pid = ns_pid; rt_addAttr_data(&r.n, IFLA_NET_NS_PID, (void*)&pid, sizeof(unsigned int)); } /*unsigned char mac2[6] = { 0xa, 1, 2, 3, 4, 6}; rt_addAttr_data(&r.n, IFA_ADDRESS, mac2, 6);*/ rt_compAttr_hdr(&r.n, rta_peerinfo); rt_compAttr_hdr(&r.n, rta_datainfo); rt_compAttr_hdr(&r.n, rta_linkinfo); } else if (cmd == RTM_DELLINK) { rt_link_init(&r.n, RTM_DELLINK, NLM_F_REQUEST); llen = strlen(if_name) + 1; rt_addAttr_data(&r.n, IFLA_IFNAME, if_name, llen); } /*--------*/ /* Now open a netlink socket */ if ((fd = rt_open(&loc_addr)) < 0) { ret = -1; goto end; } seq = time(NULL); /* Send the attribute message and wait for ACK */ if (rt_send(fd, &seq, &r.n, &loc_addr) < 0) { ret = -1; goto end; } /*---------*/ printf("modifyLink: happy end. \n"); end: if (fd > 0) { close(fd); } return ret; }
static int rt_Main(int priority) { SEM *hard_timers_cnt; char name[7]; RTIME rt_BaseTaskPeriod; struct timespec err_timeout; int i; rt_allow_nonroot_hrt(); for (i = 0; i < MAX_NTARGETS; i++) { sprintf(name,"MNT%d",i); if (!rt_get_adr(nam2num(name))) break; } if (!(rt_MainTask = rt_task_init_schmod(nam2num(name), rt_MainTaskPriority, 0, 0, SCHED_RR, 0xFF))) { fprintf(stderr,"Cannot init rt_MainTask.\n"); return 1; } sem_init(&err_sem, 0, 0); printf("TARGET STARTS.\n"); pthread_create(&rt_HostInterfaceThread, NULL, rt_HostInterface, NULL); err_timeout.tv_sec = (long int)(time(NULL)) + 1; err_timeout.tv_nsec = 0; if ((sem_timedwait(&err_sem, &err_timeout)) != 0) { fprintf(stderr, "Target is terminated.\n"); goto finish; } pthread_create(&rt_BaseRateThread, NULL, rt_BaseRate, &priority); err_timeout.tv_sec = (long int)(time(NULL)) + 1; err_timeout.tv_nsec = 0; if ((sem_timedwait(&err_sem, &err_timeout)) != 0) { endInterface = 1; rt_send(rt_HostInterfaceTask, 0); pthread_join(rt_HostInterfaceThread, NULL); fprintf(stderr, "Target is terminated.\n"); goto finish; } rt_BaseTaskPeriod = (RTIME) (1e9*get_tsamp()); if (InternTimer) { WaitTimingEvent = (void *)rt_task_wait_period; if (!(hard_timers_cnt = rt_get_adr(nam2num("HTMRCN")))) { if (!ClockTick) { rt_set_oneshot_mode(); start_rt_timer(0); rt_BaseRateTick = nano2count(rt_BaseTaskPeriod); } else { rt_set_periodic_mode(); rt_BaseRateTick = start_rt_timer(nano2count(rt_BaseTaskPeriod)); } hard_timers_cnt = rt_sem_init(nam2num("HTMRCN"), 0); } else { rt_BaseRateTick = nano2count(rt_BaseTaskPeriod); rt_sem_signal(hard_timers_cnt); } } else { WaitTimingEvent = (void *)DummyWait; SendTimingEvent = (void *)DummySend; } if (verbose) { printf("Model : %s .\n", modelname); printf("Executes on CPU map : %x.\n", CpuMap); printf("Sampling time : %e (s).\n", get_tsamp()); } { int msg; rt_receive(0, &msg); } if (WaitToStart) { if (verbose) { printf("Target is waiting to start ... "); fflush(stdout); } rt_task_suspend(rt_MainTask); } if (verbose) { printf("Target is running.\n"); } rt_return(rt_BaseRateTask, 0); isRunning = 1; while (!endex && (!FinalTime || SIM_TIME < FinalTime)) { msleep(POLL_PERIOD); } endBaseRate = 1; if (!InternTimer) { SendTimingEvent(TimingEventArg); } pthread_join(rt_BaseRateThread, NULL); isRunning = 0; endInterface = 1; rt_send(rt_HostInterfaceTask, 0); if (verbose) { printf("Target has been stopped.\n"); } pthread_join(rt_HostInterfaceThread, NULL); if (InternTimer) { if (!rt_sem_wait_if(hard_timers_cnt)) { rt_sem_delete(hard_timers_cnt); } } finish: for (i=0 ; i<NSCOPE ; i++) RT_named_mbx_delete(0, 0, rtaiScope[i].mbx); for (i=0 ; i<NLOGS ; i++) RT_named_mbx_delete(0, 0, rtaiLogData[i].mbx); for (i=0 ; i<NLEDS ; i++) RT_named_mbx_delete(0, 0, rtaiLed[i].mbx); for (i=0 ; i<NMETERS ; i++) RT_named_mbx_delete(0, 0, rtaiMeter[i].mbx); for ( i=0 ; i<MAX_COMEDI_DEVICES ; i++ ){ if ( ComediDev[i] != NULL ){ comedi_close(ComediDev[i]); } } for ( i=0 ; i<N_DATAIN ; i++){ free( ComediDataIn[i].comdev ); } for ( i=0 ; i<N_DATAOUT ; i++){ free( ComediDataOut[i].comdev ); } for ( i=0 ; i<N_DIOIN ; i++){ free( ComediDioIn[i].comdev ); } for ( i=0 ; i<N_DIOOUT ; i++){ free( ComediDioOut[i].comdev ); } SA_Output_To_File(); rt_task_delete(rt_MainTask); printf("TARGET ENDS.\n"); return 0; }
int main(void) { RTIME tsr, tss, tsm, trpc; RT_TASK *mainbuddy; int i, k, s; unsigned long msg; printf("\n\nWait for it ...\n"); if (!(mainbuddy = rt_thread_init(nam2num("MASTER"), 1000, 0, SCHED_FIFO, 0x1))) { printf("CANNOT INIT TASK %lu\n", nam2num("MASTER")); exit(1); } sem = rt_sem_init(nam2num("SEMAPH"), 1); change = 0; for (i = 0; i < NR_RT_TASKS; i++) { indx[i] = i; if (!(thread[i] = rt_thread_create(thread_fun, indx + i, 0))) { printf("ERROR IN CREATING THREAD %d\n", indx[i]); exit(1); } } do { msleep(50); s = 0; for (i = 0; i < NR_RT_TASKS; i++) { s += hrt[i]; } } while (s != NR_RT_TASKS); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); tsr = rt_get_cpu_time_ns(); for (i = 0; i < LOOPS; i++) { for (k = 0; k < NR_RT_TASKS; k++) { rt_task_resume(mytask[k]); } } tsr = rt_get_cpu_time_ns() - tsr; change = 1; for (k = 0; k < NR_RT_TASKS; k++) { rt_task_resume(mytask[k]); } tss = rt_get_cpu_time_ns(); for (i = 0; i < LOOPS; i++) { for (k = 0; k < NR_RT_TASKS; k++) { rt_sem_signal(sem); } } tss = rt_get_cpu_time_ns() - tss; change = 2; for (k = 0; k < NR_RT_TASKS; k++) { rt_sem_signal(sem); } tsm = rt_get_cpu_time_ns(); for (i = 0; i < LOOPS; i++) { for (k = 0; k < NR_RT_TASKS; k++) { rt_send(mytask[k], 0); } } tsm = rt_get_cpu_time_ns() - tsm; change = 3; for (k = 0; k < NR_RT_TASKS; k++) { rt_send(mytask[k], 0); } trpc = rt_get_cpu_time_ns(); for (i = 0; i < LOOPS; i++) { for (k = 0; k < NR_RT_TASKS; k++) { rt_rpc(mytask[k], 0, &msg); } } trpc = rt_get_cpu_time_ns() - trpc; rt_make_soft_real_time(); printf("\n\nFOR %d TASKS: ", NR_RT_TASKS); printf("TIME %d (ms), SUSP/RES SWITCHES %d, ", (int)(tsr/1000000), 2*NR_RT_TASKS*LOOPS); printf("SWITCH TIME %d (ns)\n", (int)(tsr/(2*NR_RT_TASKS*LOOPS))); printf("\nFOR %d TASKS: ", NR_RT_TASKS); printf("TIME %d (ms), SEM SIG/WAIT SWITCHES %d, ", (int)(tss/1000000), 2*NR_RT_TASKS*LOOPS); printf("SWITCH TIME %d (ns)\n", (int)(tss/(2*NR_RT_TASKS*LOOPS))); printf("\nFOR %d TASKS: ", NR_RT_TASKS); printf("TIME %d (ms), SEND/RCV SWITCHES %d, ", (int)(tsm/1000000), 2*NR_RT_TASKS*LOOPS); printf("SWITCH TIME %d (ns)\n", (int)(tsm/(2*NR_RT_TASKS*LOOPS))); printf("\nFOR %d TASKS: ", NR_RT_TASKS); printf("TIME %d (ms), RPC/RCV-RET SWITCHES %d, ", (int)(tsm/1000000), 2*NR_RT_TASKS*LOOPS); printf("SWITCH TIME %d (ns)\n\n", (int)(trpc/(2*NR_RT_TASKS*LOOPS))); fflush(stdout); end = 1; for (i = 0; i < NR_RT_TASKS; i++) { rt_rpc(mytask[i], 0, &msg); } do { msleep(50); s = 0; for (i = 0; i < NR_RT_TASKS; i++) { s += hrt[i]; } } while (s); rt_sem_delete(sem); rt_task_delete(mainbuddy); for (i = 0; i < NR_RT_TASKS; i++) { rt_thread_join(thread[i]); } return 0; }