Fix third test program. Fix EAGAIN message.

This commit is contained in:
Enrico Fraccaroli (Galfurian)
2023-05-23 14:05:41 -04:00
parent 3ae9258347
commit 1acec3e7b3
8 changed files with 101 additions and 37 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ extern int *__geterrno();
#define ENOEXEC 8 ///< Exec format error.
#define EBADF 9 ///< Bad file number.
#define ECHILD 10 ///< No child processes.
#define EAGAIN 11 ///< Try again.
#define EAGAIN 11 ///< Resource temporarily unavailable.
#define ENOMEM 12 ///< Out of memory.
#define EACCES 13 ///< Permission denied.
#define EFAULT 14 ///< Bad address.
+2
View File
@@ -216,4 +216,6 @@ void perror(const char *s)
putchar(' ');
}
puts(strerror(errno));
puts("\n");
puts("\n");
}
+1 -1
View File
@@ -76,7 +76,7 @@ char *strerror(int errnum)
#endif
#ifdef EAGAIN
case EAGAIN:
strcpy(error, "No more processes");
strcpy(error, "Resource temporarily unavailable");
break;
#endif
#ifdef ENOMEM
+1 -1
View File
@@ -6,7 +6,7 @@
// Setup the logging for this file (do this before any other include).
#include "sys/kernel_levels.h" // Include kernel log levels.
#define __DEBUG_HEADER__ "[EXT2 ]" ///< Change header.
#define __DEBUG_LEVEL__ LOGLEVEL_DEBUG ///< Set log level.
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "fs/ext2.h"
+1 -1
View File
@@ -6,7 +6,7 @@
// Setup the logging for this file (do this before any other include).
#include "sys/kernel_levels.h" // Include kernel log levels.
#define __DEBUG_HEADER__ "[VFS ]" ///< Change header.
#define __DEBUG_LEVEL__ LOGLEVEL_DEBUG ///< Set log level.
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "fs/vfs.h"
+1 -1
View File
@@ -34,7 +34,7 @@
// Setup the logging for this file (do this before any other include).
#include "sys/kernel_levels.h" // Include kernel log levels.
#define __DEBUG_HEADER__ "[IPCsem]" ///< Change header.
#define __DEBUG_LEVEL__ LOGLEVEL_DEBUG ///< Set log level.
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
// ============================================================================
+1 -1
View File
@@ -6,7 +6,7 @@
// Setup the logging for this file (do this before any other include).
#include "sys/kernel_levels.h" // Include kernel log levels.
#define __DEBUG_HEADER__ "[PROC ]" ///< Change header.
#define __DEBUG_LEVEL__ LOGLEVEL_DEBUG ///< Set log level.
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "process/process.h"
+93 -31
View File
@@ -13,45 +13,107 @@
int main(int argc, char *argv[])
{
struct sembuf op[2];
struct sembuf op[1];
union semun arg;
long ret, id;
// Create the first semaphore.
id = semget(IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
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;
}
long ret, semid;
op[0].sem_num = 0;
op[0].sem_op = 1;
op[0].sem_op = -2;
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 semop");
// ========================================================================
// Create the first semaphore.
semid = semget(IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (semid < 0) {
perror("Failed to create semaphore set");
return 1;
}
printf("[father] Created semaphore set (semid : %d)\n", semid);
// Check the value of the semaphore.
ret = semctl(id, 0, GETVAL, &arg);
printf("Check Value after(%d): %d\n", id, ret);
// ========================================================================
// Set the value of the semaphore in the structure.
arg.val = 1;
// Setting the semaphore value.
ret = semctl(semid, 0, SETVAL, &arg);
if (ret < 0) {
perror("Failed to set value of semaphore");
return 1;
}
printf("[father] Set semaphore value (id : %d, value : %d == 1)\n", semid, arg.val);
// ========================================================================
// Check if we successfully set the value of the semaphore.
ret = semctl(semid, 0, GETVAL, NULL);
if (ret < 0) {
perror("Failed to get the value of semaphore set");
return 1;
}
printf("[father] Get semaphore value (id : %d, value : %d == 1)\n", semid, ret);
// ========================================================================
// Create child process.
if (!fork()) {
struct sembuf op_child;
// Initialize the operation structure.
op_child.sem_num = 0; // Operate on semaphore 0.
op_child.sem_op = 1; // Increment value by 1.
op_child.sem_flg = 0; // No flags.
sleep(3);
// ====================================================================
// Increment semaphore value.
if (semop(semid, &op_child, 1) < 0) {
perror("Failed to perform first child operation");
return 1;
}
printf("[child] Succesfully performed opeation (id : %d)\n", semid);
// ====================================================================
// Check if we successfully set the value of the semaphore.
ret = semctl(semid, 0, GETVAL, NULL);
if (ret < 0) {
perror("Failed to get the value of semaphore set");
return 1;
}
printf("[child] Get semaphore value (id : %d, value : %d == 1)\n", semid, ret);
printf("[child] Exit, now.\n", semid, ret);
// ========================================================================
// Delete the semaphore set.
ret = semctl(semid, 0, IPC_RMID, 0);
if (ret < 0) {
perror("Failed to remove semaphore set");
}
printf("[child] Correctly removed semaphore set.\n");
return 0;
}
// ========================================================================
// Perform the operations.
if (semop(semid, op, 2) < 0) {
perror("Failed to perform operation");
return 1;
}
printf("[father] Performed semaphore operations (id : %d)\n", semid);
// ========================================================================
// Check if we successfully set the value of the semaphore.
ret = semctl(semid, 0, GETVAL, NULL);
if (ret < 0) {
perror("Failed to get the value of semaphore set");
return 1;
}
printf("[father] Get semaphore value (id : %d, value : %d == 1)\n", semid, ret);
// ========================================================================
// Delete the semaphore set.
ret = semctl(semid, 0, IPC_RMID, 0);
if (ret < 0) {
perror("Failed to remove semaphore set");
}
printf("[father] Correctly removed semaphore set.\n");
return 0;
}