Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Zademonstruj działanie operacji SETALL, GETALL, IPC_STAT, GETPID, GETZCNT.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/sem.h>
- #include <time.h>
- #define NUMBER_OF_SEMAPHORES 4
- int main(void)
- {
- srand(time(NULL));
- int semid;
- union semun arg;
- u_short *array_buffer;
- struct semid_ds buffer;
- pid_t pid;
- short semnum;
- arg.array = (u_short *)calloc(NUMBER_OF_SEMAPHORES, sizeof(u_short));
- array_buffer = (u_short *)calloc(NUMBER_OF_SEMAPHORES, sizeof(u_short));
- if((semid = semget(IPC_PRIVATE, NUMBER_OF_SEMAPHORES, 0775 | IPC_CREAT | IPC_EXCL)) == -1){
- perror("semget");
- return EXIT_FAILURE;
- }
- /* SETALL */
- for(int i = 0; i < NUMBER_OF_SEMAPHORES; i++)
- arg.array[i] = rand() % 10;
- if(semctl(semid, 0, SETALL, arg.array) == -1){
- perror("setall_semctl");
- return EXIT_FAILURE;
- }
- /* GETALL */
- if(semctl(semid, 0, GETALL, array_buffer) == -1){
- perror("getall_semctl");
- return EXIT_FAILURE;
- }
- printf("Wartosci semaforow: ");
- for(int i = 0; i < NUMBER_OF_SEMAPHORES; i++)
- printf("%hu ", array_buffer[i]);
- printf("\n");
- /* IPC_STAT */
- if(semctl(semid, 0, IPC_STAT, &buffer) == -1){
- perror("ipc_stat_semctl");
- return EXIT_FAILURE;
- }
- printf("Liczba semaforow: %hu\n", buffer.sem_nsems);
- /* GETPID */
- if(semctl(semid, 0, GETPID, &pid) == -1){
- perror("pid_semctl");
- return EXIT_FAILURE;
- }
- printf("Pid procesu, ktory ostatnio wykonywal operacje na semaforach: %d\n", pid);
- /* GETZCNT */
- if((semnum = semctl(semid, 0, GETZCNT)) == -1){
- perror("getzcnt_semctl");
- return EXIT_FAILURE;
- }
- printf("Ilosc czekajacych procesow: %hu\n", semnum);
- if(semctl(semid, 0, IPC_RMID) == -1){
- perror("ipc_rmid_semctl");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment