Exemplo n.º 1
0
void killchildren()
{
    if(0 != child_mhd)
      killproc(child_mhd,"kill mhd\n");
    if(0 != child_spdy2http)
      killproc(child_spdy2http,"kill spdy2http\n");
    if(0 != child_mhd2spdy)
      killproc(child_mhd2spdy,"kill mhd2spdy\n");
    if(0 != child_curl)
      killproc(child_curl,"kill curl\n");
}
Exemplo n.º 2
0
int daemon_stop( bool force ) {
	int pid = readlockfile();

	printf( " * stopping daemon..." );
	fflush( stdout );

	if ( !pid ) {
		printf( "failed: no daemon running\n" );
		return false;
	}

	if ( !stopproc( pid ) ) {
		if ( errno == 3 ) {
			printf( "warning: %s\n", strerror( errno ) );
			return true;
		} else {
			if ( !force || !killproc( pid ) ) {
				printf( "failed: %s\n", strerror( errno ) );
				return false;
			} else if ( force )
				printf( "force " );
		}
	}

	printf( "stopped.\n" );
	return true;
}
Exemplo n.º 3
0
pid_t au_fork()
{
  pid_t child = fork();
	if (child == -1)
	{
    killchildren();

		killproc(parent,"fork failed\n");
	}

	return child;
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
    // odchytavanie signalu
    signal(SIGTERM, killproc);
    signal(SIGINT, killproc);

    // pre nahodne generovanie cisel
    srand(time(0));

/***************************argumenty**dole**********************************************/
    char *test;

    if (argc == 5)
    {
        ParamP = strtol(argv[1], &test, 10);
        if (test[0] != 0)
        { 
            fprintf(stderr, "%s", ErrorCode[EC_NIEJECISLO]);
            return EC_ERR;
        }

        ParamH = strtol(argv[2], &test, 10);
        if (test[0] != 0)
        { 
            fprintf(stderr, "%s", ErrorCode[EC_NIEJECISLO]);
            return EC_ERR;
        }

        ParamS = strtol(argv[3], &test, 10);
        if (test[0] != 0)
        { 
            fprintf(stderr, "%s", ErrorCode[EC_NIEJECISLO]);
            return EC_ERR;
        }

        ParamR = strtol(argv[4], &test, 10);
        if (test[0] != 0)
        { 
            fprintf(stderr, "%s", ErrorCode[EC_NIEJECISLO]);
            return EC_ERR;
        }

        //overenie povoleneho rozsahu
        // overenie P ... P<0 && P %2 = 0
        if (ParamP <= 0)
        {
            fprintf(stderr, "%s", ErrorCode[EC_HODNOTA]);
            return EC_ERR;
        }
        else if (ParamP % 2 != 0)
        {
            fprintf(stderr, "%s", ErrorCode[EC_HODNOTA]);
            return EC_ERR;
        }

        // overenie H S R
        if ((ParamH < 0 || ParamH > 5001) ||
            (ParamS < 0 || ParamS > 5001) ||
            (ParamR < 0 || ParamR > 5001)
            )
        {
            fprintf(stderr, "%s", ErrorCode[EC_HODNOTA]);
            return EC_ERR;
        }
    }
    else
    {
        fprintf(stderr, "%s", ErrorCode[EC_ARGUM]);
        return EC_ERR;
    }

    /***************************argumenty**hore************************************************/

    int AllPeople = ParamP * 2; // kolko je hackerove+serfov

    //otvorenie suboru pre zapis
    suborOUT = fopen("rivercrossing.out", "w");
    if(suborOUT == NULL)
    {
        fprintf(stderr, "%s", ErrorCode[EC_FOPEN]);
        return EC_SYSERR; // ak sa nepodarilo otvorit subor pre zapis SYSERR
    }

    // nastavenie bufferu, koli zapisu do souboru
    setbuf(suborOUT, NULL);

    // Vytvorime semaforoy 9x
    Sem_Write = sem_open("/xjezov01_SEM01", O_CREAT | O_EXCL, 0644, 1);
    Sem_Citac1 = sem_open("/xjezov01_SEM02", O_CREAT | O_EXCL, 0644, 0);
    Sem_Citac2 = sem_open("/xjezov01_SEM03", O_CREAT | O_EXCL, 0644, 0);
    Sem_Molo = sem_open("/xjezov01_SEM04", O_CREAT | O_EXCL, 0644, 1);
    Sem_Hacker = sem_open("/xjezov01_SEM05", O_CREAT | O_EXCL, 0644, 0);
    Sem_Serf = sem_open("/xjezov01_SEM06", O_CREAT | O_EXCL, 0644, 0);
    Sem_VarSafe = sem_open("/xjezov01_SEM07", O_CREAT | O_EXCL, 0644, 1);
    Sem_VarAll_Safe = sem_open("/xjezov01_SEM08", O_CREAT | O_EXCL, 0644, 1);
    Sem_Finish = sem_open("/xjezov01_SEM09", O_CREAT | O_EXCL, 0644, 0);

    // prekontrolujeme vytvorenie semaforov 9x
    if(Sem_Write == SEM_FAILED || 
        Sem_Citac1 == SEM_FAILED || 
        Sem_Citac2 == SEM_FAILED ||
        Sem_Molo == SEM_FAILED ||
        Sem_Hacker == SEM_FAILED ||
        Sem_Serf == SEM_FAILED ||
        Sem_VarSafe == SEM_FAILED ||
        Sem_VarAll_Safe == SEM_FAILED ||
        Sem_Finish == SEM_FAILED
        )
    {
        fprintf(stderr, "%s", ErrorCode[EC_SEMAF]);
        fclose(suborOUT); // zavreme otvoreny subor
        Sem_closer(); // niektore sa mohli vytvorit tak ich uzavri
        return EC_SYSERR; // toto je SYStemova chyba SYSERR
    }

    // Vytvorime zdielanu pamat 5x
    shm_fd1 = shm_open("/xjezov01_SMEM01", O_CREAT | O_EXCL | O_RDWR, 0644);
    shm_fd2 = shm_open("/xjezov01_SMEM02", O_CREAT | O_EXCL | O_RDWR, 0644);
    shm_fd3 = shm_open("/xjezov01_SMEM03", O_CREAT | O_EXCL | O_RDWR, 0644);
    shm_fd4 = shm_open("/xjezov01_SMEM04", O_CREAT | O_EXCL | O_RDWR, 0644);
    shm_fd5 = shm_open("/xjezov01_SMEM05", O_CREAT | O_EXCL | O_RDWR, 0644);

    // Vytvorime si v nasej zdielanej pamati miesto pre 1 integer 5x
    ftruncate(shm_fd1, sizeof(int));
    ftruncate(shm_fd2, sizeof(int));
    ftruncate(shm_fd3, sizeof(int));
    ftruncate(shm_fd4, sizeof(int));
    ftruncate(shm_fd5, sizeof(int));

    // Namapujeme zdielanu pamat do adresneho priestoru procesu 5x
    Shared_Line = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd1, 0);
    Shared_Citac = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd2, 0);
    Shared_Hacker = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd3, 0);
    Shared_Serf = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd4, 0);
    Shared_All = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd5, 0);

    //ak sa nieco pokazilo
    if ((Shared_Line == MAP_FAILED) ||
        (Shared_Citac == MAP_FAILED) ||
        (Shared_Hacker == MAP_FAILED) ||
        (Shared_Serf == MAP_FAILED) ||
        (Shared_All == MAP_FAILED)
        )
    {
        fprintf(stderr, "%s", ErrorCode[EC_ALOC]);
        // niektore sa mohli namapovat tak ich zmazeme
        CloseALL(); // zahrnuje aj zavretie suboru, zrusenie semaforov a zrusenie shared mem 
        return EC_SYSERR;
    }

    // pociatocne nastavenie premennych
    *Shared_Line=1;

    // deklaracia procesov
    pid_t HeadSerf, HeadHacker;
    pid_t Serf, Hacker;


    /*****************tu zacina cely program*********************/

    HeadHacker = fork();
    if (HeadHacker == -1) // fork sa nepodaril == SYSERR
    {
        fprintf(stderr, "%s", ErrorCode[EC_FORK]);
        kill(HeadHacker, SIGTERM);
        killproc(0); // ukoncenie process
        exit(EC_SYSERR);
    }
    else if (HeadHacker == 0) // Childprocess tu budeme vytvarat Hacker-ov
    {
        for (int i = 0; i < ParamP; ++i)
        {
            usleep(rand()%(ParamH+1)*1000); // uspanie
            Hacker = fork();
            if (Hacker == 0) //child process
            {
                HackerPracuj(i+1, AllPeople); //tu sa vykonava cinnost Hackera
                Sem_closer(); // kazdy zavre semafory
                fclose(suborOUT); // kazdy zavre subor
                exit(EC_OK); // vsetko prebehlo ok
            }
            if (Hacker == -1) //ak sa nepodari fork == SYSERR
            {
                fprintf(stderr, "%s", ErrorCode[EC_FORK]);
                kill(HeadHacker, SIGTERM); // ukoncenie procesu Hlavneho Hackera
                killproc(0); // ukoncenie procesov
                exit(EC_SYSERR); // pretoze SYSERR
            }
        }

        //pockame na child procesy HeadHackera
        if (Hacker > 0)
        {
            while(wait(NULL) > 0);
        }
        CloseALL();
        exit(EC_OK);
    }

    HeadSerf = fork();
    if (HeadSerf == -1) // fork sa nepodaril == SYSERR
    {
        fprintf(stderr, "%s", ErrorCode[EC_FORK]);
        kill(HeadHacker, SIGTERM);
        kill(HeadSerf, SIGTERM);
        killproc(0); // ukoncenie process
        exit(EC_SYSERR);
    }
    else if (HeadSerf == 0) // Childprocess tu budeme vytvarat serf-ov
    {
        for (int i = 0; i < ParamP; ++i)
        {
            usleep(rand()%(ParamS+1)*1000); //uspanie
            Serf = fork();
            if (Serf == 0) // child process HeadSerfa
            {
                SerfPracuj(i+1, AllPeople); //tu sa vykonava cinnost Serfa
                Sem_closer(); // kazdy uzavre semafory
                fclose(suborOUT); // kazdy zavre subor
                exit(EC_OK); // vsetko ok
            }
            if (Serf == -1) //ak sa nepodari fork
            {
                fprintf(stderr, "%s", ErrorCode[EC_FORK]);
                kill(HeadSerf, SIGTERM); // ukoncenie procesu Hlavneho Serfa
                killproc(0); // ukoncenie procesov
                exit(EC_SYSERR); // pretoze SYSERR
            }
        }

        //pockame na child procesy
        if (Serf > 0)
        {
            while(wait(NULL) > 0);
        }
        CloseALL();
        exit(EC_OK);
    }

    // caka na parentov
    if (HeadHacker > 0 && HeadSerf > 0)
    {
        while(wait(NULL) > 0);
    }

    CloseALL(); // uzavri vsetko .. semafory, subor, shared mem

    return EC_OK; // vsetko ok
}