Portál AbcLinuxu, 5. května 2025 21:17
Ahoj potřeboval bych od Vás opět poradit, problém se týká synchronizace. Svůj program jsem aplikoval na problematiku 5 filozofů, ale mám pocit, že dochází k deathlock a starvation. Předem Vám děkuji, za odpovědi.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include "semun.h"
#define N 5
#define LEFT ((philosopherNumb - 1 + N) % N)
#define RIGHT ((philosopherNumb + 1) % N)
#define LOCK (N+1)
int setSemValue(void);
int delSemaphore(void);
int waitTime(void);
int think(void);
int eat(void);
int zpracujParametr(char *arg, int pocetArg);
int downSemaphore(int philosopherNumb);
int upSemaphore(int philosopherNumb);
int philosopher(int philosopherNumb);
void takeForks(int philosopherNumb);
void putForks(int philosopherNumb);
void testPhilosopher(int philosopherNumb);
enum STATE {thinking, hungry, eating};
char *state;
int semId;
int shmId;
//struct semData
//{
// int semId;
//} SEM_DATA;
struct shmData
{
int count;
} SHM_DATA;
int main(int argc, char *argv[])
{
struct shmData *sharedData;
pid_t pid;
pid_t children[N];
int i = 0;
int retCode = 0;
//int shmId = 0;
int stepsNumber = 0;
void *sharedMemory = (void *) 0;
// Overovani parametru
if ((stepsNumber = zpracujParametr(argv[1], argc)) == -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
// Inicializace sdilene pameti
if ((shmId = shmget(IPC_PRIVATE, N + 1, 0666 | IPC_CREAT)) == -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
// Zpristupneni sdilene pameti programu
if ((sharedMemory = shmat(shmId, (void *) 0, 0)) == (void*) -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
// Inicializace citace akci
sharedData = (struct shmData *) sharedMemory;
sharedData->count=0;
// Odpojeni sdilene pameti
if (shmdt(sharedMemory) == -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
// Inicializace semaforu
if ((semId = semget(IPC_PRIVATE, N + 1, 0666 | IPC_CREAT)) == -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
// Zpristupneni semaforu
setSemValue();
// Zpristupneni sdilene pameti programu
if ((state = shmat(shmId, (void *) 0, 0)) == (void*) -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
// Vytvoreni podprocesu
for (; i < N; i++)
{
pid = fork();
switch (pid)
{
case -1:
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
break;
case 0:
while (stepsNumber != 0)
{
setbuf(stdout,NULL);
philosopher(i);
stepsNumber--;
}
exit(EXIT_SUCCESS);
break;
default:
children[i] = pid;
break;
}
}
// Ukonceni potomknu;
for (i = 0; i < N; i++)
{
retCode = waitpid(children[i], NULL, 0);
if (retCode < 0)
{
retCode = kill(children[i], SIGTERM);
if (retCode == -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
}
}
// Odpojeni sdilene pameti
if (shmdt(state) == -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
// Uvolneni sdilene pameti
if (shmctl(shmId, IPC_RMID, 0) == -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
// Uvolneni semaforu
if (delSemaphore() == -1)
{
fprintf(stderr, "Failed...\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
int zpracujParametr(char *arg, int pocetArg)
{
char *chyba;
int errCode = 0;
int cislo = 0;
errno = 0;
// V parametru je obsazen nepripustny pocet parametru nebo neni obsazen zadny parametr.
if ((pocetArg > 2) || (pocetArg == 1))
{
return -1;
}
cislo = strtol(arg, &chyba, 10);
errCode = errno;
// Parametr obsahuje nepripustne hodnoty.
if (errCode != 0)
{
return -1;
}
// Parametr obsahuje neciselne znaky.
if (*chyba != '\0')
{
return -1;
}
// Parametr obsahuje zapornou hodnotu.
if (cislo < 0)
{
return -1;
}
return cislo;
}
int waitTime(void)
{
int pauseTime = 0;
int retCode = 0;
int errCode = 0;
// Generovani nahodneho cisla v rozmezi O-100.
srand((unsigned int) time(NULL) + (unsigned int) getpid());
pauseTime = rand() % 101;
retCode = usleep(pauseTime);
errCode = errno;
// Chybne skonceni funkce.
if (retCode != 0)
{
return -1;
}
// Chybne skonceni funkce.
if (errCode != 0)
{
return -1;
}
return 0;
}
int think(void)
{
waitTime();
return 0;
}
int eat(void)
{
waitTime();
return 2;
}
int setSemValue(void)
{
union semun semUnion;
semUnion.val = 0;
int i = 0;
for (; i < N + 1; i++)
{
if (semctl(semId, i, SETVAL, semUnion) == -1)
{
return -1;
}
}
return 0;
}
int delSemaphore(void)
{
union semun semUnion;
if (semctl(semId, 0, IPC_RMID, semUnion) == -1)
{
return -1;
}
return 0;
}
int downSemaphore(int philosopherNumb)
{
struct sembuf sem;
sem.sem_num = philosopherNumb;
sem.sem_op = -1;
sem.sem_flg = 0;
if (semop(semId, &sem, 1) == -1)
{
return -1;
}
return 0;
}
int upSemaphore(int philosopherNumb)
{
struct sembuf sem;
sem.sem_num = philosopherNumb;
sem.sem_op = 1;
sem.sem_flg = 0;
if (semop(semId, &sem, 1) == -1)
{
return -1;
}
return 0;
}
void takeForks(int philosopherNumb)
{
downSemaphore(LOCK);
state[philosopherNumb] = hungry;
testPhilosopher(philosopherNumb);
upSemaphore(LOCK);
downSemaphore(philosopherNumb);
}
void putForks(int philosopherNumb)
{
downSemaphore(LOCK);
state[philosopherNumb] = thinking;
testPhilosopher(LEFT);
testPhilosopher(RIGHT);
upSemaphore(LOCK);
}
void testPhilosopher(int philosopherNumb)
{
if (state[philosopherNumb] == hungry && state[LEFT] != eating && state[RIGHT] != eating)
{
state[philosopherNumb] = eating;
upSemaphore(philosopherNumb);
}
}
int philosopher(int philosopherNumb)
{
state[philosopherNumb] = thinking;
printf("F(%d) Mysli\n", philosopherNumb);
think();
printf("F(%d) Bere vidlicky\n", philosopherNumb);
takeForks(philosopherNumb);
printf("F(%d) Ji\n", philosopherNumb);
eat();
printf("F(%d) Poklada vidlicky\n", philosopherNumb);
putForks(philosopherNumb);
return 0;
}
-------------------------semun.h------------------------------------------------------------
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
#else
union semun
{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
#endif
Chyba je ve výpise, že například dokáže jíst filozof, který nemá k dispozici vidličky.Skutečně je to tak? Mě přijde, že je to celý tak divoké, že i ten výstup může být jen zmatený. Zkuste spustit
strace -f -o logfile programa pokud si budete myslet, že k chybě došlo, postněte sem logfile jako přílohu. Jinak k vyhladovění ve vašem řešení dojít může.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.