best way to debug fifo error

Mohammed Shameem Sattar sattar at cs.yorku.ca
Fri Nov 29 17:19:31 CET 2002


sorry for hitting the reply button.

I am sure I opened the right fifo
My code is attached with this email
and checkp.c contains the main program.

On Fri, 29 Nov 2002, Phil Wilshire wrote:

> Hi
> Did you open the write fifo as
> O_WRONLY ??
>
> Please post your code so that we can spot any other problems.
>
> regards
>   Phil Wilshire
>
> Mohammed Shameem Sattar wrote:
> >
> > Hi,
> > I am writing a rtai module where I am trying to pass from user space a
> > struct consiting of two integer and an array of another struct array size
> > is 10 and the other struct is made of 4 integers.
> > It seems my fifos opened fine but when I tried to write to the fifo
> > designated for passing message to kernel module it fails to write.
> > Since I am just a beginner I am having problem in figuring what seems to
> > be causing this and I am looking for ideas to try out so that I can
> > narrow down the problem.
> > Shameem
> >
> > _______________________________________________
> > RTAI mailing list
> > RTAI at rtai.org
> > https://mail.rtai.org/cgi-bin/mailman/listinfo/rtai
>
> --
> SDCS -- System Design & Consulting Services LLC, http://www.sysdcs.com
> ** Embedded Linux Training **  email me for details
> 630 Springhouse Sq., Leesburg VA 20175 t: 703 669 9766 f: 703 669 9768
>
-------------- next part --------------
#define TICK_PERIOD 1000000

#define TASK_PRIORITY 1

#define STACK_SIZE 10000

#define SHMNAM "MIRSHM"


struct process_info {
  int pid;
  int stime;
  int comp;
  int dline;
};

struct msg {
  int period;
  int nump;
  struct process_info pdata[10];
};
-------------- next part --------------
/*
This is a simple kernel module which is a dispatcher 
and it dispatch tasks from time to time base on a 
predefined schedule that was pre runtime scheduler.
*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/io.h>
#include <rtai.h>
#include <rtai_sched.h>
#include <rtai_fifos.h>

#include <rtai_shm.h>
#include "control.h"

/*MODULE_LICENSE("GPL");*/
EXPORT_NO_SYMBOLS;

#define NTASK 100
#define FIFO0 0
#define FIFO1 1

static RT_TASK tasks[NTASK];
static RT_TASK boss;

/*static struct process_info *pdata;*/
static struct msg mess;

static int num_process;
static int period;


static void fun(int t){
  
  while(rt_get_time_ns() <(mess.pdata[t].stime*1000000));

  rt_busy_sleep(mess.pdata[t].comp*1000000);  

  rtf_put(FIFO1, &mess.pdata[t] , sizeof(struct process_info));
}



static int my_handler(unsigned int fifo, int rw){
  /*struct msg h_msg;*/
  struct process_info pi;
  int err;
  rt_printk("hnadler\n");
  if (rw =='r'){
    err = rtf_get(FIFO0, &mess, sizeof(mess));
    num_process = mess.nump;
    period = mess.period;
     rtai_print_to_screen("No. Task: %d\n", mess.nump);
  }
  else  {
    err = rtf_put(FIFO1, &pi, sizeof(pi));
  }
  if(err != 0)
    return -EINVAL;
  return 0;
}




int init_module(void)
{   
  int i;
  rtai_print_to_screen("init\n");
  rtf_create(FIFO0, 20000);
  rtf_create(FIFO1, 20000);
  
  rtf_create_handler(FIFO0, X_FIFO_HANDLER(my_handler));
  
  /*if(rtf_get(FIFO0, &mess, sizeof(mess)) == sizeof(mess)){
    rtai_print_to_screen("No. Task: %d, Period: %d", mess.nump, mess.period);
    num_process = mess.nump;
    period = mess.period;
    }*/
  
  /*create and initilize tasks and suspend them just to have them resumed 
    in the dispatcher*/
  start_rt_timer(period*100000);
  for(i=0; i<num_process; i++){
    int t = i;
    /*rt_task_init(&tasks[i], fun, t, STACK_SIZE, TASK_PRIORITY, 1, 0);*/
    rtai_print_to_screen("here");
    /*rt_task_suspend(&tasks[i]);*/
  }
  

  /*rt_task_init(&boss, dispatch, 1, STACK_SIZE, TASK_PRIORITY, 1, 0);*/
  return 0;
}

void cleanup_module(void)
{
  int i;
  rt_printk("cleanup\n");
  stop_rt_timer();
  rt_busy_sleep(10000000);
  rtf_destroy(FIFO0);
  rtf_destroy(FIFO1);
  
  for(i=0; i<num_process; i++){
    rt_task_delete(&tasks[i]);
  }
  rt_task_delete(&boss);
  return;
}
-------------- next part --------------

/*This is the main program that read data from a file 
based on file name passed to it. The program read all
the important data and opens a fifo as pass it to 
kernel module and then intermittenly read data from 
another fifo and prints it on screen. */

#include "control.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>

#define true 1
#define false 0



static int end;

static void endme (int dummy) { end = 1; }

int main(void){
  char NAME[19];
  FILE *data;
  int fifo0, fifo1, i;
  struct msg mess;
  struct process_info *pdata;

  printf("Enter the name of the file with the preruntime schedule.\n");
  scanf("%s", NAME);
  
  data = fopen(NAME, "r");
  signal (SIGINT, endme);

  /*open fifo for writing*/
  if((fifo0 = open("/dev/rtf0", O_WRONLY)) < 0){
    
    fprintf(stderr, "ERROR in opening /dev/rtf1\n");
    exit(1);
  }

  /*scan in data from file*/
  fscanf(data, "%d", &mess.nump);
  fscanf(data, "%d", &mess.period);
  printf("Num process: %d\n", mess.nump);
  printf("Period to use: %d\n", mess.period);
  for (i=0;i < mess.nump; i++) {
    fscanf(data, "%d", &mess.pdata[i].pid);
    fscanf(data, "%d", &mess.pdata[i].stime);  
    fscanf(data, "%d", &mess.pdata[i].comp);  
    fscanf(data, "%d", &mess.pdata[i].dline);
    printf(">Pid: %d, started: %d, compute time: %d \n", mess.pdata[i].pid, mess.pdata[i].stime, mess.pdata[i].comp); 
  }

  /*send data over fifo to kernel module*/
  if (write(fifo0, &mess, sizeof(mess)) < 0) {
    fprintf(stderr, "I am failing\n");
    exit(1);
  }
  
  /*open fifo for reading data send from kernel*/
  if((fifo1 = open("/dev/rtf1", O_RDONLY)) < 0){    
    fprintf(stderr, "ERROR in opening /dev/rtf1\n");
    exit(1);
  }
  
  /*go in an endless loop reading data from fifo and printing it on screen
   until signal is send to stop*/
  while(!end) {
    read(fifo1, &pdata, sizeof(struct process_info));
    printf(">Pid: %d, started: %d, compute time: %d \n", pdata->pid, pdata->stime, pdata->comp);
  }

  /*print out what we have read so far for debug*/
  /*printf("Num process: %d\n", mess.nump);
  printf("Period to use: %d\n", mess.period);
  for (i=0;i < mess.nump; i++) {
    printf(">Pid: %d, started: %d, compute time: %d \n", mess.pdata[i].pid, mess.pdata[i].stime, mess.pdata[i].comp);
    }*/	
  return 0;
}


More information about the Rtai mailing list