|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Jun 2006
Messaggi: 86
|
[C] Semaforo prova con GCC Problemi
Salve quando compilo in gcc su linux kubuntu mi dà dei problemi. E mi dice che alcune librerie non si trovano quelle.h. COme posso procedere?
GRazie ecco il codice: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <linux/types.h> #include <linux/ipc.h> #include <linux/sem.h> int main(int argc, char *argv[]) { /* IPC */ pid_t pid; key_t key; int semid; union semun arg; struct sembuf lock_res = {0, -1, 0}; struct sembuf rel_res = {0, 1, 0}; struct sembuf push[2] = {1, -1, IPC_NOWAIT, 2, 1, IPC_NOWAIT}; struct sembuf pop[2] = {1, 1, IPC_NOWAIT, 2, -1, IPC_NOWAIT}; /* Other */ int i; if(argc < 2){ printf("Usage: bufdemo [dimensione]\n"); exit(0); } /* Semaphores */ key = ftok("/etc/fstab", getpid()); /* Create a semaphore set with 3 semaphore */ semid = semget(key, 3, 0666 | IPC_CREAT); /* Initialize semaphore #0 to 1 - Resource controller */ arg.val = 1; semctl(semid, 0, SETVAL, arg); /* Initialize semaphore #1 to buf_length - Overflow controller */ /* Sem value is 'free space in buffer' */ arg.val = atol(argv[1]); semctl(semid, 1, SETVAL, arg); /* Initialize semaphore #2 to buf_length - Underflow controller */ /* Sem value is 'elements in buffer' */ arg.val = 0; semctl(semid, 2, SETVAL, arg); /* Fork */ for (i = 0; i < 5; i++){ pid = fork(); if (!pid){ for (i = 0; i < 20; i++){ sleep(rand()%6); /* Try to lock resource - sem #0 */ if (semop(semid, &lock_res, 1) == -1){ perror("semop:lock_res"); } /* Lock a free space - sem #1 / Put an element - sem #2*/ if (semop(semid, &push, 2) != -1){ printf("---> Process:%d\n", getpid()); } else{ printf("---> Process:%d BUFFER FULL\n", getpid()); } /* Release resource */ semop(semid, &rel_res, 1); } exit(0); } } for (i = 0;i < 100; i++){ sleep(rand()%3); /* Try to lock resource - sem #0 */ if (semop(semid, &lock_res, 1) == -1){ perror("semop:lock_res"); } /* Unlock a free space - sem #1 / Get an element - sem #2 */ if (semop(semid, &pop, 2) != -1){ printf("<--- Process:%d\n", getpid()); } else printf("<--- Process:%d BUFFER EMPTY\n", getpid()); /* Release resource */ semop(semid, &rel_res, 1); } /* Destroy semaphores */ semctl(semid, 0, IPC_RMID); return 0; } |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Sep 2004
Messaggi: 3967
|
qual'è l'errore che ti restituisce il compilatore?
__________________
Dai wafer di silicio nasce: LoHacker... il primo biscotto Geek
|
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
#include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> Basta leggere sulle pagine di man delle singole funzioni che hai usato.
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:40.



















