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

How many characters does "/file1000" and a \0 have?
Is there room for that in your variable?

Well not that one, the later ones are in trouble.

The problem is I dont even get to input the characters...
I get this error "full: invalid argument" right after inserting the number.

Ive just found out that in line 78: fgets(aluno, 20, stdin);
If I replace 20 with 4, I dont get the error I described above when inserting 1000.
Why is that??

If you're trashing memory, like I know you are, then cause and effect rules.
Fix the size of the arrays so that you don't get buffer overflow.

Ive just found out that in line 78: fgets(aluno, 20, stdin);
If I replace 20 with 4, I dont get the error I described above when inserting 1000.
Why is that??

Changing the range of characters that fgets can read to four will only put 100 + \0 into the variable aluno. It does leave behind the last zero from 1000 in the standard stream, ready to be pick up by a successive call for reading.
However it works because now sprintf(file2, "/file2%d", nAluno); set file2 as:

file2[0] = '/';
file2[1] = 'f';
file2[2] = 'i';
file2[3] = 'l';
file2[4] = 'e';
file2[5] = '2';
file2[6] = '1';
file2[7] = '0';
file2[8] = '0';
file2[9] = '\0';

Exactly the maximum that file2 and 3 and 4, etc... can hold. Before you were going over by one character.

commented: Good answer +15

Thank you for ur replies, it wasnt being obvious to me that the size of the various "file[]"
arrays was significant, changing their size to 30 solved the problem, even though the
program still doesn't work the way it should.
Something wrong with it doesn't make it possible to run various instances of it simultaniously and successfully communicate with the other process.
But thank you, u all have been helpful

Hi man, i'am making a work similar to tha, can you send me yours complete?? answer me as soon as you can. [][]

commented: Do your own homework -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.