0

I learning SDL2 . I am trying create two processes which will render a window using fork function.The program compiles fine but it doesn't execute. When I try to execute I get folloing error:-

XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
      after 262 requests (261 known processed) with 36 events remaining.
XIO:  fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
      after 260 requests (260 known processed) with 35 events remaining.

I really have no idea what are these errors ? I don't event know that are these Linux related errors or Programming related errors.

here is my program :-

#include <iostream>
#include <SDL2/SDL.h>
#include <thread>
#include <unistd.h>

using namespace std;
void th(SDL_Window* win,SDL_Surface *win_sur)
{

    int i=0,o=0,p=0;
    SDL_Event e;
    while(1)
    {
        while(SDL_PollEvent(&e))
        {
            if(e.type == SDL_QUIT)
                goto end;
        }
        SDL_FillRect(win_sur,0,SDL_MapRGB(win_sur->format,i,o,p));
        SDL_UpdateWindowSurface(win);
        i++;o+=2;p+=3;

        SDL_Delay(10);
    }
    end:
    ;
}

int main()
{
    SDL_Window *win,*win2;
    SDL_Init(SDL_INIT_VIDEO);

    win = SDL_CreateWindow("win",10,100,500,500,SDL_WINDOW_OPENGL);
    SDL_ShowWindow(win);
    SDL_Surface *win_sur = SDL_GetWindowSurface(win);

    win2 = SDL_CreateWindow("win2",10,500,500,500,SDL_WINDOW_OPENGL);
    SDL_ShowWindow(win2);
    SDL_Surface *win_sur2 = SDL_GetWindowSurface(win);

    int i=0,o=0,p=0;

    SDL_FillRect(win_sur,0,SDL_MapRGB(win_sur->format,i,o,p));
    SDL_UpdateWindowSurface(win);


    SDL_FillRect(win_sur2,0,SDL_MapRGB(win_sur2->format,i,o,p));
    SDL_UpdateWindowSurface(win2);

    pid_t pid = fork();
    if(!pid)
        th(win2,win_sur2);
    else if(pid>0)
            th(win,win_sur);
    else
        printf("Error");


    SDL_DestroyWindow(win);
    SDL_Quit();
}
  • What exactly are you trying to achieve with the `fork`? It seems to make things more complicated than necessary. Also you should *really* rethink the `goto` – UnholySheep Mar 15 '18 at 16:40
  • @UnholySheep I heard from a lot of people to not to use goto . I just don't get it why ? –  Mar 15 '18 at 16:43
  • See [this question](https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto) and many other explanations available on the internet when searching for "Why is `goto` bad" – UnholySheep Mar 15 '18 at 16:43
  • @yojimbooru - While there just *might* be one or two uses for goto, in this particular case you could *easily* replace it with a `return`. Then there will be no doubt about *which* `end:` label control is transferred to. – Bo Persson Mar 15 '18 at 17:38

0 Answers0