diff --git a/libc/src/ipc/ipc.c b/libc/src/ipc/ipc.c index 4cc9da8..429e156 100644 --- a/libc/src/ipc/ipc.c +++ b/libc/src/ipc/ipc.c @@ -12,6 +12,7 @@ #include "ipc/msg.h" #include "sys/stat.h" #include "io/debug.h" +#include "stdio.h" _syscall3(void *, shmat, int, shmid, const void *, shmaddr, int, shmflg) @@ -41,15 +42,22 @@ long semop(int semid, struct sembuf *sops, unsigned nsops) errno = EINVAL; return -1; } - // The process continues to try to perform the operation until it completes - // or receives an error. - while (1) { - // Calling the kernel-side function. - __inline_syscall3(__res, semop, semid, sops, nsops); - // If we get an error we stop the loop. - if (__res != OPERATION_NOT_ALLOWED) - break; + + //this should be performed for each sops. + for (size_t i = 0; i < nsops; i++){ + // The process continues to try to perform the operation until it completes + // or receives an error. + while (1) { + // Calling the kernel-side function. + __inline_syscall3(__res, semop, semid, &sops[i], 1); + // If we get an error we stop the loop. + if (__res != OPERATION_NOT_ALLOWED) + break; + } + //printf("op eseguita: %d\n", __res); } + + // Now, we can return the value. __syscall_return(long, __res); } diff --git a/mentos/src/ipc/sem.c b/mentos/src/ipc/sem.c index 5c2c538..87bd770 100644 --- a/mentos/src/ipc/sem.c +++ b/mentos/src/ipc/sem.c @@ -205,18 +205,18 @@ long sys_semop(int semid, struct sembuf *sops, unsigned nsops) /*If the operation is negative then we need to check for possible blocking operation*/ /*if the value of the sem were to become negative then we return a special value*/ - if (temp->sems[sops->sem_num].sem_val < (-nsops * (sops->sem_op))) { + if (temp->sems[sops->sem_num].sem_val < (-(sops->sem_op))) { return OPERATION_NOT_ALLOWED; //not allowed } else { /*otherwise we can modify the sem_val and all the other parameters of the semaphore*/ - temp->sems[sops->sem_num].sem_val += (nsops * (sops->sem_op)); + temp->sems[sops->sem_num].sem_val += (sops->sem_op); temp->sems[sops->sem_num].sem_pid = sys_getpid(); temp->sem_ctime = sys_time(NULL); return 1; //allowed } } else { /*the operation is non negative so we can always do it*/ - temp->sems[sops->sem_num].sem_val += (nsops * (sops->sem_op)); + temp->sems[sops->sem_num].sem_val += (sops->sem_op); temp->sems[sops->sem_num].sem_pid = sys_getpid(); temp->sem_ctime = sys_time(NULL); return 1; //allowed diff --git a/programs/tests/t_semget.c b/programs/tests/t_semget.c index 9d503f3..094b2cd 100644 --- a/programs/tests/t_semget.c +++ b/programs/tests/t_semget.c @@ -25,6 +25,7 @@ void semid_print(struct semid_ds *temp) int main() { struct sembuf op; + struct sembuf father[2]; union semun arg; long ret, id; key_t key; @@ -43,7 +44,7 @@ int main() //long id4 = semget(100, 2, IPC_CREAT | IPC_EXCL); //need to return 101 (new semaphore) // Set the value of the semaphore in the structure. - arg.val = 1; + arg.val = 0; // Setting the semaphore value. printf("Set Value (%d): %d\n", id, arg.val); ret = semctl(id, 0, SETVAL, &arg); @@ -54,12 +55,12 @@ int main() // Check if we successfully set the value of the semaphore. ret = semctl(id, 0, GETVAL, &arg); - printf("Check Value (%d): %d\n", id, ret); + printf("Check Value before(%d): %d\n", id, ret); // Create child process. if (!fork()) { // Make the child process sleep. - sleep(5); + sleep(2); // Initialize the operation structure. op.sem_num = 0; // Operate on semaphore 0. @@ -70,31 +71,35 @@ int main() // Perform the operation. semop(id, &op, 1); - + sleep(5); + semop(id, &op, 1); // Remove the semaphore. - semctl(id, 0, IPC_RMID, NULL); + //semctl(id, 0, IPC_RMID, NULL); // Exit with the child process. return 0; } // Initialize the operation structure. - op.sem_num = 0; // Operate on semaphore 0. - op.sem_op = -2; // Decrement value by 2. - op.sem_flg = 0; // No flags. + father[0].sem_num = 0; // Operate on semaphore 0. + father[0].sem_op = -1; // Decrement value by 1. + father[0].sem_flg = 0; // No flags. + father[1].sem_num = 0; // Operate on semaphore 0. + father[1].sem_op = -1; // Decrement value by 1. + father[1].sem_flg = 0; // No flags. // We set the semaphore at the beginning at 1, and the child process will // increment the semaphore by 1, allowing us to decrement the semaphore by // 2. // Perform the operation. - if (semop(id, &op, 1) < 0) { + if (semop(id, father, 2) < 0) { printf("ERRORE\n"); } // Check if we successfully set the value of the semaphore. ret = semctl(id, 0, GETVAL, &arg); - printf("Check Value (%d): %d\n", id, ret); + printf("Check Value after(%d): %d\n", id, ret); /*if ((semid = semctl(id, 0, IPC_RMID, 0)) == -1){ printf("%d\n", errno);