简体   繁体   中英

Process crashes while checking string recived from other process

I making a some kind of project. Program need to create 3 processes. 1st one is getting a string from user, the model is: 'number1+number2+..+numbern'. After that it send string to 2nd process. 2nd process is checking that user send a valid (with model) string and send it to 3rd process if it is okey 3rd process sums 'numbers' and print it on a screen.

I already did fully working 1st process, and trying to do 2nd one to work well with 1st one. Sadly, 2nd process crashes after: printf("Zaczynam sprawdzanie poprawnosci wyrazenia: %s\\n", kom.mtext);

Can u help me to figure out what im doing wrong? why 2nd process stops? Thanks in advance

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h> 

struct msgbuf {
   long mtype;
   char mtext[255];
};

struct msqid_ds buf;

 int main(int argc, char **argv) 
{
int ch, i, pid1=getpid(), pid2, pid3;

struct msgbuf kom;

if(argc != 1) 
{
    printf("\nProgram nie wymaga zadnych parametrow!\n");
}

ch = msgget(1000, IPC_CREAT|0666);

if((pid3 = fork()) == 0) 
{

}
else
{
    if((pid2 = fork()) == 0)
    {
        while(1)
        {   
            char tabliczb[25][20];
            int i=1, j=0, k=0, poprawny=1, liczba_znak=0;
            char liczba[20]="";
            if(msgrcv(ch, &kom, 512, getpid(), 0) < 0) 
                break;
            printf("\nProces 2-gi PID: %d, odebral wyrazenie: %s\n", getpid(),kom.mtext);
            if(kom.mtext[0] == '#')
                break;
            printf("Zaczynam sprawdzanie poprawnosci wyrazenia: %s\n", kom.mtext);
            if (kom.mtext[0] >= '0' && kom.mtext[0] <= '9')
            {
                liczba_znak = 0;
                liczba[j] = kom.mtext[0];
                j++;
            }
            else
            {
                printf("Wyrazenie musi zaczynac sie od liczby!\n");
                poprawny=0;
                break;
            }
            printf("\n3kom.text[%d]= %c", i, kom.mtext[i]);  //WHY THIS IS NOT PRINTING ANYTHING?
            while(kom.mtext[i] != '\0' && poprawny == 1)
            {   

                if(kom.mtext[i] >= '0' && kom.mtext[i] <= '9')
                {
                    liczba[j] = kom.mtext[i];
                    j++;
                    liczba_znak = 0;
                }   
                else if(kom.mtext[i] == '+' && liczba_znak == 0)
                {
                    strcpy(tabliczb[k],liczba);
                    while(j>=0)
                        liczba[j]='\0';
                    j++;
                    liczba_znak = 1;
                }
                else
                    poprawny = 0;

                i++;
            }
            if(poprawny)
                printf("Poprawny\n");
            else
                printf("Niepoprawny\n");
            printf("\nPodaj: ");
        }   
    }
    else
    {
        int len;

        while(kom.mtext[0] != '#') 
        {   
            i=0;
            usleep(500);
            printf("Podaj wyrazenie w postaci 'liczba1+liczba2+liczba3+..+liczban': ");
            fgets(kom.mtext, 255, stdin);
            kom.mtext[strcspn(kom.mtext, "\r\n")] = '\0'; // usuwanie znaku \r \n 
            len = strlen(kom.mtext);
            kom.mtype = pid2;
            msgsnd(ch, &kom, len+1, 0);
            printf("Wyslano: %s, do procesu: %d\n", kom.mtext, kom.mtype);
            fflush(stdin);
        }
        msgctl(ch, IPC_RMID, &buf);
    }
}

return 0;
 }

EDIT

SOLVED! The problem was with strcpy(tabliczb[k],liczba); I dont know rly why :/ i change strcpy(tabliczb[k],liczba); to straight writing into tabliczb[k] few lines above

msgrcv(ch, &kom, 512, getpid(), 0) looks very suspect because kom is way less than 512 bytes long. Why not use sizeof kom instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM