First working implementation of System V Semaphores with working tests. Check for comments inside the code

This commit is contained in:
Aldegheri Alessandro
2023-04-11 21:12:44 +02:00
parent 0fb3a8d5b2
commit cc4eed7788
6 changed files with 279 additions and 52 deletions
+1
View File
@@ -86,6 +86,7 @@ set(TESTS
t_itimer.c
t_gid.c
t_semget.c
t_sem1.c
)
foreach(TEST ${TESTS})
+92
View File
@@ -0,0 +1,92 @@
//test
#include "ipc/sem.h"
#include "stdio.h"
#include "ipc/ipc.h"
#include "sys/errno.h"
#include "sys/wait.h"
#include "stdlib.h"
#include "sys/unistd.h"
/*
First test of System V Semaphores.
Correct output should be: Corso di Sistemi Operativi.
*/
void semid_print(struct semid_ds *temp){
printf("pid, IPC_KEY, Semid, semop, change: %d, %d, %d, %d, %d\n", temp->owner, temp->key, temp->semid, temp->sem_otime, temp->sem_ctime);
for(int i=0; i<(temp->sem_nsems); i++){
printf("%d semaphore:\n", i+1);
printf("value: %d, pid %d, process waiting %d\n", temp->sems[i].sem_val, temp->sems[i].sem_pid, temp->sems[i].sem_zcnt);
}
}
int main()
{
union semun arg;
struct sembuf sops[6];
sops[0].sem_num = 0;
sops[0].sem_op = -1;
sops[0].sem_flg = 0;
sops[1].sem_num = 1;
sops[1].sem_op = -1;
sops[1].sem_flg = 0;
sops[2].sem_num = 2;
sops[2].sem_op = -1;
sops[2].sem_flg = 0;
sops[3].sem_num = 0;
sops[3].sem_op = 1;
sops[3].sem_flg = 0;
sops[4].sem_num = 1;
sops[4].sem_op = 1;
sops[4].sem_flg = 0;
sops[5].sem_num = 2;
sops[5].sem_op = 1;
sops[5].sem_flg = 0;
int status;
//create a semaphore set
int semid = semget(IPC_PRIVATE, 4, IPC_CREAT);
//set the values of the semaphores
unsigned short values[] = {0,0,0,1};
arg.array = values;
if(semctl(semid,0,SETALL,&arg)==-1)
write(1,"errore set dei semafori",24);
if(!fork()){
semop(semid, &sops[0], 1);
write(1,"Operativi.\n", 11);
exit(0);
}
if(!fork()){
semop(semid, &sops[1], 1);
write(1,"Sistemi ", 8);
semop(semid, &sops[3], 1);
exit(0);
}
if(!fork()){
semop(semid, &sops[2], 1);
write(1," di ", 4);
semop(semid, &sops[4], 1);
exit(0);
}
if(!fork()){
write(1,"Corso", 5);
semop(semid, &sops[5], 1);
exit(0);
}
for(int n=0; n<4; n++){
wait(&status);
}
if(semctl(semid,0,IPC_RMID,0)==-1)
write(1,"err", 3);
return 0;
}
+49 -6
View File
@@ -3,15 +3,32 @@
#include "stdio.h"
#include "ipc/ipc.h"
#include "sys/errno.h"
#include "stdlib.h"
#include "sys/unistd.h"
/*
Testing Semaphores.
This program creates a son and then performs a blocking operation on a semaphore. The son sleeps for
five seconds and then it wakes up his father and then it deletes the semaphore.
*/
void semid_print(struct semid_ds *temp){
printf("pid, IPC_KEY, Semid, semop, change: %d, %d, %d, %d, %d\n", temp->owner, temp->key, temp->semid, temp->sem_otime, temp->sem_ctime);
for(int i=0; i<(temp->sem_nsems); i++){
printf("%d semaphore:\n", i+1);
printf("value: %d, pid %d, process waiting %d\n", temp->sems[i].sem_val, temp->sems[i].sem_pid, temp->sems[i].sem_zcnt);
}
}
int main()
{
union semun arg;
arg.val = 150;
arg.val = 1;
//key_t i = ftok("/test1.txt", 5); //checking ftok
long id = semget(2,2,0); //first semaphores
long id = semget(2,1,IPC_CREAT); //first semaphores
//long id2 = semget(IPC_PRIVATE, 1, IPC_CREAT);
//long id2 = semget(2, 2, IPC_EXCL); //need to return -1 bc already exists
@@ -19,13 +36,39 @@ int main()
//long id4 = semget(100, 2, IPC_CREAT | IPC_EXCL); //need to return 101 (new semaphore)
long value;
if ((value = semctl(id, 1, SETVAL, arg)) == -1){
printf("%d \n", value);
printf("ID: %d\n", id);
if ((value = semctl(id, 0, SETVAL, &arg)) == -1){
printf("ERRORE\n");
}else{
printf("getpid: %d\n", value);
//semid_print(arg.v);
printf("valore sem 0: %d \n", semctl(id, 0, GETVAL, &arg));
}
if (!fork()){
sleep(5);
struct sembuf op2;
op2.sem_num = 0;
op2.sem_op = 150;
op2.sem_flg = 0;
semop(id, &op2, 1);
semctl(id, 0, IPC_RMID, NULL);
exit(0);
}
struct sembuf op;
op.sem_num = 0;
op.sem_op = -1;
op.sem_flg = 0;
if (semop(id, &op, 2) < 0){
printf("ERRORE\n");
}
printf("valore sem 0: %d \n", semctl(id, 0, GETVAL, &arg));
/*if ((semid = semctl(id, 0, IPC_RMID, 0)) == -1){
printf("%d\n", errno);
}else{