#include <sys/spu.h> int spu_run(int fd, unsigned int *npc, unsigned int *event);
注: このシステムコールには glibc のラッパー関数は存在しない。「注意」の節を参照。
SPU コードの実行は同期的 (synchronously) に行われる、つまり SPU が実行中は spu_run() は停止 (block) する。 SPU コードの実行をメイン CPU や他の SPU と並行して行う必要がある場合は、 最初に、その SPU コードを実行する新しいスレッドを、(例えば pthread_create(3) などを使って) 生成しなければならない。
spu_run() が返るときには、SPU のプログラムカウンターの現在値が npc に書き込まれる。 これにより、連続する spu_run() の呼び出しで同じ npc ポインターを使うことができる。
event 引数には、拡張ステータスコード用のバッファーを指定する。 SPU_CREATE_EVENTS_ENABLED フラグ付きで SPU コンテキストが作成されると、 spu_run() が返る前に Linux カーネルによりこのバッファーに 拡張ステータスコードが格納される。
ステータスコードには以下の定数が一つ以上入る。
NULL は event 引数として有効な値である。 この場合、イベントは呼び出し元のプロセスに報告されない。
spu_status レジスターの値は、ステータスコードと SPU の stop-and-signal 命令が返す 14 ビットのコードの ビットマスクで構成される。 後者の 14 ビットのコードはオプションである。 ステータスコードのビットマスクは下記の通りである。
spu_run() がエラーを返さなかった場合、下位 8 ビットのうち 1 つ以上は 常にセットされる。
#include <stdlib.h> #include <stdint.h> #include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <fcntl.h>
#define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(void)
{
    int context, fd, spu_status;
    uint32_t instruction, npc;
    context = spu_create("/spu/example-context", 0, 0755);
    if (context == -1)
        handle_error("spu_create");
    /* write a 'stop 0x1234' instruction to the SPU's
     * local store memory
     */
    instruction = 0x00001234;
    fd = open("/spu/example-context/mem", O_RDWR);
    if (fd == -1)
        handle_error("open");
    write(fd, &instruction, sizeof(instruction));
    /* set npc to the starting instruction address of the
     * SPU program. Since we wrote the instruction at the
     * start of the mem file, the entry point will be 0x0
     */
    npc = 0;
    spu_status = spu_run(context, &npc, NULL);
    if (spu_status == -1)
        handle_error("open");
    /* we should see a status code of 0x1234002:
     *   0x00000002 (spu was stopped due to stop-and-signal)
     * | 0x12340000 (the stop-and-signal code)
     */
    printf("SPU Status: %#08x\n", spu_status);