first implementation semctl, first flags
This commit is contained in:
+2
-2
@@ -101,7 +101,7 @@ long sys_semop(int semid, struct sembuf *sops, unsigned nsops);
|
||||
/// @param cmd the command to perform.
|
||||
/// @param arg
|
||||
/// @return 0 on success, -1 on failure and errno is set to indicate the error.
|
||||
long sys_semctl(int semid, int semnum, int cmd, unsigned long arg);
|
||||
long sys_semctl(int semid, int semnum, int cmd, union semun arg);
|
||||
|
||||
#else
|
||||
|
||||
@@ -127,6 +127,6 @@ long semop(int semid, struct sembuf *sops, unsigned nsops);
|
||||
/// @param cmd the command to perform.
|
||||
/// @param arg
|
||||
/// @return 0 on success, -1 on failure and errno is set to indicate the error.
|
||||
long semctl(int semid, int semnum, int cmd, unsigned long arg);
|
||||
long semctl(int semid, int semnum, int cmd, union semun arg);
|
||||
|
||||
#endif
|
||||
|
||||
+103
-13
@@ -28,6 +28,9 @@
|
||||
key is already used
|
||||
*/
|
||||
|
||||
int semid_assign = 0;
|
||||
|
||||
|
||||
/// @brief to initialize a single semaphore
|
||||
/// @param temp the pointer to the struct of the semaphore
|
||||
void sem_init(struct sem * temp){
|
||||
@@ -43,7 +46,7 @@ void sem_init(struct sem * temp){
|
||||
void semid_init(struct semid_ds * temp, key_t key, int nsems){
|
||||
temp -> owner = sys_getpid();
|
||||
temp -> key = key;
|
||||
temp -> semid = key+1; //temporary -> gonna need a way to compute IPCKEY -> semid
|
||||
temp -> semid = ++semid_assign; //temporary -> gonna need a way to compute IPCKEY -> semid
|
||||
temp -> sem_otime = 0;
|
||||
temp -> sem_ctime = 0;
|
||||
temp -> sem_nsems = nsems;
|
||||
@@ -59,14 +62,15 @@ 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\n", temp->sems[i].sem_val);
|
||||
printf("pid of last op: %d\n", temp->sems[i].sem_pid);
|
||||
printf("process waiting: %d\n", temp->sems[i].sem_zcnt);
|
||||
printf("value: %d, pid %d, process waiting %d\n", temp->sems[i].sem_val, temp->sems[i].sem_pid, temp->sems[i].sem_zcnt);
|
||||
//printf("pid of last op: %d\n", temp->sems[i].sem_pid);
|
||||
//printf("process waiting: %d\n", temp->sems[i].sem_zcnt);
|
||||
}
|
||||
}
|
||||
|
||||
int count = 0; //temporary
|
||||
|
||||
|
||||
/// @brief list of all current active semaphores
|
||||
list_t *current_semaphores;
|
||||
|
||||
@@ -158,19 +162,105 @@ long sys_semop(int semid, struct sembuf *sops, unsigned nsops)
|
||||
return 0;
|
||||
}
|
||||
|
||||
long sys_semctl(int semid, int semnum, int cmd, unsigned long arg)
|
||||
long sys_semctl(int semid, int semnum, int cmd, union semun arg)
|
||||
{
|
||||
int flag = 0;
|
||||
struct semid_ds *temp;
|
||||
listnode_foreach(listnode, current_semaphores){ //iterate through the list
|
||||
if (((struct semid_ds *)listnode->value)->semid == semid){ //if we find a semid with the given key
|
||||
flag = 1;
|
||||
temp = ((struct semid_ds *)listnode->value);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Check if semid is a valid semaphore identifier
|
||||
*/
|
||||
/* .... */
|
||||
if (!flag){
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
Check the cmd parameter (GETVAL, SETVAL...) and perform the operation
|
||||
*/
|
||||
/* .... */
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
//remove the semaphore set; any processes blocked is awakened (errno set to EIDRM); no argument required.
|
||||
case IPC_RMID:
|
||||
list_remove_node(current_semaphores, list_find(current_semaphores, temp));
|
||||
|
||||
//printf("\ndone\n");
|
||||
/*gonna need to unblock all the processes on the semaphore*/
|
||||
|
||||
|
||||
break;
|
||||
|
||||
//place a copy of the semid_ds data structure in the buffer pointed to by arg.buf.
|
||||
case IPC_STAT:
|
||||
/* code */
|
||||
break;
|
||||
|
||||
//update selected fields of the semid_ds using values in the buffer pointed to by arg.buf.
|
||||
case IPC_SET:
|
||||
/* code */
|
||||
break;
|
||||
|
||||
//the value of the semnum-th semaphore in the set is initialized to the value specified in arg.val.
|
||||
case SETVAL:
|
||||
if(semnum<0 || semnum>=(temp->sem_nsems)){ //if the index is valid
|
||||
//printf("Errore semnum\n"); //debuggin purposes
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if(arg.val<0){
|
||||
errno = ERANGE;
|
||||
return -1;
|
||||
}
|
||||
temp->sems[semnum].sem_val = arg.val;
|
||||
return 0;
|
||||
|
||||
//returns the value of the semnum-th semaphore in the set specified by semid; no argument required.
|
||||
case GETVAL:
|
||||
if(semnum<0 || semnum>=(temp->sem_nsems)){ //if the index is valid
|
||||
//printf("Errore semnum\n"); //debuggin purposes
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return temp->sems[semnum].sem_val;
|
||||
|
||||
//initialize all semaphore in the set referred to by semid, using the values supplied in the array pointed to by arg.array.
|
||||
case SETALL:
|
||||
/* code */
|
||||
break;
|
||||
|
||||
//retrieve the values of all of the semaphores in the set referred to by semid, placing them in the array pointed to by arg.array.
|
||||
case GETALL:
|
||||
/* code */
|
||||
break;
|
||||
|
||||
//return the process ID of the last process to perform a semop on the semnum-th semaphore.
|
||||
case GETPID:
|
||||
if(semnum<0 || semnum>=(temp->sem_nsems)){ //if the index is valid
|
||||
//printf("Errore semnum\n"); //debuggin purposes
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return temp->sems[semnum].sem_pid;
|
||||
|
||||
//return the number of processes currently waiting for the value of the semnum-th semaphore to become 0.
|
||||
case GETZCNT:
|
||||
if(semnum<0 || semnum>=(temp->sem_nsems)){ //if the index is valid
|
||||
//printf("Errore semnum\n"); //debuggin purposes
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return temp->sems[semnum].sem_zcnt;
|
||||
|
||||
//not a valid argument.
|
||||
default:
|
||||
//printf("defualt\n");
|
||||
errno = EINVAL; //
|
||||
return -1;
|
||||
}
|
||||
|
||||
//TODO("Not implemented");
|
||||
return 0;
|
||||
|
||||
@@ -2,23 +2,40 @@
|
||||
#include "ipc/sem.h"
|
||||
#include "stdio.h"
|
||||
#include "ipc/ipc.h"
|
||||
int main()
|
||||
{
|
||||
#include "sys/errno.h"
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
union semun arg;
|
||||
arg.val = 150;
|
||||
//key_t i = ftok("/test1.txt", 5); //checking ftok
|
||||
long id = semget(2,2,0); //first semaphores
|
||||
//long id2 = semget(IPC_PRIVATE, 1, IPC_CREAT);
|
||||
//long id2 = semget(2, 2, IPC_EXCL); //need to return -1 bc already exists
|
||||
|
||||
long id2 = semget(2, 2, IPC_EXCL); //need to return -1 bc already exists
|
||||
//long id3 = semget(2, 2, 0); //need to return 3 (semid of the first)
|
||||
|
||||
long id3 = semget(2, 2, 0); //need to return 3 (semid of the first)
|
||||
//long id4 = semget(100, 2, IPC_CREAT | IPC_EXCL); //need to return 101 (new semaphore)
|
||||
long value;
|
||||
|
||||
long id4 = semget(100, 2, IPC_CREAT | IPC_EXCL); //need to return 101 (new semaphore)
|
||||
if ((value = semctl(id, 1, SETVAL, arg)) == -1){
|
||||
printf("%d \n", value);
|
||||
}else{
|
||||
printf("getpid: %d\n", value);
|
||||
}
|
||||
|
||||
/*if ((semid = semctl(id, 0, IPC_RMID, 0)) == -1){
|
||||
printf("%d\n", errno);
|
||||
}else{
|
||||
printf("found %d\n", semid);
|
||||
}*/
|
||||
|
||||
|
||||
//long id2 = semget(IPC_PRIVATE, 2, 0);
|
||||
//long id3 = semget(IPC_PRIVATE, 2, 0);
|
||||
printf("id %d id2 %d id3 %d id4 %d\n", id, id2, id3, id4);
|
||||
//printf("id %d id2 %d id3 %d id4 %d\n", id, id2, id3, id4);
|
||||
/*if (i == -1){
|
||||
printf("Error with IPC_KEY\n");
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user