예제 #1
0
int main(int argc, char* argv[])
{
	unsigned long srv_name = nam2num("SRV");
	RT_TASK *srv;
	pid_t pid, my_pid, proxy ;
	int err,/* i,*/ count, msglen ;
	RTIME period;
	char *pt;
	
	init_linux_scheduler(SCHED_FIFO, 98);
		
	err = lock_all(0,0);
	if(err) {
		printf("lock_all() fails\n");
		exit(-1);
	}

 	if (!(srv = rt_task_init(srv_name, 0, 0, 0))) {
		printf("CANNOT INIT SRV TASK\n");
		exit(-1);
	}

	my_pid = rt_Alias_attach("");
	if(!my_pid) {
		rtai_print_to_screen("Cannot attach name SRV\n");
		exit(-1);
	}

	period = nano2count(1000000);
    rt_set_oneshot_mode();
	start_rt_timer(period);

	printf("SRV starts (%p)\n", srv);

    rt_make_hard_real_time();
	
	rt_task_make_periodic(srv, rt_get_time(), period);

  	proxy = rt_Proxy_attach( 0, "More beer please", 17, -1);
	if(proxy <= 0 ) {
		rtai_print_to_screen("Failed to attach proxy\n");
		exit(-1);
	}

    pid = rt_Receive( 0, 0, 0, &msglen);
	if(pid) {
		// handshake to give the proxy to CLT
		rtai_print_to_screen("rt_Reply the proxy %04X msglen = %d\n", proxy, msglen);
		rt_Reply( pid, &proxy, sizeof(proxy));
	}

	rt_sleep(nano2count(1000000000));
	count = 8 ;
	while(count--) {
		memset( msg, 0, sizeof(msg));
		pid = rt_Receive( 0, msg, sizeof(msg), &msglen);

		if(pid == proxy) {
			rtai_print_to_screen("SRV receives the PROXY event [%s]\n", msg);
			continue;
		} else if( pid <= 0) {
			rtai_print_to_screen("SRV rt_Receive() failed\n");
//			goto Exit;
			continue;
		}
		
		rtai_print_to_screen("SRV received msg    [%s] %d bytes from pid %04X\n", msg, msglen, pid);

		memcpy( rep, msg, sizeof(rep));
		pt = (char *) rep;
		while(*pt) *pt++ = toupper(*pt);
		if(rt_Reply(pid, rep, sizeof(rep)))
			rtai_print_to_screen("SRV rt_Reply() failed\n");
	}

	if(rt_Proxy_detach(proxy))
		rtai_print_to_screen("SRV cannot detach proxy\n");

	if(rt_Name_detach(my_pid)) 
		rtai_print_to_screen("SRV cannot detach name\n");

//Exit:
	rt_make_soft_real_time();

	rt_sleep(nano2count(1000000000));

	if(rt_task_delete(srv))
		rtai_print_to_screen("SRV cannot delete task\n");

	return 0;
}
예제 #2
0
파일: server.c 프로젝트: ArcEye/3.4.55-rtai
int main(int argc, char* argv[])
{
	unsigned long srv_name = nam2num("SRV");
	RT_TASK *srv;
	pid_t pid, my_pid, proxy;
	int count;
	size_t msglen;
	RTIME period;
	char *pt;


 	if (!(srv = rt_task_init_schmod(srv_name, 0, 0, 0, SCHED_FIFO, 0x1))) {
		PRINTF("CANNOT INIT SRV TASK\n");
		exit(-1);
	}
	mlockall(MCL_CURRENT | MCL_FUTURE);

	my_pid = rt_Alias_attach("");
	if (my_pid <= 0) {
		PRINTF("Cannot attach name SRV\n");
		exit(-1);
	}

//	rt_set_oneshot_mode();
	period = nano2count(1000000);
	start_rt_timer(period);
	rt_make_hard_real_time();
	PRINTF("SRV starts (task = %p, pid = %d)\n", srv, my_pid);

	rt_task_make_periodic(srv, rt_get_time(), period);

  	proxy = rt_Proxy_attach(0, "More beer please", 17, -1);
	if (proxy <= 0 ) {
		PRINTF("Failed to attach proxy\n");
		exit(-1);
	}


	msglen = 0;
	pid = rt_Receive(0, 0, 0, &msglen);
	if (pid) {
		// handshake to give the proxy to CLT
		PRINTF("rt_Reply the proxy %04X msglen = %d\n", proxy, msglen);
		rt_Reply(pid, &proxy, sizeof(proxy));
	}

	rt_sleep(nano2count(1000000000));
	count = 20;
	while(count--) {
		memset( msg, 0, sizeof(msg));
		pid = rt_Receive(0, msg, sizeof(msg), &msglen);
		if(pid == proxy) {
			PRINTF("SRV receives the PROXY event [%s]\n", msg);
			continue;
		} else if (pid <= 0) {
			PRINTF("SRV rt_Receive() failed\n");
			continue;
		}

		PRINTF("SRV received msg    [%s] %d bytes from pid %04X\n", msg, msglen, pid);

		memcpy (rep, msg, sizeof(rep));
		pt = (char *) rep;
		while (*pt) {
			*pt = toupper(*pt);
			pt++;
		}
		if (rt_Reply(pid, rep, sizeof(rep))) {
			PRINTF("SRV rt_Reply() failed\n");
		}
	}

	if (rt_Proxy_detach(proxy)) {
		PRINTF("SRV cannot detach proxy\n");
	}
	if (rt_Name_detach(my_pid)) {
		PRINTF("SRV cannot detach name\n");
	}
	rt_make_soft_real_time();
	rt_sleep(nano2count(1000000000));
	if (rt_task_delete(srv)) {
		PRINTF("SRV cannot delete task\n");
	}
	return 0;
}
예제 #3
0
void linux_process_termination(void)
{
	RT_TASK *task2delete, *task2unblock, *base_linux_tasks[NR_RT_CPUS];
	int cpu, slot, nr_task_lists;
	pid_t my_pid;
	struct task_struct *ltsk;
	unsigned long num;
	void *adr;

/*
 * Linux is just about to schedule *ltsk out of existence.
 * With this feature, LXRT frees the real time resources allocated
 * by the task ltsk.
*/
	ltsk = current;
	rt_get_base_linux_task(base_linux_tasks);
	nr_task_lists = rt_sched_type() == MUP_SCHED ? NR_RT_CPUS : 1;
	rt_global_cli();
	for (cpu = 0; cpu < nr_task_lists; cpu++) {

		task2delete = base_linux_tasks[cpu];
		// Try to find if RTAI was aware of this dying Linux task.
        	while ((task2delete = task2delete->next) && task2delete->lnxtsk != ltsk);
		// First let's free the registered resources.
	        for (slot = 1; slot <= MAX_SLOTS; slot++) {
	                if ((num = is_process_registered(ltsk)) > 0) {
				adr = rt_get_adr(num);
       				switch (rt_get_type(num)) {
                       	        case IS_SEM:
					rt_printk("LXRT Informed releases SEM %p\n", adr);
					rt_sem_delete(adr);
					rt_free(adr);
					break;
                                case IS_MBX:
					rt_printk("LXRT Informed releases MBX %p\n", adr);
                                        rt_mbx_delete(adr);
                                        rt_free(adr);
                                        break;
                                case IS_PRX:
rt_printk("LXRT Informed releases PRX %p\n", adr);
                                        rt_Proxy_detach(rttask2pid(adr));
                                        break;
// to do:                       case IS_SHMEM:
				}
				rt_drg_on_adr(adr); 
                        }
                }

        // Synchronous IPC pid may need to be released
		if ((my_pid = rttask2pid(task2delete))) {
			rt_printk("Release vc %04X\n", my_pid);
			rt_vc_release(my_pid);
                }

	        if (!task2delete) {
			continue; // The user deleted the task but forgot to delete the resources.
		}

        // Other RTAI tasks may be SEND, RPC or RETURN blocked on task2delete.
Loop:
		task2unblock = base_linux_tasks[cpu];
		while ((task2unblock = task2unblock->next)) {
                	if (!(task2unblock->state & (SEND | RPC | RETURN))) {
				continue;
			} else if (task2unblock->msg_queue.task == task2delete) {
                        	task2unblock->state &= ~(SEND | RPC | RETURN | DELAYED);
	                        LXRT_RESUME(task2unblock);
				rt_global_cli();
				goto Loop;
                        }
                }

        // To do: other RTAI tasks may want to be informed as well.

	        // Ok, let's delete the task.
		if (!rt_task_delete(task2delete)) {
rt_printk("LXRT Informed releases RT %p, lnxpid %d (%p), name %s.\n", task2delete, ltsk->pid, ltsk, ltsk->comm);
			rt_free(task2delete->msg_buf[0]);
			rt_free(task2delete);
			rt_drg_on_adr(task2delete);
			break;
                }
        }
	rt_global_sti();
}