0

すべての myioctlが呼び出されると、ドライバーで 1 つのmagic()関数が呼び出され、その関数がそのコードで start と end コメントの間で呼び出されるたびに、他のプロセスまたはスレッドによってスケジュールされるべきではありません。

magic()
{
// start

some code is here.

// end
}

私がやりたいことは次のとおりです。プロセッサがコードの実行を開始するたびに、開始後、終了するまで、プロセッサによってスケジュールされるべきではありません。

では、どうすればそれを行うことができますか?これはアトミックプロセスとも呼ばれるものと同じものですか?

そのコードをミューテックスの間に保持することで、その機能が提供されますか?

4

2 に答える 2

1

複数のクライアントスレッドが書き込み、サーバースレッドとして機能するスレッドによって読み取られるスクラップブックのような場所が必要だったとき、同様のシナリオで、私が行ったことは次のようなものでした:ここの例では、引数や変数の共有について心配しないでください。

c1_thread_write() //let this be the unique function that client_thread_1 executes after it's inception  
{
   //some calls to open the scrapbook
   //the scrapbook thing I am referring to, is a buffer
   //Now I want to ensure that when client_thread_1 is writing, no other process or thread should be scheduled, which may somehow cause the client_thread_1 to exit this writing function without completing it.

   //so, I will create a MUTEX, lock it here as the first thing that gets executed in this function. MUTEX will be of ERROR_CHECK type
   //Start writing to the buffer, finish it
   //Unlock the mutex
   //call the pthread_join() and exit
}
于 2013-11-13T05:55:03.897 に答える
1

これには、状況に応じてセマフォ/スピン ロックが必要です。以下は単純なキャラクタードライバーコードです。デバイスを開く関数が呼び出されると、他のプロセスがデバイスを開いているかどうかを確認し、開いている場合は、デバイスが閉じるのを待ちます。デバイスが他のプロセスで使用されていない場合は、デバイスを開くことができます。これは、セマフォを使用して実現されます。プロセスは、セマフォを取得してコードにアクセスする必要があります。完了したら、セマフォを解放する必要があります/解放する必要があります。以下は、開く関数と閉じる関数です。

static int device_open(struct inode *inode, struct file *filp) {
    if(down_interruptible(&sem) != 0) {//take the semaphore
        printk(KERN_ALERT "megharajchard : the device has been opened by some other device, unable to open lock\n");
        return -1;
    }
    //buff_rptr = buff_wptr = device_buffer;
    printk(KERN_INFO "megharajchard : device opened succesfully\n");
    return 0;
}

ここで、プロセスはデバイスを開こうとします。セマフォを取得する必要があります。セマフォが取得されると、他のプロセスはデバイスを開くことができなくなります。

以下は、他のプロセスがデバイスを開いて使用できるように、プロセスがセマフォを解放するクローズ デバイス関数です。

static int device_close(struct inode *inode, struct file *filp) {
    up(&sem);// release the semaphore
    printk(KERN_INFO "megharajchard : device has been closed\n");
    return ret;
}

アトミックとは、プロセスがスリープできない状況を意味し、通常は割り込みコンテキストで実行中に発生します。プロセスの実行中にスリープすることはできません。このような状況でロックを実現するには、スピンロックを使用する必要があります。

さらに何か必要な場合はお知らせください。

于 2013-11-13T06:21:24.857 に答える