I have a problem with my code because I do not know why if I debug it well because when I run it, it gives me the following errors.
I can not understand if it is a problem of some include, because it has execution permissions -x I call it from ./consumidor.c and the debugging with gcc consumidor.c -or consumidor.
Thank you very much in advance I hope someone can help me maybe it's silly but I can not find the solution
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#define SEMKEY 110
#define SHMKEY 75
#define EM 0 //Semaforo de Exclusion Mutua
#define EL 1 //Semáforo de Espacios Libres
#define ED 2 //Semaforo de Elementos Disponibles
int ed=0;
int semid;
char caracter=200;
char carac[15];
struct sembuf sem_oper;
union semun
{
int val;
struct semid_ds *semstat;
unsigned short *array;
}
arg;
void wait(int id)
{
sem_oper.sem_num=id; //actuar sobre el semáforo correspondiente
sem_oper.sem_op=-1; //decremento 1 el semáforo
sem_oper.sem_flg=SEM_UNDO; // Para evitar interbloqueos si un proceso caba inesperadamente
semop(semid, &sem_oper,1);
}
void signal(int id)
{
sem_oper.sem_num=id; //actuar sobre el semáforo correspondiente
sem_oper.sem_op=1; //incremento
sem_oper.sem_flg=SEM_UNDO; // Para evitar interbloqueos
semop(semid, &sem_oper,1);
}
int main () {
char *eof;
int n;
int shmid[2];
char *addr, *buf;
//Crear la región de memoria y obtener la dirección
if((shmid[0] = shmget (SHMKEY, sizeof(char)*15, 0777 | IPC_CREAT))==-1){
printf("No se pudo reservar memoria");
exit(-1);
}
//Enlazar región al espacio de direccionamiento del proceso
addr = shmat (shmid[0], 0, 0);
if((shmid[1] = shmget (SHMKEY, sizeof(int), 0777 | IPC_CREAT))==-1){
printf("No se pudo reservar memoria");
exit(-1);
}
// Enlazar región al espacio de direccionamiento del proceso
eof = shmat (shmid[1], 0, 0);
//creación de 3 semáforos
semid=semget(SEMKEY,3,0777|IPC_CREAT);
// inicialización de los semáforos
arg.array=(unsigned short *) malloc(sizeof(short)*3);
arg.array[0]=1;
arg.array[1]=15;
arg.array[2]=0;
semctl (semid,3,SETALL,arg);
// operamos sobre los semáforos
for(n=1;n<=200;n++) // 200 caracteres
{
wait(ED); //Comprobar que hay elementos disponibles
wait(EM); // sección critica
buf=addr; //acceder a memoria compartida
caracter=buf[ed]; //leer el caracter
printf("%c",caracter); //imprimir por pantalla
ed=(ed+1)%15; //actualizar el indice
signal(EM); //salir de sección critica
signal(EL); //actualizar los espacios libres
}
semctl (semid,3,IPC_RMID,0);
// Separar la región del espacio de direccionamiento del proceso
shmdt (addr);
shmdt (eof);
for(n=0;n<2;n++)
shmctl(shmid[n],IPC_RMID,0);
free(arg.array);
return 0;
} // fin del main
This is what the terminal shows me
ismael@ismael:~$ ./consumidor.c
./consumidor.c: line 1: //PROCESO: No such file or directory
./consumidor.c: line 22: int: command not found
./consumidor.c: line 23: int: command not found
./consumidor.c: line 24: char: command not found
./consumidor.c: line 25: char: command not found
./consumidor.c: line 27: struct: command not found
./consumidor.c: line 28: union: command not found
./consumidor.c: line 30: int: command not found
./consumidor.c: line 31: struct: command not found
./consumidor.c: line 32: unsigned: command not found
./consumidor.c: line 34: arg: command not found
./consumidor.c: line 36: syntax error near unexpected token `('
./consumidor.c: line 36: `void wait(int id)
'
Comments
Post a Comment