| | |
IPC Program
![]() |
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
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:
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........
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:
C Syntax (Toggle Plain Text)
/* 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........
Last edited by LightSystem; Apr 27th, 2008 at 1:44 pm.
•
•
•
•
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??
However it works because now
sprintf(file2, "/file2%d", nAluno); set file2 as: C Syntax (Toggle Plain Text)
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';
Last edited by Aia; Apr 27th, 2008 at 7:13 pm.
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Why Use Incentives To Increase Sales (eCommerce)
- Trying to write to a file so that another program can read from it at the same time (C++)
- IPC samples needed in java? (Java)
- IPC/socket beginner (C)
- Help in terminating a program (C++)
- Running a 3rd party program from Python (Python)
- Desktop virus?? maybe (Viruses, Spyware and other Nasties)
- Program security (When Launched) (C)
Other Threads in the C Forum
- Previous Thread: How to compile C in Linux
- Next Thread: How to link two C files in linux
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch char cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research scanf scripting segmentationfault sequential shape socket socketprograming stack standard string systemcall testing threads turboc unix user voidmain() wab windows.h






