Hello,
I need some urgent help, I have a program that communicates with another program.
Its basically a Q&A, where one side introduces a question and the other answers it.
And yes this is a school work, and it needs to be delivered soon.
Im posting here because Im quite desperate, because one of the programs its quite troublesome.
Here it goes:
/*
Trabalho 2 - SO
Aluno
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXTEXTO 1024
/*** função sair, tratamento do sinal CTR+C***/
void sair(){
printf("Fim de execução\n");
exit(0);
}
int main(int argc, char* argv[]){
// Declaração de Variaveis
char *duvida;
sem_t *sem_full,*sem_empty, *sem_onlig, *sem_offlig, *sem_convon, *sem_convoff;
int i=0;
int *in=0;
int nAluno;
int *nAlunos;
char file[10];
char file3[10];
char file5[10];
char file6[10];
char file4[10];
char file2[10];
char file7[10];
char file8[10];
sem_t *sem_mutex; // Problema de numero de alunos e professores
//**********************
// Partilhar nAlunos
int f=shm_open("/fshared",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
if(f==-1){
perror("/error1");
exit(5);
}
int ret2=ftruncate(f, 10*sizeof(int));
if(ret2==-1){
perror("/error1");
exit(6);
}
nAlunos=mmap(0, 10*sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED,f,0);
//************************
// Partilhar variavel entre alunos
int alunos=shm_open("/variavel",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
if(alunos==-1){
perror("/error1");
exit(5);
}
int ret5=ftruncate(alunos, sizeof(int));
if(ret5==-1){
perror("/error1");
exit(6);
}
in=mmap(0, sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED,alunos,0);
//*************************
printf("Qual o número de aluno?\n");
char aluno[30];
fgets(aluno, 20, stdin);
sscanf(aluno,"%d", &nAluno);
sprintf(file, "/file%d", nAluno);
/*Criação da memória partilhada*/
int mem=shm_open(file,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
if(mem==-1){
perror("/error1");
exit(1);
}
int ret=ftruncate(mem,MAXTEXTO*sizeof(char));
if(ret==-1){
perror("/error1");
exit(2);
}
duvida=mmap(0,MAXTEXTO*sizeof(char),PROT_READ|PROT_WRITE,MAP_SHARED,mem,0);
sprintf(file2, "/file2%d", nAluno);
sprintf(file3, "/file3%d", nAluno);
sprintf(file4, "/file4%d", nAluno);
sprintf(file5, "/file5%d", nAluno);
sprintf(file6, "/file6%d", nAluno);
sprintf(file7, "/file7%d", nAluno);
sprintf(file8, "/file8%d", nAluno);
// SEMAFROS *******************************************
sem_full=sem_open(file2,O_CREAT,0xFFFFFFFF,0);
if(sem_full==SEM_FAILED){
perror("full");
exit(3);
}
sem_empty=sem_open(file3,O_CREAT,0xFFFFFFFF,10);
if(sem_empty==SEM_FAILED){
perror("empty");
exit(4);
}
sem_mutex=sem_open(file4,O_CREAT,0xFFFFFFFF,1);
if(sem_full==SEM_FAILED){
perror("full");
exit(3);
}
sem_offlig=sem_open(file5,O_CREAT,0xFFFFFFFF,1);
if(sem_full==SEM_FAILED){
perror("full");
exit(3);
}
sem_onlig=sem_open(file6,O_CREAT,0xFFFFFFFF,0);
if(sem_empty==SEM_FAILED){
perror("empty");
exit(4);
}
sem_convoff=sem_open(file7,O_CREAT,0xFFFFFFFF,0);
if(sem_full==SEM_FAILED){
perror("full");
exit(3);
}
sem_convon=sem_open(file8,O_CREAT,0xFFFFFFFF,1);
if(sem_empty==SEM_FAILED){
perror("empty");
exit(4);
}
//**********************************************
signal(SIGINT, sair);
sem_wait(sem_empty);
sem_wait(sem_mutex);
nAlunos[*in] = nAluno;
*in=(*in+1) % 10;
sem_post(sem_mutex);
sem_post(sem_full);
//conversa
//memo partilhada, semaforos,
printf("À espera de ligação...\n");
sem_wait(sem_onlig);
printf("Ligação estabelecida.\n");
signal(SIGINT, sair);
while(i==i){
sem_wait(sem_convon);
if(i>0)
printf("%s",duvida);
printf("> ");
fflush(stdout);
fgets(duvida,MAXTEXTO,stdin);
i++;
sem_post(sem_convoff);
sem_post(sem_offlig);
}
return 0;
}
lol, I know its quite big.
It actually works pretty well, but when u enter a number bigger than 1000 in variable nAluno
it crashes with a strange error.
Thank u very much for reading this........ :P