IPC Program

Reply

Join Date: Apr 2008
Posts: 13
Reputation: LightSystem is an unknown quantity at this point 
Solved Threads: 0
LightSystem LightSystem is offline Offline
Newbie Poster

IPC Program

 
0
  #1
Apr 27th, 2008
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:
  1. /*
  2. Trabalho 2 - SO
  3. Aluno
  4. */
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/mman.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <semaphore.h>
  11. #include <signal.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #define MAXTEXTO 1024
  15.  
  16. /*** função sair, tratamento do sinal CTR+C***/
  17. void sair(){
  18. printf("Fim de execução\n");
  19.  
  20. exit(0);
  21. }
  22.  
  23. int main(int argc, char* argv[]){
  24.  
  25. // Declaração de Variaveis
  26.  
  27. char *duvida;
  28. sem_t *sem_full,*sem_empty, *sem_onlig, *sem_offlig, *sem_convon, *sem_convoff;
  29. int i=0;
  30. int *in=0;
  31. int nAluno;
  32. int *nAlunos;
  33. char file[10];
  34. char file3[10];
  35. char file5[10];
  36. char file6[10];
  37. char file4[10];
  38. char file2[10];
  39. char file7[10];
  40. char file8[10];
  41. sem_t *sem_mutex; // Problema de numero de alunos e professores
  42. //**********************
  43.  
  44. // Partilhar nAlunos
  45. int f=shm_open("/fshared",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
  46. if(f==-1){
  47. perror("/error1");
  48. exit(5);
  49. }
  50. int ret2=ftruncate(f, 10*sizeof(int));
  51. if(ret2==-1){
  52. perror("/error1");
  53. exit(6);
  54. }
  55.  
  56. nAlunos=mmap(0, 10*sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED,f,0);
  57.  
  58.  
  59. //************************
  60. // Partilhar variavel entre alunos
  61. int alunos=shm_open("/variavel",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
  62. if(alunos==-1){
  63. perror("/error1");
  64. exit(5);
  65. }
  66. int ret5=ftruncate(alunos, sizeof(int));
  67. if(ret5==-1){
  68. perror("/error1");
  69. exit(6);
  70. }
  71.  
  72. in=mmap(0, sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED,alunos,0);
  73. //*************************
  74.  
  75. printf("Qual o número de aluno?\n");
  76.  
  77. char aluno[30];
  78. fgets(aluno, 20, stdin);
  79. sscanf(aluno,"%d", &nAluno);
  80.  
  81. sprintf(file, "/file%d", nAluno);
  82.  
  83. /*Criação da memória partilhada*/
  84. int mem=shm_open(file,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
  85. if(mem==-1){
  86. perror("/error1");
  87. exit(1);
  88. }
  89. int ret=ftruncate(mem,MAXTEXTO*sizeof(char));
  90. if(ret==-1){
  91. perror("/error1");
  92. exit(2);
  93. }
  94.  
  95. duvida=mmap(0,MAXTEXTO*sizeof(char),PROT_READ|PROT_WRITE,MAP_SHARED,mem,0);
  96.  
  97. sprintf(file2, "/file2%d", nAluno);
  98. sprintf(file3, "/file3%d", nAluno);
  99. sprintf(file4, "/file4%d", nAluno);
  100. sprintf(file5, "/file5%d", nAluno);
  101. sprintf(file6, "/file6%d", nAluno);
  102. sprintf(file7, "/file7%d", nAluno);
  103. sprintf(file8, "/file8%d", nAluno);
  104.  
  105.  
  106. // SEMAFROS *******************************************
  107. sem_full=sem_open(file2,O_CREAT,0xFFFFFFFF,0);
  108. if(sem_full==SEM_FAILED){
  109. perror("full");
  110. exit(3);
  111. }
  112. sem_empty=sem_open(file3,O_CREAT,0xFFFFFFFF,10);
  113. if(sem_empty==SEM_FAILED){
  114. perror("empty");
  115. exit(4);
  116. }
  117. sem_mutex=sem_open(file4,O_CREAT,0xFFFFFFFF,1);
  118. if(sem_full==SEM_FAILED){
  119. perror("full");
  120. exit(3);
  121. }
  122.  
  123. sem_offlig=sem_open(file5,O_CREAT,0xFFFFFFFF,1);
  124. if(sem_full==SEM_FAILED){
  125. perror("full");
  126. exit(3);
  127. }
  128. sem_onlig=sem_open(file6,O_CREAT,0xFFFFFFFF,0);
  129. if(sem_empty==SEM_FAILED){
  130. perror("empty");
  131. exit(4);
  132. }
  133. sem_convoff=sem_open(file7,O_CREAT,0xFFFFFFFF,0);
  134. if(sem_full==SEM_FAILED){
  135. perror("full");
  136. exit(3);
  137. }
  138. sem_convon=sem_open(file8,O_CREAT,0xFFFFFFFF,1);
  139. if(sem_empty==SEM_FAILED){
  140. perror("empty");
  141. exit(4);
  142. }
  143. //**********************************************
  144.  
  145. signal(SIGINT, sair);
  146.  
  147. sem_wait(sem_empty);
  148. sem_wait(sem_mutex);
  149. nAlunos[*in] = nAluno;
  150. *in=(*in+1) % 10;
  151. sem_post(sem_mutex);
  152. sem_post(sem_full);
  153.  
  154. //conversa
  155.  
  156. //memo partilhada, semaforos,
  157. printf("À espera de ligação...\n");
  158. sem_wait(sem_onlig);
  159. printf("Ligação estabelecida.\n");
  160. signal(SIGINT, sair);
  161. while(i==i){
  162. sem_wait(sem_convon);
  163. if(i>0)
  164. printf("%s",duvida);
  165.  
  166. printf("> ");
  167. fflush(stdout);
  168. fgets(duvida,MAXTEXTO,stdin);
  169. i++;
  170. sem_post(sem_convoff);
  171. sem_post(sem_offlig);
  172. }
  173. return 0;
  174. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: IPC Program

 
0
  #2
Apr 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: LightSystem is an unknown quantity at this point 
Solved Threads: 0
LightSystem LightSystem is offline Offline
Newbie Poster

Re: IPC Program

 
0
  #3
Apr 27th, 2008
The problem is I dont even get to input the characters...
I get this error "full: invalid argument" right after inserting the number.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: LightSystem is an unknown quantity at this point 
Solved Threads: 0
LightSystem LightSystem is offline Offline
Newbie Poster

Re: IPC Program

 
0
  #4
Apr 27th, 2008
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??
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: IPC Program

 
0
  #5
Apr 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: IPC Program

 
1
  #6
Apr 27th, 2008
Originally Posted by LightSystem View Post
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:
  1. file2[0] = '/';
  2. file2[1] = 'f';
  3. file2[2] = 'i';
  4. file2[3] = 'l';
  5. file2[4] = 'e';
  6. file2[5] = '2';
  7. file2[6] = '1';
  8. file2[7] = '0';
  9. file2[8] = '0';
  10. file2[9] = '\0';
Exactly the maximum that file2 and 3 and 4, etc... can hold. Before you were going over by one character.
Last edited by Aia; Apr 27th, 2008 at 7:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: LightSystem is an unknown quantity at this point 
Solved Threads: 0
LightSystem LightSystem is offline Offline
Newbie Poster

Re: IPC Program

 
0
  #7
Apr 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 1
Reputation: dreaddoC has a little shameless behaviour in the past 
Solved Threads: 0
dreaddoC dreaddoC is offline Offline
Newbie Poster

Re: IPC Program

 
-1
  #8
Nov 28th, 2008
Hi man, i'am making a work similar to tha, can you send me yours complete?? answer me as soon as you can. [][]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC