IPCS, IPCRM, IPC_NOWAIT currently working

This commit is contained in:
Aldegheri Alessandro
2023-04-22 16:14:40 +02:00
parent b9244ab96b
commit 09f0d145d0
7 changed files with 200 additions and 83 deletions
+5
View File
@@ -0,0 +1,5 @@
SYNOPSIS
ipcs [options]
DESCRIPTION
Ipcs shows information on System V inter-process communication facilities.
+26 -60
View File
@@ -41,68 +41,19 @@ _syscall5(long, msgrcv, int, msqid, struct msgbuf *, msgp, size_t, msgsz, long,
_syscall3(long, msgctl, int, msqid, int, cmd, struct msqid_ds *, buf)
_syscall0(long, semipcs)
long ipcs(int argc, char** argv){
long ipcs(int argc, char** argv)
{
/*Right now we only call the semipcs function because shared memories and message queues
do not exist. When they will be implemented you will be able to just call their printing function
down here.
*/
long __res;
__inline_syscall0(__res, semipcs);
__syscall_return(long, __res);
/*if (argc>4)return -1;
if (argc == 1){ //default
long __res;
__inline_syscall0(__res, semipcs);
return __res;
}
if (argc == 2){
if (!strcmp(argv[1], "-s")){ //semaphores
long __res;
__inline_syscall0(__res, semipcs);
return __res;
}
if (!strcmp(argv[1], "-m")) { //shared memories
printf("Not Implemented!\n");
return 0;
}
if (!strcmp(argv[1], "-q")){ //message queues
printf("Not Implemented!\n");
return 0;
}
return -1;
}
else{
if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-s")){
union semun temp;
temp.buf = (struct semid_ds *)malloc(sizeof(struct semid_ds));
long __res;
__inline_syscall4(__res, semctl, atoi(argv[2]), 0, GETNSEMS, NULL);
temp.buf -> sems = (struct sem *)malloc(sizeof(struct sem) * __res);
__inline_syscall4(__res, semctl, atoi(argv[2]), 0, IPC_STAT, &temp);
printf("%d\t\t%d\t\t%d\t\t\t%d\n", temp.buf->key, temp.buf->semid, temp.buf->owner, temp.buf->sem_nsems);
if(__res == -1)
return -1;
return __res;
}
if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-m")){
printf("Not Implemented!\n");
return 0;
}
if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-q")){
printf("Not Implemented!\n");
return 0;
}
return -1;
}
return 0;*/
__syscall_return(long, __res);
}
long semop(int semid, struct sembuf *sops, unsigned nsops)
@@ -114,22 +65,37 @@ long semop(int semid, struct sembuf *sops, unsigned nsops)
errno = EINVAL;
return -1;
}
int flag_no_wait = 0;
//this should be performed for each sops.
for (size_t i = 0; i < nsops; i++){
/*Checking for IPC_NOWAIT flag*/
if (sops[i].sem_flg & IPC_NOWAIT){
flag_no_wait = 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[i], 1);
// If we get an error we stop the loop.
if (__res != OPERATION_NOT_ALLOWED)
/*
If we get an error, the operation has been taken care of we stop the loop.
We also stop the loop if the operation is not allowed and the IPC_NOWAIT flag is 1
*/
if (__res != OPERATION_NOT_ALLOWED || flag_no_wait)
break;
}
/*If the operation couldn't be performed and we had the IPC_NOWAIT set to 1 then we ret*/
if (flag_no_wait && __res == OPERATION_NOT_ALLOWED){
errno = EAGAIN;
return -1;
}
//printf("op eseguita: %d\n", __res);
}
// Now, we can return the value.
__syscall_return(long, __res);
}
+10 -9
View File
@@ -43,6 +43,7 @@
#include "process/process.h"
#include "sys/errno.h"
#include "assert.h"
#include "stdio.h"
///@brief A value to compute the semid value.
int semid_assign = 0;
@@ -118,21 +119,20 @@ void semid_print(struct semid_ds *temp)
/// @brief prints informations about the struct pointed by temp
/// @param temp the pointer to the semid struct to print
void show_ipcs(struct semid_ds *temp){
pr_debug("%d\t\t%d\t\t%d\t\t\t%d\n", temp->key, temp->semid, temp->owner, temp->sem_nsems);
printf("%d %d %d %d\n", temp->key, temp->semid, temp->owner, temp->sem_nsems);
}
/// @brief list of all current active semaphores
list_t *current_semaphores;
int count_ipc_private = 2; //temporary -> to implement the IPC_PRIVATE mechanism
long sys_semget(key_t key, int nsems, int semflg)
{
struct semid_ds *temp = NULL;
//check if nsems is a valid value
if (nsems <= 0) {
pr_err("Errore NSEMS\n"); //debuggin purposes
if (nsems <= 0 && semflg != 0) {
//pr_err("Errore NSEMS\n"); //debuggin purposes
errno = EINVAL;
return -1;
}
@@ -356,16 +356,17 @@ long sys_semctl(int semid, int semnum, int cmd, union semun *arg)
long sys_semipcs(){
//default operation --- no semaphores
if (current_semaphores == NULL){
pr_debug("------ Matrici semafori --------\n");
pr_debug("chiave\t\tsemid\t\tproprietario\t\tnsems\n");
printf("------ Matrici semafori --------\n");
printf("chiave semid proprietario nsems\n");
return 0;
}
pr_debug("------ Matrici semafori --------\n");
pr_debug("chiave\t\tsemid\t\tproprietario\t\tnsems\n");
printf("------ Matrici semafori --------\n");
printf("chiave semid proprietario nsems\n");
listnode_foreach(listnode, current_semaphores)
{ //iterate through the list
{ //iterate through the list
show_ipcs(((struct semid_ds *)listnode->value));
}
+74 -1
View File
@@ -4,12 +4,85 @@
/// See LICENSE.md for details.
#include <stdio.h>
#include <string.h>
#include <ipc/sem.h>
#include <ipc/ipc.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/*Checking for arguments*/
if (argc < 3){
printf("Missing arguments...\n");
return 0;
}
printf("Comando...\n");
if (argc == 3){
if (!strcmp("-a", argv[1]) || !strcmp("--all", argv[1])){
printf("To be implemented...\n");
return 0;
}
if (!strcmp("-M", argv[1])){
printf("To be implemented...\n");
return 0;
}
if (!strcmp("-m", argv[1])){
printf("To be implemented...\n");
return 0;
}
if (!strcmp("-Q", argv[1])){
printf("To be implemented...\n");
return 0;
}
if (!strcmp("-q", argv[1])){
printf("To be implemented...\n");
return 0;
}
/* Remove the semaphore set with the given key. */
if (!strcmp("-S", argv[1])){
int semid;
if((semid = semget(atoi(argv[2]), 0, 0)) == -1){
printf("There is no semaphores set with this key...\n");
return -1;
}
if (semctl(semid, 0, IPC_RMID, NULL) == -1){
printf("There is no semaphore set with this sem_id...\n");
return -1;
}
printf("Done!\n");
return 0;
}
/* Remove the semaphore set with the given id. */
if (!strcmp("-s", argv[1])){
int semid = atoi(argv[2]);
if (semctl(semid, 0, IPC_RMID, NULL) == -1){
printf("There is no semaphore set with this sem_id...\n");
return -1;
}
printf("Done!\n");
return 0;
}
printf("Unknown argument...\n");
}
return 0;
#if 0
if (argc != 2) {
+19 -13
View File
@@ -36,29 +36,31 @@ static inline void print_shm_stat()
int main(int argc, char **argv)
{
/*if (ipcs(argc, argv) == -1){
printf("Errore\n");
}*/
if (argc>4)
return -1;
if (argc>4)return -1;
if (argc == 1){ //default
if (argc == 1){ /*Default operation, prints all ipcs informations*/
long __res;
__res = ipcs();
return __res;
}
if (argc == 2){
if (!strcmp(argv[1], "-s")){ //semaphores
// all semaphores
if (!strcmp(argv[1], "-s")){
long __res;
__res = ipcs();
__res = semipcs();
return __res;
}
if (!strcmp(argv[1], "-m")) { //shared memories
// all shared memories
if (!strcmp(argv[1], "-m")) {
printf("Not Implemented!\n");
return 0;
}
if (!strcmp(argv[1], "-q")){ //message queues
// all message queues
if (!strcmp(argv[1], "-q")){
printf("Not Implemented!\n");
return 0;
}
@@ -66,25 +68,29 @@ int main(int argc, char **argv)
return -1;
}
else{
// semaphore by id
if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-s")){
union semun temp;
temp.buf = (struct semid_ds *)malloc(sizeof(struct semid_ds));
long __res;
__res = semctl(atoi(argv[2]), 0, GETNSEMS, NULL);
//__inline_syscall4(__res, semctl, atoi(argv[2]), 0, GETNSEMS, NULL);
temp.buf -> sems = (struct sem *)malloc(sizeof(struct sem) * __res);
__res = semctl(atoi(argv[2]), 0, IPC_STAT, &temp);
//__inline_syscall4(__res, semctl, atoi(argv[2]), 0, IPC_STAT, &temp);
printf("%d\t\t%d\t\t%d\t\t\t%d\n", temp.buf->key, temp.buf->semid, temp.buf->owner, temp.buf->sem_nsems);
printf("------ Matrici semafori --------\n");
printf("chiave semid proprietario nsems\n");
printf("%d %d %d %d\n", temp.buf->key, temp.buf->semid, temp.buf->owner, temp.buf->sem_nsems);
if(__res == -1)
return -1;
return __res;
}
// shared memory by id
if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-m")){
printf("Not Implemented!\n");
return 0;
}
// message queue by id
if(!strcmp(argv[1], "-i" ) && !strcmp(argv[3],"-q")){
printf("Not Implemented!\n");
return 0;
+1
View File
@@ -88,6 +88,7 @@ set(TESTS
# Test semaphores.
t_semget.c
t_sem1.c
t_semflg.c
# Test scheduling feedback tests.
t_schedfb.c
)
+65
View File
@@ -0,0 +1,65 @@
//test
#include "ipc/sem.h"
#include "stdio.h"
#include "ipc/ipc.h"
#include "sys/errno.h"
#include "stdlib.h"
#include "sys/unistd.h"
/*
Testing IPC_NOWAIT flag.
*/
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()
{
struct sembuf op[2];
union semun arg;
long ret, id;
// Create the first semaphore.
id = semget(IPC_PRIVATE, 1, IPC_CREAT);
printf("Id: %d\n", id);
// Set the value of the semaphore in the structure.
arg.val = 0;
// Setting the semaphore value.
printf("Set Value (%d): %d\n", id, arg.val);
ret = semctl(id, 0, SETVAL, &arg);
if (ret == -1) {
perror("Failed to set value of semaphore.");
return 1;
}
op[0].sem_num = 0;
op[0].sem_op = 1;
op[0].sem_flg = IPC_NOWAIT;
op[1].sem_num = 0;
op[1].sem_op = -2;
op[1].sem_flg = IPC_NOWAIT;
// Check the value of the semaphore.
ret = semctl(id, 0, GETVAL, &arg);
printf("Check Value before(%d): %d\n", id, ret);
sleep(1);
if((ret = semop(id, op, 2)) == -1){
perror("Failed to perform the operations on the value of semaphore.");
return 1;
}
// Check the value of the semaphore.
ret = semctl(id, 0, GETVAL, &arg);
printf("Check Value after(%d): %d\n", id, ret);
return 0;
}