Created
December 9, 2008 14:58
-
-
Save vsalbaba/33920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <sys/types.h> | |
| #include <semaphore.h> | |
| #include <pthread.h> | |
| #include <sys/wait.h> | |
| sem_t writer_semafor; | |
| sem_t reader_semafor; | |
| char *buffer; | |
| void *producent(void *ptr) { | |
| for (int i = 0; i < 10; i++) { | |
| printf("waiting na ks - by writer\n"); | |
| sem_wait(&writer_semafor); | |
| printf("kriticka sekce! - by writer\n"); | |
| buffer = "aaaaaaa"; | |
| printf("jdu pryc z kriticke sekce! - by writer\n"); | |
| sem_post(&reader_semafor); | |
| } | |
| pthread_exit(0); | |
| } | |
| void *konzument(void * ptr) { | |
| for (int i = 0; i < 10; i++) { | |
| printf("waiting na ks - by reader\n"); | |
| sem_wait(&reader_semafor); | |
| printf("kriticka sekce! - by reader\n"); | |
| printf("%s << todle bylo v bufferu\n", buffer); | |
| printf("jdu pryc z kriticke sekce! - by reader\n"); | |
| sem_post(&writer_semafor); | |
| } | |
| pthread_exit(0); | |
| } | |
| int main() { | |
| pthread_t my_thread; | |
| pthread_t my_other_thread; | |
| pthread_attr_t thread_attributes; | |
| pthread_attr_init(&thread_attributes); | |
| sem_init(&writer_semafor, 0, 1); | |
| sem_init(&reader_semafor, 0, 0); | |
| pthread_create(&my_thread, &thread_attributes, &konzument, NULL); | |
| pthread_create(&my_other_thread, &thread_attributes, &producent, NULL); | |
| pthread_join(my_other_thread, 0); | |
| pthread_join(my_thread, 0); | |
| sem_destroy(&writer_semafor); | |
| sem_destroy(&reader_semafor); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <sys/types.h> | |
| #include <sys/ipc.h> | |
| #include <sys/sem.h> | |
| #include <unistd.h> | |
| #include <sys/wait.h> | |
| #include <sys/stat.h> | |
| #include <sys/shm.h> | |
| #include <sys/wait.h> | |
| char *buffer; | |
| /* tenhle kod by se mel VZDY pouzivat pro definici union semun */ | |
| #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) | |
| /* union semun is defined by including <sys/sem.h> */ | |
| #else | |
| /* according to X/OPEN we have to define it ourselves */ | |
| union semun { | |
| int val; /* value for SETVAL */ | |
| struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ | |
| unsigned short int *array; /* array for GETALL, SETALL */ | |
| struct seminfo *__buf; /* buffer for IPC_INFO */ | |
| }; | |
| #endif | |
| /* ziskani id semaforu ke klici key, nebo alokace noveho */ | |
| int semaphore_allocate(key_t key, int sem_flags) { | |
| return semget(key, 1, sem_flags); | |
| } | |
| /* dealokace semaforu */ | |
| int semaphore_deallocate(int semid) { | |
| union semun ignored_arg; | |
| return semctl(semid, 1, IPC_RMID, ignored_arg); | |
| } | |
| /* inicilizace semaforu */ | |
| int semaphore_initialize(int semid, int value) { | |
| union semun arg; | |
| unsigned short values[1]; | |
| values[0] = value; | |
| arg.array = values; | |
| return semctl(semid, 0, SETALL, arg); | |
| } | |
| /* operace wait */ | |
| int semaphore_wait(int semid) { | |
| struct sembuf operations[1]; | |
| operations[0].sem_num = 0; | |
| operations[0].sem_op = -1; | |
| operations[0].sem_flg = SEM_UNDO; | |
| return semop(semid, operations, 1); | |
| } | |
| /* operace post */ | |
| int semaphore_post(int semid) { | |
| struct sembuf operations[1]; | |
| operations[0].sem_num = 0; | |
| operations[0].sem_op = 1; | |
| operations[0].sem_flg = SEM_UNDO; | |
| return semop(semid, operations, 1); | |
| } | |
| int main() { | |
| key_t key = 100; | |
| int shm_id; | |
| pid_t child_pid; | |
| int shm_size = 1024; | |
| char *shm_addr; | |
| shm_id = shmget(key, shm_size, IPC_CREAT | S_IROTH | S_IWOTH | S_IRUSR | S_IWUSR); | |
| int sem; | |
| /* alokace semaforu */ | |
| sem = semaphore_allocate(IPC_PRIVATE, IPC_CREAT | 0600); | |
| /* inicializace na 0 */ | |
| semaphore_initialize(sem, 0); | |
| child_pid = fork(); | |
| if (child_pid == 0) { | |
| printf("potomek: cekam na semafor\n"); | |
| /* pockame na nastaveny semafor */ | |
| semaphore_wait(sem); | |
| struct shmid_ds shm_buf; | |
| shm_addr = (char *) shmat(shm_id, NULL, 0); | |
| shmctl(shm_id, IPC_STAT, &shm_buf); | |
| printf("%d\n", (int) shm_buf.shm_segsz); | |
| printf("%s\n", shm_addr); | |
| shmdt(shm_addr); | |
| } else if (child_pid != -1) { | |
| printf("rodic: pockame vterinku\n"); | |
| sleep(1); | |
| printf("rodic: pustime potomka (nastavime semafor)\n"); | |
| shm_addr = (char *) shmat(shm_id, NULL, 0); | |
| sprintf(shm_addr, "Banik!"); | |
| shmdt(shm_addr); | |
| /* nastavime semafor */ | |
| semaphore_post(sem); | |
| waitpid(child_pid, NULL, 0); | |
| /* zruseni semaforu - NUTNE! */ | |
| semaphore_deallocate(sem); | |
| } else return 1; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment