Aboug 03_video_cuda_enc stop enc

hi all ,i want to know how can i stop the encoder just at my owner ider,for example that following code is from the 03_video_cuda_enc, if i want to just read the frame for 3 times then i break the while loop,then i run the app will has the following errors,so how can stop the encoder normally?
Opening in BLOCKING MODE
NvMMLiteOpen : Block : BlockType = 4
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 4
875967048
842091865
-------getNumBuffers=6
H264: Profile = 77, Level = 50
[ERROR] (NvV4l2ElementPlane.cpp:256) Output Plane:Error while Qing buffer: Device or resource busy
Error while queueing buffer at output plane
Encoder is in error
[ERROR] (NvV4l2ElementPlane.cpp:178) Capture Plane:Error while DQing buffer: Broken pipe
[ERROR] (NvV4l2ElementPlane.cpp:178) Capture Plane:Error while DQing buffer: Broken pipe
[ERROR] (NvV4l2ElementPlane.cpp:256) Capture Plane:Error while Qing buffer: Device or resource busy
Error while Qing buffer at capture plane
App run failed

//////////////////////////////////////////////////////
/* Keep reading input till EOS is reached */
while (!ctx.got_error && !ctx.enc->isInError() && !eos)
{
struct v4l2_buffer v4l2_buf;
struct v4l2_plane planes[MAX_PLANES];
NvBuffer *buffer;
int fd;
void **dat;

    memset(&v4l2_buf, 0, sizeof(v4l2_buf));
    memset(planes, 0, sizeof(planes));

    v4l2_buf.m.planes = planes;

    if (ctx.enc->output_plane.dqBuffer(v4l2_buf, &buffer, NULL, 10) < 0)
    {
        cerr << "ERROR while DQing buffer at output plane" << endl;
        abort(&ctx);
        goto cleanup;
    }

    if (read_video_frame(ctx.in_file, *buffer) < 0)
    {
        cerr << "Could not read complete frame from input file" << endl;
        v4l2_buf.m.planes[0].bytesused = 0;
    }
    /**
     * buffer is touched by CPU in read_video_frame(), so NvBufferMemSyncForDevice()
     * is needed to flash cached data to memory.
     */
    fd = buffer->planes[0].fd;
    for (uint32_t j = 0 ; j < buffer->n_planes ; j++)
    {
        dat = (void **)&buffer->planes[j].data;
        ret = NvBufferMemSyncForDevice (fd, j, dat);
        if (ret < 0)
        {
            cerr << "Error while NvBufferMemSyncForDevice at output plane" << endl;
            abort(&ctx);
            goto cleanup;
        }
    }

    /* map DMA fd to eglImage for CUDA processing */
    ctx.eglimg = NvEGLImageFromFd(ctx.eglDisplay, buffer->planes[0].fd);
    /* render rectangle by CUDA */
    HandleEGLImage(&ctx.eglimg);
    /* release eglImage */
    NvDestroyEGLImage(ctx.eglDisplay, ctx.eglimg);

    ret = ctx.enc->output_plane.qBuffer(v4l2_buf, NULL);
    if (ret < 0)
    {
        cerr << "Error while queueing buffer at output plane" << endl;
        abort(&ctx);
        goto cleanup;
    }

    if (v4l2_buf.m.planes[0].bytesused == 0)
    {
        cerr << "File read complete." << endl;
        eos = true;
        break;
    }
}

Hi,
The way of terminating encoding is to send EoS buffer. Please check

        if (read_video_frame(ctx.in_file, *buffer) < 0)
        {
            cerr << "Could not read complete frame from input file" << endl;
            v4l2_buf.m.planes[0].bytesused = 0;
        }

The EoS is sent once the input YUV has reached EoF. For your case, you can send after feeding three frames to encoder.