-1

in this program about shared memory(Producer/Consumer) with semaphores(on DEBIAN) when I use the strncmp function with the string "end", in order to turn on 0 a flag(running) to kill a while cycle,strncmp doesn't recognize the word end that I insert into the shell. Thank you.

This is only the first process that I want to use:

//CONSUMER
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "semafori.h" //semaphores SYSTEM V functions
#include <sys/shm.h>
#include <string.h>

#define SHM_KEY (key_t)1234
#define SEM_KEY (key_t)5678

#define WRITE_SEM 0
#define READ_SEM 1

#define TEXT_SIZE 2048

struct SharedData{
unsigned int count;
char text[TEXT_SIZE];
};


int main(void)
{

    int running=1;
    void *shmP;
    struct SharedData * p;
    int shmID;
    int semID;

    semID=semget(SEM_KEY,2,IPC_CREAT|0666);

    SEM_SET(semID, WRITE_SEM,1);
    SEM_SET(semID, READ_SEM, 0);

shmID=shmget(SHM_KEY, sizeof(struct SharedData), IPC_CREAT|0666);

shmP=shmat(semID, (void *)0, 0);
printf("Memoria agganciata all'indirizzo: %X\n", (int)shmP);

p=(struct SharedData *)shmP;

while(running!=0){
if(SEM_P(semID, READ_SEM)==-1) exit(EXIT_FAILURE);
if (strncmp(p->text, "end", 3) == 0) {
    running = 0;
}
else {
printf("Numero scambi effettuato: %u\nHai scritto: %s\n", p->count, p->text);
}
if(SEM_V(semID, WRITE_SEM)==-1) exit(EXIT_FAILURE);
}
if(shmdt(shmP)==-1){
fprintf(stderr, "shmdetach failed\n");
exit(EXIT_FAILURE);
}
if(shmctl(shmID, IPC_RMID, 0)==-1){
fprintf(stderr, "shmctl RMID failed\n");
exit(EXIT_FAILURE);
}
SEM_DEL(semID, 0);

exit(EXIT_SUCCESS);
}

This is the second process: PRODUCER

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include "semafori.h"

#define SHM_KEY (key_t)1234
#define SEM_KEY (key_t)5678
#define WRITE_SEM 0
#define READ_SEM 1
#define TEXT_SIZE 2048


struct SharedData{
unsigned int count;
char text[TEXT_SIZE];
};

int main(void)
{

    int running=1;
unsigned int count=0;
void *shmP;
struct SharedData *p;
int shmID, semID;
char buffer[TEXT_SIZE];

semID=semget(SEM_KEY, 2, IPC_CREAT|066);
shmID=shmget(SHM_KEY, sizeof(struct SharedData),IPC_CREAT|0666);

shmP=shmat(shmID, (void *)0, 0);

p=(struct SharedData*)shmP;

while(running!=0)
{
count++;
if(SEM_P(semID, WRITE_SEM)==-1) exit(EXIT_FAILURE);
printf("Inserisci testo: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(p->text, buffer, TEXT_SIZE);
p->count=count;
if (strncmp(buffer, "end", 3) == 0) {
   running = 0;
}


if(SEM_V(semID, READ_SEM)==-1) exit(EXIT_FAILURE);
}

if(shmdt(shmP)==-1){
fprintf(stderr, "shmdt failure\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
amcnvbfgh
  • 1
  • 3
  • What is in `p->text` when this happens? – Scott Hunter Sep 07 '17 at 16:59
  • 6
    please create a [mcve] and format your code properly – r3mainer Sep 07 '17 at 16:59
  • you're reading input with `fgets()`. It does not suppress newlines from the input, so comparing the string "end" against an otherwise-unmodified line obtained from `fgets()` can succeed only under one of a few rather special circumstances, because the trailing newline that is likely to be in the input line breaks the match. – John Bollinger Sep 07 '17 at 17:22
  • @JohnBollinger can you explain writing code, please? – amcnvbfgh Sep 07 '17 at 17:37
  • @ScottHunter I think that in p->text there are all the string that I insert from shell with fgets and that I copy from buffer to p->text with strncpy – amcnvbfgh Sep 07 '17 at 17:37
  • @amcnvbfgh I might have done, if there weren't already dozens of answers here covering this. Read the documentation of `fgets()` and understand its implications. Run the producer code in a debugger if you're still uncertain what's going on. – John Bollinger Sep 07 '17 at 17:41
  • Ok, thank you everbody. I resolved the problem with debug method. In the shmat argument i wrote semID instead of shmID shmP=shmat(semID, (void *)0, 0); – amcnvbfgh Sep 07 '17 at 17:51
  • @JohnBollinger how can I closed this post as RELVED? – amcnvbfgh Sep 07 '17 at 17:52
  • @amcnvbfgh, if there were any answers, you would accept one of them. I don't intend to write an actual answer, however, so I recommend you just delete the question. Or you can self-answer, and (after an enforced delay) accept that answer. – John Bollinger Sep 07 '17 at 17:54
  • `fgets(buffer, BUFSIZ, stdin);` is suspicious. Expect `fgets(buffer, sizeof buffer, stdin);` `BUFSIZ` not defined anyplace. – chux - Reinstate Monica Sep 07 '17 at 18:08
  • Did you decide it might be best to format your code to make it easy to understand, then give up ten lines in? Because that's all I read, those ten lines... yeh... I gave up after that. If you want other people to read that, you'll need to present it for them... or not; I don't care if you don't get any help! – autistic Sep 07 '17 at 19:28
  • @chux BUFSIZ is a macro of – amcnvbfgh Sep 08 '17 at 08:53
  • @amcnvbfgh `BUFSIZ` may be defined yet with `char buffer[TEXT_SIZE];` and `fgets(buffer, BUFSIZ, stdin);`, `BUFSIZ` use is not relevant here. – chux - Reinstate Monica Sep 12 '17 at 03:11

1 Answers1

0

I resolved the problem with debug method. In the shmat argument i wrote semID instead of shmID shmP=shmat(semID, (void *)0, 0)

Thank you everybody.

[RESOLVED]

amcnvbfgh
  • 1
  • 3