LKMPG: Pianificare compiti

Da Guide@Debianizzati.Org.
Vai alla navigazione Vai alla ricerca
The Linux Kernel Module Programming Guide

Sommario

  1. Prefazione
  2. Introduzione
  3. Ciao Mondo
  4. Fasi preliminari
  5. Comportamento dei driver dei device
  6. Il filesystem /proc
  7. Usare /proc per l'input
  8. Comunicare con i file dei device
  9. Chiamate di sistema
  10. Bloccare i processi
  11. Rimpiazzare printk
  12. Pianificare compiti
  13. I gestori degli interrupt
  14. Multi-processing simmetrico
  15. Insidie comuni
  16. Appendice A
  17. Appendice B

Pianificare i tasks

Molto spesso, abbiamo dei tasks proprietari che devo essere eseguiti in tempi precisi, o molto spesso. Se il task è eseguito da un processo, possiamo metterlo nel file crontab. Se il processo invece è eseguito da un modulo del kernel, abbiamo due possibilità. La prima è di mettere un processo nel file crontab, che attiverà il modulo tramite una system call quando necessario, per esempio aprendo un file. In ogni caso questo metodo è molto inefficente -- si esegue un nuovo precesso di crontab, si legge un nuovo eseguibile nella memoria, e tutto solo per attivare il modulo del kernel che è comunque caricato nella memoria.

Invece di fare in questo modo, si può creare una funzione che sarà chiamato una volta per ogni interrupt. La strada da seguire è creare un task, tenuto in una struttura workqueue_struct, che terrà un puntatore sulla funzione. Successivamente, si userà queue_delayed_work per mettere il task in una lista chiamata my_workqueue, che è la lista dei tasks che saranno eseguiti nel prossimo interrupt. Poichè vogliamo che la funzione continui ad essere eseguita, abbiamo bisogno di metterela ancora nella lista my_workqueue ogni volta che è chiamata, per il prossimo interrupt.

C'è ancora un punto che dobbiamo ricordare. Quando un modulo è rimosso tramite rmmod, prima di tutto è controllato il conteggio delle sue referenze. Se è zero, è eseguito module_cleanup.Poi, il modulo è rimosso dalla memoria con tutte le sue funzioni. E' importante arrestare correttamente un modulo,o potrebbero accadere imprevisti spiacevoli. Si guardi il codice sotto dove si nota come può essere fatto in un modo sicuro.

/*
 *  sched.c - programmare una funzione che sia chiamata ogni interrupt.
 *
 *  Copyright (C) 2001 by Peter Jay Salzman
 */

/* 
 * I file headers necessari
 */

/* 
 * Standard kernel moduli
 */
#include <linux/kernel.h>	/* Stiamo facendo un lavoro sul kernel */
#include <linux/module.h>	/* In specifico, un modulo*/
#include <linux/proc_fs.h>	/* Necessario perchè usiamo il processo fs */
#include <linux/workqueue.h>	/* Qui programmiamo i tasks */
#include <linux/sched.h>	/* Abbiamo bisogno di sospendere e attivare successivamente */
#include <linux/init.h>		/* Per__init ed __exit */
#include <linux/interrupt.h>	/* Per irqreturn_t */

struct proc_dir_entry *Our_Proc_File;
#define PROC_ENTRY_FILENAME "sched"
#define MY_WORK_QUEUE_NAME "WQsched.c"

/* 
 * Il numero di volte che il timer interrupt sono stati chiamati 
 */
static int TimerIntrpt = 0;

static void intrpt_routine(void *);

static int die = 0;		/* set this to 1 for shutdown */

/* 
 * Il lavoro di questa struttura queue per questo task, da workqueue.h 
 */
static struct workqueue_struct *my_workqueue;

static struct work_struct Task;
static DECLARE_WORK(Task, intrpt_routine, NULL);

/* 
 * Questa funzione sarà chiamata ogni interrupt. Notare il puntatore nullo*
 *  - le funzioni task possono essere usate per più di un obbiettivo, tutte le volte
 * che questa avrà un diverso parametro.
 */
static void intrpt_routine(void *irrelevant)
{
	/* 
	 * Increment the counter 
	 */
	TimerIntrpt++;

	/* 
	 * If cleanup wants us to die
	 */
	if (die == 0)
		queue_delayed_work(my_workqueue, &Task, 100);
}

/* 
 * Metti un dato dentro il file del processo fs. 
 */
ssize_t
procfile_read(char *buffer,
	      char **buffer_location,
	      off_t offset, int buffer_length, int *eof, void *data)
{
	int len;		/* Il numero di bytes usati al momento*/

	/* 
	 * E' statico quindi sarà ancora in memoria.
	 * quando usciremo da questa funzione
	 */
	static char my_buffer[80];

	/* 
	 * Diamo tutte le nostre informazioni in one go /da sistemare/, cosi se qualcuno chiede 
            se abbiamo più informazioni la risposta deve essere no.
         */
	 	if (offset > 0)
		return 0;

	/* 
	 * Fill the buffer and get its length 
	 */
	len = sprintf(my_buffer, "Timer called %d times so far\n", TimerIntrpt);

	/* 
	 * Tell the function which called us where the buffer is 
	 */
	*buffer_location = my_buffer;

	/* 
	 * Return the length 
	 */
	return len;
}

/* 
 * Initialize the module - register the proc file 
 */
int __init init_module()
{
	/*
	 * Create our /proc file
	 */
	Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);
	
	if (Our_Proc_File == NULL) {
		remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
		printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
		       PROC_ENTRY_FILENAME);
		return -ENOMEM;
	}

	Our_Proc_File->read_proc = procfile_read;
	Our_Proc_File->owner = THIS_MODULE;
	Our_Proc_File->mode = S_IFREG | S_IRUGO;
	Our_Proc_File->uid = 0;
	Our_Proc_File->gid = 0;
	Our_Proc_File->size = 80;

	/* 
	 * Put the task in the work_timer task queue, so it will be executed at
	 * next timer interrupt
	 */
	my_workqueue = create_workqueue(MY_WORK_QUEUE_NAME);
	queue_delayed_work(my_workqueue, &Task, 100);
	
	
	printk(KERN_INFO "/proc/%s created\n", PROC_ENTRY_FILENAME);
	
	return 0;
}

/* 
 * Cleanup
 */
void __exit cleanup_module()
{
	/* 
	 * Unregister our /proc file 
	 */
	remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
	printk(KERN_INFO "/proc/%s removed\n", PROC_ENTRY_FILENAME);

	die = 1;		/* keep intrp_routine from queueing itself */
	cancel_delayed_work(&Task);	/* no "new ones" */
	flush_workqueue(my_workqueue);	/* wait till all "old ones" finished */
	destroy_workqueue(my_workqueue);

	/* 
	 * Sleep until intrpt_routine is called one last time. This is 
	 * necessary, because otherwise we'll deallocate the memory holding 
	 * intrpt_routine and Task while work_timer still references them.
	 * Notice that here we don't allow signals to interrupt us.
	 *
	 * Since WaitQ is now not NULL, this automatically tells the interrupt
	 * routine it's time to die.
	 */

}

/* 
 * some work_queue related functions
 * are just available to GPL licensed Modules 
 */
MODULE_LICENSE("GPL");