简体   繁体   中英

Seg Fault 11 during execution

I don't understand why I'm getting a segfault, I haven't used any pointers. What I'm trying to do is Send messages between parent and child processes using two pipes, To do this I first fork then close/open required pipe sections and use the make_message to create the message I want to send. The problem might be that I first write with the child process and read with the parent process.

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>

#define READ_END 0
#define WRITE_END 1
#define BUF_SIZE 256
void make_message (int num, char *message){
    char buftime[26];
    time_t timer;
    struct tm* tm_info;
    time (&timer);
    tm_info = localtime (&timer);
    strftime (buftime, 26, "%Y-%m-%d %H:%M:%S", tm_info);
    sprintf (message, "%s %d at %s\n", "Hello I'm child number", num, buftime);
}

this is the make_message function

int main (int agrc, char *argv[]){
    int p1[2];
    int p2[2];
    char *virtualaddr;
    
    if (pipe(p1) == -1){
        fprintf (stderr, "pipe failed");
        return 1;
    }
    
    if (pipe(p2) == -1){
        fprintf (stderr, "pipe failed");
        return 1;
    }
    
    close (p1[READ_END]);
    close (p2[WRITE_END]);
    close (p2[READ_END]);
    close (p1[WRITE_END]);
    
    switch(fork()){
    case -1:
        fprintf (stderr, "fork error");
        return 1;
    
    case 0: //pere mais (fils 1)
    {
        char buf[BUF_SIZE];
        
        do{
            char message[BUF_SIZE];
            //memset (message, 0, BUF_SIZE);
            /*-------------WRITE 1--------------*/
            close (p1[READ_END]); //ferme la partie non utilisé du pipe
            close (p2[WRITE_END]); //ferme la partie non utilisé du pipe
            printf ("1");
            make_message (1, message);
            strcpy (virtualaddr, message);
            //ecrit au pipe
            write (p1[WRITE_END], virtualaddr, BUF_SIZE);
            //ferme le write du pipe
            close (p1[WRITE_END]);
            /*-------------WRITE 1--------------*/
        
            sleep(1);
        
            /*-------------READ 4---------------*/
            read (p2[READ_END], buf, BUF_SIZE); //fils lit
            printf ("Message received by child 1 : %s\n", buf);
            fflush( stdout);
        
            //ferme read dans le pipe
            close (p2[READ_END]);
            /*-------------READ 4---------------*/
            
            }while(1);
            _exit(0);
        }break;
    default: { //fils 2
        char buf[BUF_SIZE];
        //char message[BUF_SIZE];
        
        do{
            char message[BUF_SIZE];
            //memset (message, 0, BUF_SIZE);
            /*-------------READ 2---------------*/
            close (p2[READ_END]);
            close (p1[WRITE_END]); //ferme partie non utilisé du pipe
            printf("2");
            read (p1[READ_END], buf, BUF_SIZE); //fils lit
            printf ("Message received by child 2 : %s\n", buf);
            fflush( stdout);
        
            //ferme read dans le pipe
            close (p1[READ_END]);
            /*-------------READ 2---------------*/
        
        
            /*-------------WRITE 3--------------*/
            make_message (2, message);
            strcpy (virtualaddr, message);
            //ecrit au pipe
            write (p2[WRITE_END], virtualaddr, BUF_SIZE);
            //printf ("%s", buf);
            fflush( stdout);
        
            //ferme le write du pipe
            close (p2[WRITE_END]);
            /*-------------WRITE 3--------------*/
        
            memset (buf, 0, sizeof(buf));
            sleep(1);
        }while(1);
        _exit(0);
    }
    break;
}
        
}
    

I haven't used any pointers.

Yes you have:

char *virtualaddr;

And it is never initialized before you do

strcpy (virtualaddr, message);

Compile with -Wall -O and I bet you get a warning about this.

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