Nowe posty

Autor Wątek: Semafory  (Przeczytany 4135 razy)

twardziel

  • Gość
Semafory
« dnia: 2011-12-30, 20:20:00 »
moze mi ktos powiedziec dlaczego przy podnoszeniu semafora funkcja semop zwraca -1??
Wywala blad Invalid argument, czylli rozumiem ze cos nie tak z arg tej funkcji tyle ze nie wiem co.

#include 
#include
#include
#include
#include
#include
//#include

struct sembuf buf;

void podnies(int semid, int semnum){
        buf.sem_num = semnum;
        buf.sem_op = 1;
        buf.sem_flg = IPC_NOWAIT;

        if (semop(semid, &buf, 2) == -1){
                perror("Podnoszenie semafora\\n");
                exit(1);
        }
}



void opusc(int semid, int semnum){

        buf.sem_num = semnum;
        buf.sem_op = -1;
        buf.sem_flg = 0;
        if (semop(semid, &buf, 1) == -1){
                perror("Opuszczenie semafora");
                exit(1);
        }

}



main(){
int see = semget(20, 4, 0600 | IPC_CREAT);

        podnies(see, 1);

}

chmooreck

  • Gość
Semafory
« Odpowiedź #1 dnia: 2011-12-30, 20:31:22 »
gdzie sprawdziłeś co zwraca semget ?

Kto Cię uczył definiować funkcje wenątrz main'a ? Taki kod jest bardzo nieczytelny.

twardziel

  • Gość
Semafory
« Odpowiedź #2 dnia: 2011-12-30, 21:01:38 »
Teraz jest File too large

#include 
#include
#include
struct sembuf buf;

void podnies(int semid, int semnum){
        buf.sem_num = semnum;
        buf.sem_op = 1;
        buf.sem_flg = IPC_NOWAIT;

        if (semop(semid, &buf, 1) == -1){
                perror("Podnoszenie semafora\\n");
                exit(1);
        }
}



void opusc(int semid, int semnum){

        buf.sem_num = semnum;
        buf.sem_op = -1;
        buf.sem_flg = 0;
        if (semop(semid, &buf, 1) == -1){
                perror("Opuszczenie semafora");
                exit(1);
        }

}



main(){
int see = semget(200, 1, IPC_CREAT | 0600 );
if (see == -1)
{
        perror("blad tworzenia tablicy semaforow\\n");
        exit(1);
}
        podnies(see, 1);

}

Offline vanhelzing

  • Users
  • Prawie jak Guru
  • ****
  • Wiadomości: 314
    • Zobacz profil
Semafory
« Odpowiedź #3 dnia: 2011-12-30, 21:47:36 »
Semafory są numerowane od zera, więc pierwszy semafor ma numer 0, nie 1.

twardziel

  • Gość
Semafory
« Odpowiedź #4 dnia: 2012-01-02, 20:54:54 »
Dzięki wam za pomoc